[pypy-commit] pypy follow_symlinks: Allow specifying path as file descriptor in os.listdir()

2016-04-04 Thread rlamy
Author: Ronan Lamy 
Branch: follow_symlinks
Changeset: r83513:c6615ca37d32
Date: 2016-04-04 19:55 +0100
http://bitbucket.org/pypy/pypy/changeset/c6615ca37d32/

Log:Allow specifying path as file descriptor in os.listdir()

diff --git a/pypy/module/posix/interp_posix.py 
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -744,35 +744,56 @@
 raise wrap_oserror(space, e)
 
 
-@unwrap_spec(w_dirname=WrappedDefault(u"."))
-def listdir(space, w_dirname):
-"""Return a list containing the names of the entries in the directory.
+@unwrap_spec(w_path=WrappedDefault(u"."))
+def listdir(space, w_path):
+"""listdir(path='.') -> list_of_filenames
 
-\tpath: path of directory to list
+Return a list containing the names of the files in the directory.
+The list is in arbitrary order.  It does not include the special
+entries '.' and '..' even if they are present in the directory.
 
-The list is in arbitrary order.  It does not include the special
-entries '.' and '..' even if they are present in the directory."""
+path can be specified as either str or bytes.  If path is bytes,
+  the filenames returned will also be bytes; in all other circumstances
+  the filenames returned will be str.
+On some platforms, path may also be specified as an open file descriptor;
+  the file descriptor must refer to a directory.
+  If this functionality is unavailable, using it raises NotImplementedError."""
+if space.isinstance_w(w_path, space.w_bytes):
+dirname = space.str0_w(w_path)
+try:
+result = rposix.listdir(dirname)
+except OSError as e:
+raise wrap_oserror2(space, e, w_path)
+return space.newlist_bytes(result)
 try:
-if space.isinstance_w(w_dirname, space.w_unicode):
-dirname = FileEncoder(space, w_dirname)
+path = space.fsencode_w(w_path)
+except OperationError as operr:
+if not rposix.HAVE_FDOPENDIR:
+raise oefmt(space.w_TypeError,
+"listdir: illegal type for path argument")
+if not space.isinstance_w(w_path, space.w_int):
+raise oefmt(space.w_TypeError,
+"argument should be string, bytes or integer, not %T", w_path)
+fd = unwrap_fd(space, w_path)
+try:
+result = rposix.fdlistdir(fd)
+except OSError as e:
+raise wrap_oserror2(space, e, w_path)
+else:
+dirname = FileEncoder(space, w_path)
+try:
 result = rposix.listdir(dirname)
-len_result = len(result)
-result_w = [None] * len_result
-for i in range(len_result):
-if _WIN32:
-result_w[i] = space.wrap(result[i])
-else:
-w_bytes = space.wrapbytes(result[i])
-result_w[i] = space.fsdecode(w_bytes)
-return space.newlist(result_w)
+except OSError as e:
+raise wrap_oserror2(space, e, w_path)
+len_result = len(result)
+result_w = [None] * len_result
+for i in range(len_result):
+if _WIN32:
+result_w[i] = space.wrap(result[i])
 else:
-dirname = space.str0_w(w_dirname)
-result = rposix.listdir(dirname)
-# The list comprehension is a workaround for an obscure translation
-# bug.
-return space.newlist_bytes([x for x in result])
-except OSError, e:
-raise wrap_oserror2(space, e, w_dirname)
+w_bytes = space.wrapbytes(result[i])
+result_w[i] = space.fsdecode(w_bytes)
+return space.newlist(result_w)
 
 def pipe(space):
 "Create a pipe.  Returns (read_end, write_end)."
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Pretend to be using a terminal that supports clear

2016-04-04 Thread stefanor
Author: Stefano Rivera 
Branch: 
Changeset: r83515:accad9a1fe9c
Date: 2016-04-04 16:45 -0700
http://bitbucket.org/pypy/pypy/changeset/accad9a1fe9c/

Log:Pretend to be using a terminal that supports clear

Some tests use readline on an internal pty. These fail if TERM is
not set to a terminal that supports "clear".

diff --git a/pypy/module/test_lib_pypy/pyrepl/infrastructure.py 
b/pypy/module/test_lib_pypy/pyrepl/infrastructure.py
--- a/pypy/module/test_lib_pypy/pyrepl/infrastructure.py
+++ b/pypy/module/test_lib_pypy/pyrepl/infrastructure.py
@@ -18,6 +18,9 @@
 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 from __future__ import print_function
+from contextlib import contextmanager
+import os
+
 from pyrepl.reader import Reader
 from pyrepl.console import Console, Event
 
@@ -71,3 +74,14 @@
 con = TestConsole(test_spec, verbose=True)
 reader = reader_class(con)
 reader.readline()
