[pypy-commit] pypy stdlib-unification: copy all pypy mods into the stdlib folder

RonnyPfannschmidt noreply at buildbot.pypy.org
Thu Apr 12 11:07:23 CEST 2012


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: stdlib-unification
Changeset: r54302:cb0772450c59
Date: 2012-04-12 11:05 +0200
http://bitbucket.org/pypy/pypy/changeset/cb0772450c59/

Log:	copy all pypy mods into the stdlib folder

diff too long, truncating to 10000 out of 186682 lines

diff --git a/lib-python/2.7/UserDict.py b/lib-python/2.7/UserDict.py
--- a/lib-python/2.7/UserDict.py
+++ b/lib-python/2.7/UserDict.py
@@ -1,5 +1,10 @@
 """A more or less complete user-defined wrapper around dictionary objects."""
 
+# XXX This is a bit of a hack (as usual :-))
+# the actual content of the file is not changed, but we put it here to make
+# virtualenv happy (because its internal logic expects at least one of the
+# REQUIRED_MODULES to be in modified-*)
+
 class UserDict:
     def __init__(self, dict=None, **kwargs):
         self.data = {}
@@ -80,8 +85,12 @@
     def __iter__(self):
         return iter(self.data)
 
-import _abcoll
-_abcoll.MutableMapping.register(IterableUserDict)
+try:
+    import _abcoll
+except ImportError:
+    pass    # e.g. no '_weakref' module on this pypy
+else:
+    _abcoll.MutableMapping.register(IterableUserDict)
 
 
 class DictMixin:
diff --git a/lib-python/2.7/_threading_local.py b/lib-python/2.7/_threading_local.py
--- a/lib-python/2.7/_threading_local.py
+++ b/lib-python/2.7/_threading_local.py
@@ -155,7 +155,7 @@
         object.__setattr__(self, '_local__args', (args, kw))
         object.__setattr__(self, '_local__lock', RLock())
 
-        if (args or kw) and (cls.__init__ is object.__init__):
+        if (args or kw) and (cls.__init__ == object.__init__):
             raise TypeError("Initialization arguments are not supported")
 
         # We need to create the thread dict in anticipation of
diff --git a/lib-python/2.7/ctypes/__init__.py b/lib-python/2.7/ctypes/__init__.py
--- a/lib-python/2.7/ctypes/__init__.py
+++ b/lib-python/2.7/ctypes/__init__.py
@@ -7,6 +7,7 @@
 
 __version__ = "1.1.0"
 
+import _ffi
 from _ctypes import Union, Structure, Array
 from _ctypes import _Pointer
 from _ctypes import CFuncPtr as _CFuncPtr
@@ -350,16 +351,17 @@
         self._FuncPtr = _FuncPtr
 
         if handle is None:
-            self._handle = _dlopen(self._name, mode)
+            self._handle = _ffi.CDLL(name, mode)
         else:
             self._handle = handle
 
     def __repr__(self):
-        return "<%s '%s', handle %x at %x>" % \
+        return "<%s '%s', handle %r at %x>" % \
                (self.__class__.__name__, self._name,
-                (self._handle & (_sys.maxint*2 + 1)),
+                (self._handle),
                 id(self) & (_sys.maxint*2 + 1))
 
+
     def __getattr__(self, name):
         if name.startswith('__') and name.endswith('__'):
             raise AttributeError(name)
@@ -487,9 +489,12 @@
         _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
     return CFunctionType
 
-_cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
 def cast(obj, typ):
-    return _cast(obj, obj, typ)
+    try:
+        c_void_p.from_param(obj)
+    except TypeError, e:
+        raise ArgumentError(str(e))
+    return _cast_addr(obj, obj, typ)
 
 _string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
 def string_at(ptr, size=-1):
diff --git a/lib-python/2.7/ctypes/test/__init__.py b/lib-python/2.7/ctypes/test/__init__.py
--- a/lib-python/2.7/ctypes/test/__init__.py
+++ b/lib-python/2.7/ctypes/test/__init__.py
@@ -206,3 +206,16 @@
         result = unittest.TestResult()
         test(result)
         return result
+
+def xfail(method):
+    """
+    Poor's man xfail: remove it when all the failures have been fixed
+    """
+    def new_method(self, *args, **kwds):
+        try:
+            method(self, *args, **kwds)
+        except:
+            pass
+        else:
+            self.assertTrue(False, "DID NOT RAISE")
+    return new_method
diff --git a/lib-python/2.7/ctypes/test/test_arrays.py b/lib-python/2.7/ctypes/test/test_arrays.py
--- a/lib-python/2.7/ctypes/test/test_arrays.py
+++ b/lib-python/2.7/ctypes/test/test_arrays.py
@@ -1,12 +1,23 @@
 import unittest
 from ctypes import *
+from test.test_support import impl_detail
 
 formats = "bBhHiIlLqQfd"
 
+# c_longdouble commented out for PyPy, look at the commend in test_longdouble
 formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
-          c_long, c_ulonglong, c_float, c_double, c_longdouble
+          c_long, c_ulonglong, c_float, c_double #, c_longdouble
 
 class ArrayTestCase(unittest.TestCase):
+
+    @impl_detail('long double not supported by PyPy', pypy=False)
+    def test_longdouble(self):
+        """
+        This test is empty. It's just here to remind that we commented out
+        c_longdouble in "formats". If pypy will ever supports c_longdouble, we
+        should kill this test and uncomment c_longdouble inside formats.
+        """
+
     def test_simple(self):
         # create classes holding simple numeric types, and check
         # various properties.
diff --git a/lib-python/2.7/ctypes/test/test_bitfields.py b/lib-python/2.7/ctypes/test/test_bitfields.py
--- a/lib-python/2.7/ctypes/test/test_bitfields.py
+++ b/lib-python/2.7/ctypes/test/test_bitfields.py
@@ -115,17 +115,21 @@
     def test_nonint_types(self):
         # bit fields are not allowed on non-integer types.
         result = self.fail_fields(("a", c_char_p, 1))
-        self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char_p'))
+        self.assertEqual(result[0], TypeError)
+        self.assertIn('bit fields not allowed for type', result[1])
 
         result = self.fail_fields(("a", c_void_p, 1))
-        self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_void_p'))
+        self.assertEqual(result[0], TypeError)
+        self.assertIn('bit fields not allowed for type', result[1])
 
         if c_int != c_long:
             result = self.fail_fields(("a", POINTER(c_int), 1))
-            self.assertEqual(result, (TypeError, 'bit fields not allowed for type LP_c_int'))
+            self.assertEqual(result[0], TypeError)
+            self.assertIn('bit fields not allowed for type', result[1])
 
         result = self.fail_fields(("a", c_char, 1))
-        self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char'))
+        self.assertEqual(result[0], TypeError)
+        self.assertIn('bit fields not allowed for type', result[1])
 
         try:
             c_wchar
@@ -133,13 +137,15 @@
             pass
         else:
             result = self.fail_fields(("a", c_wchar, 1))
-            self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_wchar'))
+            self.assertEqual(result[0], TypeError)
+            self.assertIn('bit fields not allowed for type', result[1])
 
         class Dummy(Structure):
             _fields_ = []
 
         result = self.fail_fields(("a", Dummy, 1))
-        self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy'))
+        self.assertEqual(result[0], TypeError)
+        self.assertIn('bit fields not allowed for type', result[1])
 
     def test_single_bitfield_size(self):
         for c_typ in int_types:
diff --git a/lib-python/2.7/ctypes/test/test_byteswap.py b/lib-python/2.7/ctypes/test/test_byteswap.py
--- a/lib-python/2.7/ctypes/test/test_byteswap.py
+++ b/lib-python/2.7/ctypes/test/test_byteswap.py
@@ -2,6 +2,7 @@
 from binascii import hexlify
 
 from ctypes import *
+from ctypes.test import xfail
 
 def bin(s):
     return hexlify(memoryview(s)).upper()
@@ -21,6 +22,7 @@
             setattr(bits, "i%s" % i, 1)
             dump(bits)
 
+    @xfail
     def test_endian_short(self):
         if sys.byteorder == "little":
             self.assertTrue(c_short.__ctype_le__ is c_short)
@@ -48,6 +50,7 @@
         self.assertEqual(bin(s), "3412")
         self.assertEqual(s.value, 0x1234)
 
+    @xfail
     def test_endian_int(self):
         if sys.byteorder == "little":
             self.assertTrue(c_int.__ctype_le__ is c_int)
@@ -76,6 +79,7 @@
         self.assertEqual(bin(s), "78563412")
         self.assertEqual(s.value, 0x12345678)
 
+    @xfail
     def test_endian_longlong(self):
         if sys.byteorder == "little":
             self.assertTrue(c_longlong.__ctype_le__ is c_longlong)
@@ -104,6 +108,7 @@
         self.assertEqual(bin(s), "EFCDAB9078563412")
         self.assertEqual(s.value, 0x1234567890ABCDEF)
 
+    @xfail
     def test_endian_float(self):
         if sys.byteorder == "little":
             self.assertTrue(c_float.__ctype_le__ is c_float)
@@ -122,6 +127,7 @@
         self.assertAlmostEqual(s.value, math.pi, 6)
         self.assertEqual(bin(struct.pack(">f", math.pi)), bin(s))
 
+    @xfail
     def test_endian_double(self):
         if sys.byteorder == "little":
             self.assertTrue(c_double.__ctype_le__ is c_double)
@@ -149,6 +155,7 @@
         self.assertTrue(c_char.__ctype_le__ is c_char)
         self.assertTrue(c_char.__ctype_be__ is c_char)
 
+    @xfail
     def test_struct_fields_1(self):
         if sys.byteorder == "little":
             base = BigEndianStructure
@@ -198,6 +205,7 @@
             pass
         self.assertRaises(TypeError, setattr, S, "_fields_", [("s", T)])
 
+    @xfail
     def test_struct_fields_2(self):
         # standard packing in struct uses no alignment.
         # So, we have to align using pad bytes.
@@ -221,6 +229,7 @@
         s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
         self.assertEqual(bin(s1), bin(s2))
 
+    @xfail
     def test_unaligned_nonnative_struct_fields(self):
         if sys.byteorder == "little":
             base = BigEndianStructure
diff --git a/lib-python/2.7/ctypes/test/test_callbacks.py b/lib-python/2.7/ctypes/test/test_callbacks.py
--- a/lib-python/2.7/ctypes/test/test_callbacks.py
+++ b/lib-python/2.7/ctypes/test/test_callbacks.py
@@ -1,5 +1,6 @@
 import unittest
 from ctypes import *
+from ctypes.test import xfail
 import _ctypes_test
 
 class Callbacks(unittest.TestCase):
@@ -98,6 +99,7 @@
 ##        self.check_type(c_char_p, "abc")
 ##        self.check_type(c_char_p, "def")
 
+    @xfail
     def test_pyobject(self):
         o = ()
         from sys import getrefcount as grc
diff --git a/lib-python/2.7/ctypes/test/test_cfuncs.py b/lib-python/2.7/ctypes/test/test_cfuncs.py
--- a/lib-python/2.7/ctypes/test/test_cfuncs.py
+++ b/lib-python/2.7/ctypes/test/test_cfuncs.py
@@ -3,8 +3,8 @@
 
 import unittest
 from ctypes import *
-
 import _ctypes_test
+from test.test_support import impl_detail
 
 class CFunctions(unittest.TestCase):
     _dll = CDLL(_ctypes_test.__file__)
@@ -158,12 +158,14 @@
         self.assertEqual(self._dll.tf_bd(0, 42.), 14.)
         self.assertEqual(self.S(), 42)
 
+    @impl_detail('long double not supported by PyPy', pypy=False)
     def test_longdouble(self):
         self._dll.tf_D.restype = c_longdouble
         self._dll.tf_D.argtypes = (c_longdouble,)
         self.assertEqual(self._dll.tf_D(42.), 14.)
         self.assertEqual(self.S(), 42)
-
+        
+    @impl_detail('long double not supported by PyPy', pypy=False)
     def test_longdouble_plus(self):
         self._dll.tf_bD.restype = c_longdouble
         self._dll.tf_bD.argtypes = (c_byte, c_longdouble)
diff --git a/lib-python/2.7/ctypes/test/test_delattr.py b/lib-python/2.7/ctypes/test/test_delattr.py
--- a/lib-python/2.7/ctypes/test/test_delattr.py
+++ b/lib-python/2.7/ctypes/test/test_delattr.py
@@ -6,15 +6,15 @@
 
 class TestCase(unittest.TestCase):
     def test_simple(self):
-        self.assertRaises(TypeError,
+        self.assertRaises((TypeError, AttributeError),
                           delattr, c_int(42), "value")
 
     def test_chararray(self):
-        self.assertRaises(TypeError,
+        self.assertRaises((TypeError, AttributeError),
                           delattr, (c_char * 5)(), "value")
 
     def test_struct(self):
-        self.assertRaises(TypeError,
+        self.assertRaises((TypeError, AttributeError),
                           delattr, X(), "foo")
 
 if __name__ == "__main__":
diff --git a/lib-python/2.7/ctypes/test/test_frombuffer.py b/lib-python/2.7/ctypes/test/test_frombuffer.py
--- a/lib-python/2.7/ctypes/test/test_frombuffer.py
+++ b/lib-python/2.7/ctypes/test/test_frombuffer.py
@@ -2,6 +2,7 @@
 import array
 import gc
 import unittest
+from ctypes.test import xfail
 
 class X(Structure):
     _fields_ = [("c_int", c_int)]
@@ -10,6 +11,7 @@
         self._init_called = True
 
 class Test(unittest.TestCase):
+    @xfail
     def test_fom_buffer(self):
         a = array.array("i", range(16))
         x = (c_int * 16).from_buffer(a)
@@ -35,6 +37,7 @@
         self.assertRaises(TypeError,
                           (c_char * 16).from_buffer, "a" * 16)
 
+    @xfail
     def test_fom_buffer_with_offset(self):
         a = array.array("i", range(16))
         x = (c_int * 15).from_buffer(a, sizeof(c_int))
@@ -43,6 +46,7 @@
         self.assertRaises(ValueError, lambda: (c_int * 16).from_buffer(a, sizeof(c_int)))
         self.assertRaises(ValueError, lambda: (c_int * 1).from_buffer(a, 16 * sizeof(c_int)))
 
+    @xfail
     def test_from_buffer_copy(self):
         a = array.array("i", range(16))
         x = (c_int * 16).from_buffer_copy(a)
@@ -67,6 +71,7 @@
         x = (c_char * 16).from_buffer_copy("a" * 16)
         self.assertEqual(x[:], "a" * 16)
 
+    @xfail
     def test_fom_buffer_copy_with_offset(self):
         a = array.array("i", range(16))
         x = (c_int * 15).from_buffer_copy(a, sizeof(c_int))
diff --git a/lib-python/2.7/ctypes/test/test_functions.py b/lib-python/2.7/ctypes/test/test_functions.py
--- a/lib-python/2.7/ctypes/test/test_functions.py
+++ b/lib-python/2.7/ctypes/test/test_functions.py
@@ -7,6 +7,8 @@
 
 from ctypes import *
 import sys, unittest
+from ctypes.test import xfail
+from test.test_support import impl_detail
 
 try:
     WINFUNCTYPE
@@ -143,6 +145,7 @@
         self.assertEqual(result, -21)
         self.assertEqual(type(result), float)
 
+    @impl_detail('long double not supported by PyPy', pypy=False)
     def test_longdoubleresult(self):
         f = dll._testfunc_D_bhilfD
         f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]
@@ -393,6 +396,7 @@
             self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
                                  (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
 
+    @xfail
     def test_sf1651235(self):
         # see http://www.python.org/sf/1651235
 
diff --git a/lib-python/2.7/ctypes/test/test_internals.py b/lib-python/2.7/ctypes/test/test_internals.py
--- a/lib-python/2.7/ctypes/test/test_internals.py
+++ b/lib-python/2.7/ctypes/test/test_internals.py
@@ -33,7 +33,13 @@
         refcnt = grc(s)
         cs = c_char_p(s)
         self.assertEqual(refcnt + 1, grc(s))
-        self.assertSame(cs._objects, s)
+        try:
+            # Moving gcs need to allocate a nonmoving buffer
+            cs._objects._obj
+        except AttributeError:
+            self.assertSame(cs._objects, s)
+        else:
+            self.assertSame(cs._objects._obj, s)
 
     def test_simple_struct(self):
         class X(Structure):
diff --git a/lib-python/2.7/ctypes/test/test_libc.py b/lib-python/2.7/ctypes/test/test_libc.py
--- a/lib-python/2.7/ctypes/test/test_libc.py
+++ b/lib-python/2.7/ctypes/test/test_libc.py
@@ -25,5 +25,14 @@
         lib.my_qsort(chars, len(chars)-1, sizeof(c_char), comparefunc(sort))
         self.assertEqual(chars.raw, "   ,,aaaadmmmnpppsss\x00")
 
+    def SKIPPED_test_no_more_xfail(self):
+        # We decided to not explicitly support the whole ctypes-2.7
+        # and instead go for a case-by-case, demand-driven approach.
+        # So this test is skipped instead of failing.
+        import socket
+        import ctypes.test
+        self.assertTrue(not hasattr(ctypes.test, 'xfail'),
+                        "You should incrementally grep for '@xfail' and remove them, they are real failures")
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/lib-python/2.7/ctypes/test/test_loading.py b/lib-python/2.7/ctypes/test/test_loading.py
--- a/lib-python/2.7/ctypes/test/test_loading.py
+++ b/lib-python/2.7/ctypes/test/test_loading.py
@@ -2,7 +2,7 @@
 import sys, unittest
 import os
 from ctypes.util import find_library
-from ctypes.test import is_resource_enabled
+from ctypes.test import is_resource_enabled, xfail
 
 libc_name = None
 if os.name == "nt":
@@ -75,6 +75,7 @@
             self.assertRaises(AttributeError, dll.__getitem__, 1234)
 
     if os.name == "nt":
+        @xfail
         def test_1703286_A(self):
             from _ctypes import LoadLibrary, FreeLibrary
             # On winXP 64-bit, advapi32 loads at an address that does
@@ -85,6 +86,7 @@
             handle = LoadLibrary("advapi32")
             FreeLibrary(handle)
 
+        @xfail
         def test_1703286_B(self):
             # Since on winXP 64-bit advapi32 loads like described
             # above, the (arbitrarily selected) CloseEventLog function
diff --git a/lib-python/2.7/ctypes/test/test_macholib.py b/lib-python/2.7/ctypes/test/test_macholib.py
--- a/lib-python/2.7/ctypes/test/test_macholib.py
+++ b/lib-python/2.7/ctypes/test/test_macholib.py
@@ -52,7 +52,6 @@
                                  '/usr/lib/libSystem.B.dylib')
 
             result = find_lib('z')
-            self.assertTrue(result.startswith('/usr/lib/libz.1'))
             self.assertTrue(result.endswith('.dylib'))
 
             self.assertEqual(find_lib('IOKit'),
diff --git a/lib-python/2.7/ctypes/test/test_numbers.py b/lib-python/2.7/ctypes/test/test_numbers.py
--- a/lib-python/2.7/ctypes/test/test_numbers.py
+++ b/lib-python/2.7/ctypes/test/test_numbers.py
@@ -1,6 +1,7 @@
 from ctypes import *
 import unittest
 import struct
+from ctypes.test import xfail
 
 def valid_ranges(*types):
     # given a sequence of numeric types, collect their _type_
@@ -89,12 +90,14 @@
 ##            self.assertRaises(ValueError, t, l-1)
 ##            self.assertRaises(ValueError, t, h+1)
 
+    @xfail
     def test_from_param(self):
         # the from_param class method attribute always
         # returns PyCArgObject instances
         for t in signed_types + unsigned_types + float_types:
             self.assertEqual(ArgType, type(t.from_param(0)))
 
+    @xfail
     def test_byref(self):
         # calling byref returns also a PyCArgObject instance
         for t in signed_types + unsigned_types + float_types + bool_types:
@@ -102,6 +105,7 @@
             self.assertEqual(ArgType, type(parm))
 
 
+    @xfail
     def test_floats(self):
         # c_float and c_double can be created from
         # Python int, long and float
@@ -115,6 +119,7 @@
             self.assertEqual(t(2L).value, 2.0)
             self.assertEqual(t(f).value, 2.0)
 
+    @xfail
     def test_integers(self):
         class FloatLike(object):
             def __float__(self):
diff --git a/lib-python/2.7/ctypes/test/test_objects.py b/lib-python/2.7/ctypes/test/test_objects.py
--- a/lib-python/2.7/ctypes/test/test_objects.py
+++ b/lib-python/2.7/ctypes/test/test_objects.py
@@ -22,7 +22,7 @@
 
 >>> array[4] = 'foo bar'
 >>> array._objects
-{'4': 'foo bar'}
+{'4': <CArgObject 'foo bar'>}
 >>> array[4]
 'foo bar'
 >>>
@@ -47,9 +47,9 @@
 
 >>> x.array[0] = 'spam spam spam'
 >>> x._objects
-{'0:2': 'spam spam spam'}
+{'0:2': <CArgObject 'spam spam spam'>}
 >>> x.array._b_base_._objects
-{'0:2': 'spam spam spam'}
+{'0:2': <CArgObject 'spam spam spam'>}
 >>>
 
 '''
diff --git a/lib-python/2.7/ctypes/test/test_parameters.py b/lib-python/2.7/ctypes/test/test_parameters.py
--- a/lib-python/2.7/ctypes/test/test_parameters.py
+++ b/lib-python/2.7/ctypes/test/test_parameters.py
@@ -1,5 +1,7 @@
 import unittest, sys
 
+from ctypes.test import xfail
+
 class SimpleTypesTestCase(unittest.TestCase):
 
     def setUp(self):
@@ -49,6 +51,7 @@
         self.assertEqual(CWCHARP.from_param("abc"), "abcabcabc")
 
     # XXX Replace by c_char_p tests
+    @xfail
     def test_cstrings(self):
         from ctypes import c_char_p, byref
 
@@ -86,7 +89,10 @@
 
         pa = c_wchar_p.from_param(c_wchar_p(u"123"))
         self.assertEqual(type(pa), c_wchar_p)
+    if sys.platform == "win32":
+        test_cw_strings = xfail(test_cw_strings)
 
+    @xfail
     def test_int_pointers(self):
         from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer
         LPINT = POINTER(c_int)
diff --git a/lib-python/2.7/ctypes/test/test_pep3118.py b/lib-python/2.7/ctypes/test/test_pep3118.py
--- a/lib-python/2.7/ctypes/test/test_pep3118.py
+++ b/lib-python/2.7/ctypes/test/test_pep3118.py
@@ -1,6 +1,7 @@
 import unittest
 from ctypes import *
 import re, sys
+from ctypes.test import xfail
 
 if sys.byteorder == "little":
     THIS_ENDIAN = "<"
@@ -19,6 +20,7 @@
 
 class Test(unittest.TestCase):
 
+    @xfail
     def test_native_types(self):
         for tp, fmt, shape, itemtp in native_types:
             ob = tp()
@@ -46,6 +48,7 @@
                 print(tp)
                 raise
 
+    @xfail
     def test_endian_types(self):
         for tp, fmt, shape, itemtp in endian_types:
             ob = tp()
diff --git a/lib-python/2.7/ctypes/test/test_pickling.py b/lib-python/2.7/ctypes/test/test_pickling.py
--- a/lib-python/2.7/ctypes/test/test_pickling.py
+++ b/lib-python/2.7/ctypes/test/test_pickling.py
@@ -3,6 +3,7 @@
 from ctypes import *
 import _ctypes_test
 dll = CDLL(_ctypes_test.__file__)
+from ctypes.test import xfail
 
 class X(Structure):
     _fields_ = [("a", c_int), ("b", c_double)]
@@ -21,6 +22,7 @@
     def loads(self, item):
         return pickle.loads(item)
 
+    @xfail
     def test_simple(self):
         for src in [
             c_int(42),
@@ -31,6 +33,7 @@
             self.assertEqual(memoryview(src).tobytes(),
                                  memoryview(dst).tobytes())
 
+    @xfail
     def test_struct(self):
         X.init_called = 0
 
@@ -49,6 +52,7 @@
         self.assertEqual(memoryview(y).tobytes(),
                              memoryview(x).tobytes())
 
+    @xfail
     def test_unpickable(self):
         # ctypes objects that are pointers or contain pointers are
         # unpickable.
@@ -66,6 +70,7 @@
             ]:
             self.assertRaises(ValueError, lambda: self.dumps(item))
 
+    @xfail
     def test_wchar(self):
         pickle.dumps(c_char("x"))
         # Issue 5049
diff --git a/lib-python/2.7/ctypes/test/test_python_api.py b/lib-python/2.7/ctypes/test/test_python_api.py
--- a/lib-python/2.7/ctypes/test/test_python_api.py
+++ b/lib-python/2.7/ctypes/test/test_python_api.py
@@ -1,6 +1,6 @@
 from ctypes import *
 import unittest, sys
-from ctypes.test import is_resource_enabled
+from ctypes.test import is_resource_enabled, xfail
 
 ################################################################
 # This section should be moved into ctypes\__init__.py, when it's ready.
@@ -17,6 +17,7 @@
 
 class PythonAPITestCase(unittest.TestCase):
 
+    @xfail
     def test_PyString_FromStringAndSize(self):
         PyString_FromStringAndSize = pythonapi.PyString_FromStringAndSize
 
@@ -25,6 +26,7 @@
 
         self.assertEqual(PyString_FromStringAndSize("abcdefghi", 3), "abc")
 
+    @xfail
     def test_PyString_FromString(self):
         pythonapi.PyString_FromString.restype = py_object
         pythonapi.PyString_FromString.argtypes = (c_char_p,)
@@ -56,6 +58,7 @@
             del res
             self.assertEqual(grc(42), ref42)
 
+    @xfail
     def test_PyObj_FromPtr(self):
         s = "abc def ghi jkl"
         ref = grc(s)
@@ -81,6 +84,7 @@
         # not enough arguments
         self.assertRaises(TypeError, PyOS_snprintf, buf)
 
+    @xfail
     def test_pyobject_repr(self):
         self.assertEqual(repr(py_object()), "py_object(<NULL>)")
         self.assertEqual(repr(py_object(42)), "py_object(42)")
diff --git a/lib-python/2.7/ctypes/test/test_refcounts.py b/lib-python/2.7/ctypes/test/test_refcounts.py
--- a/lib-python/2.7/ctypes/test/test_refcounts.py
+++ b/lib-python/2.7/ctypes/test/test_refcounts.py
@@ -90,6 +90,7 @@
             return a * b * 2
         f = proto(func)
 
+        gc.collect()
         a = sys.getrefcount(ctypes.c_int)
         f(1, 2)
         self.assertEqual(sys.getrefcount(ctypes.c_int), a)
diff --git a/lib-python/2.7/ctypes/test/test_stringptr.py b/lib-python/2.7/ctypes/test/test_stringptr.py
--- a/lib-python/2.7/ctypes/test/test_stringptr.py
+++ b/lib-python/2.7/ctypes/test/test_stringptr.py
@@ -2,11 +2,13 @@
 from ctypes import *
 
 import _ctypes_test
+from ctypes.test import xfail
 
 lib = CDLL(_ctypes_test.__file__)
 
 class StringPtrTestCase(unittest.TestCase):
 
+    @xfail
     def test__POINTER_c_char(self):
         class X(Structure):
             _fields_ = [("str", POINTER(c_char))]
@@ -27,6 +29,7 @@
 
         self.assertRaises(TypeError, setattr, x, "str", "Hello, World")
 
+    @xfail
     def test__c_char_p(self):
         class X(Structure):
             _fields_ = [("str", c_char_p)]
diff --git a/lib-python/2.7/ctypes/test/test_strings.py b/lib-python/2.7/ctypes/test/test_strings.py
--- a/lib-python/2.7/ctypes/test/test_strings.py
+++ b/lib-python/2.7/ctypes/test/test_strings.py
@@ -31,8 +31,9 @@
         buf.value = "Hello, World"
         self.assertEqual(buf.value, "Hello, World")
 
-        self.assertRaises(TypeError, setattr, buf, "value", memoryview("Hello, World"))
-        self.assertRaises(TypeError, setattr, buf, "value", memoryview("abc"))
+        if test_support.check_impl_detail():
+            self.assertRaises(TypeError, setattr, buf, "value", memoryview("Hello, World"))
+            self.assertRaises(TypeError, setattr, buf, "value", memoryview("abc"))
         self.assertRaises(ValueError, setattr, buf, "raw", memoryview("x" * 100))
 
     def test_c_buffer_raw(self, memoryview=memoryview):
@@ -40,7 +41,8 @@
 
         buf.raw = memoryview("Hello, World")
         self.assertEqual(buf.value, "Hello, World")
-        self.assertRaises(TypeError, setattr, buf, "value", memoryview("abc"))
+        if test_support.check_impl_detail():
+            self.assertRaises(TypeError, setattr, buf, "value", memoryview("abc"))
         self.assertRaises(ValueError, setattr, buf, "raw", memoryview("x" * 100))
 
     def test_c_buffer_deprecated(self):
diff --git a/lib-python/2.7/ctypes/test/test_structures.py b/lib-python/2.7/ctypes/test/test_structures.py
--- a/lib-python/2.7/ctypes/test/test_structures.py
+++ b/lib-python/2.7/ctypes/test/test_structures.py
@@ -194,8 +194,8 @@
         self.assertEqual(X.b.offset, min(8, longlong_align))
 
 
-        d = {"_fields_": [("a", "b"),
-                          ("b", "q")],
+        d = {"_fields_": [("a", c_byte),
+                          ("b", c_longlong)],
              "_pack_": -1}
         self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)
 
diff --git a/lib-python/2.7/ctypes/test/test_varsize_struct.py b/lib-python/2.7/ctypes/test/test_varsize_struct.py
--- a/lib-python/2.7/ctypes/test/test_varsize_struct.py
+++ b/lib-python/2.7/ctypes/test/test_varsize_struct.py
@@ -1,7 +1,9 @@
 from ctypes import *
 import unittest
+from ctypes.test import xfail
 
 class VarSizeTest(unittest.TestCase):
+    @xfail
     def test_resize(self):
         class X(Structure):
             _fields_ = [("item", c_int),
diff --git a/lib-python/2.7/ctypes/util.py b/lib-python/2.7/ctypes/util.py
--- a/lib-python/2.7/ctypes/util.py
+++ b/lib-python/2.7/ctypes/util.py
@@ -72,8 +72,8 @@
         return name
 
 if os.name == "posix" and sys.platform == "darwin":
-    from ctypes.macholib.dyld import dyld_find as _dyld_find
     def find_library(name):
+        from ctypes.macholib.dyld import dyld_find as _dyld_find
         possible = ['lib%s.dylib' % name,
                     '%s.dylib' % name,
                     '%s.framework/%s' % (name, name)]
diff --git a/lib-python/2.7/distutils/command/bdist_wininst.py b/lib-python/2.7/distutils/command/bdist_wininst.py
--- a/lib-python/2.7/distutils/command/bdist_wininst.py
+++ b/lib-python/2.7/distutils/command/bdist_wininst.py
@@ -298,7 +298,8 @@
                              bitmaplen,        # number of bytes in bitmap
                              )
         file.write(header)
-        file.write(open(arcname, "rb").read())
+        with open(arcname, "rb") as arcfile:
+            file.write(arcfile.read())
 
     # create_exe()
 
diff --git a/lib-python/2.7/distutils/command/build_ext.py b/lib-python/2.7/distutils/command/build_ext.py
--- a/lib-python/2.7/distutils/command/build_ext.py
+++ b/lib-python/2.7/distutils/command/build_ext.py
@@ -184,7 +184,7 @@
             # the 'libs' directory is for binary installs - we assume that
             # must be the *native* platform.  But we don't really support
             # cross-compiling via a binary install anyway, so we let it go.
-            self.library_dirs.append(os.path.join(sys.exec_prefix, 'libs'))
+            self.library_dirs.append(os.path.join(sys.exec_prefix, 'include'))
             if self.debug:
                 self.build_temp = os.path.join(self.build_temp, "Debug")
             else:
@@ -192,8 +192,13 @@
 
             # Append the source distribution include and library directories,
             # this allows distutils on windows to work in the source tree
-            self.include_dirs.append(os.path.join(sys.exec_prefix, 'PC'))
-            if MSVC_VERSION == 9:
+            if 0:
+                # pypy has no PC directory
+                self.include_dirs.append(os.path.join(sys.exec_prefix, 'PC'))
+            if 1:
+                # pypy has no PCBuild directory
+                pass
+            elif MSVC_VERSION == 9:
                 # Use the .lib files for the correct architecture
                 if self.plat_name == 'win32':
                     suffix = ''
@@ -695,24 +700,14 @@
         shared extension.  On most platforms, this is just 'ext.libraries';
         on Windows and OS/2, we add the Python library (eg. python20.dll).
         """
-        # The python library is always needed on Windows.  For MSVC, this
-        # is redundant, since the library is mentioned in a pragma in
-        # pyconfig.h that MSVC groks.  The other Windows compilers all seem
-        # to need it mentioned explicitly, though, so that's what we do.
-        # Append '_d' to the python import library on debug builds.
+        # The python library is always needed on Windows.
         if sys.platform == "win32":
-            from distutils.msvccompiler import MSVCCompiler
-            if not isinstance(self.compiler, MSVCCompiler):
-                template = "python%d%d"
-                if self.debug:
-                    template = template + '_d'
-                pythonlib = (template %
-                       (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
-                # don't extend ext.libraries, it may be shared with other
-                # extensions, it is a reference to the original list
-                return ext.libraries + [pythonlib]
-            else:
-                return ext.libraries
+            template = "python%d%d"
+            pythonlib = (template %
+                   (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
+            # don't extend ext.libraries, it may be shared with other
+            # extensions, it is a reference to the original list
+            return ext.libraries + [pythonlib]
         elif sys.platform == "os2emx":
             # EMX/GCC requires the python library explicitly, and I
             # believe VACPP does as well (though not confirmed) - AIM Apr01
diff --git a/lib-python/2.7/distutils/command/install.py b/lib-python/2.7/distutils/command/install.py
--- a/lib-python/2.7/distutils/command/install.py
+++ b/lib-python/2.7/distutils/command/install.py
@@ -83,6 +83,13 @@
         'scripts': '$userbase/bin',
         'data'   : '$userbase',
         },
+    'pypy': {
+        'purelib': '$base/site-packages',
+        'platlib': '$base/site-packages',
+        'headers': '$base/include',
+        'scripts': '$base/bin',
+        'data'   : '$base',
+        },
     }
 
 # The keys to an installation scheme; if any new types of files are to be
@@ -467,6 +474,8 @@
 
     def select_scheme (self, name):
         # it's the caller's problem if they supply a bad name!
+        if hasattr(sys, 'pypy_version_info'):
+            name = 'pypy'
         scheme = INSTALL_SCHEMES[name]
         for key in SCHEME_KEYS:
             attrname = 'install_' + key
diff --git a/lib-python/2.7/distutils/cygwinccompiler.py b/lib-python/2.7/distutils/cygwinccompiler.py
--- a/lib-python/2.7/distutils/cygwinccompiler.py
+++ b/lib-python/2.7/distutils/cygwinccompiler.py
@@ -75,6 +75,9 @@
         elif msc_ver == '1500':
             # VS2008 / MSVC 9.0
             return ['msvcr90']
+        elif msc_ver == '1600':
+            # VS2010 / MSVC 10.0
+            return ['msvcr100']
         else:
             raise ValueError("Unknown MS Compiler version %s " % msc_ver)
 
diff --git a/lib-python/2.7/distutils/msvc9compiler.py b/lib-python/2.7/distutils/msvc9compiler.py
--- a/lib-python/2.7/distutils/msvc9compiler.py
+++ b/lib-python/2.7/distutils/msvc9compiler.py
@@ -648,6 +648,7 @@
             temp_manifest = os.path.join(
                     build_temp,
                     os.path.basename(output_filename) + ".manifest")
+            ld_args.append('/MANIFEST')
             ld_args.append('/MANIFESTFILE:' + temp_manifest)
 
             if extra_preargs:
diff --git a/lib-python/2.7/distutils/spawn.py b/lib-python/2.7/distutils/spawn.py
--- a/lib-python/2.7/distutils/spawn.py
+++ b/lib-python/2.7/distutils/spawn.py
@@ -58,7 +58,6 @@
 
 def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
     executable = cmd[0]
-    cmd = _nt_quote_args(cmd)
     if search_path:
         # either we find one or it stays the same
         executable = find_executable(executable) or executable
@@ -66,7 +65,8 @@
     if not dry_run:
         # spawn for NT requires a full path to the .exe
         try:
-            rc = os.spawnv(os.P_WAIT, executable, cmd)
+            import subprocess
+            rc = subprocess.call(cmd)
         except OSError, exc:
             # this seems to happen when the command isn't found
             raise DistutilsExecError, \
diff --git a/lib-python/2.7/distutils/sysconfig.py b/lib-python/2.7/distutils/sysconfig.py
--- a/lib-python/2.7/distutils/sysconfig.py
+++ b/lib-python/2.7/distutils/sysconfig.py
@@ -9,563 +9,21 @@
 Email:        <fdrake at acm.org>
 """
 
-__revision__ = "$Id$"
+__revision__ = "$Id: sysconfig.py 85358 2010-10-10 09:54:59Z antoine.pitrou $"
 
-import os
-import re
-import string
 import sys
 
-from distutils.errors import DistutilsPlatformError
 
-# These are needed in a couple of spots, so just compute them once.
-PREFIX = os.path.normpath(sys.prefix)
-EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
+# The content of this file is redirected from
+# sysconfig_cpython or sysconfig_pypy.
 
-# Path to the base directory of the project. On Windows the binary may
-# live in project/PCBuild9.  If we're dealing with an x64 Windows build,
-# it'll live in project/PCbuild/amd64.
-project_base = os.path.dirname(os.path.abspath(sys.executable))
-if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
-    project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
-# PC/VS7.1
-if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
-    project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
-                                                os.path.pardir))
-# PC/AMD64
-if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
-    project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
-                                                os.path.pardir))
+if '__pypy__' in sys.builtin_module_names:
+    from distutils.sysconfig_pypy import *
+    from distutils.sysconfig_pypy import _config_vars # needed by setuptools
+    from distutils.sysconfig_pypy import _variable_rx # read_setup_file()
+else:
+    from distutils.sysconfig_cpython import *
+    from distutils.sysconfig_cpython import _config_vars # needed by setuptools
+    from distutils.sysconfig_cpython import _variable_rx # read_setup_file()
 
-# python_build: (Boolean) if true, we're either building Python or
-# building an extension with an un-installed Python, so we use
-# different (hard-wired) directories.
-# Setup.local is available for Makefile builds including VPATH builds,
-# Setup.dist is available on Windows
-def _python_build():
-    for fn in ("Setup.dist", "Setup.local"):
-        if os.path.isfile(os.path.join(project_base, "Modules", fn)):
-            return True
-    return False
-python_build = _python_build()
 
-
-def get_python_version():
-    """Return a string containing the major and minor Python version,
-    leaving off the patchlevel.  Sample return values could be '1.5'
-    or '2.2'.
-    """
-    return sys.version[:3]
-
-
-def get_python_inc(plat_specific=0, prefix=None):
-    """Return the directory containing installed Python header files.
-
-    If 'plat_specific' is false (the default), this is the path to the
-    non-platform-specific header files, i.e. Python.h and so on;
-    otherwise, this is the path to platform-specific header files
-    (namely pyconfig.h).
-
-    If 'prefix' is supplied, use it instead of sys.prefix or
-    sys.exec_prefix -- i.e., ignore 'plat_specific'.
-    """
-    if prefix is None:
-        prefix = plat_specific and EXEC_PREFIX or PREFIX
-
-    if os.name == "posix":
-        if python_build:
-            buildir = os.path.dirname(sys.executable)
-            if plat_specific:
-                # python.h is located in the buildir
-                inc_dir = buildir
-            else:
-                # the source dir is relative to the buildir
-                srcdir = os.path.abspath(os.path.join(buildir,
-                                         get_config_var('srcdir')))
-                # Include is located in the srcdir
-                inc_dir = os.path.join(srcdir, "Include")
-            return inc_dir
-        return os.path.join(prefix, "include", "python" + get_python_version())
-    elif os.name == "nt":
-        return os.path.join(prefix, "include")
-    elif os.name == "os2":
-        return os.path.join(prefix, "Include")
-    else:
-        raise DistutilsPlatformError(
-            "I don't know where Python installs its C header files "
-            "on platform '%s'" % os.name)
-
-
-def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
-    """Return the directory containing the Python library (standard or
-    site additions).
-
-    If 'plat_specific' is true, return the directory containing
-    platform-specific modules, i.e. any module from a non-pure-Python
-    module distribution; otherwise, return the platform-shared library
-    directory.  If 'standard_lib' is true, return the directory
-    containing standard Python library modules; otherwise, return the
-    directory for site-specific modules.
-
-    If 'prefix' is supplied, use it instead of sys.prefix or
-    sys.exec_prefix -- i.e., ignore 'plat_specific'.
-    """
-    if prefix is None:
-        prefix = plat_specific and EXEC_PREFIX or PREFIX
-
-    if os.name == "posix":
-        libpython = os.path.join(prefix,
-                                 "lib", "python" + get_python_version())
-        if standard_lib:
-            return libpython
-        else:
-            return os.path.join(libpython, "site-packages")
-
-    elif os.name == "nt":
-        if standard_lib:
-            return os.path.join(prefix, "Lib")
-        else:
-            if get_python_version() < "2.2":
-                return prefix
-            else:
-                return os.path.join(prefix, "Lib", "site-packages")
-
-    elif os.name == "os2":
-        if standard_lib:
-            return os.path.join(prefix, "Lib")
-        else:
-            return os.path.join(prefix, "Lib", "site-packages")
-
-    else:
-        raise DistutilsPlatformError(
-            "I don't know where Python installs its library "
-            "on platform '%s'" % os.name)
-
-
-def customize_compiler(compiler):
-    """Do any platform-specific customization of a CCompiler instance.
-
-    Mainly needed on Unix, so we can plug in the information that
-    varies across Unices and is stored in Python's Makefile.
-    """
-    if compiler.compiler_type == "unix":
-        (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
-            get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
-                            'CCSHARED', 'LDSHARED', 'SO')
-
-        if 'CC' in os.environ:
-            cc = os.environ['CC']
-        if 'CXX' in os.environ:
-            cxx = os.environ['CXX']
-        if 'LDSHARED' in os.environ:
-            ldshared = os.environ['LDSHARED']
-        if 'CPP' in os.environ:
-            cpp = os.environ['CPP']
-        else:
-            cpp = cc + " -E"           # not always
-        if 'LDFLAGS' in os.environ:
-            ldshared = ldshared + ' ' + os.environ['LDFLAGS']
-        if 'CFLAGS' in os.environ:
-            cflags = opt + ' ' + os.environ['CFLAGS']
-            ldshared = ldshared + ' ' + os.environ['CFLAGS']
-        if 'CPPFLAGS' in os.environ:
-            cpp = cpp + ' ' + os.environ['CPPFLAGS']
-            cflags = cflags + ' ' + os.environ['CPPFLAGS']
-            ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
-
-        cc_cmd = cc + ' ' + cflags
-        compiler.set_executables(
-            preprocessor=cpp,
-            compiler=cc_cmd,
-            compiler_so=cc_cmd + ' ' + ccshared,
-            compiler_cxx=cxx,
-            linker_so=ldshared,
-            linker_exe=cc)
-
-        compiler.shared_lib_extension = so_ext
-
-
-def get_config_h_filename():
-    """Return full pathname of installed pyconfig.h file."""
-    if python_build:
-        if os.name == "nt":
-            inc_dir = os.path.join(project_base, "PC")
-        else:
-            inc_dir = project_base
-    else:
-        inc_dir = get_python_inc(plat_specific=1)
-    if get_python_version() < '2.2':
-        config_h = 'config.h'
-    else:
-        # The name of the config.h file changed in 2.2
-        config_h = 'pyconfig.h'
-    return os.path.join(inc_dir, config_h)
-
-
-def get_makefile_filename():
-    """Return full pathname of installed Makefile from the Python build."""
-    if python_build:
-        return os.path.join(os.path.dirname(sys.executable), "Makefile")
-    lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
-    return os.path.join(lib_dir, "config", "Makefile")
-
-
-def parse_config_h(fp, g=None):
-    """Parse a config.h-style file.
-
-    A dictionary containing name/value pairs is returned.  If an
-    optional dictionary is passed in as the second argument, it is
-    used instead of a new dictionary.
-    """
-    if g is None:
-        g = {}
-    define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
-    undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
-    #
-    while 1:
-        line = fp.readline()
-        if not line:
-            break
-        m = define_rx.match(line)
-        if m:
-            n, v = m.group(1, 2)
-            try: v = int(v)
-            except ValueError: pass
-            g[n] = v
-        else:
-            m = undef_rx.match(line)
-            if m:
-                g[m.group(1)] = 0
-    return g
-
-
-# Regexes needed for parsing Makefile (and similar syntaxes,
-# like old-style Setup files).
-_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
-_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
-_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
-
-def parse_makefile(fn, g=None):
-    """Parse a Makefile-style file.
-
-    A dictionary containing name/value pairs is returned.  If an
-    optional dictionary is passed in as the second argument, it is
-    used instead of a new dictionary.
-    """
-    from distutils.text_file import TextFile
-    fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
-
-    if g is None:
-        g = {}
-    done = {}
-    notdone = {}
-
-    while 1:
-        line = fp.readline()
-        if line is None:  # eof
-            break
-        m = _variable_rx.match(line)
-        if m:
-            n, v = m.group(1, 2)
-            v = v.strip()
-            # `$$' is a literal `$' in make
-            tmpv = v.replace('$$', '')
-
-            if "$" in tmpv:
-                notdone[n] = v
-            else:
-                try:
-                    v = int(v)
-                except ValueError:
-                    # insert literal `$'
-                    done[n] = v.replace('$$', '$')
-                else:
-                    done[n] = v
-
-    # do variable interpolation here
-    while notdone:
-        for name in notdone.keys():
-            value = notdone[name]
-            m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
-            if m:
-                n = m.group(1)
-                found = True
-                if n in done:
-                    item = str(done[n])
-                elif n in notdone:
-                    # get it on a subsequent round
-                    found = False
-                elif n in os.environ:
-                    # do it like make: fall back to environment
-                    item = os.environ[n]
-                else:
-                    done[n] = item = ""
-                if found:
-                    after = value[m.end():]
-                    value = value[:m.start()] + item + after
-                    if "$" in after:
-                        notdone[name] = value
-                    else:
-                        try: value = int(value)
-                        except ValueError:
-                            done[name] = value.strip()
-                        else:
-                            done[name] = value
-                        del notdone[name]
-            else:
-                # bogus variable reference; just drop it since we can't deal
-                del notdone[name]
-
-    fp.close()
-
-    # strip spurious spaces
-    for k, v in done.items():
-        if isinstance(v, str):
-            done[k] = v.strip()
-
-    # save the results in the global dictionary
-    g.update(done)
-    return g
-
-
-def expand_makefile_vars(s, vars):
-    """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
-    'string' according to 'vars' (a dictionary mapping variable names to
-    values).  Variables not present in 'vars' are silently expanded to the
-    empty string.  The variable values in 'vars' should not contain further
-    variable expansions; if 'vars' is the output of 'parse_makefile()',
-    you're fine.  Returns a variable-expanded version of 's'.
-    """
-
-    # This algorithm does multiple expansion, so if vars['foo'] contains
-    # "${bar}", it will expand ${foo} to ${bar}, and then expand
-    # ${bar}... and so forth.  This is fine as long as 'vars' comes from
-    # 'parse_makefile()', which takes care of such expansions eagerly,
-    # according to make's variable expansion semantics.
-
-    while 1:
-        m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
-        if m:
-            (beg, end) = m.span()
-            s = s[0:beg] + vars.get(m.group(1)) + s[end:]
-        else:
-            break
-    return s
-
-
-_config_vars = None
-
-def _init_posix():
-    """Initialize the module as appropriate for POSIX systems."""
-    g = {}
-    # load the installed Makefile:
-    try:
-        filename = get_makefile_filename()
-        parse_makefile(filename, g)
-    except IOError, msg:
-        my_msg = "invalid Python installation: unable to open %s" % filename
-        if hasattr(msg, "strerror"):
-            my_msg = my_msg + " (%s)" % msg.strerror
-
-        raise DistutilsPlatformError(my_msg)
-
-    # load the installed pyconfig.h:
-    try:
-        filename = get_config_h_filename()
-        parse_config_h(file(filename), g)
-    except IOError, msg:
-        my_msg = "invalid Python installation: unable to open %s" % filename
-        if hasattr(msg, "strerror"):
-            my_msg = my_msg + " (%s)" % msg.strerror
-
-        raise DistutilsPlatformError(my_msg)
-
-    # On MacOSX we need to check the setting of the environment variable
-    # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
-    # it needs to be compatible.
-    # If it isn't set we set it to the configure-time value
-    if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
-        cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
-        cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
-        if cur_target == '':
-            cur_target = cfg_target
-            os.environ['MACOSX_DEPLOYMENT_TARGET'] = cfg_target
-        elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
-            my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
-                % (cur_target, cfg_target))
-            raise DistutilsPlatformError(my_msg)
-
-    # On AIX, there are wrong paths to the linker scripts in the Makefile
-    # -- these paths are relative to the Python source, but when installed
-    # the scripts are in another directory.
-    if python_build:
-        g['LDSHARED'] = g['BLDSHARED']
-
-    elif get_python_version() < '2.1':
-        # The following two branches are for 1.5.2 compatibility.
-        if sys.platform == 'aix4':          # what about AIX 3.x ?
-            # Linker script is in the config directory, not in Modules as the
-            # Makefile says.
-            python_lib = get_python_lib(standard_lib=1)
-            ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
-            python_exp = os.path.join(python_lib, 'config', 'python.exp')
-
-            g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
-
-        elif sys.platform == 'beos':
-            # Linker script is in the config directory.  In the Makefile it is
-            # relative to the srcdir, which after installation no longer makes
-            # sense.
-            python_lib = get_python_lib(standard_lib=1)
-            linkerscript_path = string.split(g['LDSHARED'])[0]
-            linkerscript_name = os.path.basename(linkerscript_path)
-            linkerscript = os.path.join(python_lib, 'config',
-                                        linkerscript_name)
-
-            # XXX this isn't the right place to do this: adding the Python
-            # library to the link, if needed, should be in the "build_ext"
-            # command.  (It's also needed for non-MS compilers on Windows, and
-            # it's taken care of for them by the 'build_ext.get_libraries()'
-            # method.)
-            g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
-                             (linkerscript, PREFIX, get_python_version()))
-
-    global _config_vars
-    _config_vars = g
-
-
-def _init_nt():
-    """Initialize the module as appropriate for NT"""
-    g = {}
-    # set basic install directories
-    g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
-    g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
-
-    # XXX hmmm.. a normal install puts include files here
-    g['INCLUDEPY'] = get_python_inc(plat_specific=0)
-
-    g['SO'] = '.pyd'
-    g['EXE'] = ".exe"
-    g['VERSION'] = get_python_version().replace(".", "")
-    g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
-
-    global _config_vars
-    _config_vars = g
-
-
-def _init_os2():
-    """Initialize the module as appropriate for OS/2"""
-    g = {}
-    # set basic install directories
-    g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
-    g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
-
-    # XXX hmmm.. a normal install puts include files here
-    g['INCLUDEPY'] = get_python_inc(plat_specific=0)
-
-    g['SO'] = '.pyd'
-    g['EXE'] = ".exe"
-
-    global _config_vars
-    _config_vars = g
-
-
-def get_config_vars(*args):
-    """With no arguments, return a dictionary of all configuration
-    variables relevant for the current platform.  Generally this includes
-    everything needed to build extensions and install both pure modules and
-    extensions.  On Unix, this means every variable defined in Python's
-    installed Makefile; on Windows and Mac OS it's a much smaller set.
-
-    With arguments, return a list of values that result from looking up
-    each argument in the configuration variable dictionary.
-    """
-    global _config_vars
-    if _config_vars is None:
-        func = globals().get("_init_" + os.name)
-        if func:
-            func()
-        else:
-            _config_vars = {}
-
-        # Normalized versions of prefix and exec_prefix are handy to have;
-        # in fact, these are the standard versions used most places in the
-        # Distutils.
-        _config_vars['prefix'] = PREFIX
-        _config_vars['exec_prefix'] = EXEC_PREFIX
-
-        if sys.platform == 'darwin':
-            kernel_version = os.uname()[2] # Kernel version (8.4.3)
-            major_version = int(kernel_version.split('.')[0])
-
-            if major_version < 8:
-                # On Mac OS X before 10.4, check if -arch and -isysroot
-                # are in CFLAGS or LDFLAGS and remove them if they are.
-                # This is needed when building extensions on a 10.3 system
-                # using a universal build of python.
-                for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
-                        # a number of derived variables. These need to be
-                        # patched up as well.
-                        'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
-                    flags = _config_vars[key]
-                    flags = re.sub('-arch\s+\w+\s', ' ', flags)
-                    flags = re.sub('-isysroot [^ \t]*', ' ', flags)
-                    _config_vars[key] = flags
-
-            else:
-
-                # Allow the user to override the architecture flags using
-                # an environment variable.
-                # NOTE: This name was introduced by Apple in OSX 10.5 and
-                # is used by several scripting languages distributed with
-                # that OS release.
-
-                if 'ARCHFLAGS' in os.environ:
-                    arch = os.environ['ARCHFLAGS']
-                    for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
-                        # a number of derived variables. These need to be
-                        # patched up as well.
-                        'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
-
-                        flags = _config_vars[key]
-                        flags = re.sub('-arch\s+\w+\s', ' ', flags)
-                        flags = flags + ' ' + arch
-                        _config_vars[key] = flags
-
-                # If we're on OSX 10.5 or later and the user tries to
-                # compiles an extension using an SDK that is not present
-                # on the current machine it is better to not use an SDK
-                # than to fail.
-                #
-                # The major usecase for this is users using a Python.org
-                # binary installer  on OSX 10.6: that installer uses
-                # the 10.4u SDK, but that SDK is not installed by default
-                # when you install Xcode.
-                #
-                m = re.search('-isysroot\s+(\S+)', _config_vars['CFLAGS'])
-                if m is not None:
-                    sdk = m.group(1)
-                    if not os.path.exists(sdk):
-                        for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',
-                             # a number of derived variables. These need to be
-                             # patched up as well.
-                            'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
-
-                            flags = _config_vars[key]
-                            flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags)
-                            _config_vars[key] = flags
-
-    if args:
-        vals = []
-        for name in args:
-            vals.append(_config_vars.get(name))
-        return vals
-    else:
-        return _config_vars
-
-def get_config_var(name):
-    """Return the value of a single variable using the dictionary
-    returned by 'get_config_vars()'.  Equivalent to
-    get_config_vars().get(name)
-    """
-    return get_config_vars().get(name)
diff --git a/lib-python/modified-2.7/distutils/sysconfig_cpython.py b/lib-python/2.7/distutils/sysconfig_cpython.py
rename from lib-python/modified-2.7/distutils/sysconfig_cpython.py
rename to lib-python/2.7/distutils/sysconfig_cpython.py
diff --git a/lib-python/modified-2.7/distutils/sysconfig_pypy.py b/lib-python/2.7/distutils/sysconfig_pypy.py
rename from lib-python/modified-2.7/distutils/sysconfig_pypy.py
rename to lib-python/2.7/distutils/sysconfig_pypy.py
diff --git a/lib-python/2.7/distutils/tests/test_build_ext.py b/lib-python/2.7/distutils/tests/test_build_ext.py
--- a/lib-python/2.7/distutils/tests/test_build_ext.py
+++ b/lib-python/2.7/distutils/tests/test_build_ext.py
@@ -293,7 +293,7 @@
         finally:
             os.chdir(old_wd)
         self.assertTrue(os.path.exists(so_file))
-        self.assertEqual(os.path.splitext(so_file)[-1],
+        self.assertEqual(so_file[so_file.index(os.path.extsep):],
                          sysconfig.get_config_var('SO'))
         so_dir = os.path.dirname(so_file)
         self.assertEqual(so_dir, other_tmp_dir)
@@ -302,7 +302,7 @@
         cmd.run()
         so_file = cmd.get_outputs()[0]
         self.assertTrue(os.path.exists(so_file))
-        self.assertEqual(os.path.splitext(so_file)[-1],
+        self.assertEqual(so_file[so_file.index(os.path.extsep):],
                          sysconfig.get_config_var('SO'))
         so_dir = os.path.dirname(so_file)
         self.assertEqual(so_dir, cmd.build_lib)
diff --git a/lib-python/2.7/distutils/tests/test_install.py b/lib-python/2.7/distutils/tests/test_install.py
--- a/lib-python/2.7/distutils/tests/test_install.py
+++ b/lib-python/2.7/distutils/tests/test_install.py
@@ -2,6 +2,7 @@
 
 import os
 import unittest
+from test import test_support
 
 from test.test_support import run_unittest
 
@@ -40,14 +41,15 @@
             expected = os.path.normpath(expected)
             self.assertEqual(got, expected)
 
-        libdir = os.path.join(destination, "lib", "python")
-        check_path(cmd.install_lib, libdir)
-        check_path(cmd.install_platlib, libdir)
-        check_path(cmd.install_purelib, libdir)
-        check_path(cmd.install_headers,
-                   os.path.join(destination, "include", "python", "foopkg"))
-        check_path(cmd.install_scripts, os.path.join(destination, "bin"))
-        check_path(cmd.install_data, destination)
+        if test_support.check_impl_detail():
+            libdir = os.path.join(destination, "lib", "python")
+            check_path(cmd.install_lib, libdir)
+            check_path(cmd.install_platlib, libdir)
+            check_path(cmd.install_purelib, libdir)
+            check_path(cmd.install_headers,
+                       os.path.join(destination, "include", "python", "foopkg"))
+            check_path(cmd.install_scripts, os.path.join(destination, "bin"))
+            check_path(cmd.install_data, destination)
 
 
 def test_suite():
diff --git a/lib-python/2.7/distutils/unixccompiler.py b/lib-python/2.7/distutils/unixccompiler.py
--- a/lib-python/2.7/distutils/unixccompiler.py
+++ b/lib-python/2.7/distutils/unixccompiler.py
@@ -125,7 +125,22 @@
                   }
 
     if sys.platform[:6] == "darwin":
+        import platform
+        if platform.machine() == 'i386':
+            if platform.architecture()[0] == '32bit':
+                arch = 'i386'
+            else:
+                arch = 'x86_64'
+        else:
+            # just a guess
+            arch = platform.machine()
         executables['ranlib'] = ["ranlib"]
+        executables['linker_so'] += ['-undefined', 'dynamic_lookup']
+
+        for k, v in executables.iteritems():
+            if v and v[0] == 'cc':
+                v += ['-arch', arch]
+
 
     # Needed for the filename generation methods provided by the base
     # class, CCompiler.  NB. whoever instantiates/uses a particular
@@ -309,7 +324,7 @@
             # On OSX users can specify an alternate SDK using
             # '-isysroot', calculate the SDK root if it is specified
             # (and use it further on)
-            cflags = sysconfig.get_config_var('CFLAGS')
+            cflags = sysconfig.get_config_var('CFLAGS') or ''
             m = re.search(r'-isysroot\s+(\S+)', cflags)
             if m is None:
                 sysroot = '/'
diff --git a/lib-python/2.7/heapq.py b/lib-python/2.7/heapq.py
--- a/lib-python/2.7/heapq.py
+++ b/lib-python/2.7/heapq.py
@@ -193,6 +193,8 @@
 
     Equivalent to:  sorted(iterable, reverse=True)[:n]
     """
+    if n < 0: # for consistency with the c impl
+        return []
     it = iter(iterable)
     result = list(islice(it, n))
     if not result:
@@ -209,6 +211,8 @@
 
     Equivalent to:  sorted(iterable)[:n]
     """
+    if n < 0: # for consistency with the c impl
+        return []
     if hasattr(iterable, '__len__') and n * 10 <= len(iterable):
         # For smaller values of n, the bisect method is faster than a minheap.
         # It is also memory efficient, consuming only n elements of space.
diff --git a/lib-python/2.7/httplib.py b/lib-python/2.7/httplib.py
--- a/lib-python/2.7/httplib.py
+++ b/lib-python/2.7/httplib.py
@@ -1024,7 +1024,11 @@
             kwds["buffering"] = True;
         response = self.response_class(*args, **kwds)
 
-        response.begin()
+        try:
+            response.begin()
+        except:
+            response.close()
+            raise
         assert response.will_close != _UNKNOWN
         self.__state = _CS_IDLE
 
diff --git a/lib-python/2.7/idlelib/Delegator.py b/lib-python/2.7/idlelib/Delegator.py
--- a/lib-python/2.7/idlelib/Delegator.py
+++ b/lib-python/2.7/idlelib/Delegator.py
@@ -12,6 +12,14 @@
         self.__cache[name] = attr
         return attr
 
+    def __nonzero__(self):
+        # this is needed for PyPy: else, if self.delegate is None, the
+        # __getattr__ above picks NoneType.__nonzero__, which returns
+        # False. Thus, bool(Delegator()) is False as well, but it's not what
+        # we want.  On CPython, bool(Delegator()) is True because NoneType
+        # does not have __nonzero__
+        return True
+
     def resetcache(self):
         for key in self.__cache.keys():
             try:
diff --git a/lib-python/2.7/inspect.py b/lib-python/2.7/inspect.py
--- a/lib-python/2.7/inspect.py
+++ b/lib-python/2.7/inspect.py
@@ -746,8 +746,15 @@
     'varargs' 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))
 
+    code = getattr(co, 'co_code', '')
     nargs = co.co_argcount
     names = co.co_varnames
     args = list(names[:nargs])
@@ -757,12 +764,12 @@
     for i in range(nargs):
         if args[i][:1] in ('', '.'):
             stack, remain, count = [], [], []
-            while step < len(co.co_code):
-                op = ord(co.co_code[step])
+            while step < len(code):
+                op = ord(code[step])
                 step = step + 1
                 if op >= dis.HAVE_ARGUMENT:
                     opname = dis.opname[op]
-                    value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
+                    value = ord(code[step]) + ord(code[step+1])*256
                     step = step + 2
                     if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
                         remain.append(value)
@@ -809,7 +816,9 @@
 
     if ismethod(func):
         func = func.im_func
-    if not isfunction(func):
+    if not (isfunction(func) or
+            isbuiltin(func) and hasattr(func, 'func_code')):
+            # PyPy extension: this works for built-in functions too
         raise TypeError('{!r} is not a Python function'.format(func))
     args, varargs, varkw = getargs(func.func_code)
     return ArgSpec(args, varargs, varkw, func.func_defaults)
@@ -949,7 +958,7 @@
                 raise TypeError('%s() takes exactly 0 arguments '
                                 '(%d given)' % (f_name, num_total))
         else:
-            raise TypeError('%s() takes no arguments (%d given)' %
+            raise TypeError('%s() takes no argument (%d given)' %
                             (f_name, num_total))
     for arg in args:
         if isinstance(arg, str) and arg in named:
diff --git a/lib-python/2.7/json/encoder.py b/lib-python/2.7/json/encoder.py
--- a/lib-python/2.7/json/encoder.py
+++ b/lib-python/2.7/json/encoder.py
@@ -2,14 +2,7 @@
 """
 import re
 
-try:
-    from _json import encode_basestring_ascii as c_encode_basestring_ascii
-except ImportError:
-    c_encode_basestring_ascii = None
-try:
-    from _json import make_encoder as c_make_encoder
-except ImportError:
-    c_make_encoder = None
+from __pypy__.builders import StringBuilder, UnicodeBuilder
 
 ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')
 ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])')
@@ -24,23 +17,22 @@
     '\t': '\\t',
 }
 for i in range(0x20):
-    ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i))
-    #ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
+    ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
 
 # Assume this produces an infinity on all machines (probably not guaranteed)
 INFINITY = float('1e66666')
 FLOAT_REPR = repr
 
-def encode_basestring(s):
+def raw_encode_basestring(s):
     """Return a JSON representation of a Python string
 
     """
     def replace(match):
         return ESCAPE_DCT[match.group(0)]
-    return '"' + ESCAPE.sub(replace, s) + '"'
+    return ESCAPE.sub(replace, s)
+encode_basestring = lambda s: '"' + raw_encode_basestring(s) + '"'
 
-
-def py_encode_basestring_ascii(s):
+def raw_encode_basestring_ascii(s):
     """Return an ASCII-only JSON representation of a Python string
 
     """
@@ -53,21 +45,19 @@
         except KeyError:
             n = ord(s)
             if n < 0x10000:
-                return '\\u{0:04x}'.format(n)
-                #return '\\u%04x' % (n,)
+                return '\\u%04x' % (n,)
             else:
                 # surrogate pair
                 n -= 0x10000
                 s1 = 0xd800 | ((n >> 10) & 0x3ff)
                 s2 = 0xdc00 | (n & 0x3ff)
-                return '\\u{0:04x}\\u{1:04x}'.format(s1, s2)
-                #return '\\u%04x\\u%04x' % (s1, s2)
-    return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"'
+                return '\\u%04x\\u%04x' % (s1, s2)
+    if ESCAPE_ASCII.search(s):
+        return str(ESCAPE_ASCII.sub(replace, s))
+    return s
+encode_basestring_ascii = lambda s: '"' + raw_encode_basestring_ascii(s) + '"'
 
 
-encode_basestring_ascii = (
-    c_encode_basestring_ascii or py_encode_basestring_ascii)
-
 class JSONEncoder(object):
     """Extensible JSON <http://json.org> encoder for Python data structures.
 
@@ -147,6 +137,17 @@
 
         self.skipkeys = skipkeys
         self.ensure_ascii = ensure_ascii
+        if ensure_ascii:
+            self.encoder = raw_encode_basestring_ascii
+        else:
+            self.encoder = raw_encode_basestring
+        if encoding != 'utf-8':
+            orig_encoder = self.encoder
+            def encoder(o):
+                if isinstance(o, str):
+                    o = o.decode(encoding)
+                return orig_encoder(o)
+            self.encoder = encoder
         self.check_circular = check_circular
         self.allow_nan = allow_nan
         self.sort_keys = sort_keys
@@ -184,24 +185,126 @@
         '{"foo": ["bar", "baz"]}'
 
         """
-        # This is for extremely simple cases and benchmarks.
+        if self.check_circular:
+            markers = {}
+        else:
+            markers = None
+        if self.ensure_ascii:
+            builder = StringBuilder()
+        else:
+            builder = UnicodeBuilder()
+        self._encode(o, markers, builder, 0)
+        return builder.build()
+
+    def _emit_indent(self, builder, _current_indent_level):
+        if self.indent is not None:
+            _current_indent_level += 1
+            newline_indent = '\n' + (' ' * (self.indent *
+                                            _current_indent_level))
+            separator = self.item_separator + newline_indent
+            builder.append(newline_indent)
+        else:
+            separator = self.item_separator
+        return separator, _current_indent_level
+
+    def _emit_unindent(self, builder, _current_indent_level):
+        if self.indent is not None:
+            builder.append('\n')
+            builder.append(' ' * (self.indent * (_current_indent_level - 1)))
+
+    def _encode(self, o, markers, builder, _current_indent_level):
         if isinstance(o, basestring):
-            if isinstance(o, str):
-                _encoding = self.encoding
-                if (_encoding is not None
-                        and not (_encoding == 'utf-8')):
-                    o = o.decode(_encoding)
-            if self.ensure_ascii:
-                return encode_basestring_ascii(o)
+            builder.append('"')
+            builder.append(self.encoder(o))
+            builder.append('"')
+        elif o is None:
+            builder.append('null')
+        elif o is True:
+            builder.append('true')
+        elif o is False:
+            builder.append('false')
+        elif isinstance(o, (int, long)):
+            builder.append(str(o))
+        elif isinstance(o, float):
+            builder.append(self._floatstr(o))
+        elif isinstance(o, (list, tuple)):
+            if not o:
+                builder.append('[]')
+                return
+            self._encode_list(o, markers, builder, _current_indent_level)
+        elif isinstance(o, dict):
+            if not o:
+                builder.append('{}')
+                return
+            self._encode_dict(o, markers, builder, _current_indent_level)
+        else:
+            self._mark_markers(markers, o)
+            res = self.default(o)
+            self._encode(res, markers, builder, _current_indent_level)
+            self._remove_markers(markers, o)
+            return res
+
+    def _encode_list(self, l, markers, builder, _current_indent_level):
+        self._mark_markers(markers, l)
+        builder.append('[')
+        first = True
+        separator, _current_indent_level = self._emit_indent(builder,
+                                                      _current_indent_level)
+        for elem in l:
+            if first:
+                first = False
             else:
-                return encode_basestring(o)
-        # This doesn't pass the iterator directly to ''.join() because the
-        # exceptions aren't as detailed.  The list call should be roughly
-        # equivalent to the PySequence_Fast that ''.join() would do.
-        chunks = self.iterencode(o, _one_shot=True)
-        if not isinstance(chunks, (list, tuple)):
-            chunks = list(chunks)
-        return ''.join(chunks)
+                builder.append(separator)
+            self._encode(elem, markers, builder, _current_indent_level)
+            del elem # XXX grumble
+        self._emit_unindent(builder, _current_indent_level)
+        builder.append(']')
+        self._remove_markers(markers, l)
+
+    def _encode_dict(self, d, markers, builder, _current_indent_level):
+        self._mark_markers(markers, d)
+        first = True
+        builder.append('{')
+        separator, _current_indent_level = self._emit_indent(builder,
+                                                         _current_indent_level)
+        if self.sort_keys:
+            items = sorted(d.items(), key=lambda kv: kv[0])
+        else:
+            items = d.iteritems()
+
+        for key, v in items:
+            if first:
+                first = False
+            else:
+                builder.append(separator)
+            if isinstance(key, basestring):
+                pass
+            # JavaScript is weakly typed for these, so it makes sense to
+            # also allow them.  Many encoders seem to do something like this.
+            elif isinstance(key, float):
+                key = self._floatstr(key)
+            elif key is True:
+                key = 'true'
+            elif key is False:
+                key = 'false'
+            elif key is None:
+                key = 'null'
+            elif isinstance(key, (int, long)):
+                key = str(key)
+            elif self.skipkeys:
+                continue
+            else:
+                raise TypeError("key " + repr(key) + " is not a string")
+            builder.append('"')
+            builder.append(self.encoder(key))
+            builder.append('"')
+            builder.append(self.key_separator)
+            self._encode(v, markers, builder, _current_indent_level)
+            del key
+            del v # XXX grumble
+        self._emit_unindent(builder, _current_indent_level)
+        builder.append('}')
+        self._remove_markers(markers, d)
 
     def iterencode(self, o, _one_shot=False):
         """Encode the given object and yield each string
@@ -217,86 +320,54 @@
             markers = {}
         else:
             markers = None
-        if self.ensure_ascii:
-            _encoder = encode_basestring_ascii
+        return self._iterencode(o, markers, 0)
+
+    def _floatstr(self, o):
+        # Check for specials.  Note that this type of test is processor
+        # and/or platform-specific, so do tests which don't depend on the
+        # internals.
+
+        if o != o:
+            text = 'NaN'
+        elif o == INFINITY:
+            text = 'Infinity'
+        elif o == -INFINITY:
+            text = '-Infinity'
         else:
-            _encoder = encode_basestring
-        if self.encoding != 'utf-8':
-            def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding):
-                if isinstance(o, str):
-                    o = o.decode(_encoding)
-                return _orig_encoder(o)
+            return FLOAT_REPR(o)
 
-        def floatstr(o, allow_nan=self.allow_nan,
-                _repr=FLOAT_REPR, _inf=INFINITY, _neginf=-INFINITY):
-            # Check for specials.  Note that this type of test is processor
-            # and/or platform-specific, so do tests which don't depend on the
-            # internals.
+        if not self.allow_nan:
+            raise ValueError(
+                "Out of range float values are not JSON compliant: " +
+                repr(o))
 
-            if o != o:
-                text = 'NaN'
-            elif o == _inf:
-                text = 'Infinity'
-            elif o == _neginf:
-                text = '-Infinity'
-            else:
-                return _repr(o)
+        return text
 
-            if not allow_nan:
-                raise ValueError(
-                    "Out of range float values are not JSON compliant: " +
-                    repr(o))
+    def _mark_markers(self, markers, o):
+        if markers is not None:
+            if id(o) in markers:
+                raise ValueError("Circular reference detected")
+            markers[id(o)] = None
 
-            return text
+    def _remove_markers(self, markers, o):
+        if markers is not None:
+            del markers[id(o)]
 
-
-        if (_one_shot and c_make_encoder is not None
-                and self.indent is None and not self.sort_keys):
-            _iterencode = c_make_encoder(
-                markers, self.default, _encoder, self.indent,
-                self.key_separator, self.item_separator, self.sort_keys,
-                self.skipkeys, self.allow_nan)
-        else:
-            _iterencode = _make_iterencode(
-                markers, self.default, _encoder, self.indent, floatstr,
-                self.key_separator, self.item_separator, self.sort_keys,
-                self.skipkeys, _one_shot)
-        return _iterencode(o, 0)
-
-def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
-        _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
-        ## HACK: hand-optimized bytecode; turn globals into locals
-        ValueError=ValueError,
-        basestring=basestring,
-        dict=dict,
-        float=float,
-        id=id,
-        int=int,
-        isinstance=isinstance,
-        list=list,
-        long=long,
-        str=str,
-        tuple=tuple,
-    ):
-
-    def _iterencode_list(lst, _current_indent_level):
+    def _iterencode_list(self, lst, markers, _current_indent_level):
         if not lst:
             yield '[]'
             return
-        if markers is not None:
-            markerid = id(lst)
-            if markerid in markers:
-                raise ValueError("Circular reference detected")
-            markers[markerid] = lst
+        self._mark_markers(markers, lst)
         buf = '['
-        if _indent is not None:
+        if self.indent is not None:
             _current_indent_level += 1
-            newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
-            separator = _item_separator + newline_indent
+            newline_indent = '\n' + (' ' * (self.indent *
+                                            _current_indent_level))
+            separator = self.item_separator + newline_indent
             buf += newline_indent
         else:
             newline_indent = None
-            separator = _item_separator
+            separator = self.item_separator
         first = True
         for value in lst:
             if first:
@@ -304,7 +375,7 @@
             else:
                 buf = separator
             if isinstance(value, basestring):
-                yield buf + _encoder(value)
+                yield buf + '"' + self.encoder(value) + '"'
             elif value is None:
                 yield buf + 'null'
             elif value is True:
@@ -314,44 +385,43 @@
             elif isinstance(value, (int, long)):
                 yield buf + str(value)
             elif isinstance(value, float):
-                yield buf + _floatstr(value)
+                yield buf + self._floatstr(value)
             else:
                 yield buf
                 if isinstance(value, (list, tuple)):
-                    chunks = _iterencode_list(value, _current_indent_level)
+                    chunks = self._iterencode_list(value, markers,
+                                                   _current_indent_level)
                 elif isinstance(value, dict):
-                    chunks = _iterencode_dict(value, _current_indent_level)
+                    chunks = self._iterencode_dict(value, markers,
+                                                   _current_indent_level)
                 else:
-                    chunks = _iterencode(value, _current_indent_level)
+                    chunks = self._iterencode(value, markers,
+                                              _current_indent_level)
                 for chunk in chunks:
                     yield chunk
         if newline_indent is not None:
             _current_indent_level -= 1
-            yield '\n' + (' ' * (_indent * _current_indent_level))
+            yield '\n' + (' ' * (self.indent * _current_indent_level))
         yield ']'
-        if markers is not None:
-            del markers[markerid]
+        self._remove_markers(markers, lst)
 
-    def _iterencode_dict(dct, _current_indent_level):
+    def _iterencode_dict(self, dct, markers, _current_indent_level):
         if not dct:
             yield '{}'
             return
-        if markers is not None:
-            markerid = id(dct)
-            if markerid in markers:
-                raise ValueError("Circular reference detected")
-            markers[markerid] = dct
+        self._mark_markers(markers, dct)
         yield '{'
-        if _indent is not None:
+        if self.indent is not None:
             _current_indent_level += 1
-            newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
-            item_separator = _item_separator + newline_indent
+            newline_indent = '\n' + (' ' * (self.indent *
+                                            _current_indent_level))
+            item_separator = self.item_separator + newline_indent
             yield newline_indent
         else:
             newline_indent = None
-            item_separator = _item_separator
+            item_separator = self.item_separator
         first = True
-        if _sort_keys:
+        if self.sort_keys:
             items = sorted(dct.items(), key=lambda kv: kv[0])
         else:
             items = dct.iteritems()
@@ -361,7 +431,7 @@
             # JavaScript is weakly typed for these, so it makes sense to
             # also allow them.  Many encoders seem to do something like this.
             elif isinstance(key, float):
-                key = _floatstr(key)
+                key = self._floatstr(key)
             elif key is True:
                 key = 'true'
             elif key is False:
@@ -370,7 +440,7 @@
                 key = 'null'
             elif isinstance(key, (int, long)):
                 key = str(key)
-            elif _skipkeys:
+            elif self.skipkeys:
                 continue
             else:
                 raise TypeError("key " + repr(key) + " is not a string")
@@ -378,10 +448,10 @@
                 first = False
             else:
                 yield item_separator
-            yield _encoder(key)
-            yield _key_separator
+            yield '"' + self.encoder(key) + '"'
+            yield self.key_separator
             if isinstance(value, basestring):
-                yield _encoder(value)
+                yield '"' + self.encoder(value) + '"'
             elif value is None:
                 yield 'null'
             elif value is True:
@@ -391,26 +461,28 @@
             elif isinstance(value, (int, long)):
                 yield str(value)
             elif isinstance(value, float):
-                yield _floatstr(value)
+                yield self._floatstr(value)
             else:
                 if isinstance(value, (list, tuple)):
-                    chunks = _iterencode_list(value, _current_indent_level)
+                    chunks = self._iterencode_list(value, markers,
+                                                   _current_indent_level)
                 elif isinstance(value, dict):
-                    chunks = _iterencode_dict(value, _current_indent_level)
+                    chunks = self._iterencode_dict(value, markers,
+                                                   _current_indent_level)
                 else:
-                    chunks = _iterencode(value, _current_indent_level)
+                    chunks = self._iterencode(value, markers,
+                                              _current_indent_level)
                 for chunk in chunks:
                     yield chunk
         if newline_indent is not None:
             _current_indent_level -= 1
-            yield '\n' + (' ' * (_indent * _current_indent_level))
+            yield '\n' + (' ' * (self.indent * _current_indent_level))
         yield '}'
-        if markers is not None:
-            del markers[markerid]
+        self._remove_markers(markers, dct)
 
-    def _iterencode(o, _current_indent_level):
+    def _iterencode(self, o, markers, _current_indent_level):
         if isinstance(o, basestring):
-            yield _encoder(o)
+            yield '"' + self.encoder(o) + '"'
         elif o is None:
             yield 'null'
         elif o is True:
@@ -420,23 +492,19 @@
         elif isinstance(o, (int, long)):
             yield str(o)
         elif isinstance(o, float):
-            yield _floatstr(o)
+            yield self._floatstr(o)
         elif isinstance(o, (list, tuple)):
-            for chunk in _iterencode_list(o, _current_indent_level):
+            for chunk in self._iterencode_list(o, markers,
+                                               _current_indent_level):
                 yield chunk
         elif isinstance(o, dict):
-            for chunk in _iterencode_dict(o, _current_indent_level):
+            for chunk in self._iterencode_dict(o, markers,
+                                               _current_indent_level):
                 yield chunk
         else:
-            if markers is not None:
-                markerid = id(o)
-                if markerid in markers:
-                    raise ValueError("Circular reference detected")
-                markers[markerid] = o
-            o = _default(o)
-            for chunk in _iterencode(o, _current_indent_level):
+            self._mark_markers(markers, o)
+            obj = self.default(o)
+            for chunk in self._iterencode(obj, markers,
+                                          _current_indent_level):
                 yield chunk
-            if markers is not None:
-                del markers[markerid]
-
-    return _iterencode
+            self._remove_markers(markers, o)
diff --git a/lib-python/2.7/json/tests/test_unicode.py b/lib-python/2.7/json/tests/test_unicode.py
--- a/lib-python/2.7/json/tests/test_unicode.py
+++ b/lib-python/2.7/json/tests/test_unicode.py
@@ -80,6 +80,12 @@
         # Issue 10038.
         self.assertEqual(type(self.loads('"foo"')), unicode)
 
+    def test_encode_not_utf_8(self):
+        self.assertEqual(self.dumps('\xb1\xe6', encoding='iso8859-2'),
+                         '"\\u0105\\u0107"')
+        self.assertEqual(self.dumps(['\xb1\xe6'], encoding='iso8859-2'),
+                         '["\\u0105\\u0107"]')
+
 
 class TestPyUnicode(TestUnicode, PyTest): pass
 class TestCUnicode(TestUnicode, CTest): pass
diff --git a/lib-python/2.7/multiprocessing/forking.py b/lib-python/2.7/multiprocessing/forking.py
--- a/lib-python/2.7/multiprocessing/forking.py
+++ b/lib-python/2.7/multiprocessing/forking.py
@@ -73,15 +73,12 @@
         return getattr, (m.im_self, m.im_func.func_name)
 ForkingPickler.register(type(ForkingPickler.save), _reduce_method)
 
-def _reduce_method_descriptor(m):
-    return getattr, (m.__objclass__, m.__name__)
-ForkingPickler.register(type(list.append), _reduce_method_descriptor)
-ForkingPickler.register(type(int.__add__), _reduce_method_descriptor)
-
-#def _reduce_builtin_function_or_method(m):
-#    return getattr, (m.__self__, m.__name__)
-#ForkingPickler.register(type(list().append), _reduce_builtin_function_or_method)
-#ForkingPickler.register(type(int().__add__), _reduce_builtin_function_or_method)
+if type(list.append) is not type(ForkingPickler.save):
+    # Some python implementations have unbound methods even for builtin types
+    def _reduce_method_descriptor(m):
+        return getattr, (m.__objclass__, m.__name__)
+    ForkingPickler.register(type(list.append), _reduce_method_descriptor)
+    ForkingPickler.register(type(int.__add__), _reduce_method_descriptor)
 
 try:
     from functools import partial
diff --git a/lib-python/2.7/opcode.py b/lib-python/2.7/opcode.py
--- a/lib-python/2.7/opcode.py
+++ b/lib-python/2.7/opcode.py
@@ -1,4 +1,3 @@
-
 """
 opcode module - potentially shared between dis and other modules which
 operate on bytecodes (e.g. peephole optimizers).
@@ -189,4 +188,10 @@
 def_op('SET_ADD', 146)
 def_op('MAP_ADD', 147)
 
+# pypy modification, experimental bytecode
+def_op('LOOKUP_METHOD', 201)          # Index in name list
+hasname.append(201)
+def_op('CALL_METHOD', 202)            # #args not including 'self'
+def_op('BUILD_LIST_FROM_ARG', 203)
+
 del def_op, name_op, jrel_op, jabs_op
diff --git a/lib-python/2.7/pickle.py b/lib-python/2.7/pickle.py
--- a/lib-python/2.7/pickle.py
+++ b/lib-python/2.7/pickle.py
@@ -168,7 +168,7 @@
 
 # Pickling machinery
 
-class Pickler:
+class Pickler(object):
 
     def __init__(self, file, protocol=None):
         """This takes a file-like object for writing a pickle data stream.
@@ -638,6 +638,10 @@
             # else tmp is empty, and we're done
 
     def save_dict(self, obj):
+        modict_saver = self._pickle_moduledict(obj)
+        if modict_saver is not None:
+            return self.save_reduce(*modict_saver)
+
         write = self.write
 
         if self.bin:
@@ -687,6 +691,29 @@
                 write(SETITEM)
             # else tmp is empty, and we're done
 
+    def _pickle_moduledict(self, obj):
+        # save module dictionary as "getattr(module, '__dict__')"
+
+        # build index of module dictionaries
+        try:
+            modict = self.module_dict_ids
+        except AttributeError:
+            modict = {}
+            from sys import modules
+            for mod in modules.values():
+                if isinstance(mod, ModuleType):
+                    modict[id(mod.__dict__)] = mod
+            self.module_dict_ids = modict
+
+        thisid = id(obj)
+        try:
+            themodule = modict[thisid]
+        except KeyError:
+            return None
+        from __builtin__ import getattr
+        return getattr, (themodule, '__dict__')
+
+
     def save_inst(self, obj):
         cls = obj.__class__
 
@@ -727,6 +754,29 @@
 
     dispatch[InstanceType] = save_inst
 
+    def save_function(self, obj):
+        try:
+            return self.save_global(obj)
+        except PicklingError, e:
+            pass
+        # Check copy_reg.dispatch_table
+        reduce = dispatch_table.get(type(obj))
+        if reduce:
+            rv = reduce(obj)
+        else:
+            # Check for a __reduce_ex__ method, fall back to __reduce__
+            reduce = getattr(obj, "__reduce_ex__", None)
+            if reduce:
+                rv = reduce(self.proto)
+            else:
+                reduce = getattr(obj, "__reduce__", None)
+                if reduce:
+                    rv = reduce()
+                else:
+                    raise e
+        return self.save_reduce(obj=obj, *rv)
+    dispatch[FunctionType] = save_function
+
     def save_global(self, obj, name=None, pack=struct.pack):
         write = self.write
         memo = self.memo
@@ -768,7 +818,6 @@
         self.memoize(obj)
 
     dispatch[ClassType] = save_global
-    dispatch[FunctionType] = save_global
     dispatch[BuiltinFunctionType] = save_global
     dispatch[TypeType] = save_global
 
@@ -824,7 +873,7 @@
 
 # Unpickling machinery
 
-class Unpickler:
+class Unpickler(object):
 
     def __init__(self, file):
         """This takes a file-like object for reading a pickle data stream.
diff --git a/lib-python/2.7/pkgutil.py b/lib-python/2.7/pkgutil.py
--- a/lib-python/2.7/pkgutil.py
+++ b/lib-python/2.7/pkgutil.py
@@ -244,7 +244,8 @@
         return mod
 
     def get_data(self, pathname):
-        return open(pathname, "rb").read()
+        with open(pathname, "rb") as f:
+            return f.read()
 
     def _reopen(self):
         if self.file and self.file.closed:
diff --git a/lib-python/2.7/pprint.py b/lib-python/2.7/pprint.py
--- a/lib-python/2.7/pprint.py
+++ b/lib-python/2.7/pprint.py
@@ -144,7 +144,7 @@
             return
 
         r = getattr(typ, "__repr__", None)
-        if issubclass(typ, dict) and r is dict.__repr__:
+        if issubclass(typ, dict) and r == dict.__repr__:
             write('{')
             if self._indent_per_level > 1:
                 write((self._indent_per_level - 1) * ' ')
@@ -173,10 +173,10 @@
             write('}')
             return
 
-        if ((issubclass(typ, list) and r is list.__repr__) or
-            (issubclass(typ, tuple) and r is tuple.__repr__) or
-            (issubclass(typ, set) and r is set.__repr__) or
-            (issubclass(typ, frozenset) and r is frozenset.__repr__)
+        if ((issubclass(typ, list) and r == list.__repr__) or
+            (issubclass(typ, tuple) and r == tuple.__repr__) or
+            (issubclass(typ, set) and r == set.__repr__) or
+            (issubclass(typ, frozenset) and r == frozenset.__repr__)
            ):
             length = _len(object)
             if issubclass(typ, list):
@@ -266,7 +266,7 @@
         return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
 
     r = getattr(typ, "__repr__", None)
-    if issubclass(typ, dict) and r is dict.__repr__:
+    if issubclass(typ, dict) and r == dict.__repr__:
         if not object:
             return "{}", True, False
         objid = _id(object)
@@ -291,8 +291,8 @@
         del context[objid]
         return "{%s}" % _commajoin(components), readable, recursive
 
-    if (issubclass(typ, list) and r is list.__repr__) or \
-       (issubclass(typ, tuple) and r is tuple.__repr__):
+    if (issubclass(typ, list) and r == list.__repr__) or \
+       (issubclass(typ, tuple) and r == tuple.__repr__):
         if issubclass(typ, list):
             if not object:
                 return "[]", True, False
diff --git a/lib-python/2.7/pydoc.py b/lib-python/2.7/pydoc.py
--- a/lib-python/2.7/pydoc.py
+++ b/lib-python/2.7/pydoc.py
@@ -623,7 +623,9 @@
             head, '#ffffff', '#7799ee',
             '<a href=".">index</a><br>' + filelink + docloc)
 
-        modules = inspect.getmembers(object, inspect.ismodule)
+        def isnonbuiltinmodule(obj):
+            return inspect.ismodule(obj) and obj is not __builtin__
+        modules = inspect.getmembers(object, isnonbuiltinmodule)
 
         classes, cdict = [], {}
         for key, value in inspect.getmembers(object, inspect.isclass):
diff --git a/lib-python/2.7/random.py b/lib-python/2.7/random.py
--- a/lib-python/2.7/random.py
+++ b/lib-python/2.7/random.py
@@ -41,7 +41,6 @@
 
 from __future__ import division
 from warnings import warn as _warn
-from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
 from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
 from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
 from os import urandom as _urandom
@@ -240,8 +239,7 @@
 
         return self.randrange(a, b+1)
 
-    def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF,
-                   _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
+    def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF):
         """Return a random int in the range [0,n)
 
         Handles the case where n has more bits than returned
@@ -256,7 +254,8 @@
             # Only call self.getrandbits if the original random() builtin method
             # has not been overridden or if a new getrandbits() was supplied.
             # This assures that the two methods correspond.
-            if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
+            if (self.random == super(Random, self).random or
+                getrandbits != super(Random, self).getrandbits):
                 k = int(1.00001 + _log(n-1, 2.0))   # 2**k > n-1 > 2**(k-2)
                 r = getrandbits(k)
                 while r >= n:
diff --git a/lib-python/2.7/site.py b/lib-python/2.7/site.py
--- a/lib-python/2.7/site.py
+++ b/lib-python/2.7/site.py
@@ -75,7 +75,6 @@
 USER_SITE = None
 USER_BASE = None
 
-
 def makepath(*paths):
     dir = os.path.join(*paths)
     try:
@@ -91,7 +90,10 @@
         if hasattr(m, '__loader__'):
             continue   # don't mess with a PEP 302-supplied __file__
         try:
-            m.__file__ = os.path.abspath(m.__file__)
+            prev = m.__file__
+            new = os.path.abspath(m.__file__)
+            if prev != new:
+                m.__file__ = new
         except (AttributeError, OSError):
             pass
 
@@ -289,6 +291,7 @@
     will find its `site-packages` subdirectory depending on the system
     environment, and will return a list of full paths.
     """
+    is_pypy = '__pypy__' in sys.builtin_module_names
     sitepackages = []
     seen = set()
 
@@ -299,6 +302,10 @@
 
         if sys.platform in ('os2emx', 'riscos'):
             sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
+        elif is_pypy:
+            from distutils.sysconfig import get_python_lib
+            sitedir = get_python_lib(standard_lib=False, prefix=prefix)
+            sitepackages.append(sitedir)
         elif os.sep == '/':
             sitepackages.append(os.path.join(prefix, "lib",
                                         "python" + sys.version[:3],
@@ -435,22 +442,33 @@
                 if key == 'q':
                     break
 
+##def setcopyright():
+##    """Set 'copyright' and 'credits' in __builtin__"""
+##    __builtin__.copyright = _Printer("copyright", sys.copyright)
+##    if sys.platform[:4] == 'java':
+##        __builtin__.credits = _Printer(
+##            "credits",
+##            "Jython is maintained by the Jython developers (www.jython.org).")
+##    else:
+##        __builtin__.credits = _Printer("credits", """\
+##    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
+##    for supporting Python development.  See www.python.org for more information.""")
+##    here = os.path.dirname(os.__file__)
+##    __builtin__.license = _Printer(
+##        "license", "See http://www.python.org/%.3s/license.html" % sys.version,
+##        ["LICENSE.txt", "LICENSE"],
+##        [os.path.join(here, os.pardir), here, os.curdir])
+
 def setcopyright():
-    """Set 'copyright' and 'credits' in __builtin__"""
+    # XXX this is the PyPy-specific version.  Should be unified with the above.
     __builtin__.copyright = _Printer("copyright", sys.copyright)
-    if sys.platform[:4] == 'java':
-        __builtin__.credits = _Printer(
-            "credits",
-            "Jython is maintained by the Jython developers (www.jython.org).")
-    else:
-        __builtin__.credits = _Printer("credits", """\
-    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
-    for supporting Python development.  See www.python.org for more information.""")
-    here = os.path.dirname(os.__file__)
+    __builtin__.credits = _Printer(
+        "credits",
+        "PyPy is maintained by the PyPy developers: http://pypy.org/")
     __builtin__.license = _Printer(
-        "license", "See http://www.python.org/%.3s/license.html" % sys.version,
-        ["LICENSE.txt", "LICENSE"],
-        [os.path.join(here, os.pardir), here, os.curdir])
+        "license",
+        "See https://bitbucket.org/pypy/pypy/src/default/LICENSE")
+
 
 
 class _Helper(object):
@@ -476,7 +494,7 @@
     if sys.platform == 'win32':
         import locale, codecs
         enc = locale.getdefaultlocale()[1]
-        if enc.startswith('cp'):            # "cp***" ?
+        if enc is not None and enc.startswith('cp'):            # "cp***" ?
             try:
                 codecs.lookup(enc)
             except LookupError:
@@ -532,9 +550,18 @@
                 "'import usercustomize' failed; use -v for traceback"
 
 
+def import_builtin_stuff():
+    """PyPy specific: pre-import a few built-in modules, because
+    some programs actually rely on them to be in sys.modules :-("""
+    import exceptions
+    if 'zipimport' in sys.builtin_module_names:
+        import zipimport
+
+
 def main():
     global ENABLE_USER_SITE
 
+    import_builtin_stuff()
     abs__file__()
     known_paths = removeduppaths()
     if (os.name == "posix" and sys.path and
diff --git a/lib-python/2.7/socket.py b/lib-python/2.7/socket.py
--- a/lib-python/2.7/socket.py
+++ b/lib-python/2.7/socket.py
@@ -46,8 +46,6 @@
 
 import _socket
 from _socket import *
-from functools import partial
-from types import MethodType
 
 try:
     import _ssl
@@ -159,11 +157,6 @@
 if sys.platform == "riscos":
     _socketmethods = _socketmethods + ('sleeptaskw',)
 
-# All the method names that must be delegated to either the real socket
-# object or the _closedsocket object.
-_delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into",
-                     "send", "sendto")
-
 class _closedsocket(object):
     __slots__ = []
     def _dummy(*args):
@@ -180,22 +173,43 @@
 
     __doc__ = _realsocket.__doc__
 
-    __slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
-
     def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
         if _sock is None:
             _sock = _realsocket(family, type, proto)
         self._sock = _sock
-        for method in _delegate_methods:
-            setattr(self, method, getattr(_sock, method))
+        self._io_refs = 0
+        self._closed = False
 
-    def close(self, _closedsocket=_closedsocket,
-              _delegate_methods=_delegate_methods, setattr=setattr):
+    def send(self, data, flags=0):
+        return self._sock.send(data, flags=flags)
+    send.__doc__ = _realsocket.send.__doc__
+
+    def recv(self, buffersize, flags=0):
+        return self._sock.recv(buffersize, flags=flags)
+    recv.__doc__ = _realsocket.recv.__doc__
+
+    def recv_into(self, buffer, nbytes=0, flags=0):
+        return self._sock.recv_into(buffer, nbytes=nbytes, flags=flags)
+    recv_into.__doc__ = _realsocket.recv_into.__doc__
+
+    def recvfrom(self, buffersize, flags=0):
+        return self._sock.recvfrom(buffersize, flags=flags)
+    recvfrom.__doc__ = _realsocket.recvfrom.__doc__
+
+    def recvfrom_into(self, buffer, nbytes=0, flags=0):
+        return self._sock.recvfrom_into(buffer, nbytes=nbytes, flags=flags)
+    recvfrom_into.__doc__ = _realsocket.recvfrom_into.__doc__
+
+    def sendto(self, data, param2, param3=None):
+        if param3 is None:
+            return self._sock.sendto(data, param2)
+        else:
+            return self._sock.sendto(data, param2, param3)
+    sendto.__doc__ = _realsocket.sendto.__doc__
+
+    def close(self):
         # This function should not reference any globals. See issue #808164.
         self._sock = _closedsocket()
-        dummy = self._sock._dummy
-        for method in _delegate_methods:
-            setattr(self, method, dummy)
     close.__doc__ = _realsocket.close.__doc__
 
     def accept(self):
@@ -214,21 +228,49 @@
 
         Return a regular file object corresponding to the socket.  The mode
         and bufsize arguments are as for the built-in open() function."""
-        return _fileobject(self._sock, mode, bufsize)
+        self._io_refs += 1
+        return _fileobject(self, mode, bufsize)
+
+    def _decref_socketios(self):
+        if self._io_refs > 0:
+            self._io_refs -= 1
+        if self._closed:
+            self.close()
+
+    def _real_close(self):
+        # This function should not reference any globals. See issue #808164.
+        self._sock.close()
+
+    def close(self):
+        # This function should not reference any globals. See issue #808164.
+        self._closed = True
+        if self._io_refs <= 0:
+            self._real_close()
 
     family = property(lambda self: self._sock.family, doc="the socket family")
     type = property(lambda self: self._sock.type, doc="the socket type")
     proto = property(lambda self: self._sock.proto, doc="the socket protocol")
 
-def meth(name,self,*args):
-    return getattr(self._sock,name)(*args)
+    # Delegate many calls to the raw socket object.
+    _s = ("def %(name)s(self, %(args)s): return self._sock.%(name)s(%(args)s)\n\n"
+          "%(name)s.__doc__ = _realsocket.%(name)s.__doc__\n")
+    for _m in _socketmethods:
+        # yupi! we're on pypy, all code objects have this interface
+        argcount = getattr(_realsocket, _m).im_func.func_code.co_argcount - 1
+        exec _s % {'name': _m, 'args': ', '.join('arg%d' % i for i in range(argcount))}
+    del _m, _s, argcount
 
-for _m in _socketmethods:
-    p = partial(meth,_m)
-    p.__name__ = _m
-    p.__doc__ = getattr(_realsocket,_m).__doc__
-    m = MethodType(p,None,_socketobject)
-    setattr(_socketobject,_m,m)
+    # Delegation methods with default arguments, that the code above
+    # cannot handle correctly
+    def sendall(self, data, flags=0):
+        self._sock.sendall(data, flags)
+    sendall.__doc__ = _realsocket.sendall.__doc__
+
+    def getsockopt(self, level, optname, buflen=None):
+        if buflen is None:
+            return self._sock.getsockopt(level, optname)
+        return self._sock.getsockopt(level, optname, buflen)
+    getsockopt.__doc__ = _realsocket.getsockopt.__doc__
 
 socket = SocketType = _socketobject
 
@@ -278,8 +320,11 @@
             if self._sock:
                 self.flush()
         finally:
-            if self._close:
-                self._sock.close()
+            if self._sock:
+                if self._close:
+                    self._sock.close()
+                else:
+                    self._sock._decref_socketios()
             self._sock = None
 
     def __del__(self):
diff --git a/lib-python/2.7/sqlite3/test/dbapi.py b/lib-python/2.7/sqlite3/test/dbapi.py
--- a/lib-python/2.7/sqlite3/test/dbapi.py
+++ b/lib-python/2.7/sqlite3/test/dbapi.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
 # pysqlite2/test/dbapi.py: tests for DB-API compliance
 #
 # Copyright (C) 2004-2010 Gerhard H&#65533;ring <gh at ghaering.de>
@@ -332,6 +332,9 @@
             def __init__(self):
                 self.value = 5
 
+            def __iter__(self):
+                return self
+
             def next(self):
                 if self.value == 10:
                     raise StopIteration
@@ -826,7 +829,7 @@
         con = sqlite.connect(":memory:")
         con.close()
         try:
-            con()
+            con("select 1")
             self.fail("Should have raised a ProgrammingError")
         except sqlite.ProgrammingError:
             pass
diff --git a/lib-python/2.7/sqlite3/test/regression.py b/lib-python/2.7/sqlite3/test/regression.py
--- a/lib-python/2.7/sqlite3/test/regression.py
+++ b/lib-python/2.7/sqlite3/test/regression.py
@@ -264,6 +264,28 @@
         """
         self.assertRaises(sqlite.Warning, self.con, 1)
 
+    def CheckUpdateDescriptionNone(self):
+        """
+        Call Cursor.update with an UPDATE query and check that it sets the
+        cursor's description to be None.
+        """
+        cur = self.con.cursor()
+        cur.execute("CREATE TABLE foo (id INTEGER)")
+        cur.execute("UPDATE foo SET id = 3 WHERE id = 1")
+        self.assertEqual(cur.description, None)
+
+    def CheckStatementCache(self):
+        cur = self.con.cursor()
+        cur.execute("CREATE TABLE foo (id INTEGER)")
+        values = [(i,) for i in xrange(5)]
+        cur.executemany("INSERT INTO foo (id) VALUES (?)", values)
+
+        cur.execute("SELECT id FROM foo")
+        self.assertEqual(list(cur), values)
+        self.con.commit()
+        cur.execute("SELECT id FROM foo")
+        self.assertEqual(list(cur), values)
+
 def suite():
     regression_suite = unittest.makeSuite(RegressionTests, "Check")
     return unittest.TestSuite((regression_suite,))
diff --git a/lib-python/2.7/sqlite3/test/userfunctions.py b/lib-python/2.7/sqlite3/test/userfunctions.py
--- a/lib-python/2.7/sqlite3/test/userfunctions.py
+++ b/lib-python/2.7/sqlite3/test/userfunctions.py
@@ -275,12 +275,14 @@
             pass
 
     def CheckAggrNoStep(self):
+        # XXX it's better to raise OperationalError in order to stop
+        # the query earlier.
         cur = self.con.cursor()
         try:
             cur.execute("select nostep(t) from test")
-            self.fail("should have raised an AttributeError")
-        except AttributeError, e:
-            self.assertEqual(e.args[0], "AggrNoStep instance has no attribute 'step'")
+            self.fail("should have raised an OperationalError")
+        except sqlite.OperationalError, e:
+            self.assertEqual(e.args[0], "user-defined aggregate's 'step' method raised error")
 
     def CheckAggrNoFinalize(self):
         cur = self.con.cursor()
diff --git a/lib-python/2.7/ssl.py b/lib-python/2.7/ssl.py
--- a/lib-python/2.7/ssl.py
+++ b/lib-python/2.7/ssl.py
@@ -86,7 +86,7 @@
 else:
     _PROTOCOL_NAMES[PROTOCOL_SSLv2] = "SSLv2"
 
-from socket import socket, _fileobject, _delegate_methods, error as socket_error
+from socket import socket, _fileobject, error as socket_error
 from socket import getnameinfo as _getnameinfo
 import base64        # for DER-to-PEM translation
 import errno
@@ -103,14 +103,6 @@
                  do_handshake_on_connect=True,
                  suppress_ragged_eofs=True, ciphers=None):
         socket.__init__(self, _sock=sock._sock)
-        # The initializer for socket overrides the methods send(), recv(), etc.
-        # in the instancce, which we don't need -- but we want to provide the
-        # methods defined in SSLSocket.
-        for attr in _delegate_methods:
-            try:
-                delattr(self, attr)
-            except AttributeError:
-                pass
 
         if certfile and not keyfile:
             keyfile = certfile
diff --git a/lib-python/2.7/subprocess.py b/lib-python/2.7/subprocess.py
--- a/lib-python/2.7/subprocess.py
+++ b/lib-python/2.7/subprocess.py
@@ -803,7 +803,7 @@
             elif stderr == PIPE:
                 errread, errwrite = _subprocess.CreatePipe(None, 0)
             elif stderr == STDOUT:
-                errwrite = c2pwrite
+                errwrite = c2pwrite.handle # pass id to not close it
             elif isinstance(stderr, int):
                 errwrite = msvcrt.get_osfhandle(stderr)
             else:
@@ -818,9 +818,13 @@
 
         def _make_inheritable(self, handle):
             """Return a duplicate of handle, which is inheritable"""
-            return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
+            dupl = _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
                                 handle, _subprocess.GetCurrentProcess(), 0, 1,
                                 _subprocess.DUPLICATE_SAME_ACCESS)
+            # If the initial handle was obtained with CreatePipe, close it.
+            if not isinstance(handle, int):
+                handle.Close()
+            return dupl
 
 
         def _find_w9xpopen(self):
diff --git a/lib-python/2.7/sysconfig.py b/lib-python/2.7/sysconfig.py
--- a/lib-python/2.7/sysconfig.py
+++ b/lib-python/2.7/sysconfig.py
@@ -26,6 +26,16 @@
         'scripts': '{base}/bin',
         'data'   : '{base}',
         },
+    'pypy': {
+        'stdlib': '{base}/lib-python',
+        'platstdlib': '{base}/lib-python',
+        'purelib': '{base}/lib-python',
+        'platlib': '{base}/lib-python',
+        'include': '{base}/include',
+        'platinclude': '{base}/include',
+        'scripts': '{base}/bin',
+        'data'   : '{base}',
+        },
     'nt': {
         'stdlib': '{base}/Lib',
         'platstdlib': '{base}/Lib',
@@ -158,7 +168,9 @@
     return res
 
 def _get_default_scheme():
-    if os.name == 'posix':
+    if '__pypy__' in sys.builtin_module_names:
+        return 'pypy'
+    elif os.name == 'posix':
         # the default scheme for posix is posix_prefix
         return 'posix_prefix'
     return os.name
@@ -182,126 +194,9 @@
     return env_base if env_base else joinuser("~", ".local")
 
 
-def _parse_makefile(filename, vars=None):
-    """Parse a Makefile-style file.
-
-    A dictionary containing name/value pairs is returned.  If an
-    optional dictionary is passed in as the second argument, it is
-    used instead of a new dictionary.
-    """
-    import re
-    # Regexes needed for parsing Makefile (and similar syntaxes,
-    # like old-style Setup files).
-    _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
-    _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
-    _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
-
-    if vars is None:
-        vars = {}
-    done = {}
-    notdone = {}
-
-    with open(filename) as f:
-        lines = f.readlines()
-
-    for line in lines:
-        if line.startswith('#') or line.strip() == '':
-            continue
-        m = _variable_rx.match(line)
-        if m:
-            n, v = m.group(1, 2)
-            v = v.strip()
-            # `$$' is a literal `$' in make
-            tmpv = v.replace('$$', '')
-
-            if "$" in tmpv:
-                notdone[n] = v
-            else:
-                try:
-                    v = int(v)
-                except ValueError:
-                    # insert literal `$'
-                    done[n] = v.replace('$$', '$')
-                else:
-                    done[n] = v
-
-    # do variable interpolation here
-    while notdone:
-        for name in notdone.keys():
-            value = notdone[name]
-            m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
-            if m:
-                n = m.group(1)
-                found = True
-                if n in done:
-                    item = str(done[n])
-                elif n in notdone:
-                    # get it on a subsequent round
-                    found = False
-                elif n in os.environ:
-                    # do it like make: fall back to environment
-                    item = os.environ[n]
-                else:
-                    done[n] = item = ""
-                if found:
-                    after = value[m.end():]
-                    value = value[:m.start()] + item + after
-                    if "$" in after:
-                        notdone[name] = value
-                    else:
-                        try: value = int(value)
-                        except ValueError:
-                            done[name] = value.strip()
-                        else:
-                            done[name] = value
-                        del notdone[name]
-            else:
-                # bogus variable reference; just drop it since we can't deal
-                del notdone[name]
-    # strip spurious spaces
-    for k, v in done.items():
-        if isinstance(v, str):
-            done[k] = v.strip()
-
-    # save the results in the global dictionary
-    vars.update(done)
-    return vars
-
-
-def _get_makefile_filename():
-    if _PYTHON_BUILD:
-        return os.path.join(_PROJECT_BASE, "Makefile")
-    return os.path.join(get_path('platstdlib'), "config", "Makefile")
-
-
 def _init_posix(vars):
     """Initialize the module as appropriate for POSIX systems."""
-    # load the installed Makefile:
-    makefile = _get_makefile_filename()
-    try:
-        _parse_makefile(makefile, vars)
-    except IOError, e:
-        msg = "invalid Python installation: unable to open %s" % makefile
-        if hasattr(e, "strerror"):
-            msg = msg + " (%s)" % e.strerror
-        raise IOError(msg)
-
-    # load the installed pyconfig.h:
-    config_h = get_config_h_filename()
-    try:
-        with open(config_h) as f:
-            parse_config_h(f, vars)
-    except IOError, e:
-        msg = "invalid Python installation: unable to open %s" % config_h
-        if hasattr(e, "strerror"):
-            msg = msg + " (%s)" % e.strerror
-        raise IOError(msg)
-
-    # On AIX, there are wrong paths to the linker scripts in the Makefile
-    # -- these paths are relative to the Python source, but when installed
-    # the scripts are in another directory.
-    if _PYTHON_BUILD:
-        vars['LDSHARED'] = vars['BLDSHARED']
+    return
 
 def _init_non_posix(vars):
     """Initialize the module as appropriate for NT"""
@@ -474,10 +369,11 @@
                         # patched up as well.
                         'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
 
-                        flags = _CONFIG_VARS[key]
-                        flags = re.sub('-arch\s+\w+\s', ' ', flags)
-                        flags = flags + ' ' + arch
-                        _CONFIG_VARS[key] = flags
+                        if key in _CONFIG_VARS: 
+                            flags = _CONFIG_VARS[key]
+                            flags = re.sub('-arch\s+\w+\s', ' ', flags)
+                            flags = flags + ' ' + arch
+                            _CONFIG_VARS[key] = flags
 
                 # If we're on OSX 10.5 or later and the user tries to
                 # compiles an extension using an SDK that is not present
diff --git a/lib-python/2.7/tarfile.py b/lib-python/2.7/tarfile.py
--- a/lib-python/2.7/tarfile.py
+++ b/lib-python/2.7/tarfile.py
@@ -1716,9 +1716,6 @@
         except (ImportError, AttributeError):
             raise CompressionError("gzip module is not available")
 
-        if fileobj is None:
-            fileobj = bltn_open(name, mode + "b")
-
         try:
             t = cls.taropen(name, mode,
                 gzip.GzipFile(name, mode, compresslevel, fileobj),
diff --git a/lib-python/2.7/test/__init__.py b/lib-python/2.7/test/__init__.py
--- a/lib-python/2.7/test/__init__.py
+++ b/lib-python/2.7/test/__init__.py
@@ -1,1 +1,11 @@
-# Dummy file to make this directory a package.
+"""
+This package only contains the tests that we have modified for PyPy.
+It uses the 'official' hack to include the rest of the standard
+'test' package from CPython.
+
+This assumes that sys.path is configured to contain
+'lib-python/modified-2.7.0' before 'lib-python/2.7.0'.
+"""
+
+from pkgutil import extend_path
+__path__ = extend_path(__path__, __name__)
diff --git a/lib-python/2.7/test/list_tests.py b/lib-python/2.7/test/list_tests.py
--- a/lib-python/2.7/test/list_tests.py
+++ b/lib-python/2.7/test/list_tests.py
@@ -45,8 +45,12 @@
         self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
         self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")
 
+        if test_support.check_impl_detail():
+            depth = sys.getrecursionlimit() + 100
+        else:
+            depth = 1000 * 1000 # should be enough to exhaust the stack
         l0 = []
-        for i in xrange(sys.getrecursionlimit() + 100):
+        for i in xrange(depth):
             l0 = [l0]
         self.assertRaises(RuntimeError, repr, l0)
 
@@ -472,7 +476,11 @@
         u += "eggs"
         self.assertEqual(u, self.type2test("spameggs"))
 
-        self.assertRaises(TypeError, u.__iadd__, None)
+        def f_iadd(u, x):
+            u += x
+            return u
+
+        self.assertRaises(TypeError, f_iadd, u, None)
 
     def test_imul(self):
         u = self.type2test([0, 1])
diff --git a/lib-python/2.7/test/mapping_tests.py b/lib-python/2.7/test/mapping_tests.py
--- a/lib-python/2.7/test/mapping_tests.py
+++ b/lib-python/2.7/test/mapping_tests.py
@@ -531,7 +531,10 @@
                     self.assertEqual(va, int(ka))
                     kb, vb = tb = b.popitem()
                     self.assertEqual(vb, int(kb))
-                    self.assertTrue(not(copymode < 0 and ta != tb))
+                    if copymode < 0 and test_support.check_impl_detail():
+                        # popitem() is not guaranteed to be deterministic on
+                        # all implementations
+                        self.assertEqual(ta, tb)
                 self.assertTrue(not a)
                 self.assertTrue(not b)
 
diff --git a/lib-python/2.7/test/pickletester.py b/lib-python/2.7/test/pickletester.py
--- a/lib-python/2.7/test/pickletester.py
+++ b/lib-python/2.7/test/pickletester.py
@@ -6,7 +6,7 @@
 import pickletools
 import copy_reg
 
-from test.test_support import TestFailed, have_unicode, TESTFN
+from test.test_support import TestFailed, have_unicode, TESTFN, impl_detail
 
 # Tests that try a number of pickle protocols should have a
 #     for proto in protocols:
@@ -949,6 +949,7 @@
                              "Failed protocol %d: %r != %r"
                              % (proto, obj, loaded))
 
+    @impl_detail("pypy does not store attribute names", pypy=False)
     def test_attribute_name_interning(self):
         # Test that attribute names of pickled objects are interned when
         # unpickling.
@@ -1091,6 +1092,7 @@
         s = StringIO.StringIO("X''.")
         self.assertRaises(EOFError, self.module.load, s)
 
+    @impl_detail("no full restricted mode in pypy", pypy=False)
     def test_restricted(self):
         # issue7128: cPickle failed in restricted mode
         builtins = {self.module.__name__: self.module,
diff --git a/lib-python/2.7/test/regrtest.py b/lib-python/2.7/test/regrtest.py
--- a/lib-python/2.7/test/regrtest.py
+++ b/lib-python/2.7/test/regrtest.py
@@ -680,8 +680,13 @@
 
 def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
     """Return a list of all applicable test modules."""
-    testdir = findtestdir(testdir)
-    names = os.listdir(testdir)
+    if testdir:
+        testdirs = [testdir]
+    else:
+        testdirs = findtestdirs()
+    names = {}
+    for testdir in testdirs:
+        names.update(dict.fromkeys(os.listdir(testdir)))
     tests = []
     others = set(stdtests) | nottests
     for name in names:
@@ -1080,8 +1085,19 @@
     # Collect cyclic trash.
     gc.collect()
 
-def findtestdir(path=None):
-    return path or os.path.dirname(__file__) or os.curdir
+def findtestdirs():
+    # XXX hacking: returns a list of both the '2.7.0/test' and the
+    # 'modified-2.7.0/test' directories, as full paths.
+    testdir = os.path.abspath(os.path.dirname(__file__) or os.curdir)
+    assert os.path.basename(testdir).lower() == 'test'
+    maindir = os.path.dirname(testdir)
+    libpythondir = os.path.dirname(maindir)
+    maindirname = os.path.basename(maindir).lower()
+    if maindirname.startswith('modified-'):
+        maindirname = maindirname[len('modified-'):]
+    testdir1 = os.path.join(libpythondir, maindirname, 'test')
+    testdir2 = os.path.join(libpythondir, 'modified-'+maindirname, 'test')
+    return [testdir1, testdir2]
 
 def removepy(names):
     if not names:
@@ -1388,7 +1404,26 @@
         test_zipimport
         test_zlib
         """,
-    'openbsd3':
+    'openbsd4':
+        """
+        test_ascii_formatd
+        test_bsddb
+        test_bsddb3
+        test_ctypes
+        test_dl
+        test_epoll
+        test_gdbm
+        test_locale
+        test_normalization
+        test_ossaudiodev
+        test_pep277
+        test_tcl
+        test_tk
+        test_ttk_guionly
+        test_ttk_textonly
+        test_multiprocessing
+        """,
+    'openbsd5':
         """
         test_ascii_formatd
         test_bsddb
@@ -1503,13 +1538,7 @@
         return self.expected
 
 if __name__ == '__main__':
-    # findtestdir() gets the dirname out of __file__, so we have to make it
-    # absolute before changing the working directory.
-    # For example __file__ may be relative when running trace or profile.
-    # See issue #9323.
-    __file__ = os.path.abspath(__file__)
-
-    # sanity check
+    # Simplification for findtestdir().
     assert __file__ == os.path.abspath(sys.argv[0])
 
     # When tests are run from the Python build directory, it is best practice
diff --git a/lib-python/2.7/test/seq_tests.py b/lib-python/2.7/test/seq_tests.py
--- a/lib-python/2.7/test/seq_tests.py
+++ b/lib-python/2.7/test/seq_tests.py
@@ -307,12 +307,18 @@
 
     def test_bigrepeat(self):
         import sys
-        if sys.maxint <= 2147483647:
-            x = self.type2test([0])
-            x *= 2**16
-            self.assertRaises(MemoryError, x.__mul__, 2**16)
-            if hasattr(x, '__imul__'):
-                self.assertRaises(MemoryError, x.__imul__, 2**16)
+        # we chose an N such as 2**16 * N does not fit into a cpu word
+        if sys.maxint == 2147483647:
+            # 32 bit system
+            N = 2**16
+        else:
+            # 64 bit system
+            N = 2**48
+        x = self.type2test([0])
+        x *= 2**16
+        self.assertRaises(MemoryError, x.__mul__, N)
+        if hasattr(x, '__imul__'):
+            self.assertRaises(MemoryError, x.__imul__, N)
 
     def test_subscript(self):
         a = self.type2test([10, 11])
diff --git a/lib-python/2.7/test/string_tests.py b/lib-python/2.7/test/string_tests.py
--- a/lib-python/2.7/test/string_tests.py
+++ b/lib-python/2.7/test/string_tests.py
@@ -1024,7 +1024,10 @@
         self.checkequal('abc', 'abc', '__mul__', 1)
         self.checkequal('abcabcabc', 'abc', '__mul__', 3)
         self.checkraises(TypeError, 'abc', '__mul__')
-        self.checkraises(TypeError, 'abc', '__mul__', '')
+        class Mul(object):
+            def mul(self, a, b):
+                return a * b
+        self.checkraises(TypeError, Mul(), 'mul', 'abc', '')
         # XXX: on a 64-bit system, this doesn't raise an overflow error,
         # but either raises a MemoryError, or succeeds (if you have 54TiB)
         #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
diff --git a/lib-python/2.7/test/test_abstract_numbers.py b/lib-python/2.7/test/test_abstract_numbers.py
--- a/lib-python/2.7/test/test_abstract_numbers.py
+++ b/lib-python/2.7/test/test_abstract_numbers.py
@@ -40,7 +40,8 @@
 
         c1, c2 = complex(3, 2), complex(4,1)
         # XXX: This is not ideal, but see the comment in math_trunc().
-        self.assertRaises(AttributeError, math.trunc, c1)
+        # Modified to suit PyPy, which gives TypeError in all cases
+        self.assertRaises((AttributeError, TypeError), math.trunc, c1)
         self.assertRaises(TypeError, float, c1)
         self.assertRaises(TypeError, int, c1)
 
diff --git a/lib-python/2.7/test/test_aifc.py b/lib-python/2.7/test/test_aifc.py
--- a/lib-python/2.7/test/test_aifc.py
+++ b/lib-python/2.7/test/test_aifc.py
@@ -1,4 +1,4 @@
-from test.test_support import findfile, run_unittest, TESTFN
+from test.test_support import findfile, run_unittest, TESTFN, impl_detail
 import unittest
 import os
 
@@ -68,6 +68,7 @@
         self.assertEqual(f.getparams(), fout.getparams())
         self.assertEqual(f.readframes(5), fout.readframes(5))
 
+    @impl_detail("PyPy has no audioop module yet", pypy=False)
     def test_compress(self):
         f = self.f = aifc.open(self.sndfilepath)
         fout = self.fout = aifc.open(TESTFN, 'wb')
diff --git a/lib-python/2.7/test/test_array.py b/lib-python/2.7/test/test_array.py
--- a/lib-python/2.7/test/test_array.py
+++ b/lib-python/2.7/test/test_array.py
@@ -295,9 +295,10 @@
         )
 
         b = array.array(self.badtypecode())
-        self.assertRaises(TypeError, a.__add__, b)
-
-        self.assertRaises(TypeError, a.__add__, "bad")
+        with self.assertRaises(TypeError):
+            a + b
+        with self.assertRaises(TypeError):
+            a + 'bad'
 
     def test_iadd(self):
         a = array.array(self.typecode, self.example[::-1])
@@ -316,9 +317,10 @@
         )
 
         b = array.array(self.badtypecode())
-        self.assertRaises(TypeError, a.__add__, b)
-
-        self.assertRaises(TypeError, a.__iadd__, "bad")
+        with self.assertRaises(TypeError):
+            a += b
+        with self.assertRaises(TypeError):
+            a += 'bad'
 
     def test_mul(self):
         a = 5*array.array(self.typecode, self.example)
@@ -345,7 +347,8 @@
             array.array(self.typecode)
         )
 
-        self.assertRaises(TypeError, a.__mul__, "bad")
+        with self.assertRaises(TypeError):
+            a * 'bad'
 
     def test_imul(self):
         a = array.array(self.typecode, self.example)
@@ -374,7 +377,8 @@
         a *= -1
         self.assertEqual(a, array.array(self.typecode))
 
-        self.assertRaises(TypeError, a.__imul__, "bad")
+        with self.assertRaises(TypeError):
+            a *= 'bad'
 
     def test_getitem(self):
         a = array.array(self.typecode, self.example)
@@ -769,6 +773,7 @@
         p = proxy(s)
         self.assertEqual(p.tostring(), s.tostring())
         s = None
+        test_support.gc_collect()
         self.assertRaises(ReferenceError, len, p)
 
     def test_bug_782369(self):
diff --git a/lib-python/2.7/test/test_ascii_formatd.py b/lib-python/2.7/test/test_ascii_formatd.py
--- a/lib-python/2.7/test/test_ascii_formatd.py
+++ b/lib-python/2.7/test/test_ascii_formatd.py
@@ -4,6 +4,10 @@
 
 import unittest
 from test.test_support import check_warnings, run_unittest, import_module
+from test.test_support import check_impl_detail
+
+if not check_impl_detail(cpython=True):
+    raise unittest.SkipTest("this test is only for CPython")
 
 # Skip tests if _ctypes module does not exist
 import_module('_ctypes')
diff --git a/lib-python/2.7/test/test_ast.py b/lib-python/2.7/test/test_ast.py
--- a/lib-python/2.7/test/test_ast.py
+++ b/lib-python/2.7/test/test_ast.py
@@ -20,10 +20,24 @@
 # These tests are compiled through "exec"
 # There should be atleast one test per statement
 exec_tests = [
+    # None
+    "None",
     # FunctionDef
     "def f(): pass",
+    # FunctionDef with arg
+    "def f(a): pass",
+    # FunctionDef with arg and default value
+    "def f(a=0): pass",
+    # FunctionDef with varargs
+    "def f(*args): pass",
+    # FunctionDef with kwargs
+    "def f(**kwargs): pass",
+    # FunctionDef with all kind of args
+    "def f(a, b=1, c=None, d=[], e={}, *args, **kwargs): pass",
     # ClassDef
     "class C:pass",
+    # ClassDef, new style class
+    "class C(object): pass",
     # Return
     "def f():return 1",
     # Delete
@@ -68,6 +82,27 @@
     "for a,b in c: pass",
     "[(a,b) for a,b in c]",
     "((a,b) for a,b in c)",
+    "((a,b) for (a,b) in c)",
+    # Multiline generator expression
+    """(
+    (
+    Aa
+    ,
+       Bb
+    )
+    for
+    Aa
+    ,
+    Bb in Cc
+    )""",
+    # dictcomp
+    "{a : b for w in x for m in p if g}",
+    # dictcomp with naked tuple
+    "{a : b for v,w in x}",
+    # setcomp
+    "{r for l in x if g}",
+    # setcomp with naked tuple
+    "{r for l,m in x}",
 ]
 
 # These are compiled through "single"
@@ -80,6 +115,8 @@
 # These are compiled through "eval"
 # It should test all expressions
 eval_tests = [
+  # None
+  "None",
   # BoolOp
   "a and b",
   # BinOp
@@ -90,6 +127,16 @@
   "lambda:None",
   # Dict
   "{ 1:2 }",
+  # Empty dict
+  "{}",
+  # Set
+  "{None,}",
+  # Multiline dict
+  """{
+      1
+        :
+          2
+     }""",
   # ListComp
   "[a for b in c if d]",
   # GeneratorExp
@@ -114,8 +161,14 @@
   "v",
   # List
   "[1,2,3]",
+  # Empty list
+  "[]",
   # Tuple
   "1,2,3",
+  # Tuple
+  "(1,2,3)",
+  # Empty tuple
+  "()",
   # Combination
   "a.b.c.d(a.b[1:2])",
 
@@ -141,6 +194,35 @@
             elif value is not None:
                 self._assertTrueorder(value, parent_pos)
 
+    def test_AST_objects(self):
+        if test_support.check_impl_detail():
+            # PyPy also provides a __dict__ to the ast.AST base class.
+
+            x = ast.AST()
+            try:
+                x.foobar = 21
+            except AttributeError, e:
+                self.assertEquals(e.args[0],
+                                  "'_ast.AST' object has no attribute 'foobar'")
+            else:
+                self.assert_(False)
+
+            try:
+                ast.AST(lineno=2)
+            except AttributeError, e:
+                self.assertEquals(e.args[0],
+                                  "'_ast.AST' object has no attribute 'lineno'")
+            else:
+                self.assert_(False)
+
+        try:
+            ast.AST(2)
+        except TypeError, e:
+            self.assertEquals(e.args[0],
+                              "_ast.AST constructor takes 0 positional arguments")
+        else:
+            self.assert_(False)
+
     def test_snippets(self):
         for input, output, kind in ((exec_tests, exec_results, "exec"),
                                     (single_tests, single_results, "single"),
@@ -169,6 +251,114 @@
         self.assertTrue(issubclass(ast.comprehension, ast.AST))
         self.assertTrue(issubclass(ast.Gt, ast.AST))
 
+    def test_field_attr_existence(self):
+        for name, item in ast.__dict__.iteritems():
+            if isinstance(item, type) and name != 'AST' and name[0].isupper(): # XXX: pypy does not allow abstract ast class instanciation
+                x = item()
+                if isinstance(x, ast.AST):
+                    self.assertEquals(type(x._fields), tuple)
+
+    def test_arguments(self):
+        x = ast.arguments()
+        self.assertEquals(x._fields, ('args', 'vararg', 'kwarg', 'defaults'))
+        try:
+            x.vararg
+        except AttributeError, e:
+            self.assertEquals(e.args[0],
+                              "'arguments' object has no attribute 'vararg'")
+        else:
+            self.assert_(False)
+        x = ast.arguments(1, 2, 3, 4)
+        self.assertEquals(x.vararg, 2)
+
+    def test_field_attr_writable(self):
+        x = ast.Num()
+        # We can assign to _fields
+        x._fields = 666
+        self.assertEquals(x._fields, 666)
+
+    def test_classattrs(self):
+        x = ast.Num()
+        self.assertEquals(x._fields, ('n',))
+        try:
+            x.n
+        except AttributeError, e:
+            self.assertEquals(e.args[0],
+                              "'Num' object has no attribute 'n'")
+        else:
+            self.assert_(False)
+
+        x = ast.Num(42)
+        self.assertEquals(x.n, 42)
+        try:
+            x.lineno
+        except AttributeError, e:
+            self.assertEquals(e.args[0],
+                              "'Num' object has no attribute 'lineno'")
+        else:
+            self.assert_(False)
+
+        y = ast.Num()
+        x.lineno = y
+        self.assertEquals(x.lineno, y)
+
+        try:
+            x.foobar
+        except AttributeError, e:
+            self.assertEquals(e.args[0],
+                              "'Num' object has no attribute 'foobar'")
+        else:
+            self.assert_(False)
+
+        x = ast.Num(lineno=2)
+        self.assertEquals(x.lineno, 2)
+
+        x = ast.Num(42, lineno=0)
+        self.assertEquals(x.lineno, 0)
+        self.assertEquals(x._fields, ('n',))
+        self.assertEquals(x.n, 42)
+
+        self.assertRaises(TypeError, ast.Num, 1, 2)
+        self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)
+
+    def test_module(self):
+        body = [ast.Num(42)]
+        x = ast.Module(body)
+        self.assertEquals(x.body, body)
+
+    def test_nodeclass(self):
+        x = ast.BinOp()
+        self.assertEquals(x._fields, ('left', 'op', 'right'))
+
+        # Zero arguments constructor explicitely allowed
+        x = ast.BinOp()
+        # Random attribute allowed too
+        x.foobarbaz = 5
+        self.assertEquals(x.foobarbaz, 5)
+
+        n1 = ast.Num(1)
+        n3 = ast.Num(3)
+        addop = ast.Add()
+        x = ast.BinOp(n1, addop, n3)
+        self.assertEquals(x.left, n1)
+        self.assertEquals(x.op, addop)
+        self.assertEquals(x.right, n3)
+        
+        x = ast.BinOp(1, 2, 3)
+        self.assertEquals(x.left, 1)
+        self.assertEquals(x.op, 2)
+        self.assertEquals(x.right, 3)
+
+        x = ast.BinOp(1, 2, 3, lineno=0)
+        self.assertEquals(x.lineno, 0)
+
+    def test_nodeclasses(self):
+        x = ast.BinOp(1, 2, 3, lineno=0)
+        self.assertEquals(x.left, 1)
+        self.assertEquals(x.op, 2)
+        self.assertEquals(x.right, 3)
+        self.assertEquals(x.lineno, 0)
+
     def test_nodeclasses(self):
         x = ast.BinOp(1, 2, 3, lineno=0)
         self.assertEqual(x.left, 1)
@@ -178,6 +368,12 @@
 
         # node raises exception when not given enough arguments
         self.assertRaises(TypeError, ast.BinOp, 1, 2)
+        # node raises exception when given too many arguments
+        self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
+        # node raises exception when not given enough arguments
+        self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
+        # node raises exception when given too many arguments
+        self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
 
         # can set attributes through kwargs too
         x = ast.BinOp(left=1, op=2, right=3, lineno=0)
@@ -186,8 +382,14 @@
         self.assertEqual(x.right, 3)
         self.assertEqual(x.lineno, 0)
 
+        # Random kwargs also allowed
+        x = ast.BinOp(1, 2, 3, foobarbaz=42)
+        self.assertEquals(x.foobarbaz, 42)
+
+    def test_no_fields(self):
         # this used to fail because Sub._fields was None
         x = ast.Sub()
+        self.assertEquals(x._fields, ())
 
     def test_pickling(self):
         import pickle
@@ -330,8 +532,15 @@
 
 #### EVERYTHING BELOW IS GENERATED #####
 exec_results = [
+('Module', [('Expr', (1, 0), ('Name', (1, 0), 'None', ('Load',)))]),
 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
+('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, []), [('Pass', (1, 10))], [])]),
+('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',))], None, None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [])]),
+('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', None, []), [('Pass', (1, 14))], [])]),
+('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, 'kwargs', []), [('Pass', (1, 17))], [])]),
+('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('Name', (1, 6), 'a', ('Param',)), ('Name', (1, 9), 'b', ('Param',)), ('Name', (1, 14), 'c', ('Param',)), ('Name', (1, 22), 'd', ('Param',)), ('Name', (1, 28), 'e', ('Param',))], 'args', 'kwargs', [('Num', (1, 11), 1), ('Name', (1, 16), 'None', ('Load',)), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 52))], [])]),
 ('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
+('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [('Pass', (1, 17))], [])]),
 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
 ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
 ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
@@ -355,16 +564,26 @@
 ('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
 ('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
 ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
+('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [])]))]),
+('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [])]))]),
+('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), []), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))])]))]),
+('Module', [('Expr', (1, 0), ('DictComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [])]))]),
+('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))])]))]),
+('Module', [('Expr', (1, 0), ('SetComp', (1, 1), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [])]))]),
 ]
 single_results = [
 ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
 ]
 eval_results = [
+('Expression', ('Name', (1, 0), 'None', ('Load',))),
 ('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
 ('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
 ('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
 ('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, []), ('Name', (1, 7), 'None', ('Load',)))),
 ('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
+('Expression', ('Dict', (1, 0), [], [])),
+('Expression', ('Set', (1, 0), [('Name', (1, 1), 'None', ('Load',))])),
+('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
 ('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
 ('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
 ('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
@@ -376,7 +595,10 @@
 ('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
 ('Expression', ('Name', (1, 0), 'v', ('Load',))),
 ('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
+('Expression', ('List', (1, 0), [], ('Load',))),
 ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
+('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
+('Expression', ('Tuple', (1, 0), [], ('Load',))),
 ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
 ]
 main()
diff --git a/lib-python/2.7/test/test_builtin.py b/lib-python/2.7/test/test_builtin.py
--- a/lib-python/2.7/test/test_builtin.py
+++ b/lib-python/2.7/test/test_builtin.py
@@ -3,7 +3,8 @@
 import platform
 import unittest
 from test.test_support import fcmp, have_unicode, TESTFN, unlink, \
-                              run_unittest, check_py3k_warnings
+                              run_unittest, check_py3k_warnings, \
+                              check_impl_detail
 import warnings
 from operator import neg
 
@@ -247,12 +248,14 @@
         self.assertRaises(TypeError, compile)
         self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'badmode')
         self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'single', 0xff)
-        self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
+        if check_impl_detail(cpython=True):
+            self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
         self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
                           mode='eval', source='0', filename='tmp')
         if have_unicode:
             compile(unicode('print u"\xc3\xa5"\n', 'utf8'), '', 'exec')
-            self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec')
+            if check_impl_detail(cpython=True):
+                self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec')
             self.assertRaises(ValueError, compile, unicode('a = 1'), 'f', 'bad')
 
 
@@ -395,12 +398,16 @@
         self.assertEqual(eval('dir()', g, m), list('xyz'))
         self.assertEqual(eval('globals()', g, m), g)
         self.assertEqual(eval('locals()', g, m), m)
-        self.assertRaises(TypeError, eval, 'a', m)
+        # on top of CPython, the first dictionary (the globals) has to
+        # be a real dict.  This is not the case on top of PyPy.
+        if check_impl_detail(pypy=False):
+            self.assertRaises(TypeError, eval, 'a', m)
+
         class A:
             "Non-mapping"
             pass
         m = A()
-        self.assertRaises(TypeError, eval, 'a', g, m)
+        self.assertRaises((TypeError, AttributeError), eval, 'a', g, m)
 
         # Verify that dict subclasses work as well
         class D(dict):
@@ -491,9 +498,10 @@
         execfile(TESTFN, globals, locals)
         self.assertEqual(locals['z'], 2)
 
+        self.assertRaises(TypeError, execfile, TESTFN, {}, ())
         unlink(TESTFN)
         self.assertRaises(TypeError, execfile)
-        self.assertRaises(TypeError, execfile, TESTFN, {}, ())
+        self.assertRaises((TypeError, IOError), execfile, TESTFN, {}, ())
         import os
         self.assertRaises(IOError, execfile, os.curdir)
         self.assertRaises(IOError, execfile, "I_dont_exist")
@@ -1108,7 +1116,8 @@
             def __cmp__(self, other):
                 raise RuntimeError
             __hash__ = None # Invalid cmp makes this unhashable
-        self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
+        if check_impl_detail(cpython=True):
+            self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
 
         # Reject floats.
         self.assertRaises(TypeError, range, 1., 1., 1.)
diff --git a/lib-python/2.7/test/test_bytes.py b/lib-python/2.7/test/test_bytes.py
--- a/lib-python/2.7/test/test_bytes.py
+++ b/lib-python/2.7/test/test_bytes.py
@@ -694,6 +694,7 @@
         self.assertEqual(b, b1)
         self.assertTrue(b is b1)
 
+    @test.test_support.impl_detail("undocumented bytes.__alloc__()")
     def test_alloc(self):
         b = bytearray()
         alloc = b.__alloc__()
@@ -821,6 +822,8 @@
         self.assertEqual(b, b"")
         self.assertEqual(c, b"")
 
+    @test.test_support.impl_detail(
+        "resizing semantics of CPython rely on refcounting")
     def test_resize_forbidden(self):
         # #4509: can't resize a bytearray when there are buffer exports, even
         # if it wouldn't reallocate the underlying buffer.
@@ -853,6 +856,26 @@
         self.assertRaises(BufferError, delslice)
         self.assertEqual(b, orig)
 
+    @test.test_support.impl_detail("resizing semantics", cpython=False)
+    def test_resize_forbidden_non_cpython(self):
+        # on non-CPython implementations, we cannot prevent changes to
+        # bytearrays just because there are buffers around.  Instead,
+        # we get (on PyPy) a buffer that follows the changes and resizes.
+        b = bytearray(range(10))
+        for v in [memoryview(b), buffer(b)]:
+            b[5] = 99
+            self.assertIn(v[5], (99, chr(99)))
+            b[5] = 100
+            b += b
+            b += b
+            b += b
+            self.assertEquals(len(v), 80)
+            self.assertIn(v[5], (100, chr(100)))
+            self.assertIn(v[79], (9, chr(9)))
+            del b[10:]
+            self.assertRaises(IndexError, lambda: v[10])
+            self.assertEquals(len(v), 10)
+
     def test_empty_bytearray(self):
         # Issue #7561: operations on empty bytearrays could crash in many
         # situations, due to a fragile implementation of the
diff --git a/lib-python/2.7/test/test_bz2.py b/lib-python/2.7/test/test_bz2.py
--- a/lib-python/2.7/test/test_bz2.py
+++ b/lib-python/2.7/test/test_bz2.py
@@ -50,6 +50,7 @@
         self.filename = TESTFN
 
     def tearDown(self):
+        test_support.gc_collect()
         if os.path.isfile(self.filename):
             os.unlink(self.filename)
 
@@ -246,6 +247,8 @@
         for i in xrange(10000):
             o = BZ2File(self.filename)
             del o
+            if i % 100 == 0:
+                test_support.gc_collect()
 
     def testOpenNonexistent(self):
         # "Test opening a nonexistent file"
@@ -310,6 +313,7 @@
             for t in threads:
                 t.join()
 
+    @test_support.impl_detail()
     def testMixedIterationReads(self):
         # Issue #8397: mixed iteration and reads should be forbidden.
         with bz2.BZ2File(self.filename, 'wb') as f:
diff --git a/lib-python/2.7/test/test_cmd_line_script.py b/lib-python/2.7/test/test_cmd_line_script.py
--- a/lib-python/2.7/test/test_cmd_line_script.py
+++ b/lib-python/2.7/test/test_cmd_line_script.py
@@ -112,6 +112,8 @@
             self._check_script(script_dir, script_name, script_dir, '')
 
     def test_directory_compiled(self):
+        if test.test_support.check_impl_detail(pypy=True):
+            raise unittest.SkipTest("pypy won't load lone .pyc files")
         with temp_dir() as script_dir:
             script_name = _make_test_script(script_dir, '__main__')
             compiled_name = compile_script(script_name)
@@ -173,6 +175,8 @@
                                script_name, 'test_pkg')
 
     def test_package_compiled(self):
+        if test.test_support.check_impl_detail(pypy=True):
+            raise unittest.SkipTest("pypy won't load lone .pyc files")
         with temp_dir() as script_dir:
             pkg_dir = os.path.join(script_dir, 'test_pkg')
             make_pkg(pkg_dir)
diff --git a/lib-python/2.7/test/test_code.py b/lib-python/2.7/test/test_code.py
--- a/lib-python/2.7/test/test_code.py
+++ b/lib-python/2.7/test/test_code.py
@@ -82,7 +82,7 @@
 
 import unittest
 import weakref
-import _testcapi
+from test import test_support
 
 
 def consts(t):
@@ -104,7 +104,9 @@
 
 class CodeTest(unittest.TestCase):
 
+    @test_support.impl_detail("test for PyCode_NewEmpty")
     def test_newempty(self):
+        import _testcapi
         co = _testcapi.code_newempty("filename", "funcname", 15)
         self.assertEqual(co.co_filename, "filename")
         self.assertEqual(co.co_name, "funcname")
@@ -132,6 +134,7 @@
         coderef = weakref.ref(f.__code__, callback)
         self.assertTrue(bool(coderef()))
         del f
+        test_support.gc_collect()
         self.assertFalse(bool(coderef()))
         self.assertTrue(self.called)
 
diff --git a/lib-python/2.7/test/test_codeop.py b/lib-python/2.7/test/test_codeop.py
--- a/lib-python/2.7/test/test_codeop.py
+++ b/lib-python/2.7/test/test_codeop.py
@@ -3,7 +3,7 @@
    Nick Mathewson
 """
 import unittest
-from test.test_support import run_unittest, is_jython
+from test.test_support import run_unittest, is_jython, check_impl_detail
 
 from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
 
@@ -270,7 +270,9 @@
         ai("a = 'a\\\n")
 
         ai("a = 1","eval")
-        ai("a = (","eval")
+        if check_impl_detail():   # on PyPy it asks for more data, which is not
+            ai("a = (","eval")    # completely correct but hard to fix and
+                                  # really a detail (in my opinion <arigo>)
         ai("]","eval")
         ai("())","eval")
         ai("[}","eval")
diff --git a/lib-python/2.7/test/test_coercion.py b/lib-python/2.7/test/test_coercion.py
--- a/lib-python/2.7/test/test_coercion.py
+++ b/lib-python/2.7/test/test_coercion.py
@@ -1,6 +1,7 @@
 import copy
 import unittest
-from test.test_support import run_unittest, TestFailed, check_warnings
+from test.test_support import (
+    run_unittest, TestFailed, check_warnings, check_impl_detail)
 
 
 # Fake a number that implements numeric methods through __coerce__
@@ -306,12 +307,18 @@
         self.assertNotEqual(cmp(u'fish', evil_coercer), 0)
         self.assertNotEqual(cmp(slice(1), evil_coercer), 0)
         # ...but that this still works
-        class WackyComparer(object):
-            def __cmp__(slf, other):
-                self.assertTrue(other == 42, 'expected evil_coercer, got %r' % other)
-                return 0
-            __hash__ = None # Invalid cmp makes this unhashable
-        self.assertEqual(cmp(WackyComparer(), evil_coercer), 0)
+        if check_impl_detail():
+            # NB. I (arigo) would consider the following as implementation-
+            # specific.  For example, in CPython, if we replace 42 with 42.0
+            # both below and in CoerceTo() above, then the test fails.  This
+            # hints that the behavior is really dependent on some obscure
+            # internal details.
+            class WackyComparer(object):
+                def __cmp__(slf, other):
+                    self.assertTrue(other == 42, 'expected evil_coercer, got %r' % other)
+                    return 0
+                __hash__ = None # Invalid cmp makes this unhashable
+            self.assertEqual(cmp(WackyComparer(), evil_coercer), 0)
         # ...and classic classes too, since that code path is a little different
         class ClassicWackyComparer:
             def __cmp__(slf, other):
diff --git a/lib-python/2.7/test/test_compile.py b/lib-python/2.7/test/test_compile.py
--- a/lib-python/2.7/test/test_compile.py
+++ b/lib-python/2.7/test/test_compile.py
@@ -3,6 +3,7 @@
 import _ast
 from test import test_support
 import textwrap
+from test.test_support import check_impl_detail
 
 class TestSpecifics(unittest.TestCase):
 
@@ -90,12 +91,13 @@
         self.assertEqual(m.results, ('z', g))
         exec 'z = locals()' in g, m
         self.assertEqual(m.results, ('z', m))
-        try:
-            exec 'z = b' in m
-        except TypeError:
-            pass
-        else:
-            self.fail('Did not validate globals as a real dict')
+        if check_impl_detail():
+            try:
+                exec 'z = b' in m
+            except TypeError:
+                pass
+            else:
+                self.fail('Did not validate globals as a real dict')
 
         class A:
             "Non-mapping"
diff --git a/lib-python/2.7/test/test_copy.py b/lib-python/2.7/test/test_copy.py
--- a/lib-python/2.7/test/test_copy.py
+++ b/lib-python/2.7/test/test_copy.py
@@ -637,6 +637,7 @@
         self.assertEqual(v[c], d)
         self.assertEqual(len(v), 2)
         del c, d
+        test_support.gc_collect()
         self.assertEqual(len(v), 1)
         x, y = C(), C()
         # The underlying containers are decoupled
@@ -666,6 +667,7 @@
         self.assertEqual(v[a].i, b.i)
         self.assertEqual(v[c].i, d.i)
         del c
+        test_support.gc_collect()
         self.assertEqual(len(v), 1)
 
     def test_deepcopy_weakvaluedict(self):
@@ -689,6 +691,7 @@
         self.assertTrue(t is d)
         del x, y, z, t
         del d
+        test_support.gc_collect()
         self.assertEqual(len(v), 1)
 
     def test_deepcopy_bound_method(self):
diff --git a/lib-python/2.7/test/test_cpickle.py b/lib-python/2.7/test/test_cpickle.py
--- a/lib-python/2.7/test/test_cpickle.py
+++ b/lib-python/2.7/test/test_cpickle.py
@@ -61,27 +61,27 @@
     error = cPickle.BadPickleGet
 
     def test_recursive_list(self):
-        self.assertRaises(ValueError,
+        self.assertRaises((ValueError, RuntimeError),
                           AbstractPickleTests.test_recursive_list,
                           self)
 
     def test_recursive_tuple(self):
-        self.assertRaises(ValueError,
+        self.assertRaises((ValueError, RuntimeError),
                           AbstractPickleTests.test_recursive_tuple,
                           self)
 
     def test_recursive_inst(self):
-        self.assertRaises(ValueError,
+        self.assertRaises((ValueError, RuntimeError),
                           AbstractPickleTests.test_recursive_inst,
                           self)
 
     def test_recursive_dict(self):
-        self.assertRaises(ValueError,
+        self.assertRaises((ValueError, RuntimeError),
                           AbstractPickleTests.test_recursive_dict,
                           self)
 
     def test_recursive_multi(self):
-        self.assertRaises(ValueError,
+        self.assertRaises((ValueError, RuntimeError),
                           AbstractPickleTests.test_recursive_multi,
                           self)
 
diff --git a/lib-python/2.7/test/test_csv.py b/lib-python/2.7/test/test_csv.py
--- a/lib-python/2.7/test/test_csv.py
+++ b/lib-python/2.7/test/test_csv.py
@@ -54,8 +54,10 @@
         self.assertEqual(obj.dialect.skipinitialspace, False)
         self.assertEqual(obj.dialect.strict, False)
         # Try deleting or changing attributes (they are read-only)
-        self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter')
-        self.assertRaises(TypeError, setattr, obj.dialect, 'delimiter', ':')
+        self.assertRaises((TypeError, AttributeError), delattr, obj.dialect,
+                          'delimiter')
+        self.assertRaises((TypeError, AttributeError), setattr, obj.dialect,
+                          'delimiter', ':')
         self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
         self.assertRaises(AttributeError, setattr, obj.dialect,
                           'quoting', None)
diff --git a/lib-python/2.7/test/test_deque.py b/lib-python/2.7/test/test_deque.py
--- a/lib-python/2.7/test/test_deque.py
+++ b/lib-python/2.7/test/test_deque.py
@@ -109,7 +109,7 @@
         self.assertEqual(deque('abc', maxlen=4).maxlen, 4)
         self.assertEqual(deque('abc', maxlen=2).maxlen, 2)
         self.assertEqual(deque('abc', maxlen=0).maxlen, 0)
-        with self.assertRaises(AttributeError):
+        with self.assertRaises((AttributeError, TypeError)):
             d = deque('abc')
             d.maxlen = 10
 
@@ -352,7 +352,10 @@
         for match in (True, False):
             d = deque(['ab'])
             d.extend([MutateCmp(d, match), 'c'])
-            self.assertRaises(IndexError, d.remove, 'c')
+            # On CPython we get IndexError: deque mutated during remove().
+            # Why is it an IndexError during remove() only???
+            # On PyPy it is a RuntimeError, as in the other operations.
+            self.assertRaises((IndexError, RuntimeError), d.remove, 'c')
             self.assertEqual(d, deque())
 
     def test_repr(self):
@@ -514,7 +517,7 @@
                 container = reversed(deque([obj, 1]))
             obj.x = iter(container)
             del obj, container
-            gc.collect()
+            test_support.gc_collect()
             self.assertTrue(ref() is None, "Cycle was not collected")
 
 class TestVariousIteratorArgs(unittest.TestCase):
@@ -630,6 +633,7 @@
         p = weakref.proxy(d)
         self.assertEqual(str(p), str(d))
         d = None
+        test_support.gc_collect()
         self.assertRaises(ReferenceError, str, p)
 
     def test_strange_subclass(self):
diff --git a/lib-python/2.7/test/test_descr.py b/lib-python/2.7/test/test_descr.py
--- a/lib-python/2.7/test/test_descr.py
+++ b/lib-python/2.7/test/test_descr.py
@@ -2,6 +2,7 @@
 import sys
 import types
 import unittest
+import popen2     # trigger early the warning from popen2.py
 
 from copy import deepcopy
 from test import test_support
@@ -1128,7 +1129,7 @@
 
         # Test lookup leaks [SF bug 572567]
         import gc
-        if hasattr(gc, 'get_objects'):
+        if test_support.check_impl_detail():
             class G(object):
                 def __cmp__(self, other):
                     return 0
@@ -1741,6 +1742,10 @@
                 raise MyException
 
         for name, runner, meth_impl, ok, env in specials:
+            if name == '__length_hint__' or name == '__sizeof__':
+                if not test_support.check_impl_detail():
+                    continue
+
             class X(Checker):
                 pass
             for attr, obj in env.iteritems():
@@ -1980,7 +1985,9 @@
         except TypeError, msg:
             self.assertTrue(str(msg).find("weak reference") >= 0)
         else:
-            self.fail("weakref.ref(no) should be illegal")
+            if test_support.check_impl_detail(pypy=False):
+                self.fail("weakref.ref(no) should be illegal")
+            #else: pypy supports taking weakrefs to some more objects
         class Weak(object):
             __slots__ = ['foo', '__weakref__']
         yes = Weak()
@@ -3092,7 +3099,16 @@
         class R(J):
             __slots__ = ["__dict__", "__weakref__"]
 
-        for cls, cls2 in ((G, H), (G, I), (I, H), (Q, R), (R, Q)):
+        if test_support.check_impl_detail(pypy=False):
+            lst = ((G, H), (G, I), (I, H), (Q, R), (R, Q))
+        else:
+            # Not supported in pypy: changing the __class__ of an object
+            # to another __class__ that just happens to have the same slots.
+            # If needed, we can add the feature, but what we'll likely do
+            # then is to allow mostly any __class__ assignment, even if the
+            # classes have different __slots__, because we it's easier.
+            lst = ((Q, R), (R, Q))
+        for cls, cls2 in lst:
             x = cls()
             x.a = 1
             x.__class__ = cls2
@@ -3175,7 +3191,8 @@
             except TypeError:
                 pass
             else:
-                self.fail("%r's __dict__ can be modified" % cls)
+                if test_support.check_impl_detail(pypy=False):
+                    self.fail("%r's __dict__ can be modified" % cls)
 
         # Modules also disallow __dict__ assignment
         class Module1(types.ModuleType, Base):
@@ -4383,13 +4400,10 @@
         self.assertTrue(l.__add__ != [5].__add__)
         self.assertTrue(l.__add__ != l.__mul__)
         self.assertTrue(l.__add__.__name__ == '__add__')
-        if hasattr(l.__add__, '__self__'):
-            # CPython
-            self.assertTrue(l.__add__.__self__ is l)
+        self.assertTrue(l.__add__.__self__ is l)
+        if hasattr(l.__add__, '__objclass__'):   # CPython
             self.assertTrue(l.__add__.__objclass__ is list)
-        else:
-            # Python implementations where [].__add__ is a normal bound method
-            self.assertTrue(l.__add__.im_self is l)
+        else:                                    # PyPy
             self.assertTrue(l.__add__.im_class is list)
         self.assertEqual(l.__add__.__doc__, list.__add__.__doc__)
         try:
@@ -4578,8 +4592,12 @@
             str.split(fake_str)
 
         # call a slot wrapper descriptor
-        with self.assertRaises(TypeError):
-            str.__add__(fake_str, "abc")
+        try:
+            r = str.__add__(fake_str, "abc")
+        except TypeError:
+            pass
+        else:
+            self.assertEqual(r, NotImplemented)
 
 
 class DictProxyTests(unittest.TestCase):
diff --git a/lib-python/2.7/test/test_descrtut.py b/lib-python/2.7/test/test_descrtut.py
--- a/lib-python/2.7/test/test_descrtut.py
+++ b/lib-python/2.7/test/test_descrtut.py
@@ -172,46 +172,12 @@
     AttributeError: 'list' object has no attribute '__methods__'
     >>>
 
-Instead, you can get the same information from the list type:
+Instead, you can get the same information from the list type
+(the following example filters out the numerous method names
+starting with '_'):
 
-    >>> pprint.pprint(dir(list))    # like list.__dict__.keys(), but sorted
-    ['__add__',
-     '__class__',
-     '__contains__',
-     '__delattr__',
-     '__delitem__',
-     '__delslice__',
-     '__doc__',
-     '__eq__',
-     '__format__',
-     '__ge__',
-     '__getattribute__',
-     '__getitem__',
-     '__getslice__',
-     '__gt__',
-     '__hash__',
-     '__iadd__',
-     '__imul__',
-     '__init__',
-     '__iter__',
-     '__le__',
-     '__len__',
-     '__lt__',
-     '__mul__',
-     '__ne__',
-     '__new__',
-     '__reduce__',
-     '__reduce_ex__',
-     '__repr__',
-     '__reversed__',
-     '__rmul__',
-     '__setattr__',
-     '__setitem__',
-     '__setslice__',
-     '__sizeof__',
-     '__str__',
-     '__subclasshook__',
-     'append',
+    >>> pprint.pprint([name for name in dir(list) if not name.startswith('_')])
+    ['append',
      'count',
      'extend',
      'index',
diff --git a/lib-python/2.7/test/test_dict.py b/lib-python/2.7/test/test_dict.py
--- a/lib-python/2.7/test/test_dict.py
+++ b/lib-python/2.7/test/test_dict.py
@@ -319,7 +319,8 @@
                     self.assertEqual(va, int(ka))
                     kb, vb = tb = b.popitem()
                     self.assertEqual(vb, int(kb))
-                    self.assertFalse(copymode < 0 and ta != tb)
+                    if test_support.check_impl_detail():
+                        self.assertFalse(copymode < 0 and ta != tb)
                 self.assertFalse(a)
                 self.assertFalse(b)
 
diff --git a/lib-python/2.7/test/test_dis.py b/lib-python/2.7/test/test_dis.py
--- a/lib-python/2.7/test/test_dis.py
+++ b/lib-python/2.7/test/test_dis.py
@@ -56,8 +56,8 @@
  %-4d         0 LOAD_CONST               1 (0)
               3 POP_JUMP_IF_TRUE        38
               6 LOAD_GLOBAL              0 (AssertionError)
-              9 BUILD_LIST               0
-             12 LOAD_FAST                0 (x)
+              9 LOAD_FAST                0 (x)
+             12 BUILD_LIST_FROM_ARG      0
              15 GET_ITER
         >>   16 FOR_ITER                12 (to 31)
              19 STORE_FAST               1 (s)
diff --git a/lib-python/2.7/test/test_doctest.py b/lib-python/2.7/test/test_doctest.py
--- a/lib-python/2.7/test/test_doctest.py
+++ b/lib-python/2.7/test/test_doctest.py
@@ -782,7 +782,7 @@
     ...     >>> x = 12
     ...     >>> print x//0
     ...     Traceback (most recent call last):
-    ...     ZeroDivisionError: integer division or modulo by zero
+    ...     ZeroDivisionError: integer division by zero
     ...     '''
     >>> test = doctest.DocTestFinder().find(f)[0]
     >>> doctest.DocTestRunner(verbose=False).run(test)
@@ -799,7 +799,7 @@
     ...     >>> print 'pre-exception output', x//0
     ...     pre-exception output
     ...     Traceback (most recent call last):
-    ...     ZeroDivisionError: integer division or modulo by zero
+    ...     ZeroDivisionError: integer division by zero
     ...     '''
     >>> test = doctest.DocTestFinder().find(f)[0]
     >>> doctest.DocTestRunner(verbose=False).run(test)
@@ -810,7 +810,7 @@
         print 'pre-exception output', x//0
     Exception raised:
         ...
-        ZeroDivisionError: integer division or modulo by zero
+        ZeroDivisionError: integer division by zero
     TestResults(failed=1, attempted=2)
 
 Exception messages may contain newlines:
@@ -978,7 +978,7 @@
     Exception raised:
         Traceback (most recent call last):
         ...
-        ZeroDivisionError: integer division or modulo by zero
+        ZeroDivisionError: integer division by zero
     TestResults(failed=1, attempted=1)
 """
     def displayhook(): r"""
@@ -1924,7 +1924,7 @@
     > <doctest foo-b&#228;r at baz[1]>(1)<module>()
     -> calls_set_trace()
     (Pdb) print foo
-    *** NameError: name 'foo' is not defined
+    *** NameError: global name 'foo' is not defined
     (Pdb) continue
     TestResults(failed=0, attempted=2)
 """
@@ -2229,7 +2229,7 @@
           favorite_color
       Exception raised:
           ...
-          NameError: name 'favorite_color' is not defined
+          NameError: global name 'favorite_color' is not defined
       <BLANKLINE>
       <BLANKLINE>
 
@@ -2289,7 +2289,7 @@
         favorite_color
     Exception raised:
         ...
-        NameError: name 'favorite_color' is not defined
+        NameError: global name 'favorite_color' is not defined
     **********************************************************************
     1 items had failures:
        1 of   2 in test_doctest.txt
@@ -2382,7 +2382,7 @@
         favorite_color
     Exception raised:
         ...
-        NameError: name 'favorite_color' is not defined
+        NameError: global name 'favorite_color' is not defined
     TestResults(failed=1, attempted=2)
     >>> doctest.master = None  # Reset master.
 
diff --git a/lib-python/2.7/test/test_dumbdbm.py b/lib-python/2.7/test/test_dumbdbm.py
--- a/lib-python/2.7/test/test_dumbdbm.py
+++ b/lib-python/2.7/test/test_dumbdbm.py
@@ -107,9 +107,11 @@
         f.close()
 
         # Mangle the file by adding \r before each newline
-        data = open(_fname + '.dir').read()
+        with open(_fname + '.dir') as f:
+            data = f.read()
         data = data.replace('\n', '\r\n')
-        open(_fname + '.dir', 'wb').write(data)
+        with open(_fname + '.dir', 'wb') as f:
+            f.write(data)
 
         f = dumbdbm.open(_fname)
         self.assertEqual(f['1'], 'hello')
diff --git a/lib-python/2.7/test/test_extcall.py b/lib-python/2.7/test/test_extcall.py
--- a/lib-python/2.7/test/test_extcall.py
+++ b/lib-python/2.7/test/test_extcall.py
@@ -90,19 +90,19 @@
 
     >>> class Nothing: pass
     ...
-    >>> g(*Nothing())
+    >>> g(*Nothing())                     #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: g() argument after * must be a sequence, not instance
+    TypeError: ...argument after * must be a sequence, not instance
 
     >>> class Nothing:
     ...     def __len__(self): return 5
     ...
 
-    >>> g(*Nothing())
+    >>> g(*Nothing())                     #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: g() argument after * must be a sequence, not instance
+    TypeError: ...argument after * must be a sequence, not instance
 
     >>> class Nothing():
     ...     def __len__(self): return 5
@@ -154,52 +154,50 @@
       ...
     TypeError: g() got multiple values for keyword argument 'x'
 
-    >>> f(**{1:2})
+    >>> f(**{1:2})                             #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: f() keywords must be strings
+    TypeError: ...keywords must be strings
 
     >>> h(**{'e': 2})
     Traceback (most recent call last):
       ...
     TypeError: h() got an unexpected keyword argument 'e'
 
-    >>> h(*h)
+    >>> h(*h)                                  #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: h() argument after * must be a sequence, not function
+    TypeError: ...argument after * must be a sequence, not function
 
-    >>> dir(*h)
+    >>> dir(*h)                                #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: dir() argument after * must be a sequence, not function
+    TypeError: ...argument after * must be a sequence, not function
 
-    >>> None(*h)
+    >>> None(*h)                               #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: NoneType object argument after * must be a sequence, \
-not function
+    TypeError: ...argument after * must be a sequence, not function
 
-    >>> h(**h)
+    >>> h(**h)                                 #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: h() argument after ** must be a mapping, not function
+    TypeError: ...argument after ** must be a mapping, not function
 
-    >>> dir(**h)
+    >>> dir(**h)                               #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: dir() argument after ** must be a mapping, not function
+    TypeError: ...argument after ** must be a mapping, not function
 
-    >>> None(**h)
+    >>> None(**h)                              #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: NoneType object argument after ** must be a mapping, \
-not function
+    TypeError: ...argument after ** must be a mapping, not function
 
-    >>> dir(b=1, **{'b': 1})
+    >>> dir(b=1, **{'b': 1})                   #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: dir() got multiple values for keyword argument 'b'
+    TypeError: ...got multiple values for keyword argument 'b'
 
 Another helper function
 
@@ -247,10 +245,10 @@
     ...     False
     True
 
-    >>> id(1, **{'foo': 1})
+    >>> id(1, **{'foo': 1})                 #doctest: +ELLIPSIS
     Traceback (most recent call last):
       ...
-    TypeError: id() takes no keyword arguments
+    TypeError: id() ... keyword argument...
 
 A corner case of keyword dictionary items being deleted during
 the function call setup. See <http://bugs.python.org/issue2016>.
diff --git a/lib-python/2.7/test/test_fcntl.py b/lib-python/2.7/test/test_fcntl.py
--- a/lib-python/2.7/test/test_fcntl.py
+++ b/lib-python/2.7/test/test_fcntl.py
@@ -32,7 +32,7 @@
                         'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
                         'freebsd6', 'freebsd7', 'freebsd8',
                         'bsdos2', 'bsdos3', 'bsdos4',
-                        'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
+                        'openbsd', 'openbsd2', 'openbsd3', 'openbsd4', 'openbsd5'):
         if struct.calcsize('l') == 8:
             off_t = 'l'
             pid_t = 'i'
diff --git a/lib-python/2.7/test/test_file.py b/lib-python/2.7/test/test_file.py
--- a/lib-python/2.7/test/test_file.py
+++ b/lib-python/2.7/test/test_file.py
@@ -12,7 +12,7 @@
 import io
 import _pyio as pyio
 
-from test.test_support import TESTFN, run_unittest
+from test.test_support import TESTFN, run_unittest, gc_collect
 from UserList import UserList
 
 class AutoFileTests(unittest.TestCase):
@@ -33,6 +33,7 @@
         self.assertEqual(self.f.tell(), p.tell())
         self.f.close()
         self.f = None
+        gc_collect()
         self.assertRaises(ReferenceError, getattr, p, 'tell')
 
     def testAttributes(self):
@@ -157,7 +158,12 @@
     def testStdin(self):
         # This causes the interpreter to exit on OSF1 v5.1.
         if sys.platform != 'osf1V5':
-            self.assertRaises((IOError, ValueError), sys.stdin.seek, -1)
+            if sys.stdin.isatty():
+                self.assertRaises((IOError, ValueError), sys.stdin.seek, -1)
+            else:
+                print((
+                    '  Skipping sys.stdin.seek(-1): stdin is not a tty.'
+                    ' Test manually.'), file=sys.__stdout__)
         else:
             print((
                 '  Skipping sys.stdin.seek(-1), it may crash the interpreter.'
diff --git a/lib-python/2.7/test/test_file2k.py b/lib-python/2.7/test/test_file2k.py
--- a/lib-python/2.7/test/test_file2k.py
+++ b/lib-python/2.7/test/test_file2k.py
@@ -11,7 +11,7 @@
     threading = None
 
 from test import test_support
-from test.test_support import TESTFN, run_unittest
+from test.test_support import TESTFN, run_unittest, gc_collect
 from UserList import UserList
 
 class AutoFileTests(unittest.TestCase):
@@ -32,6 +32,7 @@
         self.assertEqual(self.f.tell(), p.tell())
         self.f.close()
         self.f = None
+        gc_collect()
         self.assertRaises(ReferenceError, getattr, p, 'tell')
 
     def testAttributes(self):
@@ -116,8 +117,12 @@
 
         for methodname in methods:
             method = getattr(self.f, methodname)
+            args = {'readinto': (bytearray(''),),
+                    'seek':     (0,),
+                    'write':    ('',),
+                    }.get(methodname, ())
             # should raise on closed file
-            self.assertRaises(ValueError, method)
+            self.assertRaises(ValueError, method, *args)
         with test_support.check_py3k_warnings():
             for methodname in deprecated_methods:
                 method = getattr(self.f, methodname)
@@ -216,7 +221,12 @@
     def testStdin(self):
         # This causes the interpreter to exit on OSF1 v5.1.
         if sys.platform != 'osf1V5':
-            self.assertRaises(IOError, sys.stdin.seek, -1)
+            if sys.stdin.isatty():
+                self.assertRaises(IOError, sys.stdin.seek, -1)
+            else:
+                print >>sys.__stdout__, (
+                    '  Skipping sys.stdin.seek(-1): stdin is not a tty.'
+                    ' Test manualy.')
         else:
             print >>sys.__stdout__, (
                 '  Skipping sys.stdin.seek(-1), it may crash the interpreter.'
@@ -336,8 +346,9 @@
                 except ValueError:
                     pass
                 else:
-                    self.fail("%s%r after next() didn't raise ValueError" %
-                                     (methodname, args))
+                    if test_support.check_impl_detail():
+                        self.fail("%s%r after next() didn't raise ValueError" %
+                                  (methodname, args))
                 f.close()
 
             # Test to see if harmless (by accident) mixing of read* and
@@ -388,6 +399,7 @@
             if lines != testlines:
                 self.fail("readlines() after next() with empty buffer "
                           "failed. Got %r, expected %r" % (line, testline))
+            f.close()
             # Reading after iteration hit EOF shouldn't hurt either
             f = open(TESTFN)
             try:
@@ -438,6 +450,9 @@
         self.close_count = 0
         self.close_success_count = 0
         self.use_buffering = False
+        # to prevent running out of file descriptors on PyPy,
+        # we only keep the 50 most recent files open
+        self.all_files = [None] * 50
 
     def tearDown(self):
         if self.f:
@@ -453,9 +468,14 @@
 
     def _create_file(self):
         if self.use_buffering:
-            self.f = open(self.filename, "w+", buffering=1024*16)
+            f = open(self.filename, "w+", buffering=1024*16)
         else:
-            self.f = open(self.filename, "w+")
+            f = open(self.filename, "w+")
+        self.f = f
+        self.all_files.append(f)
+        oldf = self.all_files.pop(0)
+        if oldf is not None:
+            oldf.close()
 
     def _close_file(self):
         with self._count_lock:
@@ -496,7 +516,6 @@
 
     def _test_close_open_io(self, io_func, nb_workers=5):
         def worker():
-            self._create_file()
             funcs = itertools.cycle((
                 lambda: io_func(),
                 lambda: self._close_and_reopen_file(),
@@ -508,7 +527,11 @@
                     f()
                 except (IOError, ValueError):
                     pass
+        self._create_file()
         self._run_workers(worker, nb_workers)
+        # make sure that all files can be closed now
+        del self.all_files
+        gc_collect()
         if test_support.verbose:
             # Useful verbose statistics when tuning this test to take
             # less time to run but still ensuring that its still useful.
diff --git a/lib-python/2.7/test/test_fileio.py b/lib-python/2.7/test/test_fileio.py
--- a/lib-python/2.7/test/test_fileio.py
+++ b/lib-python/2.7/test/test_fileio.py
@@ -12,6 +12,7 @@
 
 from test.test_support import TESTFN, check_warnings, run_unittest, make_bad_fd
 from test.test_support import py3k_bytes as bytes
+from test.test_support import gc_collect
 from test.script_helper import run_python
 
 from _io import FileIO as _FileIO
@@ -34,6 +35,7 @@
         self.assertEqual(self.f.tell(), p.tell())
         self.f.close()
         self.f = None
+        gc_collect()
         self.assertRaises(ReferenceError, getattr, p, 'tell')
 
     def testSeekTell(self):
@@ -104,8 +106,8 @@
         self.assertTrue(f.closed)
 
     def testMethods(self):
-        methods = ['fileno', 'isatty', 'read', 'readinto',
-                   'seek', 'tell', 'truncate', 'write', 'seekable',
+        methods = ['fileno', 'isatty', 'read',
+                   'tell', 'truncate', 'seekable',
                    'readable', 'writable']
         if sys.platform.startswith('atheos'):
             methods.remove('truncate')
@@ -117,6 +119,10 @@
             method = getattr(self.f, methodname)
             # should raise on closed file
             self.assertRaises(ValueError, method)
+        # methods with one argument
+        self.assertRaises(ValueError, self.f.readinto, 0)
+        self.assertRaises(ValueError, self.f.write, 0)
+        self.assertRaises(ValueError, self.f.seek, 0)
 
     def testOpendir(self):
         # Issue 3703: opening a directory should fill the errno
diff --git a/lib-python/2.7/test/test_format.py b/lib-python/2.7/test/test_format.py
--- a/lib-python/2.7/test/test_format.py
+++ b/lib-python/2.7/test/test_format.py
@@ -242,7 +242,7 @@
             try:
                 testformat(formatstr, args)
             except exception, exc:
-                if str(exc) == excmsg:
+                if str(exc) == excmsg or not test_support.check_impl_detail():
                     if verbose:
                         print "yes"
                 else:
@@ -272,13 +272,16 @@
         test_exc(u'no format', u'1', TypeError,
                  "not all arguments converted during string formatting")
 
-        class Foobar(long):
-            def __oct__(self):
-                # Returning a non-string should not blow up.
-                return self + 1
-
-        test_exc('%o', Foobar(), TypeError,
-                 "expected string or Unicode object, long found")
+        if test_support.check_impl_detail():
+            # __oct__() is called if Foobar inherits from 'long', but
+            # not, say, 'object' or 'int' or 'str'.  This seems strange
+            # enough to consider it a complete implementation detail.
+            class Foobar(long):
+                def __oct__(self):
+                    # Returning a non-string should not blow up.
+                    return self + 1
+            test_exc('%o', Foobar(), TypeError,
+                     "expected string or Unicode object, long found")
 
         if maxsize == 2**31-1:
             # crashes 2.2.1 and earlier:
diff --git a/lib-python/2.7/test/test_funcattrs.py b/lib-python/2.7/test/test_funcattrs.py
--- a/lib-python/2.7/test/test_funcattrs.py
+++ b/lib-python/2.7/test/test_funcattrs.py
@@ -14,6 +14,8 @@
         self.b = b
 
     def cannot_set_attr(self, obj, name, value, exceptions):
+        if not test_support.check_impl_detail():
+            exceptions = (TypeError, AttributeError)
         # Helper method for other tests.
         try:
             setattr(obj, name, value)
@@ -286,13 +288,13 @@
     def test_delete_func_dict(self):
         try:
             del self.b.__dict__
-        except TypeError:
+        except (AttributeError, TypeError):
             pass
         else:
             self.fail("deleting function dictionary should raise TypeError")
         try:
             del self.b.func_dict
-        except TypeError:
+        except (AttributeError, TypeError):
             pass
         else:
             self.fail("deleting function dictionary should raise TypeError")
diff --git a/lib-python/2.7/test/test_functools.py b/lib-python/2.7/test/test_functools.py
--- a/lib-python/2.7/test/test_functools.py
+++ b/lib-python/2.7/test/test_functools.py
@@ -45,6 +45,8 @@
         # attributes should not be writable
         if not isinstance(self.thetype, type):
             return
+        if not test_support.check_impl_detail():
+            return
         self.assertRaises(TypeError, setattr, p, 'func', map)
         self.assertRaises(TypeError, setattr, p, 'args', (1, 2))
         self.assertRaises(TypeError, setattr, p, 'keywords', dict(a=1, b=2))
@@ -136,6 +138,7 @@
         p = proxy(f)
         self.assertEqual(f.func, p.func)
         f = None
+        test_support.gc_collect()
         self.assertRaises(ReferenceError, getattr, p, 'func')
 
     def test_with_bound_and_unbound_methods(self):
@@ -172,7 +175,7 @@
                       updated=functools.WRAPPER_UPDATES):
         # Check attributes were assigned
         for name in assigned:
-            self.assertTrue(getattr(wrapper, name) is getattr(wrapped, name))
+            self.assertTrue(getattr(wrapper, name) == getattr(wrapped, name), name)
         # Check attributes were updated
         for name in updated:
             wrapper_attr = getattr(wrapper, name)
diff --git a/lib-python/2.7/test/test_generators.py b/lib-python/2.7/test/test_generators.py
--- a/lib-python/2.7/test/test_generators.py
+++ b/lib-python/2.7/test/test_generators.py
@@ -190,7 +190,7 @@
       File "<stdin>", line 1, in ?
       File "<stdin>", line 2, in g
       File "<stdin>", line 2, in f
-    ZeroDivisionError: integer division or modulo by zero
+    ZeroDivisionError: integer division by zero
     >>> k.next()  # and the generator cannot be resumed
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
@@ -733,14 +733,16 @@
 ...     yield 1
 Traceback (most recent call last):
   ..
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[0]>, line 3)
+  File "<doctest test.test_generators.__test__.syntax[0]>", line 3
+SyntaxError: 'return' with argument inside generator
 
 >>> def f():
 ...     yield 1
 ...     return 22
 Traceback (most recent call last):
   ..
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[1]>, line 3)
+  File "<doctest test.test_generators.__test__.syntax[1]>", line 3
+SyntaxError: 'return' with argument inside generator
 
 "return None" is not the same as "return" in a generator:
 
@@ -749,7 +751,8 @@
 ...     return None
 Traceback (most recent call last):
   ..
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[2]>, line 3)
+  File "<doctest test.test_generators.__test__.syntax[2]>", line 3
+SyntaxError: 'return' with argument inside generator
 
 These are fine:
 
@@ -878,7 +881,9 @@
 ...     if 0:
 ...         yield 2             # because it's a generator (line 10)
 Traceback (most recent call last):
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[24]>, line 10)
+  ...
+  File "<doctest test.test_generators.__test__.syntax[24]>", line 10
+SyntaxError: 'return' with argument inside generator
 
 This one caused a crash (see SF bug 567538):
 
@@ -1496,6 +1501,10 @@
 """
 
 coroutine_tests = """\
+A helper function to call gc.collect() without printing
+>>> import gc
+>>> def gc_collect(): gc.collect()
+
 Sending a value into a started generator:
 
 >>> def f():
@@ -1570,13 +1579,14 @@
 >>> def f(): return lambda x=(yield): 1
 Traceback (most recent call last):
   ...
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.coroutine[22]>, line 1)
+  File "<doctest test.test_generators.__test__.coroutine[22]>", line 1
+SyntaxError: 'return' with argument inside generator
 
 >>> def f(): x = yield = y
 Traceback (most recent call last):
   ...
   File "<doctest test.test_generators.__test__.coroutine[23]>", line 1
-SyntaxError: assignment to yield expression not possible
+SyntaxError: can't assign to yield expression
 
 >>> def f(): (yield bar) = y
 Traceback (most recent call last):
@@ -1665,7 +1675,7 @@
 >>> f().throw("abc")     # throw on just-opened generator
 Traceback (most recent call last):
   ...
-TypeError: exceptions must be classes, or instances, not str
+TypeError: exceptions must be old-style classes or derived from BaseException, not str
 
 Now let's try closing a generator:
 
@@ -1697,7 +1707,7 @@
 
 >>> g = f()
 >>> g.next()
->>> del g
+>>> del g; gc_collect()
 exiting
 
 >>> class context(object):
@@ -1708,7 +1718,7 @@
 ...          yield
 >>> g = f()
 >>> g.next()
->>> del g
+>>> del g; gc_collect()
 exiting
 
 
@@ -1721,7 +1731,7 @@
 
 >>> g = f()
 >>> g.next()
->>> del g
+>>> del g; gc_collect()
 finally
 
 
@@ -1747,6 +1757,7 @@
 >>> g = f()
 >>> g.next()
 >>> del g
+>>> gc_collect()
 >>> sys.stderr.getvalue().startswith(
 ...     "Exception RuntimeError: 'generator ignored GeneratorExit' in "
 ... )
@@ -1812,6 +1823,9 @@
 references. We add it to the standard suite so the routine refleak-tests
 would trigger if it starts being uncleanable again.
 
+>>> import gc
+>>> def gc_collect(): gc.collect()
+
 >>> import itertools
 >>> def leak():
 ...     class gen:
@@ -1863,9 +1877,10 @@
 ...
 ...     l = Leaker()
 ...     del l
+...     gc_collect()
 ...     err = sys.stderr.getvalue().strip()
 ...     err.startswith(
-...         "Exception RuntimeError: RuntimeError() in <"
+...         "Exception RuntimeError: RuntimeError() in "
 ...     )
 ...     err.endswith("> ignored")
 ...     len(err.splitlines())
diff --git a/lib-python/2.7/test/test_genexps.py b/lib-python/2.7/test/test_genexps.py
--- a/lib-python/2.7/test/test_genexps.py
+++ b/lib-python/2.7/test/test_genexps.py
@@ -128,8 +128,9 @@
 
 Verify re-use of tuples (a side benefit of using genexps over listcomps)
 
+    >>> from test.test_support import check_impl_detail
     >>> tupleids = map(id, ((i,i) for i in xrange(10)))
-    >>> int(max(tupleids) - min(tupleids))
+    >>> int(max(tupleids) - min(tupleids)) if check_impl_detail() else 0
     0
 
 Verify that syntax error's are raised for genexps used as lvalues
@@ -198,13 +199,13 @@
     >>> g = (10 // i for i in (5, 0, 2))
     >>> g.next()
     2
-    >>> g.next()
+    >>> g.next()       # doctest: +ELLIPSIS
     Traceback (most recent call last):
       File "<pyshell#37>", line 1, in -toplevel-
         g.next()
       File "<pyshell#35>", line 1, in <generator expression>
         g = (10 // i for i in (5, 0, 2))
-    ZeroDivisionError: integer division or modulo by zero
+    ZeroDivisionError: integer division...by zero
     >>> g.next()
     Traceback (most recent call last):
       File "<pyshell#38>", line 1, in -toplevel-
diff --git a/lib-python/2.7/test/test_heapq.py b/lib-python/2.7/test/test_heapq.py
--- a/lib-python/2.7/test/test_heapq.py
+++ b/lib-python/2.7/test/test_heapq.py
@@ -215,6 +215,11 @@
 class TestHeapPython(TestHeap):
     module = py_heapq
 
+    def test_islice_protection(self):
+        m = self.module
+        self.assertFalse(m.nsmallest(-1, [1]))
+        self.assertFalse(m.nlargest(-1, [1]))
+
 
 @skipUnless(c_heapq, 'requires _heapq')
 class TestHeapC(TestHeap):
diff --git a/lib-python/2.7/test/test_import.py b/lib-python/2.7/test/test_import.py
--- a/lib-python/2.7/test/test_import.py
+++ b/lib-python/2.7/test/test_import.py
@@ -7,7 +7,8 @@
 import sys
 import unittest
 from test.test_support import (unlink, TESTFN, unload, run_unittest, rmtree,
-                               is_jython, check_warnings, EnvironmentVarGuard)
+                               is_jython, check_warnings, EnvironmentVarGuard,
+                               impl_detail, check_impl_detail)
 import textwrap
 from test import script_helper
 
@@ -69,7 +70,8 @@
                 self.assertEqual(mod.b, b,
                     "module loaded (%s) but contents invalid" % mod)
             finally:
-                unlink(source)
+                if check_impl_detail(pypy=False):
+                    unlink(source)
 
             try:
                 imp.reload(mod)
@@ -149,13 +151,16 @@
         # Compile & remove .py file, we only need .pyc (or .pyo).
         with open(filename, 'r') as f:
             py_compile.compile(filename)
-        unlink(filename)
+        if check_impl_detail(pypy=False):
+            # pypy refuses to import a .pyc if the .py does not exist
+            unlink(filename)
 
         # Need to be able to load from current dir.
         sys.path.append('')
 
         # This used to crash.
         exec 'import ' + module
+        reload(longlist)
 
         # Cleanup.
         del sys.path[-1]
@@ -326,6 +331,7 @@
         self.assertEqual(mod.code_filename, self.file_name)
         self.assertEqual(mod.func_filename, self.file_name)
 
+    @impl_detail("pypy refuses to import without a .py source", pypy=False)
     def test_module_without_source(self):
         target = "another_module.py"
         py_compile.compile(self.file_name, dfile=target)
diff --git a/lib-python/2.7/test/test_inspect.py b/lib-python/2.7/test/test_inspect.py
--- a/lib-python/2.7/test/test_inspect.py
+++ b/lib-python/2.7/test/test_inspect.py
@@ -4,11 +4,11 @@
 import unittest
 import inspect
 import linecache
-import datetime
 from UserList import UserList
 from UserDict import UserDict
 
 from test.test_support import run_unittest, check_py3k_warnings
+from test.test_support import check_impl_detail
 
 with check_py3k_warnings(
         ("tuple parameter unpacking has been removed", SyntaxWarning),
@@ -74,7 +74,8 @@
 
     def test_excluding_predicates(self):
         self.istest(inspect.isbuiltin, 'sys.exit')
-        self.istest(inspect.isbuiltin, '[].append')
+        if check_impl_detail():
+            self.istest(inspect.isbuiltin, '[].append')
         self.istest(inspect.iscode, 'mod.spam.func_code')
         self.istest(inspect.isframe, 'tb.tb_frame')
         self.istest(inspect.isfunction, 'mod.spam')
@@ -92,9 +93,9 @@
         else:
             self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
         if hasattr(types, 'MemberDescriptorType'):
-            self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
+            self.istest(inspect.ismemberdescriptor, 'type(lambda: None).func_globals')
         else:
-            self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
+            self.assertFalse(inspect.ismemberdescriptor(type(lambda: None).func_globals))
 
     def test_isroutine(self):
         self.assertTrue(inspect.isroutine(mod.spam))
@@ -567,7 +568,8 @@
         else:
             self.fail('Exception not raised')
         self.assertIs(type(ex1), type(ex2))
-        self.assertEqual(str(ex1), str(ex2))
+        if check_impl_detail():
+            self.assertEqual(str(ex1), str(ex2))
 
     def makeCallable(self, signature):
         """Create a function that returns its locals(), excluding the
diff --git a/lib-python/2.7/test/test_int.py b/lib-python/2.7/test/test_int.py
--- a/lib-python/2.7/test/test_int.py
+++ b/lib-python/2.7/test/test_int.py
@@ -1,7 +1,7 @@
 import sys
 
 import unittest
-from test.test_support import run_unittest, have_unicode
+from test.test_support import run_unittest, have_unicode, check_impl_detail
 import math
 
 L = [
@@ -392,9 +392,10 @@
                 try:
                     int(TruncReturnsNonIntegral())
                 except TypeError as e:
-                    self.assertEqual(str(e),
-                                      "__trunc__ returned non-Integral"
-                                      " (type NonIntegral)")
+                    if check_impl_detail(cpython=True):
+                        self.assertEqual(str(e),
+                                          "__trunc__ returned non-Integral"
+                                          " (type NonIntegral)")
                 else:
                     self.fail("Failed to raise TypeError with %s" %
                               ((base, trunc_result_base),))
diff --git a/lib-python/2.7/test/test_io.py b/lib-python/2.7/test/test_io.py
--- a/lib-python/2.7/test/test_io.py
+++ b/lib-python/2.7/test/test_io.py
@@ -2561,6 +2561,31 @@
         """Check that a partial write, when it gets interrupted, properly
         invokes the signal handler, and bubbles up the exception raised
         in the latter."""
+
+        # XXX This test has three flaws that appear when objects are
+        # XXX not reference counted.
+
+        # - if wio.write() happens to trigger a garbage collection,
+        #   the signal exception may be raised when some __del__
+        #   method is running; it will not reach the assertRaises()
+        #   call.
+
+        # - more subtle, if the wio object is not destroyed at once
+        #   and survives this function, the next opened file is likely
+        #   to have the same fileno (since the file descriptor was
+        #   actively closed).  When wio.__del__ is finally called, it
+        #   will close the other's test file...  To trigger this with
+        #   CPython, try adding "global wio" in this function.
+
+        # - This happens only for streams created by the _pyio module,
+        #   because a wio.close() that fails still consider that the
+        #   file needs to be closed again.  You can try adding an
+        #   "assert wio.closed" at the end of the function.
+
+        # Fortunately, a little gc.gollect() seems to be enough to
+        # work around all these issues.
+        support.gc_collect()
+
         read_results = []
         def _read():
             s = os.read(r, 1)
diff --git a/lib-python/2.7/test/test_isinstance.py b/lib-python/2.7/test/test_isinstance.py
--- a/lib-python/2.7/test/test_isinstance.py
+++ b/lib-python/2.7/test/test_isinstance.py
@@ -260,7 +260,18 @@
     # Make sure that calling isinstance with a deeply nested tuple for its
     # argument will raise RuntimeError eventually.
     tuple_arg = (compare_to,)
-    for cnt in xrange(sys.getrecursionlimit()+5):
+
+
+    if test_support.check_impl_detail(cpython=True):
+        RECURSION_LIMIT = sys.getrecursionlimit()
+    else:
+        # on non-CPython implementations, the maximum
+        # actual recursion limit might be higher, but
+        # probably not higher than 99999
+        #
+        RECURSION_LIMIT = 99999
+
+    for cnt in xrange(RECURSION_LIMIT+5):
         tuple_arg = (tuple_arg,)
         fxn(arg, tuple_arg)
 
diff --git a/lib-python/2.7/test/test_itertools.py b/lib-python/2.7/test/test_itertools.py
--- a/lib-python/2.7/test/test_itertools.py
+++ b/lib-python/2.7/test/test_itertools.py
@@ -137,6 +137,8 @@
                 self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
                 self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
 
+    @test_support.impl_detail("tuple reuse is specific to CPython")
+    def test_combinations_tuple_reuse(self):
         # Test implementation detail:  tuple re-use
         self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1)
         self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1)
@@ -207,7 +209,10 @@
                 self.assertEqual(result, list(cwr1(values, r)))         # matches first pure python version
                 self.assertEqual(result, list(cwr2(values, r)))         # matches second pure python version
 
+    @test_support.impl_detail("tuple reuse is specific to CPython")
+    def test_combinations_with_replacement_tuple_reuse(self):
         # Test implementation detail:  tuple re-use
+        cwr = combinations_with_replacement
         self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1)
         self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1)
 
@@ -271,6 +276,8 @@
                     self.assertEqual(result, list(permutations(values, None))) # test r as None
                     self.assertEqual(result, list(permutations(values)))       # test default r
 
+    @test_support.impl_detail("tuple reuse is specific to CPython")
+    def test_permutations_tuple_reuse(self):
         # Test implementation detail:  tuple re-use
         self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
         self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1)
@@ -526,6 +533,9 @@
         self.assertEqual(list(izip()), zip())
         self.assertRaises(TypeError, izip, 3)
         self.assertRaises(TypeError, izip, range(3), 3)
+
+    @test_support.impl_detail("tuple reuse is specific to CPython")
+    def test_izip_tuple_reuse(self):
         # Check tuple re-use (implementation detail)
         self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')],
                          zip('abc', 'def'))
@@ -575,6 +585,8 @@
             else:
                 self.fail('Did not raise Type in:  ' + stmt)
 
+    @test_support.impl_detail("tuple reuse is specific to CPython")
+    def test_iziplongest_tuple_reuse(self):
         # Check tuple re-use (implementation detail)
         self.assertEqual([tuple(list(pair)) for pair in izip_longest('abc', 'def')],
                          zip('abc', 'def'))
@@ -683,6 +695,8 @@
             args = map(iter, args)
             self.assertEqual(len(list(product(*args))), expected_len)
 
+    @test_support.impl_detail("tuple reuse is specific to CPython")
+    def test_product_tuple_reuse(self):
         # Test implementation detail:  tuple re-use
         self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
         self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
@@ -771,11 +785,11 @@
         self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1)
         self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1)
         self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0)
-        self.assertRaises(ValueError, islice, xrange(10), 'a')
-        self.assertRaises(ValueError, islice, xrange(10), 'a', 1)
-        self.assertRaises(ValueError, islice, xrange(10), 1, 'a')
-        self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1)
-        self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1)
+        self.assertRaises((ValueError, TypeError), islice, xrange(10), 'a')
+        self.assertRaises((ValueError, TypeError), islice, xrange(10), 'a', 1)
+        self.assertRaises((ValueError, TypeError), islice, xrange(10), 1, 'a')
+        self.assertRaises((ValueError, TypeError), islice, xrange(10), 'a', 1, 1)
+        self.assertRaises((ValueError, TypeError), islice, xrange(10), 1, 'a', 1)
         self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
 
         # Issue #10323:  Less islice in a predictable state
@@ -855,9 +869,17 @@
         self.assertRaises(TypeError, tee, [1,2], 3, 'x')
 
         # tee object should be instantiable
-        a, b = tee('abc')
-        c = type(a)('def')
-        self.assertEqual(list(c), list('def'))
+        if test_support.check_impl_detail():
+            # XXX I (arigo) would argue that 'type(a)(iterable)' has
+            # ill-defined semantics: it always return a fresh tee object,
+            # but depending on whether 'iterable' is itself a tee object
+            # or not, it is ok or not to continue using 'iterable' after
+            # the call.  I cannot imagine why 'type(a)(non_tee_object)'
+            # would be useful, as 'iter(non_tee_obect)' is equivalent
+            # as far as I can see.
+            a, b = tee('abc')
+            c = type(a)('def')
+            self.assertEqual(list(c), list('def'))
 
         # test long-lagged and multi-way split
         a, b, c = tee(xrange(2000), 3)
@@ -895,6 +917,7 @@
         p = proxy(a)
         self.assertEqual(getattr(p, '__class__'), type(b))
         del a
+        test_support.gc_collect()
         self.assertRaises(ReferenceError, getattr, p, '__class__')
 
     def test_StopIteration(self):
@@ -1317,6 +1340,7 @@
 
 class LengthTransparency(unittest.TestCase):
 
+    @test_support.impl_detail("__length_hint__() API is undocumented")
     def test_repeat(self):
         from test.test_iterlen import len
         self.assertEqual(len(repeat(None, 50)), 50)
diff --git a/lib-python/2.7/test/test_linecache.py b/lib-python/2.7/test/test_linecache.py
--- a/lib-python/2.7/test/test_linecache.py
+++ b/lib-python/2.7/test/test_linecache.py
@@ -54,13 +54,13 @@
 
         # Check whether lines correspond to those from file iteration
         for entry in TESTS:
-            filename = os.path.join(TEST_PATH, entry) + '.py'
+            filename = support.findfile( entry + '.py')
             for index, line in enumerate(open(filename)):
                 self.assertEqual(line, getline(filename, index + 1))
 
         # Check module loading
         for entry in MODULES:
-            filename = os.path.join(MODULE_PATH, entry) + '.py'
+            filename = support.findfile( entry + '.py')
             for index, line in enumerate(open(filename)):
                 self.assertEqual(line, getline(filename, index + 1))
 
@@ -78,7 +78,7 @@
     def test_clearcache(self):
         cached = []
         for entry in TESTS:
-            filename = os.path.join(TEST_PATH, entry) + '.py'
+            filename = support.findfile( entry + '.py')
             cached.append(filename)
             linecache.getline(filename, 1)
 
diff --git a/lib-python/2.7/test/test_list.py b/lib-python/2.7/test/test_list.py
--- a/lib-python/2.7/test/test_list.py
+++ b/lib-python/2.7/test/test_list.py
@@ -15,6 +15,10 @@
         self.assertEqual(list(''), [])
         self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
 
+    # the following test also works with pypy, but eats all your address
+    # space's RAM before raising and takes too long.
+    @test_support.impl_detail("eats all your RAM before working", pypy=False)
+    def test_segfault_1(self):
         if sys.maxsize == 0x7fffffff:
             # This test can currently only work on 32-bit machines.
             # XXX If/when PySequence_Length() returns a ssize_t, it should be
@@ -32,6 +36,7 @@
             #     http://sources.redhat.com/ml/newlib/2002/msg00369.html
             self.assertRaises(MemoryError, list, xrange(sys.maxint // 2))
 
+    def test_segfault_2(self):
         # This code used to segfault in Py2.4a3
         x = []
         x.extend(-y for y in x)
diff --git a/lib-python/2.7/test/test_long.py b/lib-python/2.7/test/test_long.py
--- a/lib-python/2.7/test/test_long.py
+++ b/lib-python/2.7/test/test_long.py
@@ -530,9 +530,10 @@
                 try:
                     long(TruncReturnsNonIntegral())
                 except TypeError as e:
-                    self.assertEqual(str(e),
-                                     "__trunc__ returned non-Integral"
-                                     " (type NonIntegral)")
+                    if test_support.check_impl_detail(cpython=True):
+                        self.assertEqual(str(e),
+                                          "__trunc__ returned non-Integral"
+                                          " (type NonIntegral)")
                 else:
                     self.fail("Failed to raise TypeError with %s" %
                               ((base, trunc_result_base),))
diff --git a/lib-python/2.7/test/test_marshal.py b/lib-python/2.7/test/test_marshal.py
--- a/lib-python/2.7/test/test_marshal.py
+++ b/lib-python/2.7/test/test_marshal.py
@@ -7,20 +7,31 @@
 import unittest
 import os
 
-class IntTestCase(unittest.TestCase):
+class HelperMixin:
+    def helper(self, sample, *extra, **kwargs):
+        expected = kwargs.get('expected', sample)
+        new = marshal.loads(marshal.dumps(sample, *extra))
+        self.assertEqual(expected, new)
+        self.assertEqual(type(expected), type(new))
+        try:
+            with open(test_support.TESTFN, "wb") as f:
+                marshal.dump(sample, f, *extra)
+            with open(test_support.TESTFN, "rb") as f:
+                new = marshal.load(f)
+            self.assertEqual(expected, new)
+            self.assertEqual(type(expected), type(new))
+        finally:
+            test_support.unlink(test_support.TESTFN)
+
+
+class IntTestCase(unittest.TestCase, HelperMixin):
     def test_ints(self):
         # Test the full range of Python ints.
         n = sys.maxint
         while n:
             for expected in (-n, n):
-                s = marshal.dumps(expected)
-                got = marshal.loads(s)
-                self.assertEqual(expected, got)
-                marshal.dump(expected, file(test_support.TESTFN, "wb"))
-                got = marshal.load(file(test_support.TESTFN, "rb"))
-                self.assertEqual(expected, got)
+                self.helper(expected)
             n = n >> 1
-        os.unlink(test_support.TESTFN)
 
     def test_int64(self):
         # Simulate int marshaling on a 64-bit box.  This is most interesting if
@@ -48,28 +59,16 @@
 
     def test_bool(self):
         for b in (True, False):
-            new = marshal.loads(marshal.dumps(b))
-            self.assertEqual(b, new)
-            self.assertEqual(type(b), type(new))
-            marshal.dump(b, file(test_support.TESTFN, "wb"))
-            new = marshal.load(file(test_support.TESTFN, "rb"))
-            self.assertEqual(b, new)
-            self.assertEqual(type(b), type(new))
+            self.helper(b)
 
-class FloatTestCase(unittest.TestCase):
+class FloatTestCase(unittest.TestCase, HelperMixin):
     def test_floats(self):
         # Test a few floats
         small = 1e-25
         n = sys.maxint * 3.7e250
         while n > small:
             for expected in (-n, n):
-                f = float(expected)
-                s = marshal.dumps(f)
-                got = marshal.loads(s)
-                self.assertEqual(f, got)
-                marshal.dump(f, file(test_support.TESTFN, "wb"))
-                got = marshal.load(file(test_support.TESTFN, "rb"))
-                self.assertEqual(f, got)
+                self.helper(expected)
             n /= 123.4567
 
         f = 0.0
@@ -85,59 +84,25 @@
         while n < small:
             for expected in (-n, n):
                 f = float(expected)
+                self.helper(f)
+                self.helper(f, 1)
+            n *= 123.4567
 
-                s = marshal.dumps(f)
-                got = marshal.loads(s)
-                self.assertEqual(f, got)
-
-                s = marshal.dumps(f, 1)
-                got = marshal.loads(s)
-                self.assertEqual(f, got)
-
-                marshal.dump(f, file(test_support.TESTFN, "wb"))
-                got = marshal.load(file(test_support.TESTFN, "rb"))
-                self.assertEqual(f, got)
-
-                marshal.dump(f, file(test_support.TESTFN, "wb"), 1)
-                got = marshal.load(file(test_support.TESTFN, "rb"))
-                self.assertEqual(f, got)
-            n *= 123.4567
-        os.unlink(test_support.TESTFN)
-
-class StringTestCase(unittest.TestCase):
+class StringTestCase(unittest.TestCase, HelperMixin):
     def test_unicode(self):
         for s in [u"", u"Andr&#65533; Previn", u"abc", u" "*10000]:
-            new = marshal.loads(marshal.dumps(s))
-            self.assertEqual(s, new)
-            self.assertEqual(type(s), type(new))
-            marshal.dump(s, file(test_support.TESTFN, "wb"))
-            new = marshal.load(file(test_support.TESTFN, "rb"))
-            self.assertEqual(s, new)
-            self.assertEqual(type(s), type(new))
-        os.unlink(test_support.TESTFN)
+            self.helper(s)
 
     def test_string(self):
         for s in ["", "Andr&#65533; Previn", "abc", " "*10000]:
-            new = marshal.loads(marshal.dumps(s))
-            self.assertEqual(s, new)
-            self.assertEqual(type(s), type(new))
-            marshal.dump(s, file(test_support.TESTFN, "wb"))
-            new = marshal.load(file(test_support.TESTFN, "rb"))
-            self.assertEqual(s, new)
-            self.assertEqual(type(s), type(new))
-        os.unlink(test_support.TESTFN)
+            self.helper(s)
 
     def test_buffer(self):
         for s in ["", "Andr&#65533; Previn", "abc", " "*10000]:
             with test_support.check_py3k_warnings(("buffer.. not supported",
                                                      DeprecationWarning)):
                 b = buffer(s)
-            new = marshal.loads(marshal.dumps(b))
-            self.assertEqual(s, new)
-            marshal.dump(b, file(test_support.TESTFN, "wb"))
-            new = marshal.load(file(test_support.TESTFN, "rb"))
-            self.assertEqual(s, new)
-        os.unlink(test_support.TESTFN)
+            self.helper(b, expected=s)
 
 class ExceptionTestCase(unittest.TestCase):
     def test_exceptions(self):
@@ -150,7 +115,7 @@
         new = marshal.loads(marshal.dumps(co))
         self.assertEqual(co, new)
 
-class ContainerTestCase(unittest.TestCase):
+class ContainerTestCase(unittest.TestCase, HelperMixin):
     d = {'astring': 'foo at bar.baz.spam',
          'afloat': 7283.43,
          'anint': 2**20,
@@ -161,42 +126,20 @@
          'aunicode': u"Andr&#65533; Previn"
          }
     def test_dict(self):
-        new = marshal.loads(marshal.dumps(self.d))
-        self.assertEqual(self.d, new)
-        marshal.dump(self.d, file(test_support.TESTFN, "wb"))
-        new = marshal.load(file(test_support.TESTFN, "rb"))
-        self.assertEqual(self.d, new)
-        os.unlink(test_support.TESTFN)
+        self.helper(self.d)
 
     def test_list(self):
         lst = self.d.items()
-        new = marshal.loads(marshal.dumps(lst))
-        self.assertEqual(lst, new)
-        marshal.dump(lst, file(test_support.TESTFN, "wb"))
-        new = marshal.load(file(test_support.TESTFN, "rb"))
-        self.assertEqual(lst, new)
-        os.unlink(test_support.TESTFN)
+        self.helper(lst)
 
     def test_tuple(self):
         t = tuple(self.d.keys())
-        new = marshal.loads(marshal.dumps(t))
-        self.assertEqual(t, new)
-        marshal.dump(t, file(test_support.TESTFN, "wb"))
-        new = marshal.load(file(test_support.TESTFN, "rb"))
-        self.assertEqual(t, new)
-        os.unlink(test_support.TESTFN)
+        self.helper(t)
 
     def test_sets(self):
         for constructor in (set, frozenset):
             t = constructor(self.d.keys())
-            new = marshal.loads(marshal.dumps(t))
-            self.assertEqual(t, new)
-            self.assertTrue(isinstance(new, constructor))
-            self.assertNotEqual(id(t), id(new))
-            marshal.dump(t, file(test_support.TESTFN, "wb"))
-            new = marshal.load(file(test_support.TESTFN, "rb"))
-            self.assertEqual(t, new)
-            os.unlink(test_support.TESTFN)
+            self.helper(t)
 
 class BugsTestCase(unittest.TestCase):
     def test_bug_5888452(self):
@@ -226,6 +169,7 @@
         s = 'c' + ('X' * 4*4) + '{' * 2**20
         self.assertRaises(ValueError, marshal.loads, s)
 
+    @test_support.impl_detail('specific recursion check')
     def test_recursion_limit(self):
         # Create a deeply nested structure.
         head = last = []
diff --git a/lib-python/2.7/test/test_memoryio.py b/lib-python/2.7/test/test_memoryio.py
--- a/lib-python/2.7/test/test_memoryio.py
+++ b/lib-python/2.7/test/test_memoryio.py
@@ -617,7 +617,7 @@
         state = memio.__getstate__()
         self.assertEqual(len(state), 3)
         bytearray(state[0]) # Check if state[0] supports the buffer interface.
-        self.assertIsInstance(state[1], int)
+        self.assertIsInstance(state[1], (int, long))
         self.assertTrue(isinstance(state[2], dict) or state[2] is None)
         memio.close()
         self.assertRaises(ValueError, memio.__getstate__)
diff --git a/lib-python/2.7/test/test_memoryview.py b/lib-python/2.7/test/test_memoryview.py
--- a/lib-python/2.7/test/test_memoryview.py
+++ b/lib-python/2.7/test/test_memoryview.py
@@ -26,7 +26,8 @@
     def check_getitem_with_type(self, tp):
         item = self.getitem_type
         b = tp(self._source)
-        oldrefcount = sys.getrefcount(b)
+        if hasattr(sys, 'getrefcount'):
+            oldrefcount = sys.getrefcount(b)
         m = self._view(b)
         self.assertEqual(m[0], item(b"a"))
         self.assertIsInstance(m[0], bytes)
@@ -43,7 +44,8 @@
         self.assertRaises(TypeError, lambda: m[0.0])
         self.assertRaises(TypeError, lambda: m["a"])
         m = None
-        self.assertEqual(sys.getrefcount(b), oldrefcount)
+        if hasattr(sys, 'getrefcount'):
+            self.assertEqual(sys.getrefcount(b), oldrefcount)
 
     def test_getitem(self):
         for tp in self._types:
@@ -65,7 +67,8 @@
         if not self.ro_type:
             return
         b = self.ro_type(self._source)
-        oldrefcount = sys.getrefcount(b)
+        if hasattr(sys, 'getrefcount'):
+            oldrefcount = sys.getrefcount(b)
         m = self._view(b)
         def setitem(value):
             m[0] = value
@@ -73,14 +76,16 @@
         self.assertRaises(TypeError, setitem, 65)
         self.assertRaises(TypeError, setitem, memoryview(b"a"))
         m = None
-        self.assertEqual(sys.getrefcount(b), oldrefcount)
+        if hasattr(sys, 'getrefcount'):
+            self.assertEqual(sys.getrefcount(b), oldrefcount)
 
     def test_setitem_writable(self):
         if not self.rw_type:
             return
         tp = self.rw_type
         b = self.rw_type(self._source)
-        oldrefcount = sys.getrefcount(b)
+        if hasattr(sys, 'getrefcount'):
+            oldrefcount = sys.getrefcount(b)
         m = self._view(b)
         m[0] = tp(b"0")
         self._check_contents(tp, b, b"0bcdef")
@@ -110,13 +115,14 @@
         self.assertRaises(TypeError, setitem, (0,), b"a")
         self.assertRaises(TypeError, setitem, "a", b"a")
         # Trying to resize the memory object
-        self.assertRaises(ValueError, setitem, 0, b"")
-        self.assertRaises(ValueError, setitem, 0, b"ab")
+        self.assertRaises((ValueError, TypeError), setitem, 0, b"")
+        self.assertRaises((ValueError, TypeError), setitem, 0, b"ab")
         self.assertRaises(ValueError, setitem, slice(1,1), b"a")
         self.assertRaises(ValueError, setitem, slice(0,2), b"a")
 
         m = None
-        self.assertEqual(sys.getrefcount(b), oldrefcount)
+        if hasattr(sys, 'getrefcount'):
+            self.assertEqual(sys.getrefcount(b), oldrefcount)
 
     def test_delitem(self):
         for tp in self._types:
@@ -292,6 +298,7 @@
     def _check_contents(self, tp, obj, contents):
         self.assertEqual(obj[1:7], tp(contents))
 
+    @unittest.skipUnless(hasattr(sys, 'getrefcount'), "Reference counting")
     def test_refs(self):
         for tp in self._types:
             m = memoryview(tp(self._source))
diff --git a/lib-python/2.7/test/test_mmap.py b/lib-python/2.7/test/test_mmap.py
--- a/lib-python/2.7/test/test_mmap.py
+++ b/lib-python/2.7/test/test_mmap.py
@@ -119,7 +119,8 @@
     def test_access_parameter(self):
         # Test for "access" keyword parameter
         mapsize = 10
-        open(TESTFN, "wb").write("a"*mapsize)
+        with open(TESTFN, "wb") as f:
+            f.write("a"*mapsize)
         f = open(TESTFN, "rb")
         m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
         self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")
@@ -168,9 +169,11 @@
         else:
             self.fail("Able to resize readonly memory map")
         f.close()
+        m.close()
         del m, f
-        self.assertEqual(open(TESTFN, "rb").read(), 'a'*mapsize,
-               "Readonly memory map data file was modified")
+        with open(TESTFN, "rb") as f:
+            self.assertEqual(f.read(), 'a'*mapsize,
+                "Readonly memory map data file was modified")
 
         # Opening mmap with size too big
         import sys
@@ -220,11 +223,13 @@
         self.assertEqual(m[:], 'd' * mapsize,
                "Copy-on-write memory map data not written correctly.")
         m.flush()
-        self.assertEqual(open(TESTFN, "rb").read(), 'c'*mapsize,
-               "Copy-on-write test data file should not be modified.")
+        f.close()
+        with open(TESTFN, "rb") as f:
+            self.assertEqual(f.read(), 'c'*mapsize,
+                "Copy-on-write test data file should not be modified.")
         # Ensuring copy-on-write maps cannot be resized
         self.assertRaises(TypeError, m.resize, 2*mapsize)
-        f.close()
+        m.close()
         del m, f
 
         # Ensuring invalid access parameter raises exception
@@ -287,6 +292,7 @@
         self.assertEqual(m.find('one', 1), 8)
         self.assertEqual(m.find('one', 1, -1), 8)
         self.assertEqual(m.find('one', 1, -2), -1)
+        m.close()
 
 
     def test_rfind(self):
@@ -305,6 +311,7 @@
         self.assertEqual(m.rfind('one', 0, -2), 0)
         self.assertEqual(m.rfind('one', 1, -1), 8)
         self.assertEqual(m.rfind('one', 1, -2), -1)
+        m.close()
 
 
     def test_double_close(self):
@@ -533,7 +540,8 @@
         if not hasattr(mmap, 'PROT_READ'):
             return
         mapsize = 10
-        open(TESTFN, "wb").write("a"*mapsize)
+        with open(TESTFN, "wb") as f:
+            f.write("a"*mapsize)
         f = open(TESTFN, "rb")
         m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
         self.assertRaises(TypeError, m.write, "foo")
@@ -545,7 +553,8 @@
 
     def test_io_methods(self):
         data = "0123456789"
-        open(TESTFN, "wb").write("x"*len(data))
+        with open(TESTFN, "wb") as f:
+            f.write("x"*len(data))
         f = open(TESTFN, "r+b")
         m = mmap.mmap(f.fileno(), len(data))
         f.close()
@@ -574,6 +583,7 @@
         self.assertEqual(m[:], "012bar6789")
         m.seek(8)
         self.assertRaises(ValueError, m.write, "bar")
+        m.close()
 
     if os.name == 'nt':
         def test_tagname(self):
@@ -611,7 +621,8 @@
             m.close()
 
             # Should not crash (Issue 5385)
-            open(TESTFN, "wb").write("x"*10)
+            with open(TESTFN, "wb") as f:
+                f.write("x"*10)
             f = open(TESTFN, "r+b")
             m = mmap.mmap(f.fileno(), 0)
             f.close()
diff --git a/lib-python/2.7/test/test_module.py b/lib-python/2.7/test/test_module.py
--- a/lib-python/2.7/test/test_module.py
+++ b/lib-python/2.7/test/test_module.py
@@ -1,6 +1,6 @@
 # Test the module type
 import unittest
-from test.test_support import run_unittest, gc_collect
+from test.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/2.7/test/test_multibytecodec.py b/lib-python/2.7/test/test_multibytecodec.py
--- a/lib-python/2.7/test/test_multibytecodec.py
+++ b/lib-python/2.7/test/test_multibytecodec.py
@@ -42,7 +42,7 @@
         dec = codecs.getdecoder('euc-kr')
         myreplace  = lambda exc: (u'', sys.maxint+1)
         codecs.register_error('test.cjktest', myreplace)
-        self.assertRaises(IndexError, dec,
+        self.assertRaises((IndexError, OverflowError), dec,
                           'apple\x92ham\x93spam', 'test.cjktest')
 
     def test_codingspec(self):
@@ -148,7 +148,8 @@
 class Test_StreamReader(unittest.TestCase):
     def test_bug1728403(self):
         try:
-            open(TESTFN, 'w').write('\xa1')
+            with open(TESTFN, 'w') as f:
+                f.write('\xa1')
             f = codecs.open(TESTFN, encoding='cp949')
             self.assertRaises(UnicodeDecodeError, f.read, 2)
         finally:
diff --git a/lib-python/2.7/test/test_multibytecodec_support.py b/lib-python/2.7/test/test_multibytecodec_support.py
--- a/lib-python/2.7/test/test_multibytecodec_support.py
+++ b/lib-python/2.7/test/test_multibytecodec_support.py
@@ -110,8 +110,8 @@
         def myreplace(exc):
             return (u'x', sys.maxint + 1)
         codecs.register_error("test.cjktest", myreplace)
-        self.assertRaises(IndexError, self.encode, self.unmappedunicode,
-                          'test.cjktest')
+        self.assertRaises((IndexError, OverflowError), self.encode,
+                          self.unmappedunicode, 'test.cjktest')
 
     def test_callback_None_index(self):
         def myreplace(exc):
@@ -330,7 +330,7 @@
                             repr(csetch), repr(unich), exc.reason))
 
 def load_teststring(name):
-    dir = os.path.join(os.path.dirname(__file__), 'cjkencodings')
+    dir = test_support.findfile('cjkencodings')
     with open(os.path.join(dir, name + '.txt'), 'rb') as f:
         encoded = f.read()
     with open(os.path.join(dir, name + '-utf8.txt'), 'rb') as f:
diff --git a/lib-python/2.7/test/test_multiprocessing.py b/lib-python/2.7/test/test_multiprocessing.py
--- a/lib-python/2.7/test/test_multiprocessing.py
+++ b/lib-python/2.7/test/test_multiprocessing.py
@@ -1316,6 +1316,7 @@
         queue = manager.get_queue()
         self.assertEqual(queue.get(), 'hello world')
         del queue
+        test_support.gc_collect()
         manager.shutdown()
         manager = QueueManager(
             address=addr, authkey=authkey, serializer=SERIALIZER)
@@ -1605,6 +1606,10 @@
             if len(blocks) > maxblocks:
                 i = random.randrange(maxblocks)
                 del blocks[i]
+            # XXX There should be a better way to release resources for a
+            # single block
+            if i % maxblocks == 0:
+                import gc; gc.collect()
 
         # get the heap object
         heap = multiprocessing.heap.BufferWrapper._heap
@@ -1704,6 +1709,7 @@
         a = Foo()
         util.Finalize(a, conn.send, args=('a',))
         del a           # triggers callback for a
+        test_support.gc_collect()
 
         b = Foo()
         close_b = util.Finalize(b, conn.send, args=('b',))
diff --git a/lib-python/2.7/test/test_mutants.py b/lib-python/2.7/test/test_mutants.py
--- a/lib-python/2.7/test/test_mutants.py
+++ b/lib-python/2.7/test/test_mutants.py
@@ -1,4 +1,4 @@
-from test.test_support import verbose, TESTFN
+from test.test_support import verbose, TESTFN, check_impl_detail
 import random
 import os
 
@@ -137,10 +137,16 @@
     while dict1 and len(dict1) == len(dict2):
         if verbose:
             print ".",
-        if random.random() < 0.5:
-            c = cmp(dict1, dict2)
-        else:
-            c = dict1 == dict2
+        try:
+            if random.random() < 0.5:
+                c = cmp(dict1, dict2)
+            else:
+                c = dict1 == dict2
+        except RuntimeError:
+            # CPython never raises RuntimeError here, but other implementations
+            # might, and it's fine.
+            if check_impl_detail(cpython=True):
+                raise
     if verbose:
         print
 
diff --git a/lib-python/2.7/test/test_optparse.py b/lib-python/2.7/test/test_optparse.py
--- a/lib-python/2.7/test/test_optparse.py
+++ b/lib-python/2.7/test/test_optparse.py
@@ -383,6 +383,7 @@
         self.assertRaises(self.parser.remove_option, ('foo',), None,
                           ValueError, "no such option 'foo'")
 
+    @test_support.impl_detail("sys.getrefcount")
     def test_refleak(self):
         # If an OptionParser is carrying around a reference to a large
         # object, various cycles can prevent it from being GC'd in
diff --git a/lib-python/2.7/test/test_peepholer.py b/lib-python/2.7/test/test_peepholer.py
--- a/lib-python/2.7/test/test_peepholer.py
+++ b/lib-python/2.7/test/test_peepholer.py
@@ -41,7 +41,7 @@
     def test_none_as_constant(self):
         # LOAD_GLOBAL None  -->  LOAD_CONST None
         def f(x):
-            None
+            y = None
             return x
         asm = disassemble(f)
         for elem in ('LOAD_GLOBAL',):
@@ -67,10 +67,13 @@
             self.assertIn(elem, asm)
 
     def test_pack_unpack(self):
+        # On PyPy, "a, b = ..." is even more optimized, by removing
+        # the ROT_TWO.  But the ROT_TWO is not removed if assigning
+        # to more complex expressions, so check that.
         for line, elem in (
             ('a, = a,', 'LOAD_CONST',),
-            ('a, b = a, b', 'ROT_TWO',),
-            ('a, b, c = a, b, c', 'ROT_THREE',),
+            ('a[1], b = a, b', 'ROT_TWO',),
+            ('a, b[2], c = a, b, c', 'ROT_THREE',),
             ):
             asm = dis_single(line)
             self.assertIn(elem, asm)
@@ -78,6 +81,8 @@
             self.assertNotIn('UNPACK_TUPLE', asm)
 
     def test_folding_of_tuples_of_constants(self):
+        # On CPython, "a,b,c=1,2,3" turns into "a,b,c=<constant (1,2,3)>"
+        # but on PyPy, it turns into "a=1;b=2;c=3".
         for line, elem in (
             ('a = 1,2,3', '((1, 2, 3))'),
             ('("a","b","c")', "(('a', 'b', 'c'))"),
@@ -86,7 +91,8 @@
             ('((1, 2), 3, 4)', '(((1, 2), 3, 4))'),
             ):
             asm = dis_single(line)
-            self.assertIn(elem, asm)
+            self.assert_(elem in asm or (
+                line == 'a,b,c = 1,2,3' and 'UNPACK_TUPLE' not in asm))
             self.assertNotIn('BUILD_TUPLE', asm)
 
         # Bug 1053819:  Tuple of constants misidentified when presented with:
diff --git a/lib-python/2.7/test/test_pprint.py b/lib-python/2.7/test/test_pprint.py
--- a/lib-python/2.7/test/test_pprint.py
+++ b/lib-python/2.7/test/test_pprint.py
@@ -233,7 +233,16 @@
                                   frozenset([0, 2]),
                                   frozenset([0, 1])])}"""
         cube = test.test_set.cube(3)
-        self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
+        # XXX issues of dictionary order, and for the case below,
+        # order of items in the frozenset([...]) representation.
+        # Whether we get precisely cube_repr_tgt or not is open
+        # to implementation-dependent choices (this test probably
+        # fails horribly in CPython if we tweak the dict order too).
+        got = pprint.pformat(cube)
+        if test.test_support.check_impl_detail(cpython=True):
+            self.assertEqual(got, cube_repr_tgt)
+        else:
+            self.assertEqual(eval(got), cube)
         cubo_repr_tgt = """\
 {frozenset([frozenset([0, 2]), frozenset([0])]): frozenset([frozenset([frozenset([0,
                                                                                   2]),
@@ -393,7 +402,11 @@
                                                                                         2])])])}"""
 
         cubo = test.test_set.linegraph(cube)
-        self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
+        got = pprint.pformat(cubo)
+        if test.test_support.check_impl_detail(cpython=True):
+            self.assertEqual(got, cubo_repr_tgt)
+        else:
+            self.assertEqual(eval(got), cubo)
 
     def test_depth(self):
         nested_tuple = (1, (2, (3, (4, (5, 6)))))
diff --git a/lib-python/2.7/test/test_pydoc.py b/lib-python/2.7/test/test_pydoc.py
--- a/lib-python/2.7/test/test_pydoc.py
+++ b/lib-python/2.7/test/test_pydoc.py
@@ -267,8 +267,8 @@
         testpairs = (
             ('i_am_not_here', 'i_am_not_here'),
             ('test.i_am_not_here_either', 'i_am_not_here_either'),
-            ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
-            ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
+            ('test.i_am_not_here.neither_am_i', 'i_am_not_here'),
+            ('i_am_not_here.{}'.format(modname), 'i_am_not_here'),
             ('test.{}'.format(modname), modname),
             )
 
@@ -292,8 +292,8 @@
                     result = run_pydoc(modname)
                 finally:
                     forget(modname)
-                expected = badimport_pattern % (modname, expectedinmsg)
-                self.assertEqual(expected, result)
+                expected = badimport_pattern % (modname, '(.+\\.)?' + expectedinmsg + '(\\..+)?$')
+                self.assertTrue(re.match(expected, result))
 
     def test_input_strip(self):
         missing_module = " test.i_am_not_here "
diff --git a/lib-python/2.7/test/test_pyexpat.py b/lib-python/2.7/test/test_pyexpat.py
--- a/lib-python/2.7/test/test_pyexpat.py
+++ b/lib-python/2.7/test/test_pyexpat.py
@@ -570,6 +570,9 @@
         self.assertEqual(self.n, 4)
 
 class MalformedInputText(unittest.TestCase):
+    # CPython seems to ship its own version of expat, they fixed it on this commit : 
+    # http://svn.python.org/view?revision=74429&view=revision
+    @unittest.skipIf(sys.platform == "darwin", "Expat is broken on Mac OS X 10.6.6")
     def test1(self):
         xml = "\0\r\n"
         parser = expat.ParserCreate()
@@ -579,6 +582,7 @@
         except expat.ExpatError as e:
             self.assertEqual(str(e), 'unclosed token: line 2, column 0')
 
+    @unittest.skipIf(sys.platform == "darwin", "Expat is broken on Mac OS X 10.6.6")
     def test2(self):
         xml = "<?xml version\xc2\x85='1.0'?>\r\n"
         parser = expat.ParserCreate()
diff --git a/lib-python/2.7/test/test_repr.py b/lib-python/2.7/test/test_repr.py
--- a/lib-python/2.7/test/test_repr.py
+++ b/lib-python/2.7/test/test_repr.py
@@ -9,6 +9,7 @@
 import unittest
 
 from test.test_support import run_unittest, check_py3k_warnings
+from test.test_support import check_impl_detail
 from repr import repr as r # Don't shadow builtin repr
 from repr import Repr
 
@@ -145,8 +146,11 @@
         # Functions
         eq(repr(hash), '<built-in function hash>')
         # Methods
-        self.assertTrue(repr(''.split).startswith(
-            '<built-in method split of str object at 0x'))
+        if check_impl_detail(cpython=True):
+            self.assertTrue(repr(''.split).startswith(
+                '<built-in method split of str object at 0x'))
+        elif check_impl_detail(pypy=True):
+            eq(repr(''.split), "<bound method str.split of ''>")
 
     def test_xrange(self):
         eq = self.assertEqual
@@ -185,7 +189,10 @@
     def test_descriptors(self):
         eq = self.assertEqual
         # method descriptors
-        eq(repr(dict.items), "<method 'items' of 'dict' objects>")
+        if check_impl_detail(cpython=True):
+            eq(repr(dict.items), "<method 'items' of 'dict' objects>")
+        elif check_impl_detail(pypy=True):
+            eq(repr(dict.items), "<unbound method dict.items>")
         # XXX member descriptors
         # XXX attribute descriptors
         # XXX slot descriptors
@@ -247,8 +254,14 @@
         eq = self.assertEqual
         touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py'))
         from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation
-        eq(repr(areallylongpackageandmodulenametotestreprtruncation),
-           "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
+        # On PyPy, we use %r to format the file name; on CPython it is done
+        # with '%s'.  It seems to me that %r is safer <arigo>.
+        if '__pypy__' in sys.builtin_module_names:
+            eq(repr(areallylongpackageandmodulenametotestreprtruncation),
+               "<module %r from %r>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
+        else:
+            eq(repr(areallylongpackageandmodulenametotestreprtruncation),
+               "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__))
         eq(repr(sys), "<module 'sys' (built-in)>")
 
     def test_type(self):
diff --git a/lib-python/2.7/test/test_runpy.py b/lib-python/2.7/test/test_runpy.py
--- a/lib-python/2.7/test/test_runpy.py
+++ b/lib-python/2.7/test/test_runpy.py
@@ -5,10 +5,15 @@
 import sys
 import re
 import tempfile
-from test.test_support import verbose, run_unittest, forget
+from test.test_support import verbose, run_unittest, forget, check_impl_detail
 from test.script_helper import (temp_dir, make_script, compile_script,
                                 make_pkg, make_zip_script, make_zip_pkg)
 
+if check_impl_detail(pypy=True):
+    no_lone_pyc_file = True
+else:
+    no_lone_pyc_file = False
+
 
 from runpy import _run_code, _run_module_code, run_module, run_path
 # Note: This module can't safely test _run_module_as_main as it
@@ -168,13 +173,14 @@
             self.assertIn("x", d1)
             self.assertTrue(d1["x"] == 1)
             del d1 # Ensure __loader__ entry doesn't keep file open
-            __import__(mod_name)
-            os.remove(mod_fname)
-            if verbose: print "Running from compiled:", mod_name
-            d2 = run_module(mod_name) # Read from bytecode
-            self.assertIn("x", d2)
-            self.assertTrue(d2["x"] == 1)
-            del d2 # Ensure __loader__ entry doesn't keep file open
+            if not no_lone_pyc_file:
+                __import__(mod_name)
+                os.remove(mod_fname)
+                if verbose: print "Running from compiled:", mod_name
+                d2 = run_module(mod_name) # Read from bytecode
+                self.assertIn("x", d2)
+                self.assertTrue(d2["x"] == 1)
+                del d2 # Ensure __loader__ entry doesn't keep file open
         finally:
             self._del_pkg(pkg_dir, depth, mod_name)
         if verbose: print "Module executed successfully"
@@ -190,13 +196,14 @@
             self.assertIn("x", d1)
             self.assertTrue(d1["x"] == 1)
             del d1 # Ensure __loader__ entry doesn't keep file open
-            __import__(mod_name)
-            os.remove(mod_fname)
-            if verbose: print "Running from compiled:", pkg_name
-            d2 = run_module(pkg_name) # Read from bytecode
-            self.assertIn("x", d2)
-            self.assertTrue(d2["x"] == 1)
-            del d2 # Ensure __loader__ entry doesn't keep file open
+            if not no_lone_pyc_file:
+                __import__(mod_name)
+                os.remove(mod_fname)
+                if verbose: print "Running from compiled:", pkg_name
+                d2 = run_module(pkg_name) # Read from bytecode
+                self.assertIn("x", d2)
+                self.assertTrue(d2["x"] == 1)
+                del d2 # Ensure __loader__ entry doesn't keep file open
         finally:
             self._del_pkg(pkg_dir, depth, pkg_name)
         if verbose: print "Package executed successfully"
@@ -244,15 +251,17 @@
             self.assertIn("sibling", d1)
             self.assertIn("nephew", d1)
             del d1 # Ensure __loader__ entry doesn't keep file open
-            __import__(mod_name)
-            os.remove(mod_fname)
-            if verbose: print "Running from compiled:", mod_name
-            d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
-            self.assertIn("__package__", d2)
-            self.assertTrue(d2["__package__"] == pkg_name)
-            self.assertIn("sibling", d2)
-            self.assertIn("nephew", d2)
-            del d2 # Ensure __loader__ entry doesn't keep file open
+            if not no_lone_pyc_file:
+                __import__(mod_name)
+                os.remove(mod_fname)
+                if verbose: print "Running from compiled:", mod_name
+                # Read from bytecode
+                d2 = run_module(mod_name, run_name=run_name)
+                self.assertIn("__package__", d2)
+                self.assertTrue(d2["__package__"] == pkg_name)
+                self.assertIn("sibling", d2)
+                self.assertIn("nephew", d2)
+                del d2 # Ensure __loader__ entry doesn't keep file open
         finally:
             self._del_pkg(pkg_dir, depth, mod_name)
         if verbose: print "Module executed successfully"
@@ -345,6 +354,8 @@
                                script_dir, '')
 
     def test_directory_compiled(self):
+        if no_lone_pyc_file:
+            return
         with temp_dir() as script_dir:
             mod_name = '__main__'
             script_name = self._make_test_script(script_dir, mod_name)
diff --git a/lib-python/2.7/test/test_scope.py b/lib-python/2.7/test/test_scope.py
--- a/lib-python/2.7/test/test_scope.py
+++ b/lib-python/2.7/test/test_scope.py
@@ -1,6 +1,6 @@
 import unittest
 from test.test_support import check_syntax_error, check_py3k_warnings, \
-                              check_warnings, run_unittest
+                              check_warnings, run_unittest, gc_collect
 
 
 class ScopeTests(unittest.TestCase):
@@ -432,6 +432,7 @@
 
         for i in range(100):
             f1()
+        gc_collect()
 
         self.assertEqual(Foo.count, 0)
 
diff --git a/lib-python/2.7/test/test_set.py b/lib-python/2.7/test/test_set.py
--- a/lib-python/2.7/test/test_set.py
+++ b/lib-python/2.7/test/test_set.py
@@ -309,6 +309,7 @@
             fo.close()
             test_support.unlink(test_support.TESTFN)
 
+    @test_support.impl_detail(pypy=False)
     def test_do_not_rehash_dict_keys(self):
         n = 10
         d = dict.fromkeys(map(HashCountingInt, xrange(n)))
@@ -559,6 +560,7 @@
         p = weakref.proxy(s)
         self.assertEqual(str(p), str(s))
         s = None
+        test_support.gc_collect()
         self.assertRaises(ReferenceError, str, p)
 
     # C API test only available in a debug build
@@ -590,6 +592,7 @@
         s.__init__(self.otherword)
         self.assertEqual(s, set(self.word))
 
+    @test_support.impl_detail()
     def test_singleton_empty_frozenset(self):
         f = frozenset()
         efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''),
@@ -770,9 +773,10 @@
         for v in self.set:
             self.assertIn(v, self.values)
         setiter = iter(self.set)
-        # note: __length_hint__ is an internal undocumented API,
-        # don't rely on it in your own programs
-        self.assertEqual(setiter.__length_hint__(), len(self.set))
+        if test_support.check_impl_detail():
+            # note: __length_hint__ is an internal undocumented API,
+            # don't rely on it in your own programs
+            self.assertEqual(setiter.__length_hint__(), len(self.set))
 
     def test_pickling(self):
         p = pickle.dumps(self.set)
@@ -1564,7 +1568,7 @@
             for meth in (s.union, s.intersection, s.difference, s.symmetric_difference, s.isdisjoint):
                 for g in (G, I, Ig, L, R):
                     expected = meth(data)
-                    actual = meth(G(data))
+                    actual = meth(g(data))
                     if isinstance(expected, bool):
                         self.assertEqual(actual, expected)
                     else:
diff --git a/lib-python/2.7/test/test_sets.py b/lib-python/2.7/test/test_sets.py
--- a/lib-python/2.7/test/test_sets.py
+++ b/lib-python/2.7/test/test_sets.py
@@ -686,7 +686,9 @@
         set_list = sorted(self.set)
         self.assertEqual(len(dup_list), len(set_list))
         for i, el in enumerate(dup_list):
-            self.assertIs(el, set_list[i])
+            # Object identity is not guarnteed for immutable objects, so we
+            # can't use assertIs here.
+            self.assertEqual(el, set_list[i])
 
     def test_deep_copy(self):
         dup = copy.deepcopy(self.set)
diff --git a/lib-python/2.7/test/test_site.py b/lib-python/2.7/test/test_site.py
--- a/lib-python/2.7/test/test_site.py
+++ b/lib-python/2.7/test/test_site.py
@@ -226,6 +226,10 @@
             self.assertEqual(len(dirs), 1)
             wanted = os.path.join('xoxo', 'Lib', 'site-packages')
             self.assertEqual(dirs[0], wanted)
+        elif '__pypy__' in sys.builtin_module_names:
+            self.assertEquals(len(dirs), 1)
+            wanted = os.path.join('xoxo', 'site-packages')
+            self.assertEquals(dirs[0], wanted)
         elif os.sep == '/':
             self.assertEqual(len(dirs), 2)
             wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
diff --git a/lib-python/2.7/test/test_socket.py b/lib-python/2.7/test/test_socket.py
--- a/lib-python/2.7/test/test_socket.py
+++ b/lib-python/2.7/test/test_socket.py
@@ -252,6 +252,7 @@
         self.assertEqual(p.fileno(), s.fileno())
         s.close()
         s = None
+        test_support.gc_collect()
         try:
             p.fileno()
         except ReferenceError:
@@ -285,32 +286,34 @@
             s.sendto(u'\u2620', sockname)
         with self.assertRaises(TypeError) as cm:
             s.sendto(5j, sockname)
-        self.assertIn('not complex', str(cm.exception))
+        self.assertIn('complex', str(cm.exception))
         with self.assertRaises(TypeError) as cm:
             s.sendto('foo', None)
-        self.assertIn('not NoneType', str(cm.exception))
+        self.assertIn('NoneType', str(cm.exception))
         # 3 args
         with self.assertRaises(UnicodeEncodeError):
             s.sendto(u'\u2620', 0, sockname)
         with self.assertRaises(TypeError) as cm:
             s.sendto(5j, 0, sockname)
-        self.assertIn('not complex', str(cm.exception))
+        self.assertIn('complex', str(cm.exception))
         with self.assertRaises(TypeError) as cm:
             s.sendto('foo', 0, None)
-        self.assertIn('not NoneType', str(cm.exception))
+        if test_support.check_impl_detail():
+            self.assertIn('not NoneType', str(cm.exception))
         with self.assertRaises(TypeError) as cm:
             s.sendto('foo', 'bar', sockname)
-        self.assertIn('an integer is required', str(cm.exception))
+        self.assertIn('integer', str(cm.exception))
         with self.assertRaises(TypeError) as cm:
             s.sendto('foo', None, None)
-        self.assertIn('an integer is required', str(cm.exception))
+        if test_support.check_impl_detail():
+            self.assertIn('an integer is required', str(cm.exception))
         # wrong number of args
         with self.assertRaises(TypeError) as cm:
             s.sendto('foo')
-        self.assertIn('(1 given)', str(cm.exception))
+        self.assertIn(' given)', str(cm.exception))
         with self.assertRaises(TypeError) as cm:
             s.sendto('foo', 0, sockname, 4)
-        self.assertIn('(4 given)', str(cm.exception))
+        self.assertIn(' given)', str(cm.exception))
 
 
     def testCrucialConstants(self):
@@ -385,10 +388,10 @@
             socket.htonl(k)
             socket.htons(k)
         for k in bad_values:
-            self.assertRaises(OverflowError, socket.ntohl, k)
-            self.assertRaises(OverflowError, socket.ntohs, k)
-            self.assertRaises(OverflowError, socket.htonl, k)
-            self.assertRaises(OverflowError, socket.htons, k)
+            self.assertRaises((OverflowError, ValueError), socket.ntohl, k)
+            self.assertRaises((OverflowError, ValueError), socket.ntohs, k)
+            self.assertRaises((OverflowError, ValueError), socket.htonl, k)
+            self.assertRaises((OverflowError, ValueError), socket.htons, k)
 
     def testGetServBy(self):
         eq = self.assertEqual
@@ -428,8 +431,8 @@
         if udpport is not None:
             eq(socket.getservbyport(udpport, 'udp'), service)
         # Make sure getservbyport does not accept out of range ports.
-        self.assertRaises(OverflowError, socket.getservbyport, -1)
-        self.assertRaises(OverflowError, socket.getservbyport, 65536)
+        self.assertRaises((OverflowError, ValueError), socket.getservbyport, -1)
+        self.assertRaises((OverflowError, ValueError), socket.getservbyport, 65536)
 
     def testDefaultTimeout(self):
         # Testing default timeout
@@ -608,8 +611,8 @@
         neg_port = port - 65536
         sock = socket.socket()
         try:
-            self.assertRaises(OverflowError, sock.bind, (host, big_port))
-            self.assertRaises(OverflowError, sock.bind, (host, neg_port))
+            self.assertRaises((OverflowError, ValueError), sock.bind, (host, big_port))
+            self.assertRaises((OverflowError, ValueError), sock.bind, (host, neg_port))
             sock.bind((host, port))
         finally:
             sock.close()
@@ -1309,6 +1312,7 @@
             closed = False
             def flush(self): pass
             def close(self): self.closed = True
+            def _decref_socketios(self): pass
 
         # must not close unless we request it: the original use of _fileobject
         # by module socket requires that the underlying socket not be closed until
diff --git a/lib-python/2.7/test/test_sort.py b/lib-python/2.7/test/test_sort.py
--- a/lib-python/2.7/test/test_sort.py
+++ b/lib-python/2.7/test/test_sort.py
@@ -140,7 +140,10 @@
                 return random.random() < 0.5
 
         L = [C() for i in range(50)]
-        self.assertRaises(ValueError, L.sort)
+        try:
+            L.sort()
+        except ValueError:
+            pass
 
     def test_cmpNone(self):
         # Testing None as a comparison function.
@@ -150,8 +153,10 @@
         L.sort(None)
         self.assertEqual(L, range(50))
 
+    @test_support.impl_detail(pypy=False)
     def test_undetected_mutation(self):
         # Python 2.4a1 did not always detect mutation
+        # So does pypy...
         memorywaster = []
         for i in range(20):
             def mutating_cmp(x, y):
@@ -226,7 +231,10 @@
             def __del__(self):
                 del data[:]
                 data[:] = range(20)
-        self.assertRaises(ValueError, data.sort, key=SortKiller)
+        try:
+            data.sort(key=SortKiller)
+        except ValueError:
+            pass
 
     def test_key_with_mutating_del_and_exception(self):
         data = range(10)
diff --git a/lib-python/2.7/test/test_ssl.py b/lib-python/2.7/test/test_ssl.py
--- a/lib-python/2.7/test/test_ssl.py
+++ b/lib-python/2.7/test/test_ssl.py
@@ -881,6 +881,8 @@
                 c = socket.socket()
                 c.connect((HOST, port))
                 listener_gone.wait()
+                # XXX why is it necessary?
+                test_support.gc_collect()
                 try:
                     ssl_sock = ssl.wrap_socket(c)
                 except IOError:
@@ -1330,10 +1332,8 @@
 
 def test_main(verbose=False):
     global CERTFILE, SVN_PYTHON_ORG_ROOT_CERT
-    CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir,
-                            "keycert.pem")
-    SVN_PYTHON_ORG_ROOT_CERT = os.path.join(
-        os.path.dirname(__file__) or os.curdir,
+    CERTFILE = test_support.findfile("keycert.pem")
+    SVN_PYTHON_ORG_ROOT_CERT = test_support.findfile(
         "https_svn_python_org_root.pem")
 
     if (not os.path.exists(CERTFILE) or
diff --git a/lib-python/2.7/test/test_str.py b/lib-python/2.7/test/test_str.py
--- a/lib-python/2.7/test/test_str.py
+++ b/lib-python/2.7/test/test_str.py
@@ -422,10 +422,11 @@
         for meth in ('foo'.startswith, 'foo'.endswith):
             with self.assertRaises(TypeError) as cm:
                 meth(['f'])
-            exc = str(cm.exception)
-            self.assertIn('unicode', exc)
-            self.assertIn('str', exc)
-            self.assertIn('tuple', exc)
+            if test_support.check_impl_detail():
+                exc = str(cm.exception)
+                self.assertIn('unicode', exc)
+                self.assertIn('str', exc)
+                self.assertIn('tuple', exc)
 
 def test_main():
     test_support.run_unittest(StrTest)
diff --git a/lib-python/2.7/test/test_struct.py b/lib-python/2.7/test/test_struct.py
--- a/lib-python/2.7/test/test_struct.py
+++ b/lib-python/2.7/test/test_struct.py
@@ -535,7 +535,8 @@
 
     @unittest.skipUnless(IS32BIT, "Specific to 32bit machines")
     def test_crasher(self):
-        self.assertRaises(MemoryError, struct.pack, "357913941c", "a")
+        self.assertRaises((MemoryError, struct.error), struct.pack,
+                          "357913941c", "a")
 
     def test_count_overflow(self):
         hugecount = '{}b'.format(sys.maxsize+1)
diff --git a/lib-python/2.7/test/test_subprocess.py b/lib-python/2.7/test/test_subprocess.py
--- a/lib-python/2.7/test/test_subprocess.py
+++ b/lib-python/2.7/test/test_subprocess.py
@@ -16,11 +16,11 @@
 # Depends on the following external programs: Python
 #
 
-if mswindows:
-    SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
-                                                'os.O_BINARY);')
-else:
-    SETBINARY = ''
+#if mswindows:
+#    SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
+#                                                'os.O_BINARY);')
+#else:
+#    SETBINARY = ''
 
 
 try:
@@ -420,8 +420,9 @@
         self.assertStderrEqual(stderr, "")
 
     def test_universal_newlines(self):
-        p = subprocess.Popen([sys.executable, "-c",
-                          'import sys,os;' + SETBINARY +
+        # NB. replaced SETBINARY with the -u flag
+        p = subprocess.Popen([sys.executable, "-u", "-c",
+                          'import sys,os;' + #SETBINARY +
                           'sys.stdout.write("line1\\n");'
                           'sys.stdout.flush();'
                           'sys.stdout.write("line2\\r");'
@@ -448,8 +449,9 @@
 
     def test_universal_newlines_communicate(self):
         # universal newlines through communicate()
-        p = subprocess.Popen([sys.executable, "-c",
-                          'import sys,os;' + SETBINARY +
+        # NB. replaced SETBINARY with the -u flag
+        p = subprocess.Popen([sys.executable, "-u", "-c",
+                          'import sys,os;' + #SETBINARY +
                           'sys.stdout.write("line1\\n");'
                           'sys.stdout.flush();'
                           'sys.stdout.write("line2\\r");'
diff --git a/lib-python/2.7/test/test_support.py b/lib-python/2.7/test/test_support.py
--- a/lib-python/2.7/test/test_support.py
+++ b/lib-python/2.7/test/test_support.py
@@ -431,16 +431,20 @@
             rmtree(name)
 
 
-def findfile(file, here=__file__, subdir=None):
+def findfile(file, here=None, subdir=None):
     """Try to find a file on sys.path and the working directory.  If it is not
     found the argument passed to the function is returned (this does not
     necessarily signal failure; could still be the legitimate path)."""
+    import test
     if os.path.isabs(file):
         return file
     if subdir is not None:
         file = os.path.join(subdir, file)
     path = sys.path
-    path = [os.path.dirname(here)] + path
+    if here is None:
+        path = test.__path__ + path
+    else:
+        path = [os.path.dirname(here)] + path
     for dn in path:
         fn = os.path.join(dn, file)
         if os.path.exists(fn): return fn
@@ -1050,15 +1054,33 @@
     guards, default = _parse_guards(guards)
     return guards.get(platform.python_implementation().lower(), default)
 
+# ----------------------------------
+# PyPy extension: you can run::
+#     python ..../test_foo.py --pdb
+# to get a pdb prompt in case of exceptions
 
+ResultClass = unittest.TextTestRunner.resultclass
+
+class TestResultWithPdb(ResultClass):
+
+    def addError(self, testcase, exc_info):
+        ResultClass.addError(self, testcase, exc_info)
+        if '--pdb' in sys.argv:
+            import pdb, traceback
+            traceback.print_tb(exc_info[2])
+            pdb.post_mortem(exc_info[2])
+
+# ----------------------------------
 
 def _run_suite(suite):
     """Run tests from a unittest.TestSuite-derived class."""
     if verbose:
-        runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
+        runner = unittest.TextTestRunner(sys.stdout, verbosity=2,
+                                         resultclass=TestResultWithPdb)
     else:
         runner = BasicTestRunner()
 
+
     result = runner.run(suite)
     if not result.wasSuccessful():
         if len(result.errors) == 1 and not result.failures:
@@ -1071,6 +1093,34 @@
                 err += "; run in verbose mode for details"
         raise TestFailed(err)
 
+# ----------------------------------
+# PyPy extension: you can run::
+#     python ..../test_foo.py --filter bar
+# to run only the test cases whose name contains bar
+
+def filter_maybe(suite):
+    try:
+        i = sys.argv.index('--filter')
+        filter = sys.argv[i+1]
+    except (ValueError, IndexError):
+        return suite
+    tests = []
+    for test in linearize_suite(suite):
+        if filter in test._testMethodName:
+            tests.append(test)
+    return unittest.TestSuite(tests)
+
+def linearize_suite(suite_or_test):
+    try:
+        it = iter(suite_or_test)
+    except TypeError:
+        yield suite_or_test
+        return
+    for subsuite in it:
+        for item in linearize_suite(subsuite):
+            yield item
+
+# ----------------------------------
 
 def run_unittest(*classes):
     """Run tests from unittest.TestCase-derived classes."""
@@ -1086,6 +1136,7 @@
             suite.addTest(cls)
         else:
             suite.addTest(unittest.makeSuite(cls))
+    suite = filter_maybe(suite)
     _run_suite(suite)
 
 
diff --git a/lib-python/2.7/test/test_syntax.py b/lib-python/2.7/test/test_syntax.py
--- a/lib-python/2.7/test/test_syntax.py
+++ b/lib-python/2.7/test/test_syntax.py
@@ -5,7 +5,8 @@
 >>> def f(x):
 ...     global x
 Traceback (most recent call last):
-SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1)
+  File "<doctest test.test_syntax[0]>", line 1
+SyntaxError: name 'x' is local and global
 
 The tests are all raise SyntaxErrors.  They were created by checking
 each C call that raises SyntaxError.  There are several modules that
@@ -375,7 +376,7 @@
 In 2.5 there was a missing exception and an assert was triggered in a debug
 build.  The number of blocks must be greater than CO_MAXBLOCKS.  SF #1565514
 
-   >>> while 1:
+   >>> while 1: # doctest:+SKIP
    ...  while 2:
    ...   while 3:
    ...    while 4:
diff --git a/lib-python/2.7/test/test_sys.py b/lib-python/2.7/test/test_sys.py
--- a/lib-python/2.7/test/test_sys.py
+++ b/lib-python/2.7/test/test_sys.py
@@ -264,6 +264,7 @@
             self.assertEqual(sys.getdlopenflags(), oldflags+1)
             sys.setdlopenflags(oldflags)
 
+    @test.test_support.impl_detail("reference counting")
     def test_refcount(self):
         # n here must be a global in order for this test to pass while
         # tracing with a python function.  Tracing calls PyFrame_FastToLocals
@@ -287,7 +288,7 @@
             is sys._getframe().f_code
         )
 
-    # sys._current_frames() is a CPython-only gimmick.
+    @test.test_support.impl_detail("current_frames")
     def test_current_frames(self):
         have_threads = True
         try:
@@ -383,7 +384,10 @@
         self.assertEqual(len(sys.float_info), 11)
         self.assertEqual(sys.float_info.radix, 2)
         self.assertEqual(len(sys.long_info), 2)
-        self.assertTrue(sys.long_info.bits_per_digit % 5 == 0)
+        if test.test_support.check_impl_detail(cpython=True):
+            self.assertTrue(sys.long_info.bits_per_digit % 5 == 0)
+        else:
+            self.assertTrue(sys.long_info.bits_per_digit >= 1)
         self.assertTrue(sys.long_info.sizeof_digit >= 1)
         self.assertEqual(type(sys.long_info.bits_per_digit), int)
         self.assertEqual(type(sys.long_info.sizeof_digit), int)
@@ -432,6 +436,7 @@
             self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
         self.assertTrue(repr(sys.flags))
 
+    @test.test_support.impl_detail("sys._clear_type_cache")
     def test_clear_type_cache(self):
         sys._clear_type_cache()
 
@@ -473,6 +478,7 @@
         p.wait()
         self.assertIn(executable, ["''", repr(sys.executable)])
 
+ at unittest.skipUnless(test.test_support.check_impl_detail(), "sys.getsizeof()")
 class SizeofTest(unittest.TestCase):
 
     TPFLAGS_HAVE_GC = 1<<14
diff --git a/lib-python/2.7/test/test_sys_settrace.py b/lib-python/2.7/test/test_sys_settrace.py
--- a/lib-python/2.7/test/test_sys_settrace.py
+++ b/lib-python/2.7/test/test_sys_settrace.py
@@ -213,12 +213,16 @@
         "finally"
 def generator_example():
     # any() will leave the generator before its end
-    x = any(generator_function())
+    x = any(generator_function()); gc.collect()
 
     # the following lines were not traced
     for x in range(10):
         y = x
 
+# On CPython, when the generator is decref'ed to zero, we see the trace
+# for the "finally:" portion.  On PyPy, we don't see it before the next
+# garbage collection.  That's why we put gc.collect() on the same line above.
+
 generator_example.events = ([(0, 'call'),
                              (2, 'line'),
                              (-6, 'call'),
@@ -282,11 +286,11 @@
         self.compare_events(func.func_code.co_firstlineno,
                             tracer.events, func.events)
 
-    def set_and_retrieve_none(self):
+    def test_set_and_retrieve_none(self):
         sys.settrace(None)
         assert sys.gettrace() is None
 
-    def set_and_retrieve_func(self):
+    def test_set_and_retrieve_func(self):
         def fn(*args):
             pass
 
@@ -323,17 +327,24 @@
         self.run_test(tighterloop_example)
 
     def test_13_genexp(self):
-        self.run_test(generator_example)
-        # issue1265: if the trace function contains a generator,
-        # and if the traced function contains another generator
-        # that is not completely exhausted, the trace stopped.
-        # Worse: the 'finally' clause was not invoked.
-        tracer = Tracer()
-        sys.settrace(tracer.traceWithGenexp)
-        generator_example()
-        sys.settrace(None)
-        self.compare_events(generator_example.__code__.co_firstlineno,
-                            tracer.events, generator_example.events)
+        if self.using_gc:
+            test_support.gc_collect()
+            gc.enable()
+        try:
+            self.run_test(generator_example)
+            # issue1265: if the trace function contains a generator,
+            # and if the traced function contains another generator
+            # that is not completely exhausted, the trace stopped.
+            # Worse: the 'finally' clause was not invoked.
+            tracer = Tracer()
+            sys.settrace(tracer.traceWithGenexp)
+            generator_example()
+            sys.settrace(None)
+            self.compare_events(generator_example.__code__.co_firstlineno,
+                                tracer.events, generator_example.events)
+        finally:
+            if self.using_gc:
+                gc.disable()
 
     def test_14_onliner_if(self):
         def onliners():
diff --git a/lib-python/2.7/test/test_sysconfig.py b/lib-python/2.7/test/test_sysconfig.py
--- a/lib-python/2.7/test/test_sysconfig.py
+++ b/lib-python/2.7/test/test_sysconfig.py
@@ -209,13 +209,22 @@
 
         self.assertEqual(get_platform(), 'macosx-10.4-fat64')
 
-        for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
+        for arch in ('ppc', 'i386', 'ppc64', 'x86_64'):
             get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
                                            '/Developer/SDKs/MacOSX10.4u.sdk  '
                                            '-fno-strict-aliasing -fno-common '
                                            '-dynamic -DNDEBUG -g -O3'%(arch,))
 
             self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
+        
+        # macosx with ARCHFLAGS set and empty _CONFIG_VARS
+        os.environ['ARCHFLAGS'] = '-arch i386'
+        sysconfig._CONFIG_VARS = None
+        
+        # this will attempt to recreate the _CONFIG_VARS based on environment 
+        # variables; used to check a problem with the PyPy's _init_posix
+        # implementation; see: issue 705
+        get_config_vars() 
 
         # linux debian sarge
         os.name = 'posix'
@@ -235,7 +244,7 @@
 
     def test_get_scheme_names(self):
         wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
-                  'posix_home', 'posix_prefix', 'posix_user')
+                  'posix_home', 'posix_prefix', 'posix_user', 'pypy')
         self.assertEqual(get_scheme_names(), wanted)
 
     def test_symlink(self):
diff --git a/lib-python/2.7/test/test_tarfile.py b/lib-python/2.7/test/test_tarfile.py
--- a/lib-python/2.7/test/test_tarfile.py
+++ b/lib-python/2.7/test/test_tarfile.py
@@ -169,6 +169,7 @@
         except tarfile.ReadError:
             self.fail("tarfile.open() failed on empty archive")
         self.assertListEqual(tar.getmembers(), [])
+        tar.close()
 
     def test_null_tarfile(self):
         # Test for issue6123: Allow opening empty archives.
@@ -207,16 +208,21 @@
         fobj = open(self.tarname, "rb")
         tar = tarfile.open(fileobj=fobj, mode=self.mode)
         self.assertEqual(tar.name, os.path.abspath(fobj.name))
+        tar.close()
 
     def test_no_name_attribute(self):
-        data = open(self.tarname, "rb").read()
+        f = open(self.tarname, "rb")
+        data = f.read()
+        f.close()
         fobj = StringIO.StringIO(data)
         self.assertRaises(AttributeError, getattr, fobj, "name")
         tar = tarfile.open(fileobj=fobj, mode=self.mode)
         self.assertEqual(tar.name, None)
 
     def test_empty_name_attribute(self):
-        data = open(self.tarname, "rb").read()
+        f = open(self.tarname, "rb")
+        data = f.read()
+        f.close()
         fobj = StringIO.StringIO(data)
         fobj.name = ""
         tar = tarfile.open(fileobj=fobj, mode=self.mode)
@@ -515,6 +521,7 @@
         self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
         tarinfo = self.tar.getmember("pax/umlauts-&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;")
         self._test_member(tarinfo, size=7011, chksum=md5_regtype)
+        self.tar.close()
 
 
 class LongnameTest(ReadTest):
@@ -675,6 +682,7 @@
             tar = tarfile.open(tmpname, self.mode)
             tarinfo = tar.gettarinfo(path)
             self.assertEqual(tarinfo.size, 0)
+            tar.close()
         finally:
             os.rmdir(path)
 
@@ -692,6 +700,7 @@
                 tar.gettarinfo(target)
                 tarinfo = tar.gettarinfo(link)
                 self.assertEqual(tarinfo.size, 0)
+                tar.close()
             finally:
                 os.remove(target)
                 os.remove(link)
@@ -704,6 +713,7 @@
                 tar = tarfile.open(tmpname, self.mode)
                 tarinfo = tar.gettarinfo(path)
                 self.assertEqual(tarinfo.size, 0)
+                tar.close()
             finally:
                 os.remove(path)
 
@@ -722,6 +732,7 @@
         tar.add(dstname)
         os.chdir(cwd)
         self.assertTrue(tar.getnames() == [], "added the archive to itself")
+        tar.close()
 
     def test_exclude(self):
         tempdir = os.path.join(TEMPDIR, "exclude")
@@ -742,6 +753,7 @@
             tar = tarfile.open(tmpname, "r")
             self.assertEqual(len(tar.getmembers()), 1)
             self.assertEqual(tar.getnames()[0], "empty_dir")
+            tar.close()
         finally:
             shutil.rmtree(tempdir)
 
@@ -947,7 +959,9 @@
             fobj.close()
         elif self.mode.endswith("bz2"):
             dec = bz2.BZ2Decompressor()
-            data = open(tmpname, "rb").read()
+            f = open(tmpname, "rb")
+            data = f.read()
+            f.close()
             data = dec.decompress(data)
             self.assertTrue(len(dec.unused_data) == 0,
                     "found trailing data")
@@ -1026,6 +1040,7 @@
                 "unable to read longname member")
         self.assertEqual(tarinfo.linkname, member.linkname,
                 "unable to read longname member")
+        tar.close()
 
     def test_longname_1023(self):
         self._test(("longnam/" * 127) + "longnam")
@@ -1118,6 +1133,7 @@
         else:
             n = tar.getmembers()[0].name
             self.assertTrue(name == n, "PAX longname creation failed")
+        tar.close()
 
     def test_pax_global_header(self):
         pax_headers = {
@@ -1146,6 +1162,7 @@
                     tarfile.PAX_NUMBER_FIELDS[key](val)
                 except (TypeError, ValueError):
                     self.fail("unable to convert pax header field")
+        tar.close()
 
     def test_pax_extended_header(self):
         # The fields from the pax header have priority over the
@@ -1165,6 +1182,7 @@
         self.assertEqual(t.pax_headers, pax_headers)
         self.assertEqual(t.name, "foo")
         self.assertEqual(t.uid, 123)
+        tar.close()
 
 
 class UstarUnicodeTest(unittest.TestCase):
@@ -1208,6 +1226,7 @@
         tarinfo.name = "foo"
         tarinfo.uname = u"&#65533;&#65533;&#65533;"
         self.assertRaises(UnicodeError, tar.addfile, tarinfo)
+        tar.close()
 
     def test_unicode_argument(self):
         tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict")
@@ -1262,6 +1281,7 @@
             tar = tarfile.open(tmpname, format=self.format, encoding="ascii",
                     errors=handler)
             self.assertEqual(tar.getnames()[0], name)
+            tar.close()
 
         self.assertRaises(UnicodeError, tarfile.open, tmpname,
                 encoding="ascii", errors="strict")
@@ -1274,6 +1294,7 @@
         tar = tarfile.open(tmpname, format=self.format, encoding="iso8859-1",
                 errors="utf-8")
         self.assertEqual(tar.getnames()[0], "&#65533;&#65533;&#65533;/" + u"&#65533;".encode("utf8"))
+        tar.close()
 
 
 class AppendTest(unittest.TestCase):
@@ -1301,6 +1322,7 @@
     def _test(self, names=["bar"], fileobj=None):
         tar = tarfile.open(self.tarname, fileobj=fileobj)
         self.assertEqual(tar.getnames(), names)
+        tar.close()
 
     def test_non_existing(self):
         self._add_testfile()
@@ -1319,7 +1341,9 @@
 
     def test_fileobj(self):
         self._create_testtar()
-        data = open(self.tarname).read()
+        f = open(self.tarname)
+        data = f.read()
+        f.close()
         fobj = StringIO.StringIO(data)
         self._add_testfile(fobj)
         fobj.seek(0)
@@ -1345,7 +1369,9 @@
     # Append mode is supposed to fail if the tarfile to append to
     # does not end with a zero block.
     def _test_error(self, data):
-        open(self.tarname, "wb").write(data)
+        f = open(self.tarname, "wb")
+        f.write(data)
+        f.close()
         self.assertRaises(tarfile.ReadError, self._add_testfile)
 
     def test_null(self):
diff --git a/lib-python/2.7/test/test_tempfile.py b/lib-python/2.7/test/test_tempfile.py
--- a/lib-python/2.7/test/test_tempfile.py
+++ b/lib-python/2.7/test/test_tempfile.py
@@ -23,8 +23,8 @@
 
 # TEST_FILES may need to be tweaked for systems depending on the maximum
 # number of files that can be opened at one time (see ulimit -n)
-if sys.platform in ('openbsd3', 'openbsd4'):
-    TEST_FILES = 48
+if sys.platform.startswith("openbsd"):
+    TEST_FILES = 64 # ulimit -n defaults to 128 for normal users
 else:
     TEST_FILES = 100
 
@@ -244,6 +244,7 @@
         dir = tempfile.mkdtemp()
         try:
             self.do_create(dir=dir).write("blat")
+            test_support.gc_collect()
         finally:
             os.rmdir(dir)
 
@@ -528,12 +529,15 @@
         self.do_create(suf="b")
         self.do_create(pre="a", suf="b")
         self.do_create(pre="aa", suf=".txt")
+        test_support.gc_collect()
 
     def test_many(self):
         # mktemp can choose many usable file names (stochastic)
         extant = range(TEST_FILES)
         for i in extant:
             extant[i] = self.do_create(pre="aa")
+        del extant
+        test_support.gc_collect()
 
 ##     def test_warning(self):
 ##         # mktemp issues a warning when used
diff --git a/lib-python/2.7/test/test_thread.py b/lib-python/2.7/test/test_thread.py
--- a/lib-python/2.7/test/test_thread.py
+++ b/lib-python/2.7/test/test_thread.py
@@ -128,6 +128,7 @@
         del task
         while not done:
             time.sleep(0.01)
+            test_support.gc_collect()
         self.assertEqual(thread._count(), orig)
 
 
diff --git a/lib-python/2.7/test/test_threading.py b/lib-python/2.7/test/test_threading.py
--- a/lib-python/2.7/test/test_threading.py
+++ b/lib-python/2.7/test/test_threading.py
@@ -161,6 +161,7 @@
 
     # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
     # exposed at the Python level.  This test relies on ctypes to get at it.
+    @test.test_support.cpython_only
     def test_PyThreadState_SetAsyncExc(self):
         try:
             import ctypes
@@ -266,6 +267,7 @@
         finally:
             threading._start_new_thread = _start_new_thread
 
+    @test.test_support.cpython_only
     def test_finalize_runnning_thread(self):
         # Issue 1402: the PyGILState_Ensure / _Release functions may be called
         # very late on python exit: on deallocation of a running thread for
@@ -383,6 +385,7 @@
         finally:
             sys.setcheckinterval(old_interval)
 
+    @test.test_support.cpython_only
     def test_no_refcycle_through_target(self):
         class RunSelfFunction(object):
             def __init__(self, should_raise):
@@ -425,6 +428,9 @@
             def joiningfunc(mainthread):
                 mainthread.join()
                 print 'end of thread'
+                # stdout is fully buffered because not a tty, we have to flush
+                # before exit.
+                sys.stdout.flush()
         \n""" + script
 
         p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE)
diff --git a/lib-python/2.7/test/test_threading_local.py b/lib-python/2.7/test/test_threading_local.py
--- a/lib-python/2.7/test/test_threading_local.py
+++ b/lib-python/2.7/test/test_threading_local.py
@@ -173,8 +173,9 @@
         obj = cls()
         obj.x = 5
         self.assertEqual(obj.__dict__, {'x': 5})
-        with self.assertRaises(AttributeError):
-            obj.__dict__ = {}
+        if test_support.check_impl_detail():
+            with self.assertRaises(AttributeError):
+                obj.__dict__ = {}
         with self.assertRaises(AttributeError):
             del obj.__dict__
 
diff --git a/lib-python/2.7/test/test_traceback.py b/lib-python/2.7/test/test_traceback.py
--- a/lib-python/2.7/test/test_traceback.py
+++ b/lib-python/2.7/test/test_traceback.py
@@ -5,7 +5,8 @@
 import sys
 import unittest
 from imp import reload
-from test.test_support import run_unittest, is_jython, Error
+from test.test_support import run_unittest, Error
+from test.test_support import impl_detail, check_impl_detail
 
 import traceback
 
@@ -49,10 +50,8 @@
         self.assertTrue(err[2].count('\n') == 1) # and no additional newline
         self.assertTrue(err[1].find("+") == err[2].find("^")) # in the right place
 
+    @impl_detail("other implementations may add a caret (why shouldn't they?)")
     def test_nocaret(self):
-        if is_jython:
-            # jython adds a caret in this case (why shouldn't it?)
-            return
         err = self.get_exception_format(self.syntax_error_without_caret,
                                         SyntaxError)
         self.assertTrue(len(err) == 3)
@@ -63,8 +62,11 @@
                                         IndentationError)
         self.assertTrue(len(err) == 4)
         self.assertTrue(err[1].strip() == "print 2")
-        self.assertIn("^", err[2])
-        self.assertTrue(err[1].find("2") == err[2].find("^"))
+        if check_impl_detail():
+            # on CPython, there is a "^" at the end of the line
+            # on PyPy, there is a "^" too, but at the start, more logically
+            self.assertIn("^", err[2])
+            self.assertTrue(err[1].find("2") == err[2].find("^"))
 
     def test_bug737473(self):
         import os, tempfile, time
@@ -74,7 +76,8 @@
         try:
             sys.path.insert(0, testdir)
             testfile = os.path.join(testdir, 'test_bug737473.py')
-            print >> open(testfile, 'w'), """
+            with open(testfile, 'w') as f:
+                print >> f, """
 def test():
     raise ValueError"""
 
@@ -96,7 +99,8 @@
             # three seconds are needed for this test to pass reliably :-(
             time.sleep(4)
 
-            print >> open(testfile, 'w'), """
+            with open(testfile, 'w') as f:
+                print >> f, """
 def test():
     raise NotImplementedError"""
             reload(test_bug737473)
diff --git a/lib-python/2.7/test/test_types.py b/lib-python/2.7/test/test_types.py
--- a/lib-python/2.7/test/test_types.py
+++ b/lib-python/2.7/test/test_types.py
@@ -1,7 +1,8 @@
 # Python test set -- part 6, built-in types
 
 from test.test_support import run_unittest, have_unicode, run_with_locale, \
-                              check_py3k_warnings
+                              check_py3k_warnings, \
+                              impl_detail, check_impl_detail
 import unittest
 import sys
 import locale
@@ -289,9 +290,14 @@
         # array.array() returns an object that does not implement a char buffer,
         # something which int() uses for conversion.
         import array
-        try: int(buffer(array.array('c')))
+        try: int(buffer(array.array('c', '5')))
         except TypeError: pass
-        else: self.fail("char buffer (at C level) not working")
+        else:
+            if check_impl_detail():
+                self.fail("char buffer (at C level) not working")
+            #else:
+            #   it works on PyPy, which does not have the distinction
+            #   between char buffer and binary buffer.  XXX fine enough?
 
     def test_int__format__(self):
         def test(i, format_spec, result):
@@ -741,6 +747,7 @@
         for code in 'xXobns':
             self.assertRaises(ValueError, format, 0, ',' + code)
 
+    @impl_detail("the types' internal size attributes are CPython-only")
     def test_internal_sizes(self):
         self.assertGreater(object.__basicsize__, 0)
         self.assertGreater(tuple.__itemsize__, 0)
diff --git a/lib-python/2.7/test/test_unicode.py b/lib-python/2.7/test/test_unicode.py
--- a/lib-python/2.7/test/test_unicode.py
+++ b/lib-python/2.7/test/test_unicode.py
@@ -448,10 +448,11 @@
                 meth('\xff')
             with self.assertRaises(TypeError) as cm:
                 meth(['f'])
-            exc = str(cm.exception)
-            self.assertIn('unicode', exc)
-            self.assertIn('str', exc)
-            self.assertIn('tuple', exc)
+            if test_support.check_impl_detail():
+                exc = str(cm.exception)
+                self.assertIn('unicode', exc)
+                self.assertIn('str', exc)
+                self.assertIn('tuple', exc)
 
     @test_support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
     def test_format_float(self):
@@ -1062,7 +1063,8 @@
         # to take a 64-bit long, this test should apply to all platforms.
         if sys.maxint > (1 << 32) or struct.calcsize('P') != 4:
             return
-        self.assertRaises(OverflowError, u't\tt\t'.expandtabs, sys.maxint)
+        self.assertRaises((OverflowError, MemoryError),
+                          u't\tt\t'.expandtabs, sys.maxint)
 
     def test__format__(self):
         def test(value, format, expected):
diff --git a/lib-python/2.7/test/test_unicodedata.py b/lib-python/2.7/test/test_unicodedata.py
--- a/lib-python/2.7/test/test_unicodedata.py
+++ b/lib-python/2.7/test/test_unicodedata.py
@@ -233,10 +233,12 @@
         # been loaded in this process.
         popen = subprocess.Popen(args, stderr=subprocess.PIPE)
         popen.wait()
-        self.assertEqual(popen.returncode, 1)
-        error = "SyntaxError: (unicode error) \N escapes not supported " \
-            "(can't load unicodedata module)"
-        self.assertIn(error, popen.stderr.read())
+        self.assertIn(popen.returncode, [0, 1]) # at least it did not segfault
+        if test.test_support.check_impl_detail():
+            self.assertEqual(popen.returncode, 1)
+            error = "SyntaxError: (unicode error) \N escapes not supported " \
+                "(can't load unicodedata module)"
+            self.assertIn(error, popen.stderr.read())
 
     def test_decimal_numeric_consistent(self):
         # Test that decimal and numeric are consistent,
diff --git a/lib-python/2.7/test/test_unpack.py b/lib-python/2.7/test/test_unpack.py
--- a/lib-python/2.7/test/test_unpack.py
+++ b/lib-python/2.7/test/test_unpack.py
@@ -62,14 +62,14 @@
     >>> a, b = t
     Traceback (most recent call last):
       ...
-    ValueError: too many values to unpack
+    ValueError: expected length 2, got 3
 
 Unpacking tuple of wrong size
 
     >>> a, b = l
     Traceback (most recent call last):
       ...
-    ValueError: too many values to unpack
+    ValueError: expected length 2, got 3
 
 Unpacking sequence too short
 
diff --git a/lib-python/2.7/test/test_urllib2.py b/lib-python/2.7/test/test_urllib2.py
--- a/lib-python/2.7/test/test_urllib2.py
+++ b/lib-python/2.7/test/test_urllib2.py
@@ -307,6 +307,9 @@
     def getresponse(self):
         return MockHTTPResponse(MockFile(), {}, 200, "OK")
 
+    def close(self):
+        pass
+
 class MockHandler:
     # useful for testing handler machinery
     # see add_ordered_mock_handlers() docstring
diff --git a/lib-python/2.7/test/test_warnings.py b/lib-python/2.7/test/test_warnings.py
--- a/lib-python/2.7/test/test_warnings.py
+++ b/lib-python/2.7/test/test_warnings.py
@@ -355,7 +355,8 @@
     # test_support.import_fresh_module utility function
     def test_accelerated(self):
         self.assertFalse(original_warnings is self.module)
-        self.assertFalse(hasattr(self.module.warn, 'func_code'))
+        self.assertFalse(hasattr(self.module.warn, 'func_code') and
+                         hasattr(self.module.warn.func_code, 'co_filename'))
 
 class PyWarnTests(BaseTest, WarnTests):
     module = py_warnings
@@ -364,7 +365,8 @@
     # test_support.import_fresh_module utility function
     def test_pure_python(self):
         self.assertFalse(original_warnings is self.module)
-        self.assertTrue(hasattr(self.module.warn, 'func_code'))
+        self.assertTrue(hasattr(self.module.warn, 'func_code') and
+                        hasattr(self.module.warn.func_code, 'co_filename'))
 
 
 class WCmdLineTests(unittest.TestCase):
diff --git a/lib-python/2.7/test/test_weakref.py b/lib-python/2.7/test/test_weakref.py
--- a/lib-python/2.7/test/test_weakref.py
+++ b/lib-python/2.7/test/test_weakref.py
@@ -1,4 +1,3 @@
-import gc
 import sys
 import unittest
 import UserList
@@ -6,6 +5,7 @@
 import operator
 
 from test import test_support
+from test.test_support import gc_collect
 
 # Used in ReferencesTestCase.test_ref_created_during_del() .
 ref_from_del = None
@@ -70,6 +70,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,
@@ -101,13 +102,16 @@
         ref1 = weakref.proxy(o, self.callback)
         ref2 = weakref.proxy(o, self.callback)
         del o
+        gc_collect()
 
         def check(proxy):
             proxy.bar
 
         self.assertRaises(weakref.ReferenceError, check, ref1)
         self.assertRaises(weakref.ReferenceError, check, ref2)
-        self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
+        ref3 = weakref.proxy(C())
+        gc_collect()
+        self.assertRaises(weakref.ReferenceError, bool, ref3)
         self.assertTrue(self.cbcalled == 2)
 
     def check_basic_ref(self, factory):
@@ -124,6 +128,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,
@@ -148,6 +153,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")
 
@@ -325,6 +331,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.")
@@ -338,6 +345,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")
 
@@ -345,10 +353,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")
 
@@ -400,13 +410,11 @@
         # when the second attempt to remove the instance from the "list
         # of all objects" occurs.
 
-        import gc
-
         class C(object):
             pass
 
         c = C()
-        wr = weakref.ref(c, lambda ignore: gc.collect())
+        wr = weakref.ref(c, lambda ignore: gc_collect())
         del c
 
         # There endeth the first part.  It gets worse.
@@ -414,7 +422,7 @@
 
         c1 = C()
         c1.i = C()
-        wr = weakref.ref(c1.i, lambda ignore: gc.collect())
+        wr = weakref.ref(c1.i, lambda ignore: gc_collect())
 
         c2 = C()
         c2.c1 = c1
@@ -430,8 +438,6 @@
         del c2
 
     def test_callback_in_cycle_1(self):
-        import gc
-
         class J(object):
             pass
 
@@ -467,11 +473,9 @@
         # search II.__mro__, but that's NULL.   The result was a segfault in
         # a release build, and an assert failure in a debug build.
         del I, J, II
-        gc.collect()
+        gc_collect()
 
     def test_callback_in_cycle_2(self):
-        import gc
-
         # This is just like test_callback_in_cycle_1, except that II is an
         # old-style class.  The symptom is different then:  an instance of an
         # old-style class looks in its own __dict__ first.  'J' happens to
@@ -496,11 +500,9 @@
         I.wr = weakref.ref(J, I.acallback)
 
         del I, J, II
-        gc.collect()
+        gc_collect()
 
     def test_callback_in_cycle_3(self):
-        import gc
-
         # This one broke the first patch that fixed the last two.  In this
         # case, the objects reachable from the callback aren't also reachable
         # from the object (c1) *triggering* the callback:  you can get to
@@ -520,11 +522,9 @@
         c2.wr = weakref.ref(c1, c2.cb)
 
         del c1, c2
-        gc.collect()
+        gc_collect()
 
     def test_callback_in_cycle_4(self):
-        import gc
-
         # Like test_callback_in_cycle_3, except c2 and c1 have different
         # classes.  c2's class (C) isn't reachable from c1 then, so protecting
         # objects reachable from the dying object (c1) isn't enough to stop
@@ -548,11 +548,9 @@
         c2.wr = weakref.ref(c1, c2.cb)
 
         del c1, c2, C, D
-        gc.collect()
+        gc_collect()
 
     def test_callback_in_cycle_resurrection(self):
-        import gc
-
         # Do something nasty in a weakref callback:  resurrect objects
         # from dead cycles.  For this to be attempted, the weakref and
         # its callback must also be part of the cyclic trash (else the
@@ -583,7 +581,7 @@
         del c1, c2, C   # make them all trash
         self.assertEqual(alist, [])  # del isn't enough to reclaim anything
 
-        gc.collect()
+        gc_collect()
         # c1.wr and c2.wr were part of the cyclic trash, so should have
         # been cleared without their callbacks executing.  OTOH, the weakref
         # to C is bound to a function local (wr), and wasn't trash, so that
@@ -593,12 +591,10 @@
         self.assertEqual(wr(), None)
 
         del alist[:]
-        gc.collect()
+        gc_collect()
         self.assertEqual(alist, [])
 
     def test_callbacks_on_callback(self):
-        import gc
-
         # Set up weakref callbacks *on* weakref callbacks.
         alist = []
         def safe_callback(ignore):
@@ -626,12 +622,12 @@
 
         del callback, c, d, C
         self.assertEqual(alist, [])  # del isn't enough to clean up cycles
-        gc.collect()
+        gc_collect()
         self.assertEqual(alist, ["safe_callback called"])
         self.assertEqual(external_wr(), None)
 
         del alist[:]
-        gc.collect()
+        gc_collect()
         self.assertEqual(alist, [])
 
     def test_gc_during_ref_creation(self):
@@ -641,9 +637,11 @@
         self.check_gc_during_creation(weakref.proxy)
 
     def check_gc_during_creation(self, makeref):
-        thresholds = gc.get_threshold()
-        gc.set_threshold(1, 1, 1)
-        gc.collect()
+        if test_support.check_impl_detail():
+            import gc
+            thresholds = gc.get_threshold()
+            gc.set_threshold(1, 1, 1)
+        gc_collect()
         class A:
             pass
 
@@ -663,7 +661,8 @@
             weakref.ref(referenced, callback)
 
         finally:
-            gc.set_threshold(*thresholds)
+            if test_support.check_impl_detail():
+                gc.set_threshold(*thresholds)
 
     def test_ref_created_during_del(self):
         # Bug #1377858
@@ -683,7 +682,7 @@
         r = weakref.ref(Exception)
         self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0)
         # No exception should be raised here
-        gc.collect()
+        gc_collect()
 
     def test_classes(self):
         # Check that both old-style classes and new-style classes
@@ -696,12 +695,12 @@
         weakref.ref(int)
         a = weakref.ref(A, l.append)
         A = None
-        gc.collect()
+        gc_collect()
         self.assertEqual(a(), None)
         self.assertEqual(l, [a])
         b = weakref.ref(B, l.append)
         B = None
-        gc.collect()
+        gc_collect()
         self.assertEqual(b(), None)
         self.assertEqual(l, [a, b])
 
@@ -722,6 +721,7 @@
         self.assertTrue(mr.called)
         self.assertEqual(mr.value, 24)
         del o
+        gc_collect()
         self.assertTrue(mr() is None)
         self.assertTrue(mr.called)
 
@@ -738,9 +738,11 @@
         self.assertEqual(weakref.getweakrefcount(o), 3)
         refs = weakref.getweakrefs(o)
         self.assertEqual(len(refs), 3)
-        self.assertTrue(r2 is refs[0])
-        self.assertIn(r1, refs[1:])
-        self.assertIn(r3, refs[1:])
+        assert set(refs) == set((r1, r2, r3))
+        if test_support.check_impl_detail():
+            self.assertTrue(r2 is refs[0])
+            self.assertIn(r1, refs[1:])
+            self.assertIn(r3, refs[1:])
 
     def test_subclass_refs_dont_conflate_callbacks(self):
         class MyRef(weakref.ref):
@@ -839,15 +841,18 @@
         del items1, items2
         self.assertTrue(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 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):
@@ -868,9 +873,11 @@
         del items1, items2
         self.assertTrue(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)
@@ -986,13 +993,13 @@
         self.assertTrue(len(weakdict) == 2)
         k, v = weakdict.popitem()
         self.assertTrue(len(weakdict) == 1)
-        if k is key1:
+        if k == key1:
             self.assertTrue(v is value1)
         else:
             self.assertTrue(v is value2)
         k, v = weakdict.popitem()
         self.assertTrue(len(weakdict) == 0)
-        if k is key1:
+        if k == key1:
             self.assertTrue(v is value1)
         else:
             self.assertTrue(v is value2)
@@ -1137,6 +1144,7 @@
         for o in objs:
             count += 1
             del d[o]
+        gc_collect()
         self.assertEqual(len(d), 0)
         self.assertEqual(count, 2)
 
@@ -1177,6 +1185,7 @@
 >>> o is o2
 True
 >>> del o, o2
+>>> gc_collect()
 >>> print r()
 None
 
@@ -1229,6 +1238,7 @@
 >>> id2obj(a_id) is a
 True
 >>> del a
+>>> gc_collect()
 >>> try:
 ...     id2obj(a_id)
 ... except KeyError:
diff --git a/lib-python/2.7/test/test_weakset.py b/lib-python/2.7/test/test_weakset.py
--- a/lib-python/2.7/test/test_weakset.py
+++ b/lib-python/2.7/test/test_weakset.py
@@ -57,6 +57,7 @@
         self.assertEqual(len(self.s), len(self.d))
         self.assertEqual(len(self.fs), 1)
         del self.obj
+        test_support.gc_collect()
         self.assertEqual(len(self.fs), 0)
 
     def test_contains(self):
@@ -66,6 +67,7 @@
         self.assertNotIn(1, self.s)
         self.assertIn(self.obj, self.fs)
         del self.obj
+        test_support.gc_collect()
         self.assertNotIn(SomeClass('F'), self.fs)
 
     def test_union(self):
@@ -204,6 +206,7 @@
         self.assertEqual(self.s, dup)
         self.assertRaises(TypeError, self.s.add, [])
         self.fs.add(Foo())
+        test_support.gc_collect()
         self.assertTrue(len(self.fs) == 1)
         self.fs.add(self.obj)
         self.assertTrue(len(self.fs) == 1)
@@ -330,10 +333,11 @@
         next(it)             # Trigger internal iteration
         # Destroy an item
         del items[-1]
-        gc.collect()    # just in case
+        test_support.gc_collect()
         # We have removed either the first consumed items, or another one
         self.assertIn(len(list(it)), [len(items), len(items) - 1])
         del it
+        test_support.gc_collect()
         # The removal has been committed
         self.assertEqual(len(s), len(items))
 
diff --git a/lib-python/2.7/test/test_xml_etree.py b/lib-python/2.7/test/test_xml_etree.py
--- a/lib-python/2.7/test/test_xml_etree.py
+++ b/lib-python/2.7/test/test_xml_etree.py
@@ -1633,10 +1633,10 @@
 
     Check reference leak.
     >>> xmltoolkit63()
-    >>> count = sys.getrefcount(None)
+    >>> count = sys.getrefcount(None)  #doctest: +SKIP
     >>> for i in range(1000):
     ...     xmltoolkit63()
-    >>> sys.getrefcount(None) - count
+    >>> sys.getrefcount(None) - count  #doctest: +SKIP
     0
 
     """
diff --git a/lib-python/2.7/test/test_xmlrpc.py b/lib-python/2.7/test/test_xmlrpc.py
--- a/lib-python/2.7/test/test_xmlrpc.py
+++ b/lib-python/2.7/test/test_xmlrpc.py
@@ -308,7 +308,7 @@
         global ADDR, PORT, URL
         ADDR, PORT = serv.socket.getsockname()
         #connect to IP address directly.  This avoids socket.create_connection()
-        #trying to connect to "localhost" using all address families, which
+        #trying to connect to to "localhost" using all address families, which
         #causes slowdown e.g. on vista which supports AF_INET6.  The server listens
         #on AF_INET only.
         URL = "http://%s:%d"%(ADDR, PORT)
@@ -367,7 +367,7 @@
         global ADDR, PORT, URL
         ADDR, PORT = serv.socket.getsockname()
         #connect to IP address directly.  This avoids socket.create_connection()
-        #trying to connect to "localhost" using all address families, which
+        #trying to connect to to "localhost" using all address families, which
         #causes slowdown e.g. on vista which supports AF_INET6.  The server listens
         #on AF_INET only.
         URL = "http://%s:%d"%(ADDR, PORT)
@@ -435,6 +435,7 @@
 
     def tearDown(self):
         # wait on the server thread to terminate
+        test_support.gc_collect() # to close the active connections
         self.evt.wait(10)
 
         # disable traceback reporting
@@ -472,9 +473,6 @@
                 # protocol error; provide additional information in test output
                 self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
 
-    def test_unicode_host(self):
-        server = xmlrpclib.ServerProxy(u"http://%s:%d/RPC2"%(ADDR, PORT))
-        self.assertEqual(server.add("a", u"\xe9"), u"a\xe9")
 
     # [ch] The test 404 is causing lots of false alarms.
     def XXXtest_404(self):
@@ -589,12 +587,6 @@
         # This avoids waiting for the socket timeout.
         self.test_simple1()
 
-    def test_partial_post(self):
-        # Check that a partial POST doesn't make the server loop: issue #14001.
-        conn = httplib.HTTPConnection(ADDR, PORT)
-        conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye')
-        conn.close()
-
 class MultiPathServerTestCase(BaseServerTestCase):
     threadFunc = staticmethod(http_multi_server)
     request_count = 2
diff --git a/lib-python/2.7/test/test_zlib.py b/lib-python/2.7/test/test_zlib.py
--- a/lib-python/2.7/test/test_zlib.py
+++ b/lib-python/2.7/test/test_zlib.py
@@ -1,6 +1,7 @@
 import unittest
 from test.test_support import TESTFN, run_unittest, import_module, unlink, requires
 import binascii
+import os
 import random
 from test.test_support import precisionbigmemtest, _1G, _4G
 import sys
@@ -99,14 +100,7 @@
 
 class BaseCompressTestCase(object):
     def check_big_compress_buffer(self, size, compress_func):
-        _1M = 1024 * 1024
-        fmt = "%%0%dx" % (2 * _1M)
-        # Generate 10MB worth of random, and expand it by repeating it.
-        # The assumption is that zlib's memory is not big enough to exploit
-        # such spread out redundancy.
-        data = ''.join([binascii.a2b_hex(fmt % random.getrandbits(8 * _1M))
-                        for i in range(10)])
-        data = data * (size // len(data) + 1)
+        data = os.urandom(size)
         try:
             compress_func(data)
         finally:
diff --git a/lib-python/2.7/trace.py b/lib-python/2.7/trace.py
--- a/lib-python/2.7/trace.py
+++ b/lib-python/2.7/trace.py
@@ -559,6 +559,10 @@
             if len(funcs) == 1:
                 dicts = [d for d in gc.get_referrers(funcs[0])
                              if isinstance(d, dict)]
+                if len(dicts) == 0:
+                    # PyPy may store functions directly on the class
+                    # (more exactly: the container is not a Python object)
+                    dicts = funcs
                 if len(dicts) == 1:
                     classes = [c for c in gc.get_referrers(dicts[0])
                                    if hasattr(c, "__bases__")]
diff --git a/lib-python/2.7/urllib2.py b/lib-python/2.7/urllib2.py
--- a/lib-python/2.7/urllib2.py
+++ b/lib-python/2.7/urllib2.py
@@ -1171,6 +1171,7 @@
             except TypeError: #buffering kw not supported
                 r = h.getresponse()
         except socket.error, err: # XXX what error?
+            h.close()
             raise URLError(err)
 
         # Pick apart the HTTPResponse object to get the addinfourl
diff --git a/lib-python/2.7/uuid.py b/lib-python/2.7/uuid.py
--- a/lib-python/2.7/uuid.py
+++ b/lib-python/2.7/uuid.py
@@ -406,8 +406,12 @@
             continue
         if hasattr(lib, 'uuid_generate_random'):
             _uuid_generate_random = lib.uuid_generate_random
+            _uuid_generate_random.argtypes = [ctypes.c_char * 16]
+            _uuid_generate_random.restype = None
         if hasattr(lib, 'uuid_generate_time'):
             _uuid_generate_time = lib.uuid_generate_time
+            _uuid_generate_time.argtypes = [ctypes.c_char * 16]
+            _uuid_generate_time.restype = None
 
     # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
     # in issue #8621 the function generates the same sequence of values
@@ -436,6 +440,9 @@
         lib = None
     _UuidCreate = getattr(lib, 'UuidCreateSequential',
                           getattr(lib, 'UuidCreate', None))
+    if _UuidCreate is not None:
+        _UuidCreate.argtypes = [ctypes.c_char * 16]
+        _UuidCreate.restype = ctypes.c_int
 except:
     pass
 
diff --git a/lib-python/modified-2.7/UserDict.py b/lib-python/modified-2.7/UserDict.py
deleted file mode 100644
--- a/lib-python/modified-2.7/UserDict.py
+++ /dev/null
@@ -1,189 +0,0 @@
-"""A more or less complete user-defined wrapper around dictionary objects."""
-
-# XXX This is a bit of a hack (as usual :-))
-# the actual content of the file is not changed, but we put it here to make
-# virtualenv happy (because its internal logic expects at least one of the
-# REQUIRED_MODULES to be in modified-*)
-
-class UserDict:
-    def __init__(self, dict=None, **kwargs):
-        self.data = {}
-        if dict is not None:
-            self.update(dict)
-        if len(kwargs):
-            self.update(kwargs)
-    def __repr__(self): return repr(self.data)
-    def __cmp__(self, dict):
-        if isinstance(dict, UserDict):
-            return cmp(self.data, dict.data)
-        else:
-            return cmp(self.data, dict)
-    __hash__ = None # Avoid Py3k warning
-    def __len__(self): return len(self.data)
-    def __getitem__(self, key):
-        if key in self.data:
-            return self.data[key]
-        if hasattr(self.__class__, "__missing__"):
-            return self.__class__.__missing__(self, key)
-        raise KeyError(key)
-    def __setitem__(self, key, item): self.data[key] = item
-    def __delitem__(self, key): del self.data[key]
-    def clear(self): self.data.clear()
-    def copy(self):
-        if self.__class__ is UserDict:
-            return UserDict(self.data.copy())
-        import copy
-        data = self.data
-        try:
-            self.data = {}
-            c = copy.copy(self)
-        finally:
-            self.data = data
-        c.update(self)
-        return c
-    def keys(self): return self.data.keys()
-    def items(self): return self.data.items()
-    def iteritems(self): return self.data.iteritems()
-    def iterkeys(self): return self.data.iterkeys()
-    def itervalues(self): return self.data.itervalues()
-    def values(self): return self.data.values()
-    def has_key(self, key): return key in self.data
-    def update(self, dict=None, **kwargs):
-        if dict is None:
-            pass
-        elif isinstance(dict, UserDict):
-            self.data.update(dict.data)
-        elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
-            self.data.update(dict)
-        else:
-            for k, v in dict.items():
-                self[k] = v
-        if len(kwargs):
-            self.data.update(kwargs)
-    def get(self, key, failobj=None):
-        if key not in self:
-            return failobj
-        return self[key]
-    def setdefault(self, key, failobj=None):
-        if key not in self:
-            self[key] = failobj
-        return self[key]
-    def pop(self, key, *args):
-        return self.data.pop(key, *args)
-    def popitem(self):
-        return self.data.popitem()
-    def __contains__(self, key):
-        return key in self.data
-    @classmethod
-    def fromkeys(cls, iterable, value=None):
-        d = cls()
-        for key in iterable:
-            d[key] = value
-        return d
-
-class IterableUserDict(UserDict):
-    def __iter__(self):
-        return iter(self.data)
-
-try:
-    import _abcoll
-except ImportError:
-    pass    # e.g. no '_weakref' module on this pypy
-else:
-    _abcoll.MutableMapping.register(IterableUserDict)
-
-
-class DictMixin:
-    # Mixin defining all dictionary methods for classes that already have
-    # a minimum dictionary interface including getitem, setitem, delitem,
-    # and keys. Without knowledge of the subclass constructor, the mixin
-    # does not define __init__() or copy().  In addition to the four base
-    # methods, progressively more efficiency comes with defining
-    # __contains__(), __iter__(), and iteritems().
-
-    # second level definitions support higher levels
-    def __iter__(self):
-        for k in self.keys():
-            yield k
-    def has_key(self, key):
-        try:
-            self[key]
-        except KeyError:
-            return False
-        return True
-    def __contains__(self, key):
-        return self.has_key(key)
-
-    # third level takes advantage of second level definitions
-    def iteritems(self):
-        for k in self:
-            yield (k, self[k])
-    def iterkeys(self):
-        return self.__iter__()
-
-    # fourth level uses definitions from lower levels
-    def itervalues(self):
-        for _, v in self.iteritems():
-            yield v
-    def values(self):
-        return [v for _, v in self.iteritems()]
-    def items(self):
-        return list(self.iteritems())
-    def clear(self):
-        for key in self.keys():
-            del self[key]
-    def setdefault(self, key, default=None):
-        try:
-            return self[key]
-        except KeyError:
-            self[key] = default
-        return default
-    def pop(self, key, *args):
-        if len(args) > 1:
-            raise TypeError, "pop expected at most 2 arguments, got "\
-                              + repr(1 + len(args))
-        try:
-            value = self[key]
-        except KeyError:
-            if args:
-                return args[0]
-            raise
-        del self[key]
-        return value
-    def popitem(self):
-        try:
-            k, v = self.iteritems().next()
-        except StopIteration:
-            raise KeyError, 'container is empty'
-        del self[k]
-        return (k, v)
-    def update(self, other=None, **kwargs):
-        # Make progressively weaker assumptions about "other"
-        if other is None:
-            pass
-        elif hasattr(other, 'iteritems'):  # iteritems saves memory and lookups
-            for k, v in other.iteritems():
-                self[k] = v
-        elif hasattr(other, 'keys'):
-            for k in other.keys():
-                self[k] = other[k]
-        else:
-            for k, v in other:
-                self[k] = v
-        if kwargs:
-            self.update(kwargs)
-    def get(self, key, default=None):
-        try:
-            return self[key]
-        except KeyError:
-            return default
-    def __repr__(self):
-        return repr(dict(self.iteritems()))
-    def __cmp__(self, other):
-        if other is None:
-            return 1
-        if isinstance(other, DictMixin):
-            other = dict(other.iteritems())
-        return cmp(dict(self.iteritems()), other)
-    def __len__(self):
-        return len(self.keys())
diff --git a/lib-python/modified-2.7/_threading_local.py b/lib-python/modified-2.7/_threading_local.py
deleted file mode 100644
--- a/lib-python/modified-2.7/_threading_local.py
+++ /dev/null
@@ -1,251 +0,0 @@
-"""Thread-local objects.
-
-(Note that this module provides a Python version of the threading.local
- class.  Depending on the version of Python you're using, there may be a
- faster one available.  You should always import the `local` class from
- `threading`.)
-
-Thread-local objects support the management of thread-local data.
-If you have data that you want to be local to a thread, simply create
-a thread-local object and use its attributes:
-
-  >>> mydata = local()
-  >>> mydata.number = 42
-  >>> mydata.number
-  42
-
-You can also access the local-object's dictionary:
-
-  >>> mydata.__dict__
-  {'number': 42}
-  >>> mydata.__dict__.setdefault('widgets', [])
-  []
-  >>> mydata.widgets
-  []
-
-What's important about thread-local objects is that their data are
-local to a thread. If we access the data in a different thread:
-
-  >>> log = []
-  >>> def f():
-  ...     items = mydata.__dict__.items()
-  ...     items.sort()
-  ...     log.append(items)
-  ...     mydata.number = 11
-  ...     log.append(mydata.number)
-
-  >>> import threading
-  >>> thread = threading.Thread(target=f)
-  >>> thread.start()
-  >>> thread.join()
-  >>> log
-  [[], 11]
-
-we get different data.  Furthermore, changes made in the other thread
-don't affect data seen in this thread:
-
-  >>> mydata.number
-  42
-
-Of course, values you get from a local object, including a __dict__
-attribute, are for whatever thread was current at the time the
-attribute was read.  For that reason, you generally don't want to save
-these values across threads, as they apply only to the thread they
-came from.
-
-You can create custom local objects by subclassing the local class:
-
-  >>> class MyLocal(local):
-  ...     number = 2
-  ...     initialized = False
-  ...     def __init__(self, **kw):
-  ...         if self.initialized:
-  ...             raise SystemError('__init__ called too many times')
-  ...         self.initialized = True
-  ...         self.__dict__.update(kw)
-  ...     def squared(self):
-  ...         return self.number ** 2
-
-This can be useful to support default values, methods and
-initialization.  Note that if you define an __init__ method, it will be
-called each time the local object is used in a separate thread.  This
-is necessary to initialize each thread's dictionary.
-
-Now if we create a local object:
-
-  >>> mydata = MyLocal(color='red')
-
-Now we have a default number:
-
-  >>> mydata.number
-  2
-
-an initial color:
-
-  >>> mydata.color
-  'red'
-  >>> del mydata.color
-
-And a method that operates on the data:
-
-  >>> mydata.squared()
-  4
-
-As before, we can access the data in a separate thread:
-
-  >>> log = []
-  >>> thread = threading.Thread(target=f)
-  >>> thread.start()
-  >>> thread.join()
-  >>> log
-  [[('color', 'red'), ('initialized', True)], 11]
-
-without affecting this thread's data:
-
-  >>> mydata.number
-  2
-  >>> mydata.color
-  Traceback (most recent call last):
-  ...
-  AttributeError: 'MyLocal' object has no attribute 'color'
-
-Note that subclasses can define slots, but they are not thread
-local. They are shared across threads:
-
-  >>> class MyLocal(local):
-  ...     __slots__ = 'number'
-
-  >>> mydata = MyLocal()
-  >>> mydata.number = 42
-  >>> mydata.color = 'red'
-
-So, the separate thread:
-
-  >>> thread = threading.Thread(target=f)
-  >>> thread.start()
-  >>> thread.join()
-
-affects what we see:
-
-  >>> mydata.number
-  11
-
->>> del mydata
-"""
-
-__all__ = ["local"]
-
-# We need to use objects from the threading module, but the threading
-# module may also want to use our `local` class, if support for locals
-# isn't compiled in to the `thread` module.  This creates potential problems
-# with circular imports.  For that reason, we don't import `threading`
-# until the bottom of this file (a hack sufficient to worm around the
-# potential problems).  Note that almost all platforms do have support for
-# locals in the `thread` module, and there is no circular import problem
-# then, so problems introduced by fiddling the order of imports here won't
-# manifest on most boxes.
-
-class _localbase(object):
-    __slots__ = '_local__key', '_local__args', '_local__lock'
-
-    def __new__(cls, *args, **kw):
-        self = object.__new__(cls)
-        key = '_local__key', 'thread.local.' + str(id(self))
-        object.__setattr__(self, '_local__key', key)
-        object.__setattr__(self, '_local__args', (args, kw))
-        object.__setattr__(self, '_local__lock', RLock())
-
-        if (args or kw) and (cls.__init__ == object.__init__):
-            raise TypeError("Initialization arguments are not supported")
-
-        # We need to create the thread dict in anticipation of
-        # __init__ being called, to make sure we don't call it
-        # again ourselves.
-        dict = object.__getattribute__(self, '__dict__')
-        current_thread().__dict__[key] = dict
-
-        return self
-
-def _patch(self):
-    key = object.__getattribute__(self, '_local__key')
-    d = current_thread().__dict__.get(key)
-    if d is None:
-        d = {}
-        current_thread().__dict__[key] = d
-        object.__setattr__(self, '__dict__', d)
-
-        # we have a new instance dict, so call out __init__ if we have
-        # one
-        cls = type(self)
-        if cls.__init__ is not object.__init__:
-            args, kw = object.__getattribute__(self, '_local__args')
-            cls.__init__(self, *args, **kw)
-    else:
-        object.__setattr__(self, '__dict__', d)
-
-class local(_localbase):
-
-    def __getattribute__(self, name):
-        lock = object.__getattribute__(self, '_local__lock')
-        lock.acquire()
-        try:
-            _patch(self)
-            return object.__getattribute__(self, name)
-        finally:
-            lock.release()
-
-    def __setattr__(self, name, value):
-        if name == '__dict__':
-            raise AttributeError(
-                "%r object attribute '__dict__' is read-only"
-                % self.__class__.__name__)
-        lock = object.__getattribute__(self, '_local__lock')
-        lock.acquire()
-        try:
-            _patch(self)
-            return object.__setattr__(self, name, value)
-        finally:
-            lock.release()
-
-    def __delattr__(self, name):
-        if name == '__dict__':
-            raise AttributeError(
-                "%r object attribute '__dict__' is read-only"
-                % self.__class__.__name__)
-        lock = object.__getattribute__(self, '_local__lock')
-        lock.acquire()
-        try:
-            _patch(self)
-            return object.__delattr__(self, name)
-        finally:
-            lock.release()
-
-    def __del__(self):
-        import threading
-
-        key = object.__getattribute__(self, '_local__key')
-
-        try:
-            # We use the non-locking API since we might already hold the lock
-            # (__del__ can be called at any point by the cyclic GC).
-            threads = threading._enumerate()
-        except:
-            # If enumerating the current threads fails, as it seems to do
-            # during shutdown, we'll skip cleanup under the assumption
-            # that there is nothing to clean up.
-            return
-
-        for thread in threads:
-            try:
-                __dict__ = thread.__dict__
-            except AttributeError:
-                # Thread is dying, rest in peace.
-                continue
-
-            if key in __dict__:
-                try:
-                    del __dict__[key]
-                except KeyError:
-                    pass # didn't have anything in this thread
-
-from threading import current_thread, RLock
diff --git a/lib-python/modified-2.7/ctypes/__init__.py b/lib-python/modified-2.7/ctypes/__init__.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/__init__.py
+++ /dev/null
@@ -1,554 +0,0 @@
-######################################################################
-#  This file should be kept compatible with Python 2.3, see PEP 291. #
-######################################################################
-"""create and manipulate C data types in Python"""
-
-import os as _os, sys as _sys
-
-__version__ = "1.1.0"
-
-import _ffi
-from _ctypes import Union, Structure, Array
-from _ctypes import _Pointer
-from _ctypes import CFuncPtr as _CFuncPtr
-from _ctypes import __version__ as _ctypes_version
-from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
-from _ctypes import ArgumentError
-
-from struct import calcsize as _calcsize
-
-if __version__ != _ctypes_version:
-    raise Exception("Version number mismatch", __version__, _ctypes_version)
-
-if _os.name in ("nt", "ce"):
-    from _ctypes import FormatError
-
-DEFAULT_MODE = RTLD_LOCAL
-if _os.name == "posix" and _sys.platform == "darwin":
-    # On OS X 10.3, we use RTLD_GLOBAL as default mode
-    # because RTLD_LOCAL does not work at least on some
-    # libraries.  OS X 10.3 is Darwin 7, so we check for
-    # that.
-
-    if int(_os.uname()[2].split('.')[0]) < 8:
-        DEFAULT_MODE = RTLD_GLOBAL
-
-from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \
-     FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI, \
-     FUNCFLAG_USE_ERRNO as _FUNCFLAG_USE_ERRNO, \
-     FUNCFLAG_USE_LASTERROR as _FUNCFLAG_USE_LASTERROR
-
-"""
-WINOLEAPI -> HRESULT
-WINOLEAPI_(type)
-
-STDMETHODCALLTYPE
-
-STDMETHOD(name)
-STDMETHOD_(type, name)
-
-STDAPICALLTYPE
-"""
-
-def create_string_buffer(init, size=None):
-    """create_string_buffer(aString) -> character array
-    create_string_buffer(anInteger) -> character array
-    create_string_buffer(aString, anInteger) -> character array
-    """
-    if isinstance(init, (str, unicode)):
-        if size is None:
-            size = len(init)+1
-        buftype = c_char * size
-        buf = buftype()
-        buf.value = init
-        return buf
-    elif isinstance(init, (int, long)):
-        buftype = c_char * init
-        buf = buftype()
-        return buf
-    raise TypeError(init)
-
-def c_buffer(init, size=None):
-##    "deprecated, use create_string_buffer instead"
-##    import warnings
-##    warnings.warn("c_buffer is deprecated, use create_string_buffer instead",
-##                  DeprecationWarning, stacklevel=2)
-    return create_string_buffer(init, size)
-
-_c_functype_cache = {}
-def CFUNCTYPE(restype, *argtypes, **kw):
-    """CFUNCTYPE(restype, *argtypes,
-                 use_errno=False, use_last_error=False) -> function prototype.
-
-    restype: the result type
-    argtypes: a sequence specifying the argument types
-
-    The function prototype can be called in different ways to create a
-    callable object:
-
-    prototype(integer address) -> foreign function
-    prototype(callable) -> create and return a C callable function from callable
-    prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method
-    prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal
-    prototype((function name, dll object)[, paramflags]) -> foreign function exported by name
-    """
-    flags = _FUNCFLAG_CDECL
-    if kw.pop("use_errno", False):
-        flags |= _FUNCFLAG_USE_ERRNO
-    if kw.pop("use_last_error", False):
-        flags |= _FUNCFLAG_USE_LASTERROR
-    if kw:
-        raise ValueError("unexpected keyword argument(s) %s" % kw.keys())
-    try:
-        return _c_functype_cache[(restype, argtypes, flags)]
-    except KeyError:
-        class CFunctionType(_CFuncPtr):
-            _argtypes_ = argtypes
-            _restype_ = restype
-            _flags_ = flags
-        _c_functype_cache[(restype, argtypes, flags)] = CFunctionType
-        return CFunctionType
-
-if _os.name in ("nt", "ce"):
-    from _ctypes import LoadLibrary as _dlopen
-    from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
-    if _os.name == "ce":
-        # 'ce' doesn't have the stdcall calling convention
-        _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL
-
-    _win_functype_cache = {}
-    def WINFUNCTYPE(restype, *argtypes, **kw):
-        # docstring set later (very similar to CFUNCTYPE.__doc__)
-        flags = _FUNCFLAG_STDCALL
-        if kw.pop("use_errno", False):
-            flags |= _FUNCFLAG_USE_ERRNO
-        if kw.pop("use_last_error", False):
-            flags |= _FUNCFLAG_USE_LASTERROR
-        if kw:
-            raise ValueError("unexpected keyword argument(s) %s" % kw.keys())
-        try:
-            return _win_functype_cache[(restype, argtypes, flags)]
-        except KeyError:
-            class WinFunctionType(_CFuncPtr):
-                _argtypes_ = argtypes
-                _restype_ = restype
-                _flags_ = flags
-            _win_functype_cache[(restype, argtypes, flags)] = WinFunctionType
-            return WinFunctionType
-    if WINFUNCTYPE.__doc__:
-        WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")
-
-elif _os.name == "posix":
-    from _ctypes import dlopen as _dlopen
-
-from _ctypes import sizeof, byref, addressof, alignment, resize
-from _ctypes import get_errno, set_errno
-from _ctypes import _SimpleCData
-
-def _check_size(typ, typecode=None):
-    # Check if sizeof(ctypes_type) against struct.calcsize.  This
-    # should protect somewhat against a misconfigured libffi.
-    from struct import calcsize
-    if typecode is None:
-        # Most _type_ codes are the same as used in struct
-        typecode = typ._type_
-    actual, required = sizeof(typ), calcsize(typecode)
-    if actual != required:
-        raise SystemError("sizeof(%s) wrong: %d instead of %d" % \
-                          (typ, actual, required))
-
-class py_object(_SimpleCData):
-    _type_ = "O"
-    def __repr__(self):
-        try:
-            return super(py_object, self).__repr__()
-        except ValueError:
-            return "%s(<NULL>)" % type(self).__name__
-_check_size(py_object, "P")
-
-class c_short(_SimpleCData):
-    _type_ = "h"
-_check_size(c_short)
-
-class c_ushort(_SimpleCData):
-    _type_ = "H"
-_check_size(c_ushort)
-
-class c_long(_SimpleCData):
-    _type_ = "l"
-_check_size(c_long)
-
-class c_ulong(_SimpleCData):
-    _type_ = "L"
-_check_size(c_ulong)
-
-if _calcsize("i") == _calcsize("l"):
-    # if int and long have the same size, make c_int an alias for c_long
-    c_int = c_long
-    c_uint = c_ulong
-else:
-    class c_int(_SimpleCData):
-        _type_ = "i"
-    _check_size(c_int)
-
-    class c_uint(_SimpleCData):
-        _type_ = "I"
-    _check_size(c_uint)
-
-class c_float(_SimpleCData):
-    _type_ = "f"
-_check_size(c_float)
-
-class c_double(_SimpleCData):
-    _type_ = "d"
-_check_size(c_double)
-
-class c_longdouble(_SimpleCData):
-    _type_ = "g"
-if sizeof(c_longdouble) == sizeof(c_double):
-    c_longdouble = c_double
-
-if _calcsize("l") == _calcsize("q"):
-    # if long and long long have the same size, make c_longlong an alias for c_long
-    c_longlong = c_long
-    c_ulonglong = c_ulong
-else:
-    class c_longlong(_SimpleCData):
-        _type_ = "q"
-    _check_size(c_longlong)
-
-    class c_ulonglong(_SimpleCData):
-        _type_ = "Q"
-    ##    def from_param(cls, val):
-    ##        return ('d', float(val), val)
-    ##    from_param = classmethod(from_param)
-    _check_size(c_ulonglong)
-
-class c_ubyte(_SimpleCData):
-    _type_ = "B"
-c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte
-# backward compatibility:
-##c_uchar = c_ubyte
-_check_size(c_ubyte)
-
-class c_byte(_SimpleCData):
-    _type_ = "b"
-c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte
-_check_size(c_byte)
-
-class c_char(_SimpleCData):
-    _type_ = "c"
-c_char.__ctype_le__ = c_char.__ctype_be__ = c_char
-_check_size(c_char)
-
-class c_char_p(_SimpleCData):
-    _type_ = "z"
-    if _os.name == "nt":
-        def __repr__(self):
-            if not windll.kernel32.IsBadStringPtrA(self, -1):
-                return "%s(%r)" % (self.__class__.__name__, self.value)
-            return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value)
-    else:
-        def __repr__(self):
-            return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value)
-_check_size(c_char_p, "P")
-
-class c_void_p(_SimpleCData):
-    _type_ = "P"
-c_voidp = c_void_p # backwards compatibility (to a bug)
-_check_size(c_void_p)
-
-class c_bool(_SimpleCData):
-    _type_ = "?"
-
-from _ctypes import POINTER, pointer, _pointer_type_cache
-
-try:
-    from _ctypes import set_conversion_mode
-except ImportError:
-    pass
-else:
-    if _os.name in ("nt", "ce"):
-        set_conversion_mode("mbcs", "ignore")
-    else:
-        set_conversion_mode("ascii", "strict")
-
-    class c_wchar_p(_SimpleCData):
-        _type_ = "Z"
-
-    class c_wchar(_SimpleCData):
-        _type_ = "u"
-
-    POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
-
-    def create_unicode_buffer(init, size=None):
-        """create_unicode_buffer(aString) -> character array
-        create_unicode_buffer(anInteger) -> character array
-        create_unicode_buffer(aString, anInteger) -> character array
-        """
-        if isinstance(init, (str, unicode)):
-            if size is None:
-                size = len(init)+1
-            buftype = c_wchar * size
-            buf = buftype()
-            buf.value = init
-            return buf
-        elif isinstance(init, (int, long)):
-            buftype = c_wchar * init
-            buf = buftype()
-            return buf
-        raise TypeError(init)
-
-POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
-
-# XXX Deprecated
-def SetPointerType(pointer, cls):
-    if _pointer_type_cache.get(cls, None) is not None:
-        raise RuntimeError("This type already exists in the cache")
-    if id(pointer) not in _pointer_type_cache:
-        raise RuntimeError("What's this???")
-    pointer.set_type(cls)
-    _pointer_type_cache[cls] = pointer
-    del _pointer_type_cache[id(pointer)]
-
-# XXX Deprecated
-def ARRAY(typ, len):
-    return typ * len
-
-################################################################
-
-
-class CDLL(object):
-    """An instance of this class represents a loaded dll/shared
-    library, exporting functions using the standard C calling
-    convention (named 'cdecl' on Windows).
-
-    The exported functions can be accessed as attributes, or by
-    indexing with the function name.  Examples:
-
-    <obj>.qsort -> callable object
-    <obj>['qsort'] -> callable object
-
-    Calling the functions releases the Python GIL during the call and
-    reacquires it afterwards.
-    """
-    _func_flags_ = _FUNCFLAG_CDECL
-    _func_restype_ = c_int
-
-    def __init__(self, name, mode=DEFAULT_MODE, handle=None,
-                 use_errno=False,
-                 use_last_error=False):
-        self._name = name
-        flags = self._func_flags_
-        if use_errno:
-            flags |= _FUNCFLAG_USE_ERRNO
-        if use_last_error:
-            flags |= _FUNCFLAG_USE_LASTERROR
-
-        class _FuncPtr(_CFuncPtr):
-            _flags_ = flags
-            _restype_ = self._func_restype_
-        self._FuncPtr = _FuncPtr
-
-        if handle is None:
-            self._handle = _ffi.CDLL(name, mode)
-        else:
-            self._handle = handle
-
-    def __repr__(self):
-        return "<%s '%s', handle %r at %x>" % \
-               (self.__class__.__name__, self._name,
-                (self._handle),
-                id(self) & (_sys.maxint*2 + 1))
-
-
-    def __getattr__(self, name):
-        if name.startswith('__') and name.endswith('__'):
-            raise AttributeError(name)
-        func = self.__getitem__(name)
-        setattr(self, name, func)
-        return func
-
-    def __getitem__(self, name_or_ordinal):
-        func = self._FuncPtr((name_or_ordinal, self))
-        if not isinstance(name_or_ordinal, (int, long)):
-            func.__name__ = name_or_ordinal
-        return func
-
-class PyDLL(CDLL):
-    """This class represents the Python library itself.  It allows to
-    access Python API functions.  The GIL is not released, and
-    Python exceptions are handled correctly.
-    """
-    _func_flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
-
-if _os.name in ("nt", "ce"):
-
-    class WinDLL(CDLL):
-        """This class represents a dll exporting functions using the
-        Windows stdcall calling convention.
-        """
-        _func_flags_ = _FUNCFLAG_STDCALL
-
-    # XXX Hm, what about HRESULT as normal parameter?
-    # Mustn't it derive from c_long then?
-    from _ctypes import _check_HRESULT, _SimpleCData
-    class HRESULT(_SimpleCData):
-        _type_ = "l"
-        # _check_retval_ is called with the function's result when it
-        # is used as restype.  It checks for the FAILED bit, and
-        # raises a WindowsError if it is set.
-        #
-        # The _check_retval_ method is implemented in C, so that the
-        # method definition itself is not included in the traceback
-        # when it raises an error - that is what we want (and Python
-        # doesn't have a way to raise an exception in the caller's
-        # frame).
-        _check_retval_ = _check_HRESULT
-
-    class OleDLL(CDLL):
-        """This class represents a dll exporting functions using the
-        Windows stdcall calling convention, and returning HRESULT.
-        HRESULT error values are automatically raised as WindowsError
-        exceptions.
-        """
-        _func_flags_ = _FUNCFLAG_STDCALL
-        _func_restype_ = HRESULT
-
-class LibraryLoader(object):
-    def __init__(self, dlltype):
-        self._dlltype = dlltype
-
-    def __getattr__(self, name):
-        if name[0] == '_':
-            raise AttributeError(name)
-        dll = self._dlltype(name)
-        setattr(self, name, dll)
-        return dll
-
-    def __getitem__(self, name):
-        return getattr(self, name)
-
-    def LoadLibrary(self, name):
-        return self._dlltype(name)
-
-cdll = LibraryLoader(CDLL)
-pydll = LibraryLoader(PyDLL)
-
-if _os.name in ("nt", "ce"):
-    pythonapi = PyDLL("python dll", None, _sys.dllhandle)
-elif _sys.platform == "cygwin":
-    pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
-else:
-    pythonapi = PyDLL(None)
-
-
-if _os.name in ("nt", "ce"):
-    windll = LibraryLoader(WinDLL)
-    oledll = LibraryLoader(OleDLL)
-
-    if _os.name == "nt":
-        GetLastError = windll.kernel32.GetLastError
-    else:
-        GetLastError = windll.coredll.GetLastError
-    from _ctypes import get_last_error, set_last_error
-
-    def WinError(code=None, descr=None):
-        if code is None:
-            code = GetLastError()
-        if descr is None:
-            descr = FormatError(code).strip()
-        return WindowsError(code, descr)
-
-_pointer_type_cache[None] = c_void_p
-
-if sizeof(c_uint) == sizeof(c_void_p):
-    c_size_t = c_uint
-    c_ssize_t = c_int
-elif sizeof(c_ulong) == sizeof(c_void_p):
-    c_size_t = c_ulong
-    c_ssize_t = c_long
-elif sizeof(c_ulonglong) == sizeof(c_void_p):
-    c_size_t = c_ulonglong
-    c_ssize_t = c_longlong
-
-# functions
-
-from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
-
-## void *memmove(void *, const void *, size_t);
-memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
-
-## void *memset(void *, int, size_t)
-memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
-
-def PYFUNCTYPE(restype, *argtypes):
-    class CFunctionType(_CFuncPtr):
-        _argtypes_ = argtypes
-        _restype_ = restype
-        _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
-    return CFunctionType
-
-def cast(obj, typ):
-    try:
-        c_void_p.from_param(obj)
-    except TypeError, e:
-        raise ArgumentError(str(e))
-    return _cast_addr(obj, obj, typ)
-
-_string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
-def string_at(ptr, size=-1):
-    """string_at(addr[, size]) -> string
-
-    Return the string at addr."""
-    return _string_at(ptr, size)
-
-try:
-    from _ctypes import _wstring_at_addr
-except ImportError:
-    pass
-else:
-    _wstring_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
-    def wstring_at(ptr, size=-1):
-        """wstring_at(addr[, size]) -> string
-
-        Return the string at addr."""
-        return _wstring_at(ptr, size)
-
-
-if _os.name in ("nt", "ce"): # COM stuff
-    def DllGetClassObject(rclsid, riid, ppv):
-        try:
-            ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
-        except ImportError:
-            return -2147221231 # CLASS_E_CLASSNOTAVAILABLE
-        else:
-            return ccom.DllGetClassObject(rclsid, riid, ppv)
-
-    def DllCanUnloadNow():
-        try:
-            ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
-        except ImportError:
-            return 0 # S_OK
-        return ccom.DllCanUnloadNow()
-
-from ctypes._endian import BigEndianStructure, LittleEndianStructure
-
-# Fill in specifically-sized types
-c_int8 = c_byte
-c_uint8 = c_ubyte
-for kind in [c_short, c_int, c_long, c_longlong]:
-    if sizeof(kind) == 2: c_int16 = kind
-    elif sizeof(kind) == 4: c_int32 = kind
-    elif sizeof(kind) == 8: c_int64 = kind
-for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
-    if sizeof(kind) == 2: c_uint16 = kind
-    elif sizeof(kind) == 4: c_uint32 = kind
-    elif sizeof(kind) == 8: c_uint64 = kind
-del(kind)
-
-# XXX for whatever reasons, creating the first instance of a callback
-# function is needed for the unittests on Win64 to succeed.  This MAY
-# be a compiler bug, since the problem occurs only when _ctypes is
-# compiled with the MS SDK compiler.  Or an uninitialized variable?
-CFUNCTYPE(c_int)(lambda: None)
diff --git a/lib-python/modified-2.7/ctypes/_endian.py b/lib-python/modified-2.7/ctypes/_endian.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/_endian.py
+++ /dev/null
@@ -1,60 +0,0 @@
-######################################################################
-#  This file should be kept compatible with Python 2.3, see PEP 291. #
-######################################################################
-import sys
-from ctypes import *
-
-_array_type = type(c_int * 3)
-
-def _other_endian(typ):
-    """Return the type with the 'other' byte order.  Simple types like
-    c_int and so on already have __ctype_be__ and __ctype_le__
-    attributes which contain the types, for more complicated types
-    only arrays are supported.
-    """
-    try:
-        return getattr(typ, _OTHER_ENDIAN)
-    except AttributeError:
-        if type(typ) == _array_type:
-            return _other_endian(typ._type_) * typ._length_
-        raise TypeError("This type does not support other endian: %s" % typ)
-
-class _swapped_meta(type(Structure)):
-    def __setattr__(self, attrname, value):
-        if attrname == "_fields_":
-            fields = []
-            for desc in value:
-                name = desc[0]
-                typ = desc[1]
-                rest = desc[2:]
-                fields.append((name, _other_endian(typ)) + rest)
-            value = fields
-        super(_swapped_meta, self).__setattr__(attrname, value)
-
-################################################################
-
-# Note: The Structure metaclass checks for the *presence* (not the
-# value!) of a _swapped_bytes_ attribute to determine the bit order in
-# structures containing bit fields.
-
-if sys.byteorder == "little":
-    _OTHER_ENDIAN = "__ctype_be__"
-
-    LittleEndianStructure = Structure
-
-    class BigEndianStructure(Structure):
-        """Structure with big endian byte order"""
-        __metaclass__ = _swapped_meta
-        _swappedbytes_ = None
-
-elif sys.byteorder == "big":
-    _OTHER_ENDIAN = "__ctype_le__"
-
-    BigEndianStructure = Structure
-    class LittleEndianStructure(Structure):
-        """Structure with little endian byte order"""
-        __metaclass__ = _swapped_meta
-        _swappedbytes_ = None
-
-else:
-    raise RuntimeError("Invalid byteorder")
diff --git a/lib-python/modified-2.7/ctypes/macholib/README.ctypes b/lib-python/modified-2.7/ctypes/macholib/README.ctypes
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/macholib/README.ctypes
+++ /dev/null
@@ -1,7 +0,0 @@
-Files in this directory from from Bob Ippolito's py2app.
-
-License: Any components of the py2app suite may be distributed under
-the MIT or PSF open source licenses.
-
-This is version 1.0, SVN revision 789, from 2006/01/25.
-The main repository is http://svn.red-bean.com/bob/macholib/trunk/macholib/
\ No newline at end of file
diff --git a/lib-python/modified-2.7/ctypes/macholib/__init__.py b/lib-python/modified-2.7/ctypes/macholib/__init__.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/macholib/__init__.py
+++ /dev/null
@@ -1,12 +0,0 @@
-######################################################################
-#  This file should be kept compatible with Python 2.3, see PEP 291. #
-######################################################################
-"""
-Enough Mach-O to make your head spin.
-
-See the relevant header files in /usr/include/mach-o
-
-And also Apple's documentation.
-"""
-
-__version__ = '1.0'
diff --git a/lib-python/modified-2.7/ctypes/macholib/dyld.py b/lib-python/modified-2.7/ctypes/macholib/dyld.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/macholib/dyld.py
+++ /dev/null
@@ -1,169 +0,0 @@
-######################################################################
-#  This file should be kept compatible with Python 2.3, see PEP 291. #
-######################################################################
-"""
-dyld emulation
-"""
-
-import os
-from framework import framework_info
-from dylib import dylib_info
-from itertools import *
-
-__all__ = [
-    'dyld_find', 'framework_find',
-    'framework_info', 'dylib_info',
-]
-
-# These are the defaults as per man dyld(1)
-#
-DEFAULT_FRAMEWORK_FALLBACK = [
-    os.path.expanduser("~/Library/Frameworks"),
-    "/Library/Frameworks",
-    "/Network/Library/Frameworks",
-    "/System/Library/Frameworks",
-]
-
-DEFAULT_LIBRARY_FALLBACK = [
-    os.path.expanduser("~/lib"),
-    "/usr/local/lib",
-    "/lib",
-    "/usr/lib",
-]
-
-def ensure_utf8(s):
-    """Not all of PyObjC and Python understand unicode paths very well yet"""
-    if isinstance(s, unicode):
-        return s.encode('utf8')
-    return s
-
-def dyld_env(env, var):
-    if env is None:
-        env = os.environ
-    rval = env.get(var)
-    if rval is None:
-        return []
-    return rval.split(':')
-
-def dyld_image_suffix(env=None):
-    if env is None:
-        env = os.environ
-    return env.get('DYLD_IMAGE_SUFFIX')
-
-def dyld_framework_path(env=None):
-    return dyld_env(env, 'DYLD_FRAMEWORK_PATH')
-
-def dyld_library_path(env=None):
-    return dyld_env(env, 'DYLD_LIBRARY_PATH')
-
-def dyld_fallback_framework_path(env=None):
-    return dyld_env(env, 'DYLD_FALLBACK_FRAMEWORK_PATH')
-
-def dyld_fallback_library_path(env=None):
-    return dyld_env(env, 'DYLD_FALLBACK_LIBRARY_PATH')
-
-def dyld_image_suffix_search(iterator, env=None):
-    """For a potential path iterator, add DYLD_IMAGE_SUFFIX semantics"""
-    suffix = dyld_image_suffix(env)
-    if suffix is None:
-        return iterator
-    def _inject(iterator=iterator, suffix=suffix):
-        for path in iterator:
-            if path.endswith('.dylib'):
-                yield path[:-len('.dylib')] + suffix + '.dylib'
-            else:
-                yield path + suffix
-            yield path
-    return _inject()
-
-def dyld_override_search(name, env=None):
-    # If DYLD_FRAMEWORK_PATH is set and this dylib_name is a
-    # framework name, use the first file that exists in the framework
-    # path if any.  If there is none go on to search the DYLD_LIBRARY_PATH
-    # if any.
-
-    framework = framework_info(name)
-
-    if framework is not None:
-        for path in dyld_framework_path(env):
-            yield os.path.join(path, framework['name'])
-
-    # If DYLD_LIBRARY_PATH is set then use the first file that exists
-    # in the path.  If none use the original name.
-    for path in dyld_library_path(env):
-        yield os.path.join(path, os.path.basename(name))
-
-def dyld_executable_path_search(name, executable_path=None):
-    # If we haven't done any searching and found a library and the
-    # dylib_name starts with "@executable_path/" then construct the
-    # library name.
-    if name.startswith('@executable_path/') and executable_path is not None:
-        yield os.path.join(executable_path, name[len('@executable_path/'):])
-
-def dyld_default_search(name, env=None):
-    yield name
-
-    framework = framework_info(name)
-
-    if framework is not None:
-        fallback_framework_path = dyld_fallback_framework_path(env)
-        for path in fallback_framework_path:
-            yield os.path.join(path, framework['name'])
-
-    fallback_library_path = dyld_fallback_library_path(env)
-    for path in fallback_library_path:
-        yield os.path.join(path, os.path.basename(name))
-
-    if framework is not None and not fallback_framework_path:
-        for path in DEFAULT_FRAMEWORK_FALLBACK:
-            yield os.path.join(path, framework['name'])
-
-    if not fallback_library_path:
-        for path in DEFAULT_LIBRARY_FALLBACK:
-            yield os.path.join(path, os.path.basename(name))
-
-def dyld_find(name, executable_path=None, env=None):
-    """
-    Find a library or framework using dyld semantics
-    """
-    name = ensure_utf8(name)
-    executable_path = ensure_utf8(executable_path)
-    for path in dyld_image_suffix_search(chain(
-                dyld_override_search(name, env),
-                dyld_executable_path_search(name, executable_path),
-                dyld_default_search(name, env),
-            ), env):
-        if os.path.isfile(path):
-            return path
-    raise ValueError("dylib %s could not be found" % (name,))
-
-def framework_find(fn, executable_path=None, env=None):
-    """
-    Find a framework using dyld semantics in a very loose manner.
-
-    Will take input such as:
-        Python
-        Python.framework
-        Python.framework/Versions/Current
-    """
-    try:
-        return dyld_find(fn, executable_path=executable_path, env=env)
-    except ValueError, e:
-        pass
-    fmwk_index = fn.rfind('.framework')
-    if fmwk_index == -1:
-        fmwk_index = len(fn)
-        fn += '.framework'
-    fn = os.path.join(fn, os.path.basename(fn[:fmwk_index]))
-    try:
-        return dyld_find(fn, executable_path=executable_path, env=env)
-    except ValueError:
-        raise e
-
-def test_dyld_find():
-    env = {}
-    assert dyld_find('libSystem.dylib') == '/usr/lib/libSystem.dylib'
-    assert dyld_find('System.framework/System') == '/System/Library/Frameworks/System.framework/System'
-
-if __name__ == '__main__':
-    test_dyld_find()
diff --git a/lib-python/modified-2.7/ctypes/macholib/dylib.py b/lib-python/modified-2.7/ctypes/macholib/dylib.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/macholib/dylib.py
+++ /dev/null
@@ -1,66 +0,0 @@
-######################################################################
-#  This file should be kept compatible with Python 2.3, see PEP 291. #
-######################################################################
-"""
-Generic dylib path manipulation
-"""
-
-import re
-
-__all__ = ['dylib_info']
-
-DYLIB_RE = re.compile(r"""(?x)
-(?P<location>^.*)(?:^|/)
-(?P<name>
-    (?P<shortname>\w+?)
-    (?:\.(?P<version>[^._]+))?
-    (?:_(?P<suffix>[^._]+))?
-    \.dylib$
-)
-""")
-
-def dylib_info(filename):
-    """
-    A dylib name can take one of the following four forms:
-        Location/Name.SomeVersion_Suffix.dylib
-        Location/Name.SomeVersion.dylib
-        Location/Name_Suffix.dylib
-        Location/Name.dylib
-
-    returns None if not found or a mapping equivalent to:
-        dict(
-            location='Location',
-            name='Name.SomeVersion_Suffix.dylib',
-            shortname='Name',
-            version='SomeVersion',
-            suffix='Suffix',
-        )
-
-    Note that SomeVersion and Suffix are optional and may be None
-    if not present.
-    """
-    is_dylib = DYLIB_RE.match(filename)
-    if not is_dylib:
-        return None
-    return is_dylib.groupdict()
-
-
-def test_dylib_info():
-    def d(location=None, name=None, shortname=None, version=None, suffix=None):
-        return dict(
-            location=location,
-            name=name,
-            shortname=shortname,
-            version=version,
-            suffix=suffix
-        )
-    assert dylib_info('completely/invalid') is None
-    assert dylib_info('completely/invalide_debug') is None
-    assert dylib_info('P/Foo.dylib') == d('P', 'Foo.dylib', 'Foo')
-    assert dylib_info('P/Foo_debug.dylib') == d('P', 'Foo_debug.dylib', 'Foo', suffix='debug')
-    assert dylib_info('P/Foo.A.dylib') == d('P', 'Foo.A.dylib', 'Foo', 'A')
-    assert dylib_info('P/Foo_debug.A.dylib') == d('P', 'Foo_debug.A.dylib', 'Foo_debug', 'A')
-    assert dylib_info('P/Foo.A_debug.dylib') == d('P', 'Foo.A_debug.dylib', 'Foo', 'A', 'debug')
-
-if __name__ == '__main__':
-    test_dylib_info()
diff --git a/lib-python/modified-2.7/ctypes/macholib/fetch_macholib b/lib-python/modified-2.7/ctypes/macholib/fetch_macholib
deleted file mode 100755
--- a/lib-python/modified-2.7/ctypes/macholib/fetch_macholib
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-svn export --force http://svn.red-bean.com/bob/macholib/trunk/macholib/ .
diff --git a/lib-python/modified-2.7/ctypes/macholib/fetch_macholib.bat b/lib-python/modified-2.7/ctypes/macholib/fetch_macholib.bat
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/macholib/fetch_macholib.bat
+++ /dev/null
@@ -1,1 +0,0 @@
-svn export --force http://svn.red-bean.com/bob/macholib/trunk/macholib/ .
diff --git a/lib-python/modified-2.7/ctypes/macholib/framework.py b/lib-python/modified-2.7/ctypes/macholib/framework.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/macholib/framework.py
+++ /dev/null
@@ -1,68 +0,0 @@
-######################################################################
-#  This file should be kept compatible with Python 2.3, see PEP 291. #
-######################################################################
-"""
-Generic framework path manipulation
-"""
-
-import re
-
-__all__ = ['framework_info']
-
-STRICT_FRAMEWORK_RE = re.compile(r"""(?x)
-(?P<location>^.*)(?:^|/)
-(?P<name>
-    (?P<shortname>\w+).framework/
-    (?:Versions/(?P<version>[^/]+)/)?
-    (?P=shortname)
-    (?:_(?P<suffix>[^_]+))?
-)$
-""")
-
-def framework_info(filename):
-    """
-    A framework name can take one of the following four forms:
-        Location/Name.framework/Versions/SomeVersion/Name_Suffix
-        Location/Name.framework/Versions/SomeVersion/Name
-        Location/Name.framework/Name_Suffix
-        Location/Name.framework/Name
-
-    returns None if not found, or a mapping equivalent to:
-        dict(
-            location='Location',
-            name='Name.framework/Versions/SomeVersion/Name_Suffix',
-            shortname='Name',
-            version='SomeVersion',
-            suffix='Suffix',
-        )
-
-    Note that SomeVersion and Suffix are optional and may be None
-    if not present
-    """
-    is_framework = STRICT_FRAMEWORK_RE.match(filename)
-    if not is_framework:
-        return None
-    return is_framework.groupdict()
-
-def test_framework_info():
-    def d(location=None, name=None, shortname=None, version=None, suffix=None):
-        return dict(
-            location=location,
-            name=name,
-            shortname=shortname,
-            version=version,
-            suffix=suffix
-        )
-    assert framework_info('completely/invalid') is None
-    assert framework_info('completely/invalid/_debug') is None
-    assert framework_info('P/F.framework') is None
-    assert framework_info('P/F.framework/_debug') is None
-    assert framework_info('P/F.framework/F') == d('P', 'F.framework/F', 'F')
-    assert framework_info('P/F.framework/F_debug') == d('P', 'F.framework/F_debug', 'F', suffix='debug')
-    assert framework_info('P/F.framework/Versions') is None
-    assert framework_info('P/F.framework/Versions/A') is None
-    assert framework_info('P/F.framework/Versions/A/F') == d('P', 'F.framework/Versions/A/F', 'F', 'A')
-    assert framework_info('P/F.framework/Versions/A/F_debug') == d('P', 'F.framework/Versions/A/F_debug', 'F', 'A', 'debug')
-
-if __name__ == '__main__':
-    test_framework_info()
diff --git a/lib-python/modified-2.7/ctypes/test/__init__.py b/lib-python/modified-2.7/ctypes/test/__init__.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/test/__init__.py
+++ /dev/null
@@ -1,221 +0,0 @@
-import os, sys, unittest, getopt, time
-
-use_resources = []
-
-class ResourceDenied(Exception):
-    """Test skipped because it requested a disallowed resource.
-
-    This is raised when a test calls requires() for a resource that
-    has not be enabled.  Resources are defined by test modules.
-    """
-
-def is_resource_enabled(resource):
-    """Test whether a resource is enabled.
-
-    If the caller's module is __main__ then automatically return True."""
-    if sys._getframe().f_back.f_globals.get("__name__") == "__main__":
-        return True
-    result = use_resources is not None and \
-           (resource in use_resources or "*" in use_resources)
-    if not result:
-        _unavail[resource] = None
-    return result
-
-_unavail = {}
-def requires(resource, msg=None):
-    """Raise ResourceDenied if the specified resource is not available.
-
-    If the caller's module is __main__ then automatically return True."""
-    # see if the caller's module is __main__ - if so, treat as if
-    # the resource was set
-    if sys._getframe().f_back.f_globals.get("__name__") == "__main__":
-        return
-    if not is_resource_enabled(resource):
-        if msg is None:
-            msg = "Use of the `%s' resource not enabled" % resource
-        raise ResourceDenied(msg)
-
-def find_package_modules(package, mask):
-    import fnmatch
-    if (hasattr(package, "__loader__") and
-            hasattr(package.__loader__, '_files')):
-        path = package.__name__.replace(".", os.path.sep)
-        mask = os.path.join(path, mask)
-        for fnm in package.__loader__._files.iterkeys():
-            if fnmatch.fnmatchcase(fnm, mask):
-                yield os.path.splitext(fnm)[0].replace(os.path.sep, ".")
-    else:
-        path = package.__path__[0]
-        for fnm in os.listdir(path):
-            if fnmatch.fnmatchcase(fnm, mask):
-                yield "%s.%s" % (package.__name__, os.path.splitext(fnm)[0])
-
-def get_tests(package, mask, verbosity, exclude=()):
-    """Return a list of skipped test modules, and a list of test cases."""
-    tests = []
-    skipped = []
-    for modname in find_package_modules(package, mask):
-        if modname.split(".")[-1] in exclude:
-            skipped.append(modname)
-            if verbosity > 1:
-                print >> sys.stderr, "Skipped %s: excluded" % modname
-            continue
-        try:
-            mod = __import__(modname, globals(), locals(), ['*'])
-        except ResourceDenied, detail:
-            skipped.append(modname)
-            if verbosity > 1:
-                print >> sys.stderr, "Skipped %s: %s" % (modname, detail)
-            continue
-        for name in dir(mod):
-            if name.startswith("_"):
-                continue
-            o = getattr(mod, name)
-            if type(o) is type(unittest.TestCase) and issubclass(o, unittest.TestCase):
-                tests.append(o)
-    return skipped, tests
-
-def usage():
-    print __doc__
-    return 1
-
-def test_with_refcounts(runner, verbosity, testcase):
-    """Run testcase several times, tracking reference counts."""
-    import gc
-    import ctypes
-    ptc = ctypes._pointer_type_cache.copy()
-    cfc = ctypes._c_functype_cache.copy()
-    wfc = ctypes._win_functype_cache.copy()
-
-    # when searching for refcount leaks, we have to manually reset any
-    # caches that ctypes has.
-    def cleanup():
-        ctypes._pointer_type_cache = ptc.copy()
-        ctypes._c_functype_cache = cfc.copy()
-        ctypes._win_functype_cache = wfc.copy()
-        gc.collect()
-
-    test = unittest.makeSuite(testcase)
-    for i in range(5):
-        rc = sys.gettotalrefcount()
-        runner.run(test)
-        cleanup()
-    COUNT = 5
-    refcounts = [None] * COUNT
-    for i in range(COUNT):
-        rc = sys.gettotalrefcount()
-        runner.run(test)
-        cleanup()
-        refcounts[i] = sys.gettotalrefcount() - rc
-    if filter(None, refcounts):
-        print "%s leaks:\n\t" % testcase, refcounts
-    elif verbosity:
-        print "%s: ok." % testcase
-
-class TestRunner(unittest.TextTestRunner):
-    def run(self, test, skipped):
-        "Run the given test case or test suite."
-        # Same as unittest.TextTestRunner.run, except that it reports
-        # skipped tests.
-        result = self._makeResult()
-        startTime = time.time()
-        test(result)
-        stopTime = time.time()
-        timeTaken = stopTime - startTime
-        result.printErrors()
-        self.stream.writeln(result.separator2)
-        run = result.testsRun
-        if _unavail: #skipped:
-            requested = _unavail.keys()
-            requested.sort()
-            self.stream.writeln("Ran %d test%s in %.3fs (%s module%s skipped)" %
-                                (run, run != 1 and "s" or "", timeTaken,
-                                 len(skipped),
-                                 len(skipped) != 1 and "s" or ""))
-            self.stream.writeln("Unavailable resources: %s" % ", ".join(requested))
-        else:
-            self.stream.writeln("Ran %d test%s in %.3fs" %
-                                (run, run != 1 and "s" or "", timeTaken))
-        self.stream.writeln()
-        if not result.wasSuccessful():
-            self.stream.write("FAILED (")
-            failed, errored = map(len, (result.failures, result.errors))
-            if failed:
-                self.stream.write("failures=%d" % failed)
-            if errored:
-                if failed: self.stream.write(", ")
-                self.stream.write("errors=%d" % errored)
-            self.stream.writeln(")")
-        else:
-            self.stream.writeln("OK")
-        return result
-
-
-def main(*packages):
-    try:
-        opts, args = getopt.getopt(sys.argv[1:], "rqvu:x:")
-    except getopt.error:
-        return usage()
-
-    verbosity = 1
-    search_leaks = False
-    exclude = []
-    for flag, value in opts:
-        if flag == "-q":
-            verbosity -= 1
-        elif flag == "-v":
-            verbosity += 1
-        elif flag == "-r":
-            try:
-                sys.gettotalrefcount
-            except AttributeError:
-                print >> sys.stderr, "-r flag requires Python debug build"
-                return -1
-            search_leaks = True
-        elif flag == "-u":
-            use_resources.extend(value.split(","))
-        elif flag == "-x":
-            exclude.extend(value.split(","))
-
-    mask = "test_*.py"
-    if args:
-        mask = args[0]
-
-    for package in packages:
-        run_tests(package, mask, verbosity, search_leaks, exclude)
-
-
-def run_tests(package, mask, verbosity, search_leaks, exclude):
-    skipped, testcases = get_tests(package, mask, verbosity, exclude)
-    runner = TestRunner(verbosity=verbosity)
-
-    suites = [unittest.makeSuite(o) for o in testcases]
-    suite = unittest.TestSuite(suites)
-    result = runner.run(suite, skipped)
-
-    if search_leaks:
-        # hunt for refcount leaks
-        runner = BasicTestRunner()
-        for t in testcases:
-            test_with_refcounts(runner, verbosity, t)
-
-    return bool(result.errors)
-
-class BasicTestRunner:
-    def run(self, test):
-        result = unittest.TestResult()
-        test(result)
-        return result
-
-def xfail(method):
-    """
-    Poor's man xfail: remove it when all the failures have been fixed
-    """
-    def new_method(self, *args, **kwds):
-        try:
-            method(self, *args, **kwds)
-        except:
-            pass
-        else:
-            self.assertTrue(False, "DID NOT RAISE")
-    return new_method
diff --git a/lib-python/modified-2.7/ctypes/test/runtests.py b/lib-python/modified-2.7/ctypes/test/runtests.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/test/runtests.py
+++ /dev/null
@@ -1,19 +0,0 @@
-"""Usage: runtests.py [-q] [-r] [-v] [-u resources] [mask]
-
-Run all tests found in this directory, and print a summary of the results.
-Command line flags:
-  -q     quiet mode: don't prnt anything while the tests are running
-  -r     run tests repeatedly, look for refcount leaks
-  -u<resources>
-         Add resources to the lits of allowed resources. '*' allows all
-         resources.
-  -v     verbose mode: print the test currently executed
-  -x<test1[,test2...]>
-         Exclude specified tests.
-  mask   mask to select filenames containing testcases, wildcards allowed
-"""
-import sys
-import ctypes.test
-
-if __name__ == "__main__":
-    sys.exit(ctypes.test.main(ctypes.test))
diff --git a/lib-python/modified-2.7/ctypes/test/test_anon.py b/lib-python/modified-2.7/ctypes/test/test_anon.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/test/test_anon.py
+++ /dev/null
@@ -1,60 +0,0 @@
-import unittest
-from ctypes import *
-
-class AnonTest(unittest.TestCase):
-
-    def test_anon(self):
-        class ANON(Union):
-            _fields_ = [("a", c_int),
-                        ("b", c_int)]
-
-        class Y(Structure):
-            _fields_ = [("x", c_int),
-                        ("_", ANON),
-                        ("y", c_int)]
-            _anonymous_ = ["_"]
-
-        self.assertEqual(Y.a.offset, sizeof(c_int))
-        self.assertEqual(Y.b.offset, sizeof(c_int))
-
-        self.assertEqual(ANON.a.offset, 0)
-        self.assertEqual(ANON.b.offset, 0)
-
-    def test_anon_nonseq(self):
-        # TypeError: _anonymous_ must be a sequence
-        self.assertRaises(TypeError,
-                              lambda: type(Structure)("Name",
-                                                      (Structure,),
-                                                      {"_fields_": [], "_anonymous_": 42}))
-
-    def test_anon_nonmember(self):
-        # AttributeError: type object 'Name' has no attribute 'x'
-        self.assertRaises(AttributeError,
-                              lambda: type(Structure)("Name",
-                                                      (Structure,),
-                                                      {"_fields_": [],
-                                                       "_anonymous_": ["x"]}))
-
-    def test_nested(self):
-        class ANON_S(Structure):
-            _fields_ = [("a", c_int)]
-
-        class ANON_U(Union):
-            _fields_ = [("_", ANON_S),
-                        ("b", c_int)]
-            _anonymous_ = ["_"]
-
-        class Y(Structure):
-            _fields_ = [("x", c_int),
-                        ("_", ANON_U),
-                        ("y", c_int)]
-            _anonymous_ = ["_"]
-
-        self.assertEqual(Y.x.offset, 0)
-        self.assertEqual(Y.a.offset, sizeof(c_int))
-        self.assertEqual(Y.b.offset, sizeof(c_int))
-        self.assertEqual(Y._.offset, sizeof(c_int))
-        self.assertEqual(Y.y.offset, sizeof(c_int) * 2)
-
-if __name__ == "__main__":
-    unittest.main()
diff --git a/lib-python/modified-2.7/ctypes/test/test_array_in_pointer.py b/lib-python/modified-2.7/ctypes/test/test_array_in_pointer.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/test/test_array_in_pointer.py
+++ /dev/null
@@ -1,64 +0,0 @@
-import unittest
-from ctypes import *
-from binascii import hexlify
-import re
-
-def dump(obj):
-    # helper function to dump memory contents in hex, with a hyphen
-    # between the bytes.
-    h = hexlify(memoryview(obj))
-    return re.sub(r"(..)", r"\1-", h)[:-1]
-
-
-class Value(Structure):
-    _fields_ = [("val", c_byte)]
-
-class Container(Structure):
-    _fields_ = [("pvalues", POINTER(Value))]
-
-class Test(unittest.TestCase):
-    def test(self):
-        # create an array of 4 values
-        val_array = (Value * 4)()
-
-        # create a container, which holds a pointer to the pvalues array.
-        c = Container()
-        c.pvalues = val_array
-
-        # memory contains 4 NUL bytes now, that's correct
-        self.assertEqual("00-00-00-00", dump(val_array))
-
-        # set the values of the array through the pointer:
-        for i in range(4):
-            c.pvalues[i].val = i + 1
-
-        values = [c.pvalues[i].val for i in range(4)]
-
-        # These are the expected results: here s the bug!
-        self.assertEqual(
-            (values, dump(val_array)),
-            ([1, 2, 3, 4], "01-02-03-04")
-        )
-
-    def test_2(self):
-
-        val_array = (Value * 4)()
-
-        # memory contains 4 NUL bytes now, that's correct
-        self.assertEqual("00-00-00-00", dump(val_array))
-
-        ptr = cast(val_array, POINTER(Value))
-        # set the values of the array through the pointer:
-        for i in range(4):
-            ptr[i].val = i + 1
-
-        values = [ptr[i].val for i in range(4)]
-
-        # These are the expected results: here s the bug!
-        self.assertEqual(
-            (values, dump(val_array)),
-            ([1, 2, 3, 4], "01-02-03-04")
-        )
-
-if __name__ == "__main__":
-    unittest.main()
diff --git a/lib-python/modified-2.7/ctypes/test/test_arrays.py b/lib-python/modified-2.7/ctypes/test/test_arrays.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/test/test_arrays.py
+++ /dev/null
@@ -1,145 +0,0 @@
-import unittest
-from ctypes import *
-from test.test_support import impl_detail
-
-formats = "bBhHiIlLqQfd"
-
-# c_longdouble commented out for PyPy, look at the commend in test_longdouble
-formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
-          c_long, c_ulonglong, c_float, c_double #, c_longdouble
-
-class ArrayTestCase(unittest.TestCase):
-
-    @impl_detail('long double not supported by PyPy', pypy=False)
-    def test_longdouble(self):
-        """
-        This test is empty. It's just here to remind that we commented out
-        c_longdouble in "formats". If pypy will ever supports c_longdouble, we
-        should kill this test and uncomment c_longdouble inside formats.
-        """
-
-    def test_simple(self):
-        # create classes holding simple numeric types, and check
-        # various properties.
-
-        init = range(15, 25)
-
-        for fmt in formats:
-            alen = len(init)
-            int_array = ARRAY(fmt, alen)
-
-            ia = int_array(*init)
-            # length of instance ok?
-            self.assertEqual(len(ia), alen)
-
-            # slot values ok?
-            values = [ia[i] for i in range(len(init))]
-            self.assertEqual(values, init)
-
-            # change the items
-            from operator import setitem
-            new_values = range(42, 42+alen)
-            [setitem(ia, n, new_values[n]) for n in range(alen)]
-            values = [ia[i] for i in range(len(init))]
-            self.assertEqual(values, new_values)
-
-            # are the items initialized to 0?
-            ia = int_array()
-            values = [ia[i] for i in range(len(init))]
-            self.assertEqual(values, [0] * len(init))
-
-            # Too many initializers should be caught
-            self.assertRaises(IndexError, int_array, *range(alen*2))
-
-        CharArray = ARRAY(c_char, 3)
-
-        ca = CharArray("a", "b", "c")
-
-        # Should this work? It doesn't:
-        # CharArray("abc")
-        self.assertRaises(TypeError, CharArray, "abc")
-
-        self.assertEqual(ca[0], "a")
-        self.assertEqual(ca[1], "b")
-        self.assertEqual(ca[2], "c")
-        self.assertEqual(ca[-3], "a")
-        self.assertEqual(ca[-2], "b")
-        self.assertEqual(ca[-1], "c")
-
-        self.assertEqual(len(ca), 3)
-
-        # slicing is now supported, but not extended slicing (3-argument)!
-        from operator import getslice, delitem
-        self.assertRaises(TypeError, getslice, ca, 0, 1, -1)
-
-        # cannot delete items
-        self.assertRaises(TypeError, delitem, ca, 0)
-
-    def test_numeric_arrays(self):
-
-        alen = 5
-
-        numarray = ARRAY(c_int, alen)
-
-        na = numarray()
-        values = [na[i] for i in range(alen)]
-        self.assertEqual(values, [0] * alen)
-
-        na = numarray(*[c_int()] * alen)
-        values = [na[i] for i in range(alen)]
-        self.assertEqual(values, [0]*alen)
-
-        na = numarray(1, 2, 3, 4, 5)
-        values = [i for i in na]
-        self.assertEqual(values, [1, 2, 3, 4, 5])
-
-        na = numarray(*map(c_int, (1, 2, 3, 4, 5)))
-        values = [i for i in na]
-        self.assertEqual(values, [1, 2, 3, 4, 5])
-
-    def test_classcache(self):
-        self.assertTrue(not ARRAY(c_int, 3) is ARRAY(c_int, 4))
-        self.assertTrue(ARRAY(c_int, 3) is ARRAY(c_int, 3))
-
-    def test_from_address(self):
-        # Failed with 0.9.8, reported by JUrner
-        p = create_string_buffer("foo")
-        sz = (c_char * 3).from_address(addressof(p))
-        self.assertEqual(sz[:], "foo")
-        self.assertEqual(sz[::], "foo")
-        self.assertEqual(sz[::-1], "oof")
-        self.assertEqual(sz[::3], "f")
-        self.assertEqual(sz[1:4:2], "o")
-        self.assertEqual(sz.value, "foo")
-
-    try:
-        create_unicode_buffer
-    except NameError:
-        pass
-    else:
-        def test_from_addressW(self):
-            p = create_unicode_buffer("foo")
-            sz = (c_wchar * 3).from_address(addressof(p))
-            self.assertEqual(sz[:], "foo")
-            self.assertEqual(sz[::], "foo")
-            self.assertEqual(sz[::-1], "oof")
-            self.assertEqual(sz[::3], "f")
-            self.assertEqual(sz[1:4:2], "o")
-            self.assertEqual(sz.value, "foo")
-
-    def test_cache(self):
-        # Array types are cached internally in the _ctypes extension,
-        # in a WeakValueDictionary.  Make sure the array type is
-        # removed from the cache when the itemtype goes away.  This
-        # test will not fail, but will show a leak in the testsuite.
-
-        # Create a new type:
-        class my_int(c_int):
-            pass
-        # Create a new array type based on it:
-        t1 = my_int * 1
-        t2 = my_int * 1
-        self.assertTrue(t1 is t2)
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/lib-python/modified-2.7/ctypes/test/test_as_parameter.py b/lib-python/modified-2.7/ctypes/test/test_as_parameter.py
deleted file mode 100644
--- a/lib-python/modified-2.7/ctypes/test/test_as_parameter.py
+++ /dev/null
@@ -1,227 +0,0 @@
-import unittest
-from ctypes import *
-import _ctypes_test
-
-dll = CDLL(_ctypes_test.__file__)
-
-try:
-    CALLBACK_FUNCTYPE = WINFUNCTYPE
-except NameError:
-    # fake to enable this test on Linux
-    CALLBACK_FUNCTYPE = CFUNCTYPE
-
-class POINT(Structure):
-    _fields_ = [("x", c_int), ("y", c_int)]
-
-class BasicWrapTestCase(unittest.TestCase):
-    def wrap(self, param):
-        return param
-
-    def test_wchar_parm(self):
-        try:
-            c_wchar
-        except NameError:
-            return
-        f = dll._testfunc_i_bhilfd
-        f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
-        result = f(self.wrap(1), self.wrap(u"x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
-        self.assertEqual(result, 139)
-        self.assertTrue(type(result), int)
-
-    def test_pointers(self):
-        f = dll._testfunc_p_p
-        f.restype = POINTER(c_int)
-        f.argtypes = [POINTER(c_int)]
-
-        # This only works if the value c_int(42) passed to the
-        # function is still alive while the pointer (the result) is
-        # used.
-
-        v = c_int(42)
-
-        self.assertEqual(pointer(v).contents.value, 42)
-        result = f(self.wrap(pointer(v)))
-        self.assertEqual(type(result), POINTER(c_int))
-        self.assertEqual(result.contents.value, 42)
-
-        # This on works...
-        result = f(self.wrap(pointer(v)))
-        self.assertEqual(result.contents.value, v.value)
-
-        p = pointer(c_int(99))
-        result = f(self.wrap(p))
-        self.assertEqual(result.contents.value, 99)
-
-    def test_shorts(self):
-        f = dll._testfunc_callback_i_if
-
-        args = []
-        expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
-                    1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
-
-        def callback(v):
-            args.append(v)
-            return v
-
-        CallBack = CFUNCTYPE(c_int, c_int)
-
-        cb = CallBack(callback)
-        f(self.wrap(2**18), self.wrap(cb))
-        self.assertEqual(args, expected)
-
-    ################################################################
-
-    def test_callbacks(self):
-        f = dll._testfunc_callback_i_if
-        f.restype = c_int
-
-        MyCallback = CFUNCTYPE(c_int, c_int)
-
-        def callback(value):
-            #print "called back with", value
-            return value
-
-        cb = MyCallback(callback)
-
-        result = f(self.wrap(-10), self.wrap(cb))
-        self.assertEqual(result, -18)
-
-        # test with prototype
-        f.argtypes = [c_int, MyCallback]
-        cb = MyCallback(callback)
-
-        result = f(self.wrap(-10), self.wrap(cb))
-        self.assertEqual(result, -18)
-
-        result = f(self.wrap(-10), self.wrap(cb))
-        self.assertEqual(result, -18)
-
-        AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
-
-        # check that the prototype works: we call f with wrong
-        # argument types
-        cb = AnotherCallback(callback)
-        self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
-
-    def test_callbacks_2(self):
-        # Can also use simple datatypes as argument type specifiers
-        # for the callback function.
-        # In this case the call receives an instance of that type
-        f = dll._testfunc_callback_i_if
-        f.restype = c_int
-
-        MyCallback = CFUNCTYPE(c_int, c_int)
-
-        f.argtypes = [c_int, MyCallback]
-
-        def callback(value):
-            #print "called back with", value
-            self.assertEqual(type(value), int)
-            return value
-
-        cb = MyCallback(callback)
-        result = f(self.wrap(-10), self.wrap(cb))
-        self.assertEqual(result, -18)
-
-    def test_longlong_callbacks(self):
-
-        f = dll._testfunc_callback_q_qf
-        f.restype = c_longlong
-
-        MyCallback = CFUNCTYPE(c_longlong, c_longlong)
-
-        f.argtypes = [c_longlong, MyCallback]
-
-        def callback(value):
-            self.assertTrue(isinstance(value, (int, long)))
-            return value & 0x7FFFFFFF
-
-        cb = MyCallback(callback)
-
-        self.assertEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
-
-    def test_byval(self):
-        # without prototype
-        ptin = POINT(1, 2)
-        ptout = POINT()
-        # EXPORT int _testfunc_byval(point in, point *pout)
-        result = dll._testfunc_byval(ptin, byref(ptout))
-        got = result, ptout.x, ptout.y
-        expected = 3, 1, 2
-        self.assertEqual(got, expected)
-
-        # with prototype
-        ptin = POINT(101, 102)
-        ptout = POINT()
-        dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
-        dll._testfunc_byval.restype = c_int
-        result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
-        got = result, ptout.x, ptout.y
-        expected = 203, 101, 102
-        self.assertEqual(got, expected)
-
-    def test_struct_return_2H(self):
-        class S2H(Structure):
-            _fields_ = [("x", c_short),
-                        ("y", c_short)]
-        dll.ret_2h_func.restype = S2H
-        dll.ret_2h_func.argtypes = [S2H]
-        inp = S2H(99, 88)
-        s2h = dll.ret_2h_func(self.wrap(inp))
-        self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
-
-    def test_struct_return_8H(self):
-        class S8I(Structure):
-            _fields_ = [("a", c_int),
-                        ("b", c_int),
-                        ("c", c_int),
-                        ("d", c_int),
-                        ("e", c_int),
-                        ("f", c_int),
-                        ("g", c_int),
-                        ("h", c_int)]


More information about the pypy-commit mailing list