[pypy-commit] pypy py3k: merge heads

antocuni noreply at buildbot.pypy.org
Wed Oct 24 12:40:49 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r58397:cc7ea41e3eab
Date: 2012-10-24 12:40 +0200
http://bitbucket.org/pypy/pypy/changeset/cc7ea41e3eab/

Log:	merge heads

diff too long, truncating to 2000 out of 2970 lines

diff --git a/lib-python/3.2/inspect.py b/lib-python/3.2/inspect.py
--- a/lib-python/3.2/inspect.py
+++ b/lib-python/3.2/inspect.py
@@ -767,7 +767,13 @@
     and 'varkw' are the names of the * and ** arguments or None."""
 
     if not iscode(co):
-        raise TypeError('{!r} is not a code object'.format(co))
+        if hasattr(len, 'func_code') and type(co) is type(len.func_code):
+            # PyPy extension: built-in function objects have a func_code too.
+            # There is no co_code on it, but co_argcount and co_varnames and
+            # co_flags are present.
+            pass
+        else:
+            raise TypeError('{!r} is not a code object'.format(co))
 
     nargs = co.co_argcount
     names = co.co_varnames
diff --git a/lib-python/3.2/test/test_module.py b/lib-python/3.2/test/test_module.py
--- a/lib-python/3.2/test/test_module.py
+++ b/lib-python/3.2/test/test_module.py
@@ -1,6 +1,6 @@
 # Test the module type
 import unittest
-from test.support import run_unittest, gc_collect
+from test.support import run_unittest, gc_collect, check_impl_detail
 
 import sys
 ModuleType = type(sys)
@@ -10,8 +10,10 @@
         # An uninitialized module has no __dict__ or __name__,
         # and __doc__ is None
         foo = ModuleType.__new__(ModuleType)
-        self.assertTrue(foo.__dict__ is None)
-        self.assertRaises(SystemError, dir, foo)
+        self.assertFalse(foo.__dict__)
+        if check_impl_detail():
+            self.assertTrue(foo.__dict__ is None)
+            self.assertRaises(SystemError, dir, foo)
         try:
             s = foo.__name__
             self.fail("__name__ = %s" % repr(s))
diff --git a/lib-python/3.2/test/test_weakref.py b/lib-python/3.2/test/test_weakref.py
--- a/lib-python/3.2/test/test_weakref.py
+++ b/lib-python/3.2/test/test_weakref.py
@@ -8,6 +8,7 @@
 import copy
 
 from test import support
+from test.support import gc_collect
 
 # Used in ReferencesTestCase.test_ref_created_during_del() .
 ref_from_del = None
@@ -67,6 +68,7 @@
         ref1 = weakref.ref(o, self.callback)
         ref2 = weakref.ref(o, self.callback)
         del o
+        gc_collect()
         self.assertTrue(ref1() is None,
                      "expected reference to be invalidated")
         self.assertTrue(ref2() is None,
@@ -98,13 +100,16 @@
         ref1 = weakref.proxy(o, self.callback)
         ref2 = weakref.proxy(o, self.callback)
         del o
+        gc_collect()
 
         def check(proxy):
             proxy.bar
 
         self.assertRaises(ReferenceError, check, ref1)
         self.assertRaises(ReferenceError, check, ref2)
-        self.assertRaises(ReferenceError, bool, weakref.proxy(C()))
+        ref3 = weakref.proxy(C())
+        gc_collect()
+        self.assertRaises(ReferenceError, bool, ref3)
         self.assertEqual(self.cbcalled, 2)
 
     def check_basic_ref(self, factory):
@@ -121,6 +126,7 @@
         o = factory()
         ref = weakref.ref(o, self.callback)
         del o
+        gc_collect()
         self.assertTrue(self.cbcalled == 1,
                      "callback did not properly set 'cbcalled'")
         self.assertTrue(ref() is None,
@@ -145,6 +151,7 @@
         self.assertTrue(weakref.getweakrefcount(o) == 2,
                      "wrong weak ref count for object")
         del proxy
+        gc_collect()
         self.assertTrue(weakref.getweakrefcount(o) == 1,
                      "wrong weak ref count for object after deleting proxy")
 
@@ -320,6 +327,7 @@
                      "got wrong number of weak reference objects")
 
         del ref1, ref2, proxy1, proxy2
+        gc_collect()
         self.assertTrue(weakref.getweakrefcount(o) == 0,
                      "weak reference objects not unlinked from"
                      " referent when discarded.")
@@ -333,6 +341,7 @@
         ref1 = weakref.ref(o, self.callback)
         ref2 = weakref.ref(o, self.callback)
         del ref1
+        gc_collect()
         self.assertTrue(weakref.getweakrefs(o) == [ref2],
                      "list of refs does not match")
 
@@ -340,10 +349,12 @@
         ref1 = weakref.ref(o, self.callback)
         ref2 = weakref.ref(o, self.callback)
         del ref2
+        gc_collect()
         self.assertTrue(weakref.getweakrefs(o) == [ref1],
                      "list of refs does not match")
 
         del ref1
+        gc_collect()
         self.assertTrue(weakref.getweakrefs(o) == [],
                      "list of refs not cleared")
 
@@ -629,9 +640,11 @@
         gc.collect()
         self.assertEqual(alist, [])
 
+    @support.impl_detail(pypy=False)
     def test_gc_during_ref_creation(self):
         self.check_gc_during_creation(weakref.ref)
 
+    @support.impl_detail(pypy=False)
     def test_gc_during_proxy_creation(self):
         self.check_gc_during_creation(weakref.proxy)
 
@@ -709,6 +722,7 @@
         self.assertTrue(mr.called)
         self.assertEqual(mr.value, 24)
         del o
+        gc_collect()
         self.assertTrue(mr() is None)
         self.assertTrue(mr.called)
 
@@ -835,15 +849,18 @@
         del items1, items2
         self.assertEqual(len(dict), self.COUNT)
         del objects[0]
+        gc_collect()
         self.assertEqual(len(dict), self.COUNT - 1,
                      "deleting object did not cause dictionary update")
         del objects, o
+        gc_collect()
         self.assertEqual(len(dict), 0,
                      "deleting the values did not clear the dictionary")
         # regression on SF bug #447152:
         dict = weakref.WeakValueDictionary()
         self.assertRaises(KeyError, dict.__getitem__, 1)
         dict[2] = C()
+        gc_collect()
         self.assertRaises(KeyError, dict.__getitem__, 2)
 
     def test_weak_keys(self):
@@ -864,9 +881,11 @@
         del items1, items2
         self.assertEqual(len(dict), self.COUNT)
         del objects[0]
+        gc_collect()
         self.assertTrue(len(dict) == (self.COUNT - 1),
                      "deleting object did not cause dictionary update")
         del objects, o
+        gc_collect()
         self.assertTrue(len(dict) == 0,
                      "deleting the keys did not clear the dictionary")
         o = Object(42)
@@ -1225,6 +1244,7 @@
         for o in objs:
             count += 1
             del d[o]
+        gc_collect()
         self.assertEqual(len(d), 0)
         self.assertEqual(count, 2)
 
@@ -1317,6 +1337,7 @@
 >>> id2obj(a_id) is a
 True
 >>> del a
+>>> gc_collect()
 >>> try:
 ...     id2obj(a_id)
 ... except KeyError:
diff --git a/lib_pypy/_ctypes/structure.py b/lib_pypy/_ctypes/structure.py
--- a/lib_pypy/_ctypes/structure.py
+++ b/lib_pypy/_ctypes/structure.py
@@ -69,7 +69,8 @@
                 resnames.append(name)
         names = resnames
     self._names = names
-    self.__dict__.update(fields)
+    for name, field in fields.items():
+        setattr(self, name, field)
 
 class Field(object):
     def __init__(self, name, offset, size, ctype, num, is_bitfield):
diff --git a/lib_pypy/_sha1.py b/lib_pypy/_sha1.py
--- a/lib_pypy/_sha1.py
+++ b/lib_pypy/_sha1.py
@@ -43,7 +43,7 @@
 
     # Strip off leading zeros.
     for i in range(len(s)):
-        if s[i] <> '\000':
+        if s[i] != '\000':
             break
     else:
         # Only happens when n == 0.
diff --git a/lib_pypy/_sha512.py b/lib_pypy/_sha512.py
--- a/lib_pypy/_sha512.py
+++ b/lib_pypy/_sha512.py
@@ -32,10 +32,10 @@
     W = []
     
     d = sha_info['data']
-    for i in xrange(0,16):
+    for i in range(0,16):
         W.append( (d[8*i]<<56) + (d[8*i+1]<<48) + (d[8*i+2]<<40) + (d[8*i+3]<<32) + (d[8*i+4]<<24) + (d[8*i+5]<<16) + (d[8*i+6]<<8) + d[8*i+7])
     
-    for i in xrange(16,80):
+    for i in range(16,80):
         W.append( (Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]) & 0xffffffffffffffff )
     
     ss = sha_info['digest'][:]
@@ -152,14 +152,6 @@
     sha_info['digestsize'] = 48
     return sha_info
 
-def getbuf(s):
-    if isinstance(s, str):
-        return s
-    elif isinstance(s, unicode):
-        return str(s)
-    else:
-        return buffer(s)
-
 def sha_update(sha_info, buffer):
     count = len(buffer)
     buffer_idx = 0
@@ -199,7 +191,7 @@
     
     # copy buffer
     pos = sha_info['local']
-    sha_info['data'][pos:pos+count] = [struct.unpack('B',c)[0] for c in buffer[buffer_idx:buffer_idx + count]]
+    sha_info['data'][pos:pos+count] = buffer[buffer_idx:buffer_idx + count]
     sha_info['local'] = count
 
 def sha_final(sha_info):
@@ -240,7 +232,7 @@
     dig = []
     for i in sha_info['digest']:
         dig.extend([ ((i>>56) & 0xff), ((i>>48) & 0xff), ((i>>40) & 0xff), ((i>>32) & 0xff), ((i>>24) & 0xff), ((i>>16) & 0xff), ((i>>8) & 0xff), (i & 0xff) ])
-    return ''.join([chr(i) for i in dig])
+    return bytes(dig)
 
 class sha512(object):
     digest_size = digestsize = SHA_DIGESTSIZE
@@ -249,10 +241,10 @@
     def __init__(self, s=None):
         self._sha = sha_init()
         if s:
-            sha_update(self._sha, getbuf(s))
+            sha_update(self._sha, s)
     
     def update(self, s):
-        sha_update(self._sha, getbuf(s))
+        sha_update(self._sha, s)
     
     def digest(self):
         return sha_final(self._sha.copy())[:self._sha['digestsize']]
@@ -271,7 +263,7 @@
     def __init__(self, s=None):
         self._sha = sha384_init()
         if s:
-            sha_update(self._sha, getbuf(s))
+            sha_update(self._sha, s)
 
     def copy(self):
         new = sha384.__new__(sha384)
diff --git a/lib_pypy/pypy_test/test_ctypes_support.py b/lib_pypy/pypy_test/test_ctypes_support.py
deleted file mode 100644
--- a/lib_pypy/pypy_test/test_ctypes_support.py
+++ /dev/null
@@ -1,32 +0,0 @@
-from __future__ import absolute_import
-
-import py
-from ctypes import *
-try:
-    from ctypes_support import standard_c_lib, get_errno, set_errno
-except ImportError:    # on top of cpython
-    from lib_pypy.ctypes_support import standard_c_lib, get_errno, set_errno
-
-
-def test_stdlib_and_errno():
-    py.test.skip("this is expected on top of pypy, we need to fix ctypes in a way that is now in 2.6 in order to make this reliable")
-    write = standard_c_lib.write
-    write.argtypes = [c_int, c_char_p, c_size_t]
-    write.restype = c_size_t
-    # clear errno first
-    set_errno(0)
-    assert get_errno() == 0
-    write(-345, "abc", 3)
-    assert get_errno() != 0
-    set_errno(0)
-    assert get_errno() == 0
-
-def test_argument_conversion_and_checks():
-    strlen = standard_c_lib.strlen
-    strlen.argtypes = [c_char_p]
-    strlen.restype = c_size_t
-    assert strlen("eggs") == 4
-
-    # Should raise ArgumentError, not segfault
-    py.test.raises(ArgumentError, strlen, False)
-
diff --git a/pypy/conftest.py b/pypy/conftest.py
--- a/pypy/conftest.py
+++ b/pypy/conftest.py
@@ -158,9 +158,7 @@
         body = body.lstrip()
         assert body.startswith('(')
         src = py.code.Source("def anonymous" + body)
-        d = {}
-        exec src.compile() in d
-        return d['anonymous'](*args)
+        return (src, args)
 
     def wrap(self, obj):
         return obj
@@ -208,7 +206,7 @@
     if option.runappdirect:
         py.test.skip("translation test, skipped for appdirect")
 
-def run_with_python(python, target):
+def run_with_python(python, target, **definitions):
     if python is None:
         py.test.skip("Cannot find the default python3 interpreter to run with -A")
     # we assume that the source of target is in utf-8. Unfortunately, we don't
@@ -239,10 +237,20 @@
             return res
         else:
             raise AssertionError("DID NOT RAISE")
+    class Test:
+        pass
+    self = Test()
 """
+    defs = []
+    for symbol, value in definitions.items():
+        if isinstance(value, tuple) and isinstance(value[0], py.code.Source):
+            code, args = value
+            defs.append(str(code))
+            args = ','.join(repr(arg) for arg in args)
+            defs.append("self.%s = anonymous(%s)\n" % (symbol, args))
     source = py.code.Source(target)[1:].deindent()
     pyfile = udir.join('src.py')
-    source = helpers + str(source)
+    source = helpers + '\n'.join(defs) + str(source)
     with pyfile.open('w') as f:
         f.write(source)
     res, stdout, stderr = runsubprocess.run_subprocess(
@@ -504,9 +512,11 @@
     def runtest(self):
         target = self.obj
         src = extract_docstring_if_empty_function(target.im_func)
+        space = target.im_self.space
         if self.config.option.runappdirect:
-            return run_with_python(self.config.option.python, src)
-        space = target.im_self.space
+            appexec_definitions = self.parent.obj.__dict__
+            return run_with_python(self.config.option.python, src,
+                                   **appexec_definitions)
         filename = self._getdynfilename(target)
         func = app2interp_temp(src, filename=filename)
         w_instance = self.parent.w_instance
diff --git a/pypy/doc/config/objspace.usemodules._string.txt b/pypy/doc/config/objspace.usemodules._string.txt
new file mode 100644
--- /dev/null
+++ b/pypy/doc/config/objspace.usemodules._string.txt
@@ -0,0 +1,2 @@
+Use the '_string' module, which provides the utility functions for the
+``string.Formatter`` class.
diff --git a/pypy/interpreter/astcompiler/astbuilder.py b/pypy/interpreter/astcompiler/astbuilder.py
--- a/pypy/interpreter/astcompiler/astbuilder.py
+++ b/pypy/interpreter/astcompiler/astbuilder.py
@@ -853,6 +853,11 @@
             elif comp_type == tokens.GREATEREQUAL:
                 return ast.GtE
             elif comp_type == tokens.NOTEQUAL:
+                flufl = self.compile_info.flags & consts.CO_FUTURE_BARRY_AS_BDFL
+                if flufl and comp_node.value == '!=':
+                    self.error('invalid comparison', comp_node)
+                elif not flufl and comp_node.value == '<>':
+                    self.error('invalid comparison', comp_node)
                 return ast.NotEq
             elif comp_type == tokens.NAME:
                 if comp_node.value == "is":
@@ -1118,7 +1123,6 @@
         elif first_child_type == tokens.STRING:
             space = self.space
             encoding = self.compile_info.encoding
-            flags = self.compile_info.flags
             try:
                 sub_strings_w = [parsestring.parsestr(space, encoding, s.value)
                                  for s in atom_node.children]
diff --git a/pypy/interpreter/astcompiler/consts.py b/pypy/interpreter/astcompiler/consts.py
--- a/pypy/interpreter/astcompiler/consts.py
+++ b/pypy/interpreter/astcompiler/consts.py
@@ -15,6 +15,7 @@
 CO_FUTURE_WITH_STATEMENT = 0x8000
 CO_FUTURE_PRINT_FUNCTION = 0x10000
 CO_FUTURE_UNICODE_LITERALS = 0x20000
+CO_FUTURE_BARRY_AS_BDFL = 0x40000
 CO_CONTAINSGLOBALS = 0x80000 # pypy-specific: need to check that it's not used
                              # by any other flag
 
diff --git a/pypy/interpreter/astcompiler/test/test_astbuilder.py b/pypy/interpreter/astcompiler/test/test_astbuilder.py
--- a/pypy/interpreter/astcompiler/test/test_astbuilder.py
+++ b/pypy/interpreter/astcompiler/test/test_astbuilder.py
@@ -15,15 +15,18 @@
     def setup_class(cls):
         cls.parser = pyparse.PythonParser(cls.space)
 
-    def get_ast(self, source, p_mode="exec"):
-        info = pyparse.CompileInfo("<test>", p_mode,
-                                   consts.CO_FUTURE_WITH_STATEMENT)
+    def get_ast(self, source, p_mode=None, flags=None):
+        if p_mode is None:
+            p_mode = "exec"
+        if flags is None:
+            flags = consts.CO_FUTURE_WITH_STATEMENT
+        info = pyparse.CompileInfo("<test>", p_mode, flags)
         tree = self.parser.parse_source(source, info)
         ast_node = ast_from_node(self.space, tree, info)
         return ast_node
 
-    def get_first_expr(self, source):
-        mod = self.get_ast(source)
+    def get_first_expr(self, source, p_mode=None, flags=None):
+        mod = self.get_ast(source, p_mode, flags)
         assert len(mod.body) == 1
         expr = mod.body[0]
         assert isinstance(expr, ast.Expr)
@@ -853,7 +856,6 @@
             ("<=", ast.LtE),
             ("==", ast.Eq),
             ("!=", ast.NotEq),
-            ("<>", ast.NotEq),
             ("in", ast.In),
             ("is", ast.Is),
             ("is not", ast.IsNot),
@@ -881,6 +883,20 @@
             names = comp.left.id + "".join(n.id for n in comp.comparators)
             assert names == vars
 
+    def test_flufl(self):
+        source = "x <> y"
+        raises(SyntaxError, self.get_ast, source)
+        comp = self.get_first_expr(source,
+                                   flags=consts.CO_FUTURE_BARRY_AS_BDFL)
+        assert isinstance(comp, ast.Compare)
+        assert isinstance(comp.left, ast.Name)
+        assert comp.left.ctx == ast.Load
+        assert len(comp.ops) == 1
+        assert comp.ops[0] == ast.NotEq
+        assert len(comp.comparators) == 1
+        assert isinstance(comp.comparators[0], ast.Name)
+        assert comp.comparators[0].ctx == ast.Load
+
     def test_binop(self):
         binops = (
             ("|", ast.BitOr),
diff --git a/pypy/interpreter/astcompiler/test/test_compiler.py b/pypy/interpreter/astcompiler/test/test_compiler.py
--- a/pypy/interpreter/astcompiler/test/test_compiler.py
+++ b/pypy/interpreter/astcompiler/test/test_compiler.py
@@ -817,7 +817,17 @@
         def f():
             try:
                 raise TypeError() from ValueError()
-            except TypeError:
+            except TypeError as e:
+                assert isinstance(e.__cause__, ValueError)
+                return 42
+        """
+        yield self.st, test, "f()", 42
+        test = """if 1:
+        def f():
+            try:
+                raise TypeError from ValueError
+            except TypeError as e:
+                assert isinstance(e.__cause__, ValueError)
                 return 42
         """
         yield self.st, test, "f()", 42
diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -434,6 +434,7 @@
             raise operationerrfmt(space.w_ValueError,
                 "%s() requires a code object with %d free vars, not %d",
                 self.name, closure_len, len(code.co_freevars))
+        self.fget_func_doc(space)    # see test_issue1293
         self.code = code
 
     def fget_func_closure(self, space):
diff --git a/pypy/interpreter/pycompiler.py b/pypy/interpreter/pycompiler.py
--- a/pypy/interpreter/pycompiler.py
+++ b/pypy/interpreter/pycompiler.py
@@ -101,7 +101,7 @@
     """
     def __init__(self, space, override_version=None):
         PyCodeCompiler.__init__(self, space)
-        self.future_flags = future.futureFlags_2_7
+        self.future_flags = future.futureFlags_3_2
         self.parser = pyparse.PythonParser(space, self.future_flags)
         self.additional_rules = {}
         self.compiler_flags = self.future_flags.allowed_flags
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -484,6 +484,8 @@
         w_value = w_cause = space.w_None
         if nbargs == 2:
             w_cause = self.popvalue()
+            if space.exception_is_valid_obj_as_class_w(w_cause):
+                w_cause = space.call_function(w_cause)
         w_value = self.popvalue()
         if space.exception_is_valid_obj_as_class_w(w_value):
             w_type = w_value
diff --git a/pypy/interpreter/pyparser/future.py b/pypy/interpreter/pyparser/future.py
--- a/pypy/interpreter/pyparser/future.py
+++ b/pypy/interpreter/pyparser/future.py
@@ -24,8 +24,9 @@
 the "in" comparisons with explicit numeric comparisons.
 """
 
-from pypy.interpreter.astcompiler.consts import CO_GENERATOR_ALLOWED, \
-    CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_ABSOLUTE_IMPORT
+from pypy.interpreter.astcompiler.consts import (
+    CO_GENERATOR_ALLOWED, CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT,
+    CO_FUTURE_ABSOLUTE_IMPORT, CO_FUTURE_BARRY_AS_BDFL)
 
 def get_futures(future_flags, source):
     futures = FutureAutomaton(future_flags, source)
diff --git a/pypy/interpreter/test/test_compiler.py b/pypy/interpreter/test/test_compiler.py
--- a/pypy/interpreter/test/test_compiler.py
+++ b/pypy/interpreter/test/test_compiler.py
@@ -778,6 +778,22 @@
         for case in cases:
             raises(SyntaxError, compile, case, "<test>", "exec")
 
+    def test_barry_as_bdfl(self):
+        # from test_flufl.py :-)
+        import __future__
+        code = "from __future__ import barry_as_FLUFL; 2 {0} 3"
+        compile(code.format('<>'), '<BDFL test>', 'exec',
+                __future__.CO_FUTURE_BARRY_AS_BDFL)
+        raises(SyntaxError, compile, code.format('!='),
+               '<FLUFL test>', 'exec',
+               __future__.CO_FUTURE_BARRY_AS_BDFL)
+
+    def test_guido_as_bdfl(self):
+        # from test_flufl.py :-)
+        code = '2 {0} 3'
+        compile(code.format('!='), '<BDFL test>', 'exec')
+        raises(SyntaxError, compile, code.format('<>'),
+               '<FLUFL test>', 'exec')
 
 
 class AppTestOptimizer:
diff --git a/pypy/interpreter/test/test_function.py b/pypy/interpreter/test/test_function.py
--- a/pypy/interpreter/test/test_function.py
+++ b/pypy/interpreter/test/test_function.py
@@ -322,6 +322,12 @@
         assert f.__doc__ == "hi"
         assert type(f.__doc__) is str
 
+    def test_issue1293(self):
+        def f1(): "doc f1"
+        def f2(): "doc f2"
+        f1.__code__ = f2.__code__
+        assert f1.__doc__ == "doc f1"
+
     def test_subclassing(self):
         # cannot subclass 'function' or 'builtin_function'
         def f():
diff --git a/pypy/module/__builtin__/operation.py b/pypy/module/__builtin__/operation.py
--- a/pypy/module/__builtin__/operation.py
+++ b/pypy/module/__builtin__/operation.py
@@ -3,11 +3,9 @@
 """
 
 from pypy.interpreter import gateway
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.gateway import unwrap_spec, WrappedDefault
 from pypy.rlib.runicode import UNICHR
-from pypy.rlib.rfloat import isnan, isinf, round_double
-from pypy.rlib import rfloat
 import __builtin__
 
 def abs(space, w_val):
@@ -21,11 +19,8 @@
     object, but escape the non-ASCII characters in the string returned by
     repr() using \\x, \\u or \\U escapes.  This generates a string similar
     to that returned by repr() in Python 2."""
-    from pypy.objspace.std.unicodetype import decode_object, encode_object
-    # repr is guaranteed to be unicode
-    w_repr = space.repr(w_obj)
-    w_encoded = encode_object(space, w_repr, 'ascii', 'backslashreplace')
-    return decode_object(space, w_encoded, 'ascii', None)
+    from pypy.objspace.std.unicodetype import ascii_from_object
+    return ascii_from_object(space, w_obj)
 
 @unwrap_spec(code=int)
 def chr(space, code):
@@ -102,40 +97,22 @@
 
 # ____________________________________________________________
 
-# Here 0.30103 is an upper bound for log10(2)
-NDIGITS_MAX = int((rfloat.DBL_MANT_DIG - rfloat.DBL_MIN_EXP) * 0.30103)
-NDIGITS_MIN = -int((rfloat.DBL_MAX_EXP + 1) * 0.30103)
-
- at unwrap_spec(number=float, w_ndigits = WrappedDefault(0))
-def round(space, number, w_ndigits):
-    """round(number[, ndigits]) -> floating point number
+ at unwrap_spec(w_ndigits=WrappedDefault(None))
+def round(space, w_number, w_ndigits=None):
+    """round(number[, ndigits]) -> number
 
 Round a number to a given precision in decimal digits (default 0 digits).
-This always returns a floating point number.  Precision may be negative."""
-    # Algorithm copied directly from CPython
-
-    # interpret 2nd argument as a Py_ssize_t; clip on overflow
-    ndigits = space.getindex_w(w_ndigits, None)
-
-    # nans, infinities and zeros round to themselves
-    if number == 0 or isinf(number) or isnan(number):
-        return space.wrap(number)
-
-    # Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
-    # always rounds to itself.  For ndigits < NDIGITS_MIN, x always
-    # rounds to +-0.0.
-    if ndigits > NDIGITS_MAX:
-        return space.wrap(number)
-    elif ndigits < NDIGITS_MIN:
-        # return 0.0, but with sign of x
-        return space.wrap(0.0 * number)
-
-    # finite x, and ndigits is not unreasonably large
-    z = round_double(number, ndigits)
-    if isinf(z):
-        raise OperationError(space.w_OverflowError,
-                             space.wrap("rounded value too large to represent"))
-    return space.wrap(z)
+This returns an int when called with one argument, otherwise the
+same type as the number. ndigits may be negative."""
+    round = space.lookup(w_number, '__round__')
+    if round is None:
+        raise operationerrfmt(space.w_TypeError,
+                              "type %s doesn't define __round__ method",
+                              space.type(w_number).getname(space))
+    if space.is_none(w_ndigits):
+        return space.get_and_call_function(round, w_number)
+    else:
+        return space.get_and_call_function(round, w_number, w_ndigits)
 
 # ____________________________________________________________
 
diff --git a/pypy/module/__builtin__/test/test_builtin.py b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -642,6 +642,77 @@
         assert round(562949953421312.5, 1) == 562949953421312.5
         assert round(56294995342131.5, 3) == 56294995342131.5
 
+        assert round(0.0) == 0.0
+        assert type(round(0.0)) == int
+        assert round(1.0) == 1.0
+        assert round(10.0) == 10.0
+        assert round(1000000000.0) == 1000000000.0
+        assert round(1e20) == 1e20
+
+        assert round(-1.0) == -1.0
+        assert round(-10.0) == -10.0
+        assert round(-1000000000.0) == -1000000000.0
+        assert round(-1e20) == -1e20
+
+        assert round(0.1) == 0.0
+        assert round(1.1) == 1.0
+        assert round(10.1) == 10.0
+        assert round(1000000000.1) == 1000000000.0
+
+        assert round(-1.1) == -1.0
+        assert round(-10.1) == -10.0
+        assert round(-1000000000.1) == -1000000000.0
+
+        assert round(0.9) == 1.0
+        assert round(9.9) == 10.0
+        assert round(999999999.9) == 1000000000.0
+
+        assert round(-0.9) == -1.0
+        assert round(-9.9) == -10.0
+        assert round(-999999999.9) == -1000000000.0
+
+        assert round(-8.0, -1) == -10.0
+        assert type(round(-8.0, -1)) == float
+
+        assert type(round(-8.0, 0)) == float
+        assert type(round(-8.0, 1)) == float
+
+        # Check even / odd rounding behaviour
+        assert round(5.5) == 6
+        assert round(6.5) == 6
+        assert round(-5.5) == -6
+        assert round(-6.5) == -6
+
+        # Check behavior on ints
+        assert round(0) == 0
+        assert round(8) == 8
+        assert round(-8) == -8
+        assert type(round(0)) == int
+        assert type(round(-8, -1)) == int
+        assert type(round(-8, 0)) == int
+        assert type(round(-8, 1)) == int
+
+        assert round(number=-8.0, ndigits=-1) == -10.0
+        raises(TypeError, round)
+
+        # test generic rounding delegation for reals
+        class TestRound:
+            def __round__(self):
+                return 23
+
+        class TestNoRound:
+            pass
+
+        assert round(TestRound()) == 23
+
+        raises(TypeError, round, 1, 2, 3)
+        raises(TypeError, round, TestNoRound())
+
+        t = TestNoRound()
+        t.__round__ = lambda *args: args
+        raises(TypeError, round, t)
+        raises(TypeError, round, t, 0)
+
     def test_vars_obscure_case(self):
         class C_get_vars(object):
             def getDict(self):
diff --git a/pypy/module/_cffi_backend/ctypefunc.py b/pypy/module/_cffi_backend/ctypefunc.py
--- a/pypy/module/_cffi_backend/ctypefunc.py
+++ b/pypy/module/_cffi_backend/ctypefunc.py
@@ -4,8 +4,9 @@
 
 import sys
 from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.error import wrap_oserror
 from pypy.rpython.lltypesystem import lltype, llmemory, rffi
-from pypy.rlib import jit, clibffi, jit_libffi
+from pypy.rlib import jit, clibffi, jit_libffi, rposix
 from pypy.rlib.jit_libffi import CIF_DESCRIPTION, CIF_DESCRIPTION_P
 from pypy.rlib.jit_libffi import FFI_TYPE, FFI_TYPE_P, FFI_TYPE_PP
 from pypy.rlib.jit_libffi import SIZE_OF_FFI_ARG
@@ -147,9 +148,13 @@
                 argtype = self.fargs[i]
                 if isinstance(argtype, W_CTypePointer):
                     data = rffi.ptradd(buffer, cif_descr.exchange_args[i])
-                    if get_mustfree_flag(data):
+                    flag = get_mustfree_flag(data)
+                    if flag == 1:
                         raw_string = rffi.cast(rffi.CCHARPP, data)[0]
                         lltype.free(raw_string, flavor='raw')
+                    elif flag == 2:
+                        file = rffi.cast(rffi.CCHARPP, data)[0]
+                        rffi_fclose(file)
             lltype.free(buffer, flavor='raw')
         return w_res
 
@@ -164,6 +169,27 @@
     assert isinstance(abi, int)
     return space.wrap(abi)
 
+rffi_fdopen = rffi.llexternal("fdopen", [rffi.INT, rffi.CCHARP], rffi.CCHARP)
+rffi_fclose = rffi.llexternal("fclose", [rffi.CCHARP], rffi.INT)
+
+def prepare_file_call_argument(fileobj):
+    import os
+    space = fileobj.space
+    fileobj.direct_flush()
+    fd = fileobj.direct_fileno()
+    if fd < 0:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("file has no OS file descriptor"))
+    try:
+        fd2 = os.dup(fd)
+        f = rffi_fdopen(fd2, fileobj.mode)
+        if not f:
+            os.close(fd2)
+            raise OSError(rposix.get_errno(), "fdopen failed")
+    except OSError, e:
+        raise wrap_oserror(space, e)
+    return f
+
 # ____________________________________________________________
 
 
diff --git a/pypy/module/_cffi_backend/ctypeptr.py b/pypy/module/_cffi_backend/ctypeptr.py
--- a/pypy/module/_cffi_backend/ctypeptr.py
+++ b/pypy/module/_cffi_backend/ctypeptr.py
@@ -157,7 +157,7 @@
         space = self.space
         ob = space.interpclass_w(w_ob)
         if not isinstance(ob, cdataobj.W_CData):
-            raise self._convert_error("compatible pointer", w_ob)
+            raise self._convert_error("cdata pointer", w_ob)
         other = ob.ctype
         if not isinstance(other, W_CTypePtrBase):
             from pypy.module._cffi_backend import ctypearray
@@ -177,7 +177,8 @@
 
 
 class W_CTypePointer(W_CTypePtrBase):
-    _attrs_ = []
+    _attrs_ = ['is_file']
+    _immutable_fields_ = ['is_file']
 
     def __init__(self, space, ctitem):
         from pypy.module._cffi_backend import ctypearray
@@ -186,6 +187,7 @@
             extra = "(*)"    # obscure case: see test_array_add
         else:
             extra = " *"
+        self.is_file = (ctitem.name == "struct _IO_FILE")
         W_CTypePtrBase.__init__(self, space, size, extra, 2, ctitem)
 
     def newp(self, w_init):
@@ -234,7 +236,7 @@
         p = rffi.ptradd(cdata, i * self.ctitem.size)
         return cdataobj.W_CData(space, p, self)
 
-    def _prepare_pointer_call_argument(self, w_init):
+    def _prepare_pointer_call_argument(self, w_init, cdata):
         space = self.space
         if (space.isinstance_w(w_init, space.w_list) or
             space.isinstance_w(w_init, space.w_tuple)):
@@ -243,10 +245,19 @@
               space.isinstance_w(w_init, space.w_bytes)):
             # from a string, we add the null terminator
             length = space.int_w(space.len(w_init)) + 1
+        elif self.is_file:
+            from pypy.module._file.interp_file import W_File
+            from pypy.module._cffi_backend import ctypefunc
+            ob = space.interpclass_w(w_init)
+            if isinstance(ob, W_File):
+                result = ctypefunc.prepare_file_call_argument(ob)
+                rffi.cast(rffi.CCHARPP, cdata)[0] = result
+                return 2
+            return 0
         else:
-            return lltype.nullptr(rffi.CCHARP.TO)
+            return 0
         if self.ctitem.size <= 0:
-            return lltype.nullptr(rffi.CCHARP.TO)
+            return 0
         try:
             datasize = ovfcheck(length * self.ctitem.size)
         except OverflowError:
@@ -259,25 +270,19 @@
         except Exception:
             lltype.free(result, flavor='raw')
             raise
-        return result
+        rffi.cast(rffi.CCHARPP, cdata)[0] = result
+        return 1
 
     def convert_argument_from_object(self, cdata, w_ob):
         from pypy.module._cffi_backend.ctypefunc import set_mustfree_flag
         space = self.space
         ob = space.interpclass_w(w_ob)
-        if isinstance(ob, cdataobj.W_CData):
-            buffer = lltype.nullptr(rffi.CCHARP.TO)
-        else:
-            buffer = self._prepare_pointer_call_argument(w_ob)
-        #
-        if buffer:
-            rffi.cast(rffi.CCHARPP, cdata)[0] = buffer
-            set_mustfree_flag(cdata, True)
-            return True
-        else:
-            set_mustfree_flag(cdata, False)
+        result = (not isinstance(ob, cdataobj.W_CData) and
+                  self._prepare_pointer_call_argument(w_ob, cdata))
+        if result == 0:
             self.convert_from_object(cdata, w_ob)
-            return False
+        set_mustfree_flag(cdata, result)
+        return result
 
     def getcfield(self, attr):
         return self.ctitem.getcfield(attr)
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -2211,3 +2211,56 @@
     buffer(p)[:] = bytearray(b"foo\x00")
     assert len(p) == 4
     assert list(p) == [b"f", b"o", b"o", b"\x00"]
+
+def test_FILE():
+    if sys.platform == "win32":
+        py.test.skip("testing FILE not implemented")
+    #
+    BFILE = new_struct_type("_IO_FILE")
+    BFILEP = new_pointer_type(BFILE)
+    BChar = new_primitive_type("char")
+    BCharP = new_pointer_type(BChar)
+    BInt = new_primitive_type("int")
+    BFunc = new_function_type((BCharP, BFILEP), BInt, False)
+    BFunc2 = new_function_type((BFILEP, BCharP), BInt, True)
+    ll = find_and_load_library('c')
+    fputs = ll.load_function(BFunc, "fputs")
+    fscanf = ll.load_function(BFunc2, "fscanf")
+    #
+    import posix
+    fdr, fdw = posix.pipe()
+    fr1 = posix.fdopen(fdr, 'r', 256)
+    fw1 = posix.fdopen(fdw, 'w', 256)
+    #
+    fw1.write(b"X")
+    res = fputs(b"hello world\n", fw1)
+    assert res >= 0
+    fw1.close()
+    #
+    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()
+
+def test_FILE_only_for_FILE_arg():
+    if sys.platform == "win32":
+        py.test.skip("testing FILE not implemented")
+    #
+    B_NOT_FILE = new_struct_type("NOT_FILE")
+    B_NOT_FILEP = new_pointer_type(B_NOT_FILE)
+    BChar = new_primitive_type("char")
+    BCharP = new_pointer_type(BChar)
+    BInt = new_primitive_type("int")
+    BFunc = new_function_type((BCharP, B_NOT_FILEP), BInt, False)
+    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) == ("initializer for ctype 'struct NOT_FILE *' must "
+                            "be a cdata pointer, not file")
diff --git a/pypy/module/_codecs/interp_codecs.py b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -454,7 +454,7 @@
     make_decoder_wrapper('mbcs_decode')
 
 # utf-8 functions are not regular, because we have to pass
-# "allow_surrogates=True"
+# "allow_surrogates=False"
 @unwrap_spec(uni=unicode, errors='str_or_None')
 def utf_8_encode(space, uni, errors="strict"):
     if errors is None:
@@ -462,7 +462,7 @@
     state = space.fromcache(CodecState)
     result = runicode.unicode_encode_utf_8(
         uni, len(uni), errors, state.encode_error_handler,
-        allow_surrogates=True)
+        allow_surrogates=False)
     return space.newtuple([space.wrapbytes(result), space.wrap(len(uni))])
 
 @unwrap_spec(string='bufferstr', errors='str_or_None',
@@ -475,7 +475,7 @@
     result, consumed = runicode.str_decode_utf_8(
         string, len(string), errors,
         final, state.decode_error_handler,
-        allow_surrogates=True)
+        allow_surrogates=False)
     return space.newtuple([space.wrap(result), space.wrap(consumed)])
 
 @unwrap_spec(data="bufferstr", errors='str_or_None', byteorder=int,
diff --git a/pypy/module/_csv/interp_reader.py b/pypy/module/_csv/interp_reader.py
--- a/pypy/module/_csv/interp_reader.py
+++ b/pypy/module/_csv/interp_reader.py
@@ -40,6 +40,7 @@
 
     def save_field(self, field_builder):
         field = field_builder.build()
+        print "AFA SAVE FIELD", field
         if self.numeric_field:
             from pypy.objspace.std.strutil import ParseStringError
             from pypy.objspace.std.strutil import string_to_float
@@ -71,11 +72,7 @@
                             state != START_RECORD and state != EAT_CRNL and
                             (len(field_builder.build()) > 0 or
                              state == IN_QUOTED_FIELD)):
-                        if dialect.strict:
-                            raise self.error(u"newline inside string")
-                        else:
-                            self.save_field(field_builder)
-                            break
+                        raise self.error(u"newline inside string")
                 raise
             self.line_num += 1
             line = space.unicode_w(w_line)
diff --git a/pypy/module/_csv/test/test_reader.py b/pypy/module/_csv/test/test_reader.py
--- a/pypy/module/_csv/test/test_reader.py
+++ b/pypy/module/_csv/test/test_reader.py
@@ -28,7 +28,7 @@
     def test_cannot_read_bytes(self):
         import _csv
         reader = _csv.reader([b'foo'])
-        raises(TypeError, "next(reader)")
+        raises((TypeError, _csv.Error), next, reader)
 
     def test_read_oddinputs(self):
         self._read_test([], [])
@@ -107,9 +107,9 @@
         self._read_test(['12,12,1",'], [['12', '12', '1"', '']])
 
     def test_read_eof(self):
-        self._read_test(['a,"'], [['a', '']])
-        self._read_test(['"a'], [['a']])
-        self._read_test(['^'], [['\n']], escapechar='^')
-        self._read_test(['a,"'], 'Error', strict=True)
+        self._read_test(['a,"'], [])
+        self._read_test(['"a'], 'Error')
+        self._read_test(['^'], 'Error', escapechar='^')
+        self._read_test(['a,"'], [], strict=True)
         self._read_test(['"a'], 'Error', strict=True)
         self._read_test(['^'], 'Error', escapechar='^', strict=True)
diff --git a/pypy/module/_multiprocessing/interp_connection.py b/pypy/module/_multiprocessing/interp_connection.py
--- a/pypy/module/_multiprocessing/interp_connection.py
+++ b/pypy/module/_multiprocessing/interp_connection.py
@@ -102,9 +102,9 @@
             space, self.BUFFER_SIZE, maxlength)
         try:
             if newbuf:
-                return space.wrap(rffi.charpsize2str(newbuf, res))
+                return space.wrapbytes(rffi.charpsize2str(newbuf, res))
             else:
-                return space.wrap(rffi.charpsize2str(self.buffer, res))
+                return space.wrapbytes(rffi.charpsize2str(self.buffer, res))
         finally:
             if newbuf:
                 rffi.free_charp(newbuf)
@@ -118,7 +118,7 @@
             space, length - offset, PY_SSIZE_T_MAX)
         try:
             if newbuf:
-                raise BufferTooShort(space, space.wrap(
+                raise BufferTooShort(space, space.wrapbytes(
                     rffi.charpsize2str(newbuf, res)))
             rwbuffer.setslice(offset, rffi.charpsize2str(self.buffer, res))
         finally:
@@ -148,9 +148,9 @@
             space, self.BUFFER_SIZE, PY_SSIZE_T_MAX)
         try:
             if newbuf:
-                w_received = space.wrap(rffi.charpsize2str(newbuf, res))
+                w_received = space.wrapbytes(rffi.charpsize2str(newbuf, res))
             else:
-                w_received = space.wrap(rffi.charpsize2str(self.buffer, res))
+                w_received = space.wrapbytes(rffi.charpsize2str(self.buffer, res))
         finally:
             if newbuf:
                 rffi.free_charp(newbuf)
diff --git a/pypy/module/_multiprocessing/test/test_connection.py b/pypy/module/_multiprocessing/test/test_connection.py
--- a/pypy/module/_multiprocessing/test/test_connection.py
+++ b/pypy/module/_multiprocessing/test/test_connection.py
@@ -35,11 +35,10 @@
 
 class BaseConnectionTest(object):
     def test_connection(self):
-        py3k_skip('fixme later')
         rhandle, whandle = self.make_pair()
 
-        whandle.send_bytes("abc")
-        assert rhandle.recv_bytes(100) == "abc"
+        whandle.send_bytes(b"abc")
+        assert rhandle.recv_bytes(100) == b"abc"
 
         obj = [1, 2.0, "hello"]
         whandle.send(obj)
@@ -47,7 +46,6 @@
         assert obj == obj2
 
     def test_poll(self):
-        py3k_skip('fixme later')
         rhandle, whandle = self.make_pair()
 
         assert rhandle.poll() == False
@@ -61,7 +59,6 @@
         raises(IOError, whandle.poll)
 
     def test_read_into(self):
-        py3k_skip('fixme later')
         import array, multiprocessing
         rhandle, whandle = self.make_pair()
 
@@ -96,7 +93,7 @@
 class AppTestSocketConnection(BaseConnectionTest):
     def setup_class(cls):
         space = gettestobjspace(usemodules=('_multiprocessing', 'thread', 'signal',
-                                            'struct', 'array'))
+                                            'struct', 'array', '_socket'))
         cls.space = space
         cls.w_connections = space.newlist([])
 
@@ -160,7 +157,6 @@
         raises(IOError, _multiprocessing.Connection, -15)
 
     def test_byte_order(self):
-        py3k_skip('fixme later')
         import socket
         if not 'fromfd' in dir(socket):
             skip('No fromfd in socket')
diff --git a/pypy/module/_socket/__init__.py b/pypy/module/_socket/__init__.py
--- a/pypy/module/_socket/__init__.py
+++ b/pypy/module/_socket/__init__.py
@@ -25,14 +25,13 @@
         for name in """
             gethostbyname gethostbyname_ex gethostbyaddr gethostname
             getservbyname getservbyport getprotobyname
-            fromfd socketpair
+            dup socketpair
             ntohs ntohl htons htonl inet_aton inet_ntoa inet_pton inet_ntop
             getaddrinfo getnameinfo
             getdefaulttimeout setdefaulttimeout
             """.split():
 
-            if name in ('inet_pton', 'inet_ntop',
-                        'fromfd', 'socketpair',
+            if name in ('inet_pton', 'inet_ntop', 'socketpair',
                         ) and not hasattr(rsocket, name):
                 continue
             
diff --git a/pypy/module/_socket/interp_func.py b/pypy/module/_socket/interp_func.py
--- a/pypy/module/_socket/interp_func.py
+++ b/pypy/module/_socket/interp_func.py
@@ -126,18 +126,10 @@
         raise converted_error(space, e)
     return space.newtuple([space.wrap(host), space.wrap(servport)])
 
- at unwrap_spec(fd=int, family=int, type=int, proto=int)
-def fromfd(space, fd, family, type, proto=0):
-    """fromfd(fd, family, type[, proto]) -> socket object
-
-    Create a socket object from the given file descriptor.
-    The remaining arguments are the same as for socket().
-    """
-    try:
-        sock = rsocket.fromfd(fd, family, type, proto, W_RSocket)
-    except SocketError, e:
-        raise converted_error(space, e)
-    return space.wrap(sock)
+ at unwrap_spec(fd=int)
+def dup(space, fd):
+    newfd = rsocket.dup(fd)
+    return space.wrap(newfd)
 
 @unwrap_spec(family=int, type=int, proto=int)
 def socketpair(space, family=rsocket.socketpair_default_family,
diff --git a/pypy/module/_socket/interp_socket.py b/pypy/module/_socket/interp_socket.py
--- a/pypy/module/_socket/interp_socket.py
+++ b/pypy/module/_socket/interp_socket.py
@@ -93,12 +93,6 @@
         error = self.connect_ex(addr)
         return space.wrap(error)
 
-    def dup_w(self, space):
-        try:
-            return self.dup(W_RSocket)
-        except SocketError, e:
-            raise converted_error(space, e)
-    
     def fileno_w(self, space):
         """fileno() -> integer
 
@@ -179,16 +173,6 @@
         except SocketError, e:
             raise converted_error(space, e)
 
-    @unwrap_spec(w_mode = WrappedDefault("r"),
-                 w_buffsize = WrappedDefault(-1))
-    def makefile_w(self, space, w_mode=None, w_buffsize=None):
-        """makefile([mode[, buffersize]]) -> file object
-
-        Return a regular file object corresponding to the socket.
-        The mode and buffersize arguments are as for the built-in open() function.
-        """
-        return app_makefile(space, self, w_mode, w_buffsize)
-
     @unwrap_spec(buffersize='nonnegint', flags=int)
     def recv_w(self, space, buffersize, flags=0):
         """recv(buffersize[, flags]) -> data
@@ -422,17 +406,6 @@
             return
         self.close_w(space)
 
-app_makefile = gateway.applevel(r'''
-def makefile(self, mode="r", buffersize=-1):
-    """makefile([mode[, buffersize]]) -> file object
-
-    Return a regular file object corresponding to the socket.
-    The mode and buffersize arguments are as for the built-in open() function.
-    """
-    import os
-    newfd = os.dup(self.fileno())
-    return os.fdopen(newfd, mode, buffersize)
-''', filename =__file__).interphook('makefile')
 
 # ____________________________________________________________
 # Error handling
@@ -473,15 +446,11 @@
 # ____________________________________________________________
 
 socketmethodnames = """
-_accept bind close connect connect_ex dup fileno detach
-getpeername getsockname getsockopt gettimeout listen makefile
+_accept bind close connect connect_ex fileno detach
+getpeername getsockname getsockopt gettimeout listen
 recv recvfrom send sendall sendto setblocking
 setsockopt settimeout shutdown _reuse _drop recv_into recvfrom_into
 """.split()
-# Remove non-implemented methods
-for name in ('dup',):
-    if not hasattr(RSocket, name):
-        socketmethodnames.remove(name)
 if hasattr(rsocket._c, 'WSAIoctl'):
     socketmethodnames.append('ioctl')
 
@@ -509,14 +478,12 @@
 close() -- close the socket
 connect(addr) -- connect the socket to a remote address
 connect_ex(addr) -- connect, return an error code instead of an exception
-dup() -- return a new socket object identical to the current one [*]
 fileno() -- return underlying file descriptor
 getpeername() -- return remote address [*]
 getsockname() -- return local address
 getsockopt(level, optname[, buflen]) -- get socket options
 gettimeout() -- return timeout or None
 listen(n) -- start listening for incoming connections
-makefile([mode, [bufsize]]) -- return a file object for the socket [*]
 recv(buflen[, flags]) -- receive data
 recvfrom(buflen[, flags]) -- receive data and sender's address
 sendall(data[, flags]) -- send all data
diff --git a/pypy/module/_socket/test/test_sock_app.py b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -91,25 +91,6 @@
                         "(_socket, name): return _socket.getprotobyname(name)")
     assert space.unwrap(w_n) == socket.IPPROTO_TCP
 
-def test_fromfd():
-    # XXX review
-    if not hasattr(socket, 'fromfd'):
-        py.test.skip("No socket.fromfd on this platform")
-    orig_fd = path.open()
-    fd = space.appexec([w_socket, space.wrap(orig_fd.fileno()),
-            space.wrap(socket.AF_INET), space.wrap(socket.SOCK_STREAM),
-            space.wrap(0)],
-           """(_socket, fd, family, type, proto): 
-                 return _socket.fromfd(fd, family, type, proto)""")
-
-    assert space.unwrap(space.call_method(fd, 'fileno'))
-    fd = space.appexec([w_socket, space.wrap(orig_fd.fileno()),
-            space.wrap(socket.AF_INET), space.wrap(socket.SOCK_STREAM)],
-                """(_socket, fd, family, type):
-                    return _socket.fromfd(fd, family, type)""")
-
-    assert space.unwrap(space.call_method(fd, 'fileno'))
-
 def test_ntohs():
     w_n = space.appexec([w_socket, space.wrap(125)],
                         "(_socket, x): return _socket.ntohs(x)")
@@ -503,13 +484,10 @@
 
     def test_dup(self):
         import _socket as socket
-        if not hasattr(socket.socket, 'dup'):
-            skip('No dup() on this platform')
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         s.bind(('localhost', 0))
-        s2 = s.dup()
-        assert s.fileno() != s2.fileno()
-        assert s.getsockname() == s2.getsockname()
+        fd = socket.dup(s.fileno())
+        assert s.fileno() != fd
 
     def test_buffer(self):
         # Test that send/sendall/sendto accept a buffer as arg
diff --git a/pypy/module/_sre/interp_sre.py b/pypy/module/_sre/interp_sre.py
--- a/pypy/module/_sre/interp_sre.py
+++ b/pypy/module/_sre/interp_sre.py
@@ -113,12 +113,18 @@
         if endpos < pos: endpos = pos
         if space.is_true(space.isinstance(w_string, space.w_unicode)):
             unicodestr = space.unicode_w(w_string)
+            if not space.isinstance_w(self.w_pattern, space.w_unicode):
+                raise OperationError(space.w_TypeError, space.wrap(
+                        "can't use a string pattern on a bytes-like object"))
             if pos > len(unicodestr): pos = len(unicodestr)
             if endpos > len(unicodestr): endpos = len(unicodestr)
             return rsre_core.UnicodeMatchContext(self.code, unicodestr,
                                                  pos, endpos, self.flags)
         else:
             str = space.bufferstr_w(w_string)
+            if space.isinstance_w(self.w_pattern, space.w_unicode):
+                raise OperationError(space.w_TypeError, space.wrap(
+                        "can't use a bytes pattern on a string-like object"))
             if pos > len(str): pos = len(str)
             if endpos > len(str): endpos = len(str)
             return rsre_core.StrMatchContext(self.code, str,
diff --git a/pypy/module/_sre/test/test_app_sre.py b/pypy/module/_sre/test/test_app_sre.py
--- a/pypy/module/_sre/test/test_app_sre.py
+++ b/pypy/module/_sre/test/test_app_sre.py
@@ -5,17 +5,20 @@
 from pypy.interpreter.gateway import app2interp_temp
 from pypy.conftest import gettestobjspace, option
 
-def init_globals_hack(space):
-    space.appexec([space.wrap(autopath.this_dir)], """(this_dir):
-    import builtins as b
-    import sys, os.path
-    # Uh-oh, ugly hack
-    sys.path.insert(0, this_dir)
-    import support_test_app_sre
-    b.s = support_test_app_sre
-    sys.path.pop(0)
+def init_app_test(cls, space):
+    cls.w_s = space.appexec([space.wrap(autopath.this_dir)], 
+                              """(this_dir):
+        import sys
+        # Uh-oh, ugly hack
+        sys.path.insert(0, this_dir)
+        try:
+            import support_test_app_sre
+            return support_test_app_sre
+        finally:
+            sys.path.pop(0)
     """)
 
+
 class AppTestSrePy:
 
     def test_magic(self):
@@ -194,8 +197,8 @@
         # which (if interpreted literally, as CPython does) gives the
         # following strangeish rules:
         assert isinstance(re.sub("a", "b", "diwoiioamoi"), str)
-        assert isinstance(re.sub("a", "b", b"diwoiiobmoi"), bytes)
-        assert isinstance(re.sub('x', b'y', b'x'), bytes)
+        raises(TypeError, re.sub, "a", "b", b"diwoiiobmoi")
+        raises(TypeError, re.sub, 'x', b'y', b'x')
 
     def test_sub_callable(self):
         import re
@@ -315,7 +318,7 @@
     def test_scanner_zero_width_match(self):
         import re, sys
         if sys.version_info[:2] == (2, 3):
-            return
+            skip("2.3 is different here")
         p = re.compile(".*").scanner("bla")
         assert ("bla", "") == (p.search().group(0), p.search().group(0))
         assert None == p.search()
@@ -329,7 +332,7 @@
             cls.space = gettestobjspace(usemodules=('_locale',))
         except py.test.skip.Exception:
             cls.space = gettestobjspace(usemodules=('_rawffi',))
-        init_globals_hack(cls.space)
+        init_app_test(cls, cls.space)
 
     def setup_method(self, method):
         import locale
@@ -340,11 +343,13 @@
         locale.setlocale(locale.LC_ALL, (None, None))
 
     def test_getlower_no_flags(self):
+        s = self.s
         UPPER_AE = "\xc4"
         s.assert_lower_equal([("a", "a"), ("A", "a"), (UPPER_AE, UPPER_AE),
             ("\u00c4", "\u00c4"), ("\u4444", "\u4444")], 0)
 
     def test_getlower_locale(self):
+        s = self.s
         import locale, sre_constants
         UPPER_AE = "\xc4"
         LOWER_AE = "\xe4"
@@ -359,6 +364,7 @@
             skip("unsupported locale de_DE")
 
     def test_getlower_unicode(self):
+        s = self.s
         import sre_constants
         UPPER_AE = "\xc4"
         LOWER_AE = "\xe4"
@@ -587,34 +593,41 @@
 class AppTestOpcodes:
 
     def setup_class(cls):
+        if option.runappdirect:
+            py.test.skip("can only be run on py.py: _sre opcodes don't match")
         try:
             cls.space = gettestobjspace(usemodules=('_locale',))
         except py.test.skip.Exception:
             cls.space = gettestobjspace(usemodules=('_rawffi',))
         # This imports support_test_sre as the global "s"
-        init_globals_hack(cls.space)
+        init_app_test(cls, cls.space)
 
     def test_length_optimization(self):
+        s = self.s
         pattern = "bla"
         opcodes = [s.OPCODES["info"], 3, 3, len(pattern)] \
             + s.encode_literal(pattern) + [s.OPCODES["success"]]
         s.assert_no_match(opcodes, ["b", "bl", "ab"])
 
     def test_literal(self):
+        s = self.s
         opcodes = s.encode_literal("bla") + [s.OPCODES["success"]]
         s.assert_no_match(opcodes, ["bl", "blu"])
         s.assert_match(opcodes, ["bla", "blab", "cbla", "bbla"])
 
     def test_not_literal(self):
+        s = self.s
         opcodes = s.encode_literal("b") \
             + [s.OPCODES["not_literal"], ord("a"), s.OPCODES["success"]]
         s.assert_match(opcodes, ["bx", "ababy"])
         s.assert_no_match(opcodes, ["ba", "jabadu"])
 
     def test_unknown(self):
+        s = self.s
         raises(RuntimeError, s.search, [55555], "b")
 
     def test_at_beginning(self):
+        s = self.s
         for atname in ["at_beginning", "at_beginning_string"]:
             opcodes = [s.OPCODES["at"], s.ATCODES[atname]] \
                 + s.encode_literal("bla") + [s.OPCODES["success"]]
@@ -622,30 +635,35 @@
             s.assert_no_match(opcodes, "abla")
 
     def test_at_beginning_line(self):
+        s = self.s
         opcodes = [s.OPCODES["at"], s.ATCODES["at_beginning_line"]] \
             + s.encode_literal("bla") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["bla", "x\nbla"])
         s.assert_no_match(opcodes, ["abla", "abla\nubla"])
 
     def test_at_end(self):
+        s = self.s
         opcodes = s.encode_literal("bla") \
             + [s.OPCODES["at"], s.ATCODES["at_end"], s.OPCODES["success"]]
         s.assert_match(opcodes, ["bla", "bla\n"])
         s.assert_no_match(opcodes, ["blau", "abla\nblau"])
 
     def test_at_end_line(self):
+        s = self.s
         opcodes = s.encode_literal("bla") \
             + [s.OPCODES["at"], s.ATCODES["at_end_line"], s.OPCODES["success"]]
         s.assert_match(opcodes, ["bla\n", "bla\nx", "bla"])
         s.assert_no_match(opcodes, ["blau"])
 
     def test_at_end_string(self):
+        s = self.s
         opcodes = s.encode_literal("bla") \
             + [s.OPCODES["at"], s.ATCODES["at_end_string"], s.OPCODES["success"]]
         s.assert_match(opcodes, "bla")
         s.assert_no_match(opcodes, ["blau", "bla\n"])
 
     def test_at_boundary(self):
+        s = self.s
         for atname in "at_boundary", "at_loc_boundary", "at_uni_boundary":
             opcodes = s.encode_literal("bla") \
                 + [s.OPCODES["at"], s.ATCODES[atname], s.OPCODES["success"]]
@@ -657,6 +675,7 @@
             s.assert_no_match(opcodes, "")
 
     def test_at_non_boundary(self):
+        s = self.s
         for atname in "at_non_boundary", "at_loc_non_boundary", "at_uni_non_boundary":
             opcodes = s.encode_literal("bla") \
                 + [s.OPCODES["at"], s.ATCODES[atname], s.OPCODES["success"]]
@@ -664,6 +683,7 @@
             s.assert_no_match(opcodes, ["bla ja", "bla"])
 
     def test_at_loc_boundary(self):
+        s = self.s
         import locale
         try:
             s.void_locale()
@@ -683,6 +703,7 @@
             skip("locale error")
 
     def test_at_uni_boundary(self):
+        s = self.s
         UPPER_PI = "\u03a0"
         LOWER_PI = "\u03c0"
         opcodes = s.encode_literal("bl") + [s.OPCODES["any"], s.OPCODES["at"],
@@ -694,6 +715,7 @@
         s.assert_match(opcodes, ["blaha", "bl%sja" % UPPER_PI])
 
     def test_category_loc_word(self):
+        s = self.s
         import locale
         try:
             s.void_locale()
@@ -714,23 +736,27 @@
             skip("locale error")
 
     def test_any(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["any"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["b a", "bla", "bboas"])
         s.assert_no_match(opcodes, ["b\na", "oba", "b"])
 
     def test_any_all(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["any_all"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["b a", "bla", "bboas", "b\na"])
         s.assert_no_match(opcodes, ["oba", "b"])
 
     def test_in_failure(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["in"], 2, s.OPCODES["failure"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
         s.assert_no_match(opcodes, ["ba", "bla"])
 
     def test_in_literal(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["in"], 7] \
             + s.encode_literal("la") + [s.OPCODES["failure"], s.OPCODES["failure"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
@@ -738,6 +764,7 @@
         s.assert_no_match(opcodes, ["ba", "bja", "blla"])
 
     def test_in_category(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["in"], 6, s.OPCODES["category"],
             s.CHCODES["category_digit"], s.OPCODES["category"], s.CHCODES["category_space"],
             s.OPCODES["failure"]] + s.encode_literal("a") + [s.OPCODES["success"]]
@@ -748,6 +775,7 @@
         import _sre
         if _sre.CODESIZE != 2:
             return
+        s = self.s
         # charset bitmap for characters "l" and "h"
         bitmap = 6 * [0] + [4352] + 9 * [0]
         opcodes = s.encode_literal("b") + [s.OPCODES["in"], 19, s.OPCODES["charset"]] \
@@ -759,6 +787,7 @@
         # disabled because this actually only works on big-endian machines
         if _sre.CODESIZE != 2:
             return
+        s = self.s
         # constructing bigcharset for lowercase pi (\u03c0)
         UPPER_PI = u"\u03a0"
         LOWER_PI = u"\u03c0"
@@ -774,6 +803,7 @@
     # XXX bigcharset test for ucs4 missing here
 
     def test_in_range(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["in"], 5, s.OPCODES["range"],
             ord("1"), ord("9"), s.OPCODES["failure"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
@@ -781,6 +811,7 @@
         s.assert_no_match(opcodes, ["baa", "b5"])
 
     def test_in_negate(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["in"], 7, s.OPCODES["negate"]] \
             + s.encode_literal("la") + [s.OPCODES["failure"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
@@ -788,12 +819,14 @@
         s.assert_no_match(opcodes, ["bla", "baa", "blbla"])
 
     def test_literal_ignore(self):
+        s = self.s
         opcodes = s.encode_literal("b") \
             + [s.OPCODES["literal_ignore"], ord("a"), s.OPCODES["success"]]
         s.assert_match(opcodes, ["ba", "bA"])
         s.assert_no_match(opcodes, ["bb", "bu"])
 
     def test_not_literal_ignore(self):
+        s = self.s
         UPPER_PI = "\u03a0"
         opcodes = s.encode_literal("b") \
             + [s.OPCODES["not_literal_ignore"], ord("a"), s.OPCODES["success"]]
@@ -801,6 +834,7 @@
         s.assert_no_match(opcodes, ["ba", "bA"])
 
     def test_in_ignore(self):
+        s = self.s
         opcodes = s.encode_literal("b") + [s.OPCODES["in_ignore"], 8] \
             + s.encode_literal("abc") + [s.OPCODES["failure"]] \
             + s.encode_literal("a") + [s.OPCODES["success"]]
@@ -808,6 +842,7 @@
         s.assert_no_match(opcodes, ["ba", "bja", "blla"])
 
     def test_in_jump_info(self):
+        s = self.s
         for opname in "jump", "info":
             opcodes = s.encode_literal("b") \
                 + [s.OPCODES[opname], 3, s.OPCODES["failure"], s.OPCODES["failure"]] \
@@ -815,6 +850,7 @@
             s.assert_match(opcodes, "ba")
 
     def _test_mark(self):
+        s = self.s
         # XXX need to rewrite this implementation-independent
         opcodes = s.encode_literal("a") + [s.OPCODES["mark"], 0] \
             + s.encode_literal("b") + [s.OPCODES["mark"], 1, s.OPCODES["success"]]
@@ -826,6 +862,7 @@
         assert [1, 2] == state.marks
 
     def test_branch(self):
+        s = self.s
         opcodes = [s.OPCODES["branch"], 7] + s.encode_literal("ab") \
             + [s.OPCODES["jump"], 9, 7] + s.encode_literal("cd") \
             + [s.OPCODES["jump"], 2, s.OPCODES["failure"], s.OPCODES["success"]]
@@ -833,18 +870,21 @@
         s.assert_no_match(opcodes, ["aacas", "ac", "bla"])
 
     def test_repeat_one(self):
+        s = self.s
         opcodes = [s.OPCODES["repeat_one"], 6, 1, 65535] + s.encode_literal("a") \
             + [s.OPCODES["success"]] + s.encode_literal("ab") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["aab", "aaaab"])
         s.assert_no_match(opcodes, ["ab", "a"])
 
     def test_min_repeat_one(self):
+        s = self.s
         opcodes = [s.OPCODES["min_repeat_one"], 5, 1, 65535, s.OPCODES["any"]] \
             + [s.OPCODES["success"]] + s.encode_literal("b") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["aab", "ardb", "bb"])
         s.assert_no_match(opcodes, ["b"])
 
     def test_repeat_maximizing(self):
+        s = self.s
         opcodes = [s.OPCODES["repeat"], 5, 1, 65535] + s.encode_literal("a") \
             + [s.OPCODES["max_until"]] + s.encode_literal("b") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["ab", "aaaab", "baabb"])
@@ -856,6 +896,7 @@
         # CPython 2.3 fails with a recursion limit exceeded error here.
         import sys
         if not sys.version_info[:2] == (2, 3):
+            s = self.s
             opcodes = [s.OPCODES["repeat"], 10, 1, 65535, s.OPCODES["repeat_one"],
                 6, 0, 65535] + s.encode_literal("a") + [s.OPCODES["success"],
                 s.OPCODES["max_until"], s.OPCODES["success"]]
@@ -863,6 +904,7 @@
             assert "" == s.search(opcodes, "bb").group(0)
 
     def test_repeat_minimizing(self):
+        s = self.s
         opcodes = [s.OPCODES["repeat"], 4, 1, 65535, s.OPCODES["any"],
             s.OPCODES["min_until"]] + s.encode_literal("b") + [s.OPCODES["success"]]
         s.assert_match(opcodes, ["ab", "aaaab", "baabb"])
@@ -870,24 +912,28 @@
         assert "aab" == s.search(opcodes, "aabb").group(0)
 
     def test_groupref(self):
+        s = self.s
         opcodes = [s.OPCODES["mark"], 0, s.OPCODES["any"], s.OPCODES["mark"], 1] \
             + s.encode_literal("a") + [s.OPCODES["groupref"], 0, s.OPCODES["success"]]
         s.assert_match(opcodes, ["bab", "aaa", "dad"])
         s.assert_no_match(opcodes, ["ba", "bad", "baad"])
 
     def test_groupref_ignore(self):
+        s = self.s
         opcodes = [s.OPCODES["mark"], 0, s.OPCODES["any"], s.OPCODES["mark"], 1] \
             + s.encode_literal("a") + [s.OPCODES["groupref_ignore"], 0, s.OPCODES["success"]]
         s.assert_match(opcodes, ["bab", "baB", "Dad"])
         s.assert_no_match(opcodes, ["ba", "bad", "baad"])
 
     def test_assert(self):
+        s = self.s
         opcodes = s.encode_literal("a") + [s.OPCODES["assert"], 4, 0] \
             + s.encode_literal("b") + [s.OPCODES["success"], s.OPCODES["success"]]
         assert "a" == s.search(opcodes, "ab").group(0)
         s.assert_no_match(opcodes, ["a", "aa"])
 
     def test_assert_not(self):
+        s = self.s
         opcodes = s.encode_literal("a") + [s.OPCODES["assert_not"], 4, 0] \
             + s.encode_literal("b") + [s.OPCODES["success"], s.OPCODES["success"]]
         assert "a" == s.search(opcodes, "ac").group(0)
diff --git a/pypy/module/_weakref/interp__weakref.py b/pypy/module/_weakref/interp__weakref.py
--- a/pypy/module/_weakref/interp__weakref.py
+++ b/pypy/module/_weakref/interp__weakref.py
@@ -376,12 +376,12 @@
         proxy_typedef_dict[special_method] = interp2app(func)
         callable_proxy_typedef_dict[special_method] = interp2app(func)
 
-# __unicode__ is not yet a space operation
+# __bytes__ is not yet a space operation
 def proxy_unicode(space, w_obj):
     w_obj = force(space, w_obj)
-    return space.call_method(w_obj, '__unicode__')
-proxy_typedef_dict['__unicode__'] = interp2app(proxy_unicode)
-callable_proxy_typedef_dict['__unicode__'] = interp2app(proxy_unicode)
+    return space.call_method(w_obj, '__bytes__')
+proxy_typedef_dict['__bytes__'] = interp2app(proxy_unicode)
+callable_proxy_typedef_dict['__bytes__'] = interp2app(proxy_unicode)
 
 
 W_Proxy.typedef = TypeDef("weakproxy",
diff --git a/pypy/module/_weakref/test/test_weakref.py b/pypy/module/_weakref/test/test_weakref.py
--- a/pypy/module/_weakref/test/test_weakref.py
+++ b/pypy/module/_weakref/test/test_weakref.py
@@ -419,14 +419,14 @@
             print(s)
             assert "dead" in s
 
-    def test_unicode(self):
+    def test_bytes(self):
         import _weakref
         class C(object):
-            def __str__(self):
-                return "string"
+            def __bytes__(self):
+                return b"string"
         instance = C()
-        assert "__str__" in dir(_weakref.proxy(instance))
-        assert str(_weakref.proxy(instance)) == "string"
+        assert "__bytes__" in dir(_weakref.proxy(instance))
+        assert bytes(_weakref.proxy(instance)) == b"string"
 
     def test_eq(self):
         import _weakref
diff --git a/pypy/module/fcntl/interp_fcntl.py b/pypy/module/fcntl/interp_fcntl.py
--- a/pypy/module/fcntl/interp_fcntl.py
+++ b/pypy/module/fcntl/interp_fcntl.py
@@ -255,6 +255,9 @@
     else:
         intarg = rffi.cast(rffi.INT, intarg)   # C long => C int
         rv = ioctl_int(fd, op, intarg)
+        if rv < 0:
+            raise _get_error(space, "ioctl")
+        return space.wrap(rv)
 
     try:
         arg = space.bufferstr_w(w_arg)
diff --git a/pypy/module/fcntl/test/test_fcntl.py b/pypy/module/fcntl/test/test_fcntl.py
--- a/pypy/module/fcntl/test/test_fcntl.py
+++ b/pypy/module/fcntl/test/test_fcntl.py
@@ -13,7 +13,7 @@
 
 class AppTestFcntl:
     def setup_class(cls):
-        space = gettestobjspace(usemodules=('fcntl', 'array', 'struct'))
+        space = gettestobjspace(usemodules=('fcntl', 'array', 'struct', 'termios'))
         cls.space = space
         tmpprefix = str(udir.ensure('test_fcntl', dir=1).join('tmp_'))
         cls.w_tmp = space.wrap(tmpprefix)
@@ -136,11 +136,10 @@
         import array
         import sys, os
 
-        if "linux" in sys.platform:
-            TIOCGPGRP = 0x540f
-        elif "darwin" in sys.platform or "freebsd" in sys.platform:
-            TIOCGPGRP = 0x40047477
-        else:
+        try:
+            from termios import TIOCGPGRP
+            import pty
+        except ImportError:
             skip("don't know how to test ioctl() on this platform")
 
         raises(TypeError, fcntl.ioctl, "foo")
@@ -148,28 +147,49 @@
         #raises(TypeError, fcntl.ioctl, 0, TIOCGPGRP, float(0))
         raises(TypeError, fcntl.ioctl, 0, TIOCGPGRP, 1, "foo")
 
-        if not os.isatty(0):
-            skip("stdin is not a tty")
+        child_pid, mfd = pty.fork()
+        if child_pid == 0:
+            # We're the child
+            return
+        try:
+            buf = array.array('h', [0])
+            res = fcntl.ioctl(mfd, TIOCGPGRP, buf, True)
+            assert res == 0
+            assert buf[0] != 0
+            expected = buf.tostring()
 
-        buf = array.array('h', [0])
-        res = fcntl.ioctl(0, TIOCGPGRP, buf, True)
-        assert res == 0
-        assert buf[0] != 0
-        expected = buf.tostring()
+            if '__pypy__' in sys.builtin_module_names or sys.version_info >= (2,5):
+                buf = array.array('h', [0])
+                res = fcntl.ioctl(mfd, TIOCGPGRP, buf)
+                assert res == 0
+                assert buf.tostring() == expected
 
-        if '__pypy__' in sys.builtin_module_names or sys.version_info >= (2,5):
-            buf = array.array('h', [0])
-            res = fcntl.ioctl(0, TIOCGPGRP, buf)
-            assert res == 0
-            assert buf.tostring() == expected
+            res = fcntl.ioctl(mfd, TIOCGPGRP, buf, False)
+            assert res == expected
 
-        res = fcntl.ioctl(0, TIOCGPGRP, buf, False)
-        assert res == expected
+            raises(TypeError, fcntl.ioctl, mfd, TIOCGPGRP, "\x00\x00", True)
 
-        raises(TypeError, fcntl.ioctl, 0, TIOCGPGRP, "\x00\x00", True)
+            res = fcntl.ioctl(mfd, TIOCGPGRP, "\x00\x00")
+            assert res == expected
+        finally:
+            os.close(mfd)
 
-        res = fcntl.ioctl(0, TIOCGPGRP, "\x00\x00")
-        assert res == expected
+    def test_ioctl_int(self):
+        import os
+        import fcntl
+
+        try:
+            from termios import TCFLSH, TCIOFLUSH
+            import pty
+        except ImportError:
+            skip("don't know how to test ioctl() on this platform")
+
+        mfd, sfd = pty.openpty()
+        try:
+            assert fcntl.ioctl(mfd, TCFLSH, TCIOFLUSH) == 0
+        finally:
+            os.close(mfd)
+            os.close(sfd)
 
     def test_lockf_with_ex(self):
         import fcntl
diff --git a/pypy/module/math/app_math.py b/pypy/module/math/app_math.py
--- a/pypy/module/math/app_math.py
+++ b/pypy/module/math/app_math.py
@@ -1,3 +1,5 @@
+import sys
+
 def factorial(x):
     """Find x!."""
     if isinstance(x, float):
@@ -5,6 +7,8 @@
         if fl != x:
             raise ValueError("float arguments must be integral")
         x = fl
+    if x > sys.maxsize:
+        raise OverflowError("Too large for a factorial")
     if x < 0:
         raise ValueError("x must be >= 0")
     res = 1
diff --git a/pypy/module/math/test/test_math.py b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -89,7 +89,7 @@
             assert actual == expected
 
     def test_factorial(self):
-        import math
+        import math, sys
         assert math.factorial(0) == 1
         assert math.factorial(1) == 1
         assert math.factorial(2) == 2
@@ -98,6 +98,8 @@
         raises(ValueError, math.factorial, -1)
         raises(ValueError, math.factorial, -1.)
         raises(ValueError, math.factorial, 1.1)
+        raises(OverflowError, math.factorial, sys.maxsize+1)
+        raises(OverflowError, math.factorial, 10e100)
 
     def test_log1p(self):
         import math
diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -1,6 +1,6 @@
 from pypy.interpreter.error import OperationError, wrap_oserror
 from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.rlib import rmmap, rarithmetic
 from pypy.rlib.rmmap import RValueError, RTypeError
@@ -132,6 +132,13 @@
     def __len__(self):
         return self.space.wrap(self.mmap.size)
 
+    def closed_get(self, space):
+        try:
+            self.mmap.check_valid()
+        except RValueError:
+            return space.w_True
+        return space.w_False
+
     def check_valid(self):
         try:
             self.mmap.check_valid()
@@ -199,6 +206,14 @@
         space = self.space
         return space.wrap(StringLikeBuffer(space, space.wrap(self)))
 
+    def descr_enter(self, space):
+        self.check_valid()
+        return space.wrap(self)
+
+    def descr_exit(self, space, __args__):
+        self.close()
+
+
 if rmmap._POSIX:
 
     @unwrap_spec(fileno=int, length=int, flags=int,
@@ -260,6 +275,10 @@
     __getitem__ = interp2app(W_MMap.descr_getitem),
     __setitem__ = interp2app(W_MMap.descr_setitem),
     __buffer__ = interp2app(W_MMap.descr_buffer),
+    __enter__ = interp2app(W_MMap.descr_enter),
+    __exit__ = interp2app(W_MMap.descr_exit),
+
+    closed = GetSetProperty(W_MMap.closed_get),
 )
 
 constants = rmmap.constants
diff --git a/pypy/module/mmap/test/test_mmap.py b/pypy/module/mmap/test/test_mmap.py
--- a/pypy/module/mmap/test/test_mmap.py
+++ b/pypy/module/mmap/test/test_mmap.py
@@ -622,6 +622,12 @@
             finally:
                 m.close()
 
+    def test_context_manager(self):
+        import mmap
+        with mmap.mmap(-1, 10) as m:
+            assert not m.closed
+        assert m.closed
+
     def test_all(self):
         # this is a global test, ported from test_mmap.py
         import mmap
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
@@ -39,9 +39,16 @@
 def fsencode_w(space, w_obj):
     if space.isinstance_w(w_obj, space.w_unicode):
         w_obj = space.call_method(w_obj, 'encode',
-                                  getfilesystemencoding(space))
+                                  getfilesystemencoding(space),
+                                  space.wrap('surrogateescape'))
     return space.bytes0_w(w_obj)
 
+def fsdecode(space, w_obj):
+    w_unicode = space.call_method(w_obj, 'decode',
+                                  getfilesystemencoding(space),
+                                  space.wrap('surrogateescape'))
+    return w_unicode
+
 class FileEncoder(object):
     def __init__(self, space, w_obj):
         self.space = space
@@ -63,8 +70,7 @@
 
     def as_unicode(self):
         space = self.space
-        w_unicode = space.call_method(self.w_obj, 'decode',
-                                      getfilesystemencoding(space))
+        w_unicode = fsdecode(space, self.w_obj)
         return space.unicode0_w(w_unicode)
 
 @specialize.memo()
@@ -544,17 +550,11 @@


More information about the pypy-commit mailing list