Commits

Manuel Jacob committed 2383a5b Merge

hg merge py3k

Comments (0)

Files changed (12)

pypy/config/pypyoption.py

      "thread", "itertools", "pyexpat", "_ssl", "cpyext", "array",
      "_bisect", "binascii", "_multiprocessing", '_warnings',
      "_collections", "_multibytecodec", "_ffi",
-     "_continuation", "_csv", # "micronumpy", "_cffi_backend",
+     "_continuation", "_csv", "_cffi_backend", # "micronumpy",
      "_posixsubprocess",
      ]
 ))

pypy/interpreter/error.py

         #
         # In the following table, 'Class' means a subclass of BaseException
         # and 'inst' is an instance of either 'Class' or a subclass of it.
-        # Or 'Class' can also be an old-style class and 'inst' an old-style
-        # instance of it.
         #
-        # The flow object space only deals with non-advanced case. Old-style
-        # classes and instances *are* advanced.
+        # The flow object space only deals with non-advanced case.
         #
         #  input (w_type, w_value)... becomes...                advanced case?
         # ---------------------------------------------------------------------
-        #  (tuple, w_value)           (tuple[0], w_value)             yes
         #  (Class, None)              (Class, Class())                no
         #  (Class, inst)              (inst.__class__, inst)          no
         #  (Class, tuple)             (Class, Class(*tuple))          yes
         #  (Class, x)                 (Class, Class(x))               no
-        #  ("string", ...)            ("string", ...)              deprecated
         #  (inst, None)               (inst.__class__, inst)          no
         #
         w_type  = self.w_type
         w_value = self.get_w_value(space)
-        while space.is_true(space.isinstance(w_type, space.w_tuple)):
-            w_type = space.getitem(w_type, space.wrap(0))
 
         if space.exception_is_valid_obj_as_class_w(w_type):
             # this is for all cases of the form (Class, something)

pypy/interpreter/pyopcode.py

     """Raised when exiting a frame via a 'yield' statement."""
 
 class RaiseWithExplicitTraceback(Exception):
-    """Raised at interp-level by a 0- or 3-arguments 'raise' statement."""
+    """Raised at interp-level by a 0-argument 'raise' statement."""
     def __init__(self, operr):
         self.operr = operr
 

pypy/module/_cffi_backend/ctypeptr.py

 Pointers.
 """
 
+import os
+
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.error import wrap_oserror
 from rpython.rtyper.lltypesystem import lltype, rffi
         return W_CTypePtrBase.cast(self, w_ob)
 
     def prepare_file(self, w_ob):
-        from pypy.module._file.interp_file import W_File
-        from pypy.module._cffi_backend import ctypefunc
+        from pypy.module._io.interp_iobase import W_IOBase
         ob = self.space.interpclass_w(w_ob)
-        if isinstance(ob, W_File):
-            return prepare_file_argument(self.space, ob)
+        if isinstance(ob, W_IOBase):
+            return prepare_iofile_argument(self.space, w_ob)
         else:
             return lltype.nullptr(rffi.CCHARP.TO)
 
     def close(self):
         rffi_fclose(self.llf)
 
-def prepare_file_argument(space, fileobj):
-    fileobj.direct_flush()
+def prepare_iofile_argument(space, w_fileobj):
+    fileobj = space.interpclass_w(w_fileobj)
+    from pypy.module._io.interp_iobase import W_IOBase
+    assert isinstance(fileobj, W_IOBase)
+    space.call_method(w_fileobj, "flush")
     if fileobj.cffi_fileobj is None:
-        fd = fileobj.direct_fileno()
+        fd = space.int_w(space.call_method(w_fileobj, "fileno"))
         if fd < 0:
             raise OperationError(space.w_ValueError,
                                  space.wrap("file has no OS file descriptor"))
+        fd = os.dup(fd)
+        mode = space.str_w(space.getattr(w_fileobj, space.wrap("mode")))
         try:
-            fileobj.cffi_fileobj = CffiFileObj(fd, fileobj.mode)
+            fileobj.cffi_fileobj = CffiFileObj(fd, mode)
         except OSError, e:
             raise wrap_oserror(space, e)
     return fileobj.cffi_fileobj.llf
+

pypy/module/_cffi_backend/test/_backend_test_c.py

     assert len(p) == 4
     assert list(p) == [b"f", b"o", b"o", b"\x00"]
 
-# XXX hack
-if sys.version_info >= (3,):
-    try:
-        import posix, io
-        posix.fdopen = io.open
-    except ImportError:
-        pass   # win32
+import io
+fdopen_funcs = [io.open]
+try:
+    import posix
+    fdopen_funcs.append(posix.fdopen)
+except (ImportError, AttributeError):  # win32, or py3k
+    pass
 
 def test_FILE():
     if sys.platform == "win32":
     fputs = ll.load_function(BFunc, "fputs")
     fscanf = ll.load_function(BFunc2, "fscanf")
     #
-    import posix
-    fdr, fdw = posix.pipe()
-    fr1 = posix.fdopen(fdr, 'rb', 256)
-    fw1 = posix.fdopen(fdw, 'wb', 256)
-    #
-    fw1.write(b"X")
-    res = fputs(b"hello world\n", fw1)
-    assert res >= 0
-    fw1.flush()     # should not be needed
-    #
-    p = newp(new_array_type(BCharP, 100), None)
-    res = fscanf(fr1, b"%s\n", p)
-    assert res == 1
-    assert string(p) == b"Xhello"
-    fr1.close()
-    fw1.close()
+    for fdopen in fdopen_funcs:
+        fdr, fdw = posix.pipe()
+        fr1 = fdopen(fdr, 'rb', 256)
+        fw1 = fdopen(fdw, 'wb', 256)
+        #
+        fw1.write(b"X")
+        res = fputs(b"hello world\n", fw1)
+        assert res >= 0
+        fw1.flush()     # should not be needed
+        #
+        p = newp(new_array_type(BCharP, 100), None)
+        res = fscanf(fr1, b"%s\n", p)
+        assert res == 1
+        assert string(p) == b"Xhello"
+        fr1.close()
+        fw1.close()
 
 def test_FILE_only_for_FILE_arg():
     if sys.platform == "win32":
     ll = find_and_load_library('c')
     fputs = ll.load_function(BFunc, "fputs")
     #
-    import posix
-    fdr, fdw = posix.pipe()
-    fr1 = posix.fdopen(fdr, 'r')
-    fw1 = posix.fdopen(fdw, 'w')
-    #
-    e = py.test.raises(TypeError, fputs, b"hello world\n", fw1)
-    assert str(e.value).startswith(
-        "initializer for ctype 'struct NOT_FILE *' must "
-        "be a cdata pointer, not ")
+    for fdopen in fdopen_funcs:
+        fdr, fdw = posix.pipe()
+        fr1 = fdopen(fdr, 'r')
+        fw1 = fdopen(fdw, 'w')
+        #
+        e = py.test.raises(TypeError, fputs, b"hello world\n", fw1)
+        assert str(e.value).startswith(
+            "initializer for ctype 'struct NOT_FILE *' must "
+            "be a cdata pointer, not ")
 
 def test_FILE_object():
     if sys.platform == "win32":
     fputs = ll.load_function(BFunc, "fputs")
     fileno = ll.load_function(BFunc2, "fileno")
     #
-    import posix
-    fdr, fdw = posix.pipe()
-    fw1 = posix.fdopen(fdw, 'wb', 256)
-    #
-    fw1p = cast(BFILEP, fw1)
-    fw1.write(b"X")
-    fw1.flush()
-    res = fputs(b"hello\n", fw1p)
-    assert res >= 0
-    res = fileno(fw1p)
-    assert (res == fdw) == (sys.version_info < (3,))
-    fw1.close()
-    #
-    data = posix.read(fdr, 256)
-    assert data == b"Xhello\n"
-    posix.close(fdr)
+    for fdopen in fdopen_funcs:
+        fdr, fdw = posix.pipe()
+        fw1 = fdopen(fdw, 'wb', 256)
+        #
+        fw1p = cast(BFILEP, fw1)
+        fw1.write(b"X")
+        fw1.flush()
+        res = fputs(b"hello\n", fw1p)
+        assert res >= 0
+        res = fileno(fw1p)
+        if fdopen is not io.open and 'PY_DOT_PY' not in globals():
+            assert res == fdw
+        fw1.close()
+        #
+        data = posix.read(fdr, 256)
+        assert data == b"Xhello\n"
+        posix.close(fdr)
 
 def test_GetLastError():
     if sys.platform != "win32":

pypy/module/_cffi_backend/test/test_c.py

         testfuncs_w = []
         keepalive_funcs = []
 
-        def find_and_load_library_for_test(space, w_name, w_is_global=None):
-            if w_is_global is None:
-                w_is_global = space.wrap(0)
-            if space.is_w(w_name, space.w_None):
-                path = None
-            else:
-                import ctypes.util
-                path = ctypes.util.find_library(space.str_w(w_name))
-            return space.appexec([space.wrap(path), w_is_global],
-            """(path, is_global):
-                import _cffi_backend
-                return _cffi_backend.load_library(path, is_global)""")
-
         test_lib_c = tmpdir.join('_test_lib.c')
         src_test_lib_c = py.path.local(__file__).dirpath().join('_test_lib.c')
         src_test_lib_c.copy(test_lib_c)
         eci = ExternalCompilationInfo()
-        test_lib = host.compile([test_lib_c], eci, standalone=False)
+        test_lib = str(host.compile([test_lib_c], eci, standalone=False))
 
-        cdll = ctypes.CDLL(str(test_lib))
+        cdll = ctypes.CDLL(test_lib)
         cdll.gettestfunc.restype = ctypes.c_void_p
 
-        def testfunc_for_test(space, w_num):
-            if hasattr(space, 'int_w'):
-                w_num = space.int_w(w_num)
-            addr = cdll.gettestfunc(w_num)
-            return space.wrap(addr)
-
         space = cls.space
         if cls.runappdirect:
-            def interp2app(func):
-                def run(*args):
-                    return func(space, *args)
-                return run
+            def find_and_load_library_for_test(name, is_global=False):
+                if name is None:
+                    path = None
+                else:
+                    import ctypes.util
+                    path = ctypes.util.find_library(name)
+                import _cffi_backend
+                return _cffi_backend.load_library(path, is_global)
+
+            def w_testfunc_for_test(num):
+                import ctypes
+                cdll = ctypes.CDLL(str(self.test_lib))
+                cdll.gettestfunc.restype = ctypes.c_void_p
+                return cdll.gettestfunc(num)
+
+            cls.w_test_lib = space.wrap(test_lib)
+            cls.w_func = find_and_load_library_for_test
+            cls.w_testfunc = w_testfunc_for_test
         else:
-            interp2app = gateway.interp2app
+            def find_and_load_library_for_test(space, w_name, w_is_global=None):
+                if w_is_global is None:
+                    w_is_global = space.wrap(0)
+                if space.is_w(w_name, space.w_None):
+                    path = None
+                else:
+                    import ctypes.util
+                    path = ctypes.util.find_library(space.str_w(w_name))
+                return space.appexec([space.wrap(path), w_is_global],
+                """(path, is_global):
+                    import _cffi_backend
+                    return _cffi_backend.load_library(path, is_global)""")
 
-        w_func = space.wrap(interp2app(find_and_load_library_for_test))
-        w_testfunc = space.wrap(interp2app(testfunc_for_test))
-        space.appexec([space.wrap(str(tmpdir)), w_func, w_testfunc,
-                       space.wrap(sys.version[:3])],
+            def testfunc_for_test(space, w_num):
+                if hasattr(space, 'int_w'):
+                    w_num = space.int_w(w_num)
+                addr = cdll.gettestfunc(w_num)
+                return space.wrap(addr)
+
+            cls.w_func = space.wrap(gateway.interp2app(find_and_load_library_for_test))
+            cls.w_testfunc = space.wrap(gateway.interp2app(testfunc_for_test))
+        cls.w_zz_init = space.appexec(
+            [space.wrap(str(tmpdir)), cls.w_func, cls.w_testfunc,
+             space.wrap(sys.version[:3])],
         """(path, func, testfunc, underlying_version):
             import sys
             sys.path.append(path)

pypy/module/_cffi_backend/test/test_ztranslation.py

 from pypy.module._cffi_backend import misc
 
 def test_checkmodule():
-    # prepare_file_argument() is not working without translating the _file
-    # module too
-    def dummy_prepare_file_argument(space, fileobj):
+    # W_CTypePointer.prepare_file() is not working without translating
+    # the _io module too
+    def dummy_prepare_file(self, w_ob):
         return lltype.nullptr(rffi.CCHARP.TO)
-    old = ctypeptr.prepare_file_argument
+    old = ctypeptr.W_CTypePointer.prepare_file
     try:
-        ctypeptr.prepare_file_argument = dummy_prepare_file_argument
+        ctypeptr.W_CTypePointer.prepare_file = dummy_prepare_file
         #
         checkmodule('_cffi_backend')
         #
     finally:
-        ctypeptr.prepare_file_argument = old
+        ctypeptr.W_CTypePointer.prepare_file = old

pypy/module/_io/interp_iobase.py

         raise unsupported(space, "File or stream is not seekable")
 
 class W_IOBase(Wrappable):
+    cffi_fileobj = None    # pypy/module/_cffi_backend
+
     def __init__(self, space):
         # XXX: IOBase thinks it has to maintain its own internal state in
         # `__IOBase_closed` and call flush() by itself, but it is redundant
     def close_w(self, space):
         if self._CLOSED():
             return
+
+        cffifo = self.cffi_fileobj
+        self.cffi_fileobj = None
+        if cffifo is not None:
+            cffifo.close()
+
         try:
             space.call_method(self, "flush")
         finally:

pypy/module/_socket/interp_func.py

 from pypy.interpreter.gateway import unwrap_spec, WrappedDefault
-from pypy.module._socket.interp_socket import converted_error, W_RSocket, addr_as_object, ipaddr_from_object
+from pypy.module._socket.interp_socket import (
+    converted_error, W_RSocket, addr_as_object, ipaddr_from_object, get_error)
 from rpython.rlib import rsocket
 from rpython.rlib.rsocket import SocketError, INVALID_SOCKET
 from pypy.interpreter.error import OperationError
 
     Get host and port for a sockaddr."""
     try:
-        addr = ipaddr_from_object(space, w_sockaddr)
+        w_host, w_port = space.fixedview(w_sockaddr, 2)
+        host = space.str_w(w_host)
+        port = str(space.int_w(w_port))
+        lst = rsocket.getaddrinfo(host, port, rsocket.AF_UNSPEC,
+                                  rsocket.SOCK_DGRAM, 0,
+                                  rsocket.AI_NUMERICHOST)
+        if len(lst) > 1:
+            raise OperationError(
+                get_error(space, 'error'),
+                space.wrap("sockaddr resolved to multiple addresses"))
+        addr = lst[0][4]
         host, servport = rsocket.getnameinfo(addr, flags)
     except SocketError, e:
         raise converted_error(space, e)

pypy/module/_socket/interp_socket.py

         except SocketError, e:
             raise converted_error(space, e)
 
+    def __del__(self):
+        self.clear_all_weakrefs()
+        if self.space:
+            self.enqueue_for_destruction(self.space, W_RSocket.destructor,
+                                         'internal __del__ of ')
+
+    def destructor(self):
+        assert isinstance(self, W_RSocket)
+        RSocket.__del__(self)
+
     def _dealloc_warn(self):
         space = self.space
         if not space:

pypy/module/_socket/test/test_sock_app.py

         ])))
 
 def test_getnameinfo():
+    from pypy.module._socket.interp_socket import get_error
     host = "127.0.0.1"
     port = 25
     info = socket.getnameinfo((host, port), 0)
     w_l = space.appexec([w_socket, space.wrap(host), space.wrap(port)],
                         "(_socket, host, port): return _socket.getnameinfo((host, port), 0)")
     assert space.unwrap(w_l) == info
+    sockaddr = space.newtuple([space.wrap('mail.python.org'), space.wrap(0)])
+    space.raises_w(get_error(space, 'error'), space.appexec,
+                   [w_socket, sockaddr, space.wrap(0)],
+                   "(_socket, sockaddr, flags): return _socket.getnameinfo(sockaddr, flags)")
+
 
 def test_timeout():
     space.appexec([w_socket, space.wrap(25.4)],

pypy/tool/pytest/apptest.py

         self.excinfo = excinfo
 
 
+def py3k_repr(value):
+    "return the repr() that py3k would give for an object."""
+    if isinstance(value, str):
+        # python2 string -> Bytes string
+        return "b" + repr(value)
+    elif isinstance(value, unicode):
+        # python2 unicode -> python3 string
+        return repr(value)[1:]
+    elif isinstance(value, list):
+        return '[' + ', '.join(py3k_repr(item) for item in value) + ']'
+    elif isinstance(value, tuple):
+        return '(' + ', '.join(py3k_repr(item) for item in value) + ',)'
+    elif isinstance(value, dict):
+        return '{' + ', '.join('%s: %s' % (py3k_repr(key), py3k_repr(value))
+                               for key, value in value.items()) + '}'
+    elif isinstance(value, long):
+        return repr(value)[:-1]
+    elif isinstance(value, float):
+        r = repr(value)
+        if r in ('nan', 'inf', '-inf'):
+            return "float(%r)" % r
+        else:
+            return r
+    elif isinstance(value, type):
+        return type.__name__    
+    else:
+        return repr(value)
+
+
 def run_with_python(python_, target_, **definitions):
     if python_ is None:
         py.test.skip("Cannot find the default python3 interpreter to run with -A")
     def skip(message):
         print(message)
         raise SystemExit(0)
+    __builtins__.skip = skip
+    __builtins__.py3k_skip = skip
     class ExceptionWrapper:
         pass
     def raises(exc, func, *args, **kwargs):
             return res
         else:
             raise AssertionError("DID NOT RAISE")
+    __builtins__.raises = raises
     class Test:
         pass
     self = Test()
 """
     defs = []
-    for symbol, value in definitions.items():
+    for symbol, value in sorted(definitions.items()):
         if isinstance(value, tuple) and isinstance(value[0], py.code.Source):
             code, args = value
             defs.append(str(code))
             arg_repr = []
             for arg in args:
-                if isinstance(arg, str):
-                    arg_repr.append("b%r" % arg)
-                elif isinstance(arg, unicode):
-                    arg_repr.append(repr(arg)[1:])
+                if isinstance(arg, types.FunctionType):
+                    arg_repr.append(arg.__name__)
+                elif isinstance(arg, types.MethodType):
+                    arg_repr.append(arg.__name__)
                 else:
-                    arg_repr.append(repr(arg))
-            args = ','.join(arg_repr)
+                    arg_repr.append(py3k_repr(arg))
+            args = ', '.join(arg_repr)
             defs.append("self.%s = anonymous(%s)\n" % (symbol, args))
         elif isinstance(value, types.MethodType):
             # "def w_method(self)"
         elif isinstance(value, types.ModuleType):
             name = value.__name__
             defs.append("import %s; self.%s = %s\n" % (name, symbol, name))
-        elif isinstance(value, str):
-            # python2 string -> Bytes string
-            defs.append("self.%s = b%r\n" % (symbol, value))
-        elif isinstance(value, unicode):
-            # python2 unicode -> python3 string
-            defs.append("self.%s = %s\n" % (symbol, repr(value)[1:]))
-        elif isinstance(value, (int, float, list, dict)):
-            defs.append("self.%s = %r\n" % (symbol, value))
+        elif isinstance(value, (str, unicode, int, float, list, dict)):
+            defs.append("self.%s = %s\n" % (symbol, py3k_repr(value)))
     source = py.code.Source(target_)[1:]
     pyfile = udir.join('src.py')
-    source = helpers + '\n'.join(defs) + 'if 1:\n' + str(source)
+    target_name = target_.__name__
     with pyfile.open('w') as f:
-        f.write(source)
+        f.write(helpers)
+        f.write('\n'.join(defs))
+        f.write('def %s():\n' % target_name)
+        f.write(str(source))
+        f.write("\n%s()\n" % target_name)
     res, stdout, stderr = runsubprocess.run_subprocess(
         python_, [str(pyfile)])
-    print source
+    print pyfile.read()
     print >> sys.stdout, stdout
     print >> sys.stderr, stderr
     if res > 0:
-        raise AssertionError("Subprocess failed")
+        raise AssertionError("Subprocess failed:\n" + stderr)
 
 
 def extract_docstring_if_empty_function(fn):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.