+
+
+@contextmanager
+def sane_term():
+"""Ensure a TERM that supports clear"""
+old_term, os.environ['TERM'] = os.environ.get('TERM'), 'xterm'
+yield
+if old_term is not None:
+os.environ['TERM'] = old_term
+else:
+del os.environ['TERM']
diff --git a/pypy/module/test_lib_pypy/pyrepl/test_bugs.py 
b/pypy/module/test_lib_pypy/pyrepl/test_bugs.py
--- a/pypy/module/test_lib_pypy/pyrepl/test_bugs.py
+++ b/pypy/module/test_lib_pypy/pyrepl/test_bugs.py
@@ -18,7 +18,7 @@
 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 from pyrepl.historical_reader import HistoricalReader
-from .infrastructure import EA, BaseTestReader, read_spec
+from .infrastructure import EA, BaseTestReader, sane_term, read_spec
 
 # this test case should contain as-verbatim-as-possible versions of
 # (applicable) bug reports
@@ -62,13 +62,14 @@
 
 mfd, sfd = pty.openpty()
 try:
-c = UnixConsole(sfd, sfd)
-c.prepare()
-c.restore()
-monkeypatch.setattr(signal, 'signal', failing_signal)
-c.prepare()
-monkeypatch.setattr(signal, 'signal', really_failing_signal)
-c.restore()
+with sane_term():
+c = UnixConsole(sfd, sfd)
+c.prepare()
+c.restore()
+monkeypatch.setattr(signal, 'signal', failing_signal)
+c.prepare()
+monkeypatch.setattr(signal, 'signal', really_failing_signal)
+c.restore()
 finally:
 os.close(mfd)
 os.close(sfd)
diff --git a/pypy/module/test_lib_pypy/pyrepl/test_readline.py 
b/pypy/module/test_lib_pypy/pyrepl/test_readline.py
--- a/pypy/module/test_lib_pypy/pyrepl/test_readline.py
+++ b/pypy/module/test_lib_pypy/pyrepl/test_readline.py
@@ -1,5 +1,7 @@
 import pytest
 
+from .infrastructure import sane_term
+
 
 @pytest.mark.skipif("os.name != 'posix' or 'darwin' in sys.platform or "
 "'kfreebsd' in sys.platform")
@@ -12,7 +14,8 @@
 readline_wrapper = _ReadlineWrapper(slave, slave)
 os.write(master, b'input\n')
 
-result = readline_wrapper.get_reader().readline()
+with sane_term():
+result = readline_wrapper.get_reader().readline()
 #result = readline_wrapper.raw_input('prompt:')
 assert result == 'input'
 # A bytes string on python2, a unicode string on python3.
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Skip PTY tests that hang forever on kFreeBSD

2016-04-04 Thread stefanor
Author: Stefano Rivera 
Branch: 
Changeset: r83514:9059c53718bb
Date: 2016-04-04 16:40 -0700
http://bitbucket.org/pypy/pypy/changeset/9059c53718bb/

Log:Skip PTY tests that hang forever on kFreeBSD

I don't know if this is expected behaviour. These all look skipped
on darwin, which is presumably similar to FreeBSD, here.

https://bugs.debian.org/742965 also seems relevant

diff --git a/pypy/module/_file/test/test_file.py 
b/pypy/module/_file/test/test_file.py
--- a/pypy/module/_file/test/test_file.py
+++ b/pypy/module/_file/test/test_file.py
@@ -285,6 +285,8 @@
 from posix import openpty, fdopen, write, close
 except ImportError:
 skip('no openpty on this platform')
+if 'gnukfreebsd' in sys.platform:
+skip('close() hangs forever on kFreeBSD')
 read_fd, write_fd = openpty()
 write(write_fd, 'Abc\n')
 close(write_fd)
diff --git a/pypy/module/test_lib_pypy/pyrepl/test_bugs.py 
b/pypy/module/test_lib_pypy/pyrepl/test_bugs.py
--- a/pypy/module/test_lib_pypy/pyrepl/test_bugs.py
+++ b/pypy/module/test_lib_pypy/pyrepl/test_bugs.py
@@ -46,7 +46,8 @@
 read_spec(spec, HistoricalTestReader)
 
 
-@pytest.mark.skipif("os.name != 'posix' or 'darwin' in sys.platform")
+@pytest.mark.skipif("os.name != 'posix' or 'darwin' in sys.platform or "
+"'kfreebsd' in sys.platform")
 def test_signal_failure(monkeypatch):
 import os
 import pty
diff --git a/pypy/module/test_lib_pypy/pyrepl/test_readline.py 
b/pypy/module/test_lib_pypy/pyrepl/test_readline.py
--- a/pypy/module/test_lib_pypy/pyrepl/test_readline.py
+++ b/pypy/module/test_lib_pypy/pyrepl/test_readline.py
@@ -1,7 +1,8 @@
 import pytest
 
 
-@pytest.mark.skipif("os.name != 'posix' or 'darwin' in sys.platform")
+@pytest.mark.skipif("os.name != 'posix' or 'darwin' in sys.platform or "
+"'kfreebsd' in sys.platform")
 def test_raw_input():
 import os
 import pty
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit