Author: Armin Rigo <[email protected]>
Branch: cffi-static-callback
Changeset: r80753:0b45b61fea59
Date: 2015-11-18 12:29 +0100
http://bitbucket.org/pypy/pypy/changeset/0b45b61fea59/

Log:    import cffi/1e8f6c41d71a

diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_function.py 
b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_function.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_function.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_function.py
@@ -5,6 +5,7 @@
 import ctypes.util
 from cffi.backend_ctypes import CTypesBackend
 from pypy.module.test_lib_pypy.cffi_tests.udir import udir
+from pypy.module.test_lib_pypy.cffi_tests.support import FdWriteCapture
 
 try:
     from StringIO import StringIO
@@ -12,29 +13,6 @@
     from io import StringIO
 
 
-class FdWriteCapture(object):
-    """xxx limited to capture at most 512 bytes of output, according
-    to the Posix manual."""
-
-    def __init__(self, capture_fd):
-        self.capture_fd = capture_fd
-
-    def __enter__(self):
-        self.read_fd, self.write_fd = os.pipe()
-        self.copy_fd = os.dup(self.capture_fd)
-        os.dup2(self.write_fd, self.capture_fd)
-        return self
-
-    def __exit__(self, *args):
-        os.dup2(self.copy_fd, self.capture_fd)
-        os.close(self.copy_fd)
-        os.close(self.write_fd)
-        self._value = os.read(self.read_fd, 512)
-        os.close(self.read_fd)
-
-    def getvalue(self):
-        return self._value
-
 lib_m = 'm'
 if sys.platform == 'win32':
     #there is a small chance this fails on Mingw via environ $CC
@@ -136,7 +114,7 @@
         """)
         ffi.C = ffi.dlopen(None)
         ffi.C.fputs   # fetch before capturing, for easier debugging
-        with FdWriteCapture(2) as fd:
+        with FdWriteCapture() as fd:
             ffi.C.fputs(b"hello\n", ffi.C.stderr)
             ffi.C.fputs(b"  world\n", ffi.C.stderr)
         res = fd.getvalue()
@@ -152,7 +130,7 @@
         """)
         ffi.C = ffi.dlopen(None)
         ffi.C.fputs   # fetch before capturing, for easier debugging
-        with FdWriteCapture(2) as fd:
+        with FdWriteCapture() as fd:
             ffi.C.fputs(b"hello\n", ffi.C.stderr)
             ffi.C.fputs(b"  world\n", ffi.C.stderr)
         res = fd.getvalue()
@@ -167,7 +145,7 @@
            void *stderr;
         """)
         ffi.C = ffi.dlopen(None)
-        with FdWriteCapture(2) as fd:
+        with FdWriteCapture() as fd:
             ffi.C.fprintf(ffi.C.stderr, b"hello with no arguments\n")
             ffi.C.fprintf(ffi.C.stderr,
                           b"hello, %s!\n", ffi.new("char[]", b"world"))
@@ -229,7 +207,7 @@
         fptr = ffi.cast("int(*)(const char *txt, void *)", ffi.C.fputs)
         assert fptr == ffi.C.fputs
         assert repr(fptr).startswith("<cdata 'int(*)(char *, void *)' 0x")
-        with FdWriteCapture(2) as fd:
+        with FdWriteCapture() as fd:
             fptr(b"world\n", ffi.C.stderr)
         res = fd.getvalue()
         assert res == b'world\n'
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py 
b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
@@ -5,7 +5,7 @@
 from cffi import recompiler
 from pypy.module.test_lib_pypy.cffi_tests.udir import udir
 from pypy.module.test_lib_pypy.cffi_tests.support import u
-from StringIO import StringIO
+from pypy.module.test_lib_pypy.cffi_tests.support import FdWriteCapture, 
StdErrCapture
 
 
 def check_type_table(input, expected_output, included=None):
@@ -1487,14 +1487,6 @@
     pt = ptr_call2(ffi.addressof(lib, 'cb2'))
     assert (pt.x, pt.y) == (99*500*999, -99*500*999)
 
-class StdErrCapture(object):
-    def __enter__(self):
-        self.old_stderr = sys.stderr
-        sys.stderr = f = StringIO()
-        return f
-    def __exit__(self, *args):
-        sys.stderr = self.old_stderr
-
 def test_extern_python_1():
     ffi = FFI()
     ffi.cdef("""
@@ -1507,7 +1499,7 @@
     """)
     lib = verify(ffi, 'test_extern_python_1', "")
     assert ffi.typeof(lib.bar) == ffi.typeof("int(*)(int, int)")
-    with StdErrCapture() as f:
+    with FdWriteCapture() as f:
         res = lib.bar(4, 5)
     assert res == 0
     assert f.getvalue() == (
diff --git a/pypy/module/test_lib_pypy/cffi_tests/support.py 
b/pypy/module/test_lib_pypy/cffi_tests/support.py
--- a/pypy/module/test_lib_pypy/cffi_tests/support.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/support.py
@@ -18,3 +18,40 @@
     u = ""
     unicode = str
     long = int
+
+
+class StdErrCapture(object):
+    """Capture writes to sys.stderr (not to the underlying file descriptor)."""
+    def __enter__(self):
+        import StringIO
+        self.old_stderr = sys.stderr
+        sys.stderr = f = StringIO.StringIO()
+        return f
+    def __exit__(self, *args):
+        sys.stderr = self.old_stderr
+
+
+class FdWriteCapture(object):
+    """xxx limited to capture at most 512 bytes of output, according
+    to the Posix manual."""
+
+    def __init__(self, capture_fd=2):    # stderr by default
+        self.capture_fd = capture_fd
+
+    def __enter__(self):
+        import os
+        self.read_fd, self.write_fd = os.pipe()
+        self.copy_fd = os.dup(self.capture_fd)
+        os.dup2(self.write_fd, self.capture_fd)
+        return self
+
+    def __exit__(self, *args):
+        import os
+        os.dup2(self.copy_fd, self.capture_fd)
+        os.close(self.copy_fd)
+        os.close(self.write_fd)
+        self._value = os.read(self.read_fd, 512)
+        os.close(self.read_fd)
+
+    def getvalue(self):
+        return self._value
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to