[pypy-commit] pypy stdlib-3.2.5: hg merge py3k

amauryfa noreply at buildbot.pypy.org
Wed Apr 2 02:58:06 CEST 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: stdlib-3.2.5
Changeset: r70388:911b0a74e75d
Date: 2014-04-01 23:22 +0200
http://bitbucket.org/pypy/pypy/changeset/911b0a74e75d/

Log:	hg merge py3k

diff too long, truncating to 2000 out of 8265 lines

diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -64,6 +64,7 @@
 ^pypy/goal/pypy-jvm.jar
 ^pypy/goal/.+\.exe$
 ^pypy/goal/.+\.dll$
+^pypy/goal/.+\.lib$
 ^pypy/_cache$
 ^pypy/doc/statistic/.+\.html$
 ^pypy/doc/statistic/.+\.eps$
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
@@ -301,6 +301,7 @@
                 self.fail("readlines() after next() with empty buffer "
                           "failed. Got %r, expected %r" % (line, testline))
             # Reading after iteration hit EOF shouldn't hurt either
+            f.close()
             f = self.open(TESTFN, 'rb')
             try:
                 for line in f:
diff --git a/lib-python/2.7/test/test_genericpath.py b/lib-python/2.7/test/test_genericpath.py
--- a/lib-python/2.7/test/test_genericpath.py
+++ b/lib-python/2.7/test/test_genericpath.py
@@ -231,9 +231,14 @@
         unicwd = u'\xe7w\xf0'
         try:
             fsencoding = test_support.TESTFN_ENCODING or "ascii"
-            unicwd.encode(fsencoding)
+            asciival = unicwd.encode(fsencoding)
+            if fsencoding == "mbcs":
+                # http://bugs.python.org/issue850997
+                v = asciival.find('?')
+                if v >= 0:
+                    raise UnicodeEncodeError(fsencoding, unicwd, v, v, asciival)
         except (AttributeError, UnicodeEncodeError):
-            # FS encoding is probably ASCII
+            # FS encoding is probably ASCII or windows and codepage is non-Latin1
             pass
         else:
             with test_support.temp_cwd(unicwd):
diff --git a/lib-python/2.7/test/test_httpservers.py b/lib-python/2.7/test/test_httpservers.py
--- a/lib-python/2.7/test/test_httpservers.py
+++ b/lib-python/2.7/test/test_httpservers.py
@@ -335,6 +335,7 @@
             response = self.request(self.tempdir_name + '/')
             self.check_status_and_reason(response, 404)
             os.chmod(self.tempdir, 0755)
+        f.close()    
 
     def test_head(self):
         response = self.request(
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
@@ -171,7 +171,7 @@
                 # very inconsisten on CPython. In PyPy, memoryview supports
                 # the buffer interface, and thus the following comparison
                 # succeeds. See also the comment in
-                # pypy.modules.__builtin__.interp_memoryview.W_MemoryView.descr_buffer
+                # pypy.objspace.std.memoryview.W_MemoryView.descr_buffer
                 #
                 # Comparison with objects which don't support the buffer API
                 self.assertFalse(m == u"abcdef", "%s %s" % (self, tp))
diff --git a/lib-python/3/test/test_aifc.py b/lib-python/3/test/test_aifc.py
--- a/lib-python/3/test/test_aifc.py
+++ b/lib-python/3/test/test_aifc.py
@@ -77,7 +77,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)
+    @impl_detail("PyPy has no audioop.lin2ulaw 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/3/test/test_sunau.py b/lib-python/3/test/test_sunau.py
--- a/lib-python/3/test/test_sunau.py
+++ b/lib-python/3/test/test_sunau.py
@@ -1,4 +1,4 @@
-from test.support import run_unittest, TESTFN
+from test.support import run_unittest, TESTFN, impl_detail
 import unittest
 import os
 
@@ -41,6 +41,7 @@
         self.assertEqual(self.f.readframes(nframes), output)
         self.f.close()
 
+    @impl_detail(pypy=False)
     def test_ulaw(self):
         self.f = sunau.open(TESTFN, 'w')
         self.f.setnchannels(nchannels)
diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -136,7 +136,7 @@
             return self.value
 
     def __buffer__(self):
-        return self._buffer.__buffer__()
+        return memoryview(self._buffer)
 
     def _get_b_base(self):
         try:
diff --git a/lib_pypy/_ctypes_test.py b/lib_pypy/_ctypes_test.py
--- a/lib_pypy/_ctypes_test.py
+++ b/lib_pypy/_ctypes_test.py
@@ -1,3 +1,5 @@
+import imp, os
+
 try:
     import cpyext
 except ImportError:
@@ -10,4 +12,12 @@
     pass    # obscure condition of _ctypes_test.py being imported by py.test
 else:
     import _pypy_testcapi
-    _pypy_testcapi.compile_shared('_ctypes_test.c', '_ctypes_test')
+    cfile = '_ctypes_test.c'
+    thisdir = os.path.dirname(__file__)
+    output_dir = _pypy_testcapi.get_hashed_dir(os.path.join(thisdir, cfile))
+    try:
+        fp, filename, description = imp.find_module('_ctypes_test', path=[output_dir])
+        imp.load_module('_ctypes_test', fp, filename, description)
+    except ImportError:
+        print 'could not find _ctypes_test in',output_dir
+        _pypy_testcapi.compile_shared('_ctypes_test.c', '_ctypes_test', output_dir)
diff --git a/lib_pypy/_pypy_testcapi.py b/lib_pypy/_pypy_testcapi.py
--- a/lib_pypy/_pypy_testcapi.py
+++ b/lib_pypy/_pypy_testcapi.py
@@ -1,5 +1,22 @@
 import os, sys, imp
-import tempfile
+import tempfile, binascii
+
+def get_hashed_dir(cfile):
+    with open(cfile,'r') as fid:
+        content = fid.read()
+    # from cffi's Verifier()
+    key = '\x00'.join([sys.version[:3], content])
+    if sys.version_info >= (3,):
+        key = key.encode('utf-8')
+    k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff)
+    k1 = k1.lstrip('0x').rstrip('L')
+    k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
+    k2 = k2.lstrip('0').rstrip('L')
+    output_dir = tempfile.gettempdir() + os.path.sep + 'tmp_%s%s' %(k1, k2)
+    if not os.path.exists(output_dir):
+        os.mkdir(output_dir)
+    return output_dir 
+
 
 def _get_c_extension_suffix():
     for ext, mod, typ in imp.get_suffixes():
@@ -7,12 +24,13 @@
             return ext
 
 
-def compile_shared(csource, modulename):
+def compile_shared(csource, modulename, output_dir=None):
     """Compile '_testcapi.c' or '_ctypes_test.c' into an extension module,
     and import it.
     """
     thisdir = os.path.dirname(__file__)
-    output_dir = tempfile.mkdtemp()
+    if output_dir is None:
+        output_dir = tempfile.mkdtemp()
 
     from distutils.ccompiler import new_compiler
 
diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -38,6 +38,7 @@
 
 if sys.version_info[0] >= 3:
     StandardError = Exception
+    cmp = lambda x, y: (x > y) - (x < y)
     long = int
     xrange = range
     basestring = unicode = str
diff --git a/lib_pypy/_testcapi.py b/lib_pypy/_testcapi.py
--- a/lib_pypy/_testcapi.py
+++ b/lib_pypy/_testcapi.py
@@ -1,7 +1,17 @@
+import imp, os
+
 try:
     import cpyext
 except ImportError:
     raise ImportError("No module named '_testcapi'")
-else:
-    import _pypy_testcapi
-    _pypy_testcapi.compile_shared('_testcapimodule.c', '_testcapi')
+
+import _pypy_testcapi
+cfile = '_testcapimodule.c'
+thisdir = os.path.dirname(__file__)
+output_dir = _pypy_testcapi.get_hashed_dir(os.path.join(thisdir, cfile))
+
+try:
+    fp, filename, description = imp.find_module('_testcapi', path=[output_dir])
+    imp.load_module('_testcapi', fp, filename, description)
+except ImportError:
+    _pypy_testcapi.compile_shared(cfile, '_testcapi', output_dir)
diff --git a/pypy/doc/cpython_differences.rst b/pypy/doc/cpython_differences.rst
--- a/pypy/doc/cpython_differences.rst
+++ b/pypy/doc/cpython_differences.rst
@@ -292,6 +292,10 @@
   depending on the compiler settings, the default of 768KB is enough
   for about 1400 calls.
 
+* since the implementation of dictionary is different, the exact number
+  which ``__hash__`` and ``__eq__`` are called is different. Since CPython
+  does not give any specific guarantees either, don't rely on it.
+
 * assignment to ``__class__`` is limited to the cases where it
   works on CPython 2.5.  On CPython 2.6 and 2.7 it works in a bit
   more cases, which are not supported by PyPy so far.  (If needed,
diff --git a/pypy/doc/embedding.rst b/pypy/doc/embedding.rst
--- a/pypy/doc/embedding.rst
+++ b/pypy/doc/embedding.rst
@@ -51,6 +51,8 @@
 
 .. function:: int pypy_execute_source_ptr(char* source, void* ptr);
 
+   .. note:: Not available in PyPy <= 2.2.1
+   
    Just like the above, except it registers a magic argument in the source
    scope as ``c_argument``, where ``void*`` is encoded as Python int.
 
@@ -100,9 +102,29 @@
 
 Worked!
 
+.. note:: If the compilation fails because of missing PyPy.h header file,
+          you are running PyPy <= 2.2.1, please see the section `Missing PyPy.h`_.
+
+Missing PyPy.h
+--------------
+
+.. note:: PyPy.h is in the nightly builds and goes to new PyPy releases (>2.2.1).
+
+For PyPy <= 2.2.1, you can download PyPy.h from PyPy repository (it has been added in commit c4cd6ec):
+
+.. code-block:: bash
+
+    cd /opt/pypy/include
+    wget https://bitbucket.org/pypy/pypy/raw/c4cd6eca9358066571500ac82aaacfdaa3889e8c/include/PyPy.h
+
+
 More advanced example
 ---------------------
 
+.. note:: This example depends on pypy_execute_source_ptr which is not available
+          in PyPy <= 2.2.1. You might want to see the alternative example
+          below.
+
 Typically we need something more to do than simply execute source. The following
 is a fully fledged example, please consult cffi documentation for details.
 It's a bit longish, but it captures a gist what can be done with the PyPy
@@ -161,6 +183,97 @@
 is that we would pass a struct full of callbacks to ``pypy_execute_source_ptr``
 and fill the structure from Python side for the future use.
 
+Alternative example
+-------------------
+
+As ``pypy_execute_source_ptr`` is not available in PyPy 2.2.1, you might want to try 
+an alternative approach which relies on -export-dynamic flag to the GNU linker. 
+The downside to this approach is that it is platform dependent.
+
+.. code-block:: c
+
+    #include "include/PyPy.h"
+    #include <stdio.h>
+
+    char source[] = "from cffi import FFI\n\
+    ffi = FFI()\n\
+    @ffi.callback('int(int)')\n\
+    def func(a):\n\
+        print 'Got from C %d' % a\n\
+        return a * 2\n\
+    ffi.cdef('int callback(int (*func)(int));')\n\
+    lib = ffi.verify('int callback(int (*func)(int));')\n\
+    lib.callback(func)\n\
+    print 'finished the Python part'\n\
+    ";
+
+    int callback(int (*func)(int))
+    {
+        printf("Calling to Python, result: %d\n", func(3));
+    }
+
+    int main()
+    {
+        int res;
+        void *lib, *func;
+
+        rpython_startup_code();
+        res = pypy_setup_home("/opt/pypy/bin/libpypy-c.so", 1);
+        if (res) {
+            printf("Error setting pypy home!\n");
+            return 1;
+        }
+        res = pypy_execute_source(source);
+        if (res) {
+            printf("Error calling pypy_execute_source!\n");
+        }
+        return res;
+    }
+
+
+Make sure to pass -export-dynamic flag when compiling::
+
+   $ gcc -g -o x x.c -lpypy-c -L. -export-dynamic
+   $ LD_LIBRARY_PATH=. ./x
+   Got from C 3
+   Calling to Python, result: 6
+   finished the Python part
+
+Finding pypy_home
+-----------------
+
+Function pypy_setup_home takes one parameter - the path to libpypy. There's 
+currently no "clean" way (pkg-config comes to mind) how to find this path. You 
+can try the following (GNU-specific) hack (don't forget to link against *dl*):
+
+.. code-block:: c
+
+    #if !(_GNU_SOURCE)
+    #define _GNU_SOURCE
+    #endif
+
+    #include <dlfcn.h>
+    #include <limits.h>
+    #include <stdlib.h>
+
+    // caller should free returned pointer to avoid memleaks
+    // returns NULL on error
+    char* guess_pypyhome() {
+        // glibc-only (dladdr is why we #define _GNU_SOURCE)
+        Dl_info info;
+        void *_rpython_startup_code = dlsym(0,"rpython_startup_code");
+        if (_rpython_startup_code == 0) {
+            return 0;
+        }
+        if (dladdr(_rpython_startup_code, &info) != 0) {
+            const char* lib_path = info.dli_fname;
+            char* lib_realpath = realpath(lib_path, 0);
+            return lib_realpath;
+        }
+        return 0;
+    }
+
+
 Threading
 ---------
 
diff --git a/pypy/doc/extradoc.rst b/pypy/doc/extradoc.rst
--- a/pypy/doc/extradoc.rst
+++ b/pypy/doc/extradoc.rst
@@ -87,7 +87,7 @@
 .. _`Back to the Future in One Week -- Implementing a Smalltalk VM in PyPy`: http://dx.doi.org/10.1007/978-3-540-89275-5_7
 .. _`Automatic generation of JIT compilers for dynamic languages in .NET`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ecoop2009/main.pdf
 .. _`Core Object Optimization Results`: https://bitbucket.org/pypy/extradoc/raw/tip/eu-report/D06.1_Core_Optimizations-2007-04-30.pdf
-.. _`Compiling Dynamic Language Implementations`: http://codespeak.net/pypy/extradoc/eu-report/D05.1_Publish_on_translating_a_very-high-level_description.pdf
+.. _`Compiling Dynamic Language Implementations`: https://bitbucket.org/pypy/extradoc/raw/tip/eu-report/D05.1_Publish_on_translating_a_very-high-level_description.pdf
 
 
 Talks and Presentations 
@@ -258,24 +258,24 @@
 
 .. _`PyCon 2010`: http://morepypy.blogspot.com/2010/02/pycon-2010-report.html
 .. _`RuPy 2009`: http://morepypy.blogspot.com/2009/11/pypy-on-rupy-2009.html
-.. _`PyPy 3000`: http://codespeak.net/pypy/extradoc/talk/ep2006/pypy3000.txt
-.. _`What can PyPy do for you`: http://codespeak.net/pypy/extradoc/talk/ep2006/usecases-slides.html
-.. _`PyPy introduction at EuroPython 2006`: http://codespeak.net/pypy/extradoc/talk/ep2006/intro.pdf
-.. _`PyPy - the new Python implementation on the block`: http://codespeak.net/pypy/extradoc/talk/22c3/hpk-tech.html
-.. _`PyPy development method`: http://codespeak.net/pypy/extradoc/talk/pycon2006/method_talk.html
-.. _`PyPy intro`: http://codespeak.net/pypy/extradoc/talk/accu2006/accu-2006.pdf 
-.. _oscon2003-paper: http://codespeak.net/pypy/extradoc/talk/oscon2003-paper.html
-.. _`Architecture introduction slides`: http://codespeak.net/pypy/extradoc/talk/amsterdam-sprint-intro.pdf
-.. _`EU funding for FOSS`: http://codespeak.net/pypy/extradoc/talk/2004-21C3-pypy-EU-hpk.pdf
-.. _`py lib slides`: http://codespeak.net/pypy/extradoc/talk/2005-pycon-py.pdf
-.. _`PyCon 2005`: http://codespeak.net/pypy/extradoc/talk/pypy-talk-pycon2005/README.html
-.. _`Trouble in Paradise`: http://codespeak.net/pypy/extradoc/talk/agile2006/during-oss-sprints_talk.pdf
-.. _`Sprint Driven Development`: http://codespeak.net/pypy/extradoc/talk/xp2006/during-xp2006-sprints.pdf
-.. _`Kill -1`: http://codespeak.net/pypy/extradoc/talk/ep2006/kill_1_agiletalk.pdf
-.. _`Open Source, EU-Funding and Agile Methods`: http://codespeak.net/pypy/extradoc/talk/22c3/agility.pdf
-.. _`PyPy Status`: http://codespeak.net/pypy/extradoc/talk/vancouver/talk.html
+.. _`PyPy 3000`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ep2006/pypy3000.txt
+.. _`What can PyPy do for you`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ep2006/usecases-slides.txt
+.. _`PyPy introduction at EuroPython 2006`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ep2006/intro.pdf
+.. _`PyPy - the new Python implementation on the block`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/22c3/hpk-tech.txt
+.. _`PyPy development method`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/pycon2006/method_talk.txt
+.. _`PyPy intro`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/accu2006/accu-2006.pdf 
+.. _oscon2003-paper: https://bitbucket.org/pypy/extradoc/raw/tip/talk/oscon2003-paper.txt
+.. _`Architecture introduction slides`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/amsterdam-sprint-intro.pdf
+.. _`EU funding for FOSS`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/2004-21C3-pypy-EU-hpk.pdf
+.. _`py lib slides`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/2005-pycon-py.pdf
+.. _`PyCon 2005`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/pypy-talk-pycon2005/README.txt
+.. _`Trouble in Paradise`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/agile2006/during-oss-sprints_talk.pdf
+.. _`Sprint Driven Development`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/xp2006/during-xp2006-sprints.pdf
+.. _`Kill -1`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ep2006/kill_1_agiletalk.pdf
+.. _`Open Source, EU-Funding and Agile Methods`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/22c3/agility.pdf
+.. _`PyPy Status`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/vancouver/
 .. _`Sprinting the PyPy way`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ep2005/pypy_sprinttalk_ep2005bd.pdf
-.. _`PyPy's VM Approach`: http://codespeak.net/pypy/extradoc/talk/dls2006/talk.html
+.. _`PyPy's VM Approach`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/dls2006/
 .. _`PyPy's approach to virtual machine construction`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/dls2006/pypy-vm-construction.pdf
 .. _`EuroPython talks 2009`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/ep2009/
 .. _`PyCon talks 2009`: https://bitbucket.org/pypy/extradoc/raw/tip/talk/pycon2009/
diff --git a/pypy/doc/garbage_collection.rst b/pypy/doc/garbage_collection.rst
--- a/pypy/doc/garbage_collection.rst
+++ b/pypy/doc/garbage_collection.rst
@@ -14,7 +14,7 @@
 The present document describes the specific garbage collectors that we
 wrote in our framework.
 
-.. _`EU-report on this topic`: http://codespeak.net/pypy/extradoc/eu-report/D07.1_Massive_Parallelism_and_Translation_Aspects-2007-02-28.pdf
+.. _`EU-report on this topic`: https://bitbucket.org/pypy/extradoc/raw/tip/eu-report/D07.1_Massive_Parallelism_and_Translation_Aspects-2007-02-28.pdf
 
 
 Garbage collectors currently written for the GC framework
diff --git a/pypy/doc/getting-started-dev.rst b/pypy/doc/getting-started-dev.rst
--- a/pypy/doc/getting-started-dev.rst
+++ b/pypy/doc/getting-started-dev.rst
@@ -389,7 +389,7 @@
 .. _`pypy-dev mailing list`: http://mail.python.org/mailman/listinfo/pypy-dev
 .. _`contact possibilities`: index.html
 
-.. _`py library`: http://pylib.org
+.. _`py library`: http://pylib.readthedocs.org/
 
 .. _`Spidermonkey`: http://www.mozilla.org/js/spidermonkey/
 
diff --git a/pypy/doc/index.rst b/pypy/doc/index.rst
--- a/pypy/doc/index.rst
+++ b/pypy/doc/index.rst
@@ -103,7 +103,7 @@
 .. _`more...`: architecture.html#mission-statement 
 .. _`PyPy blog`: http://morepypy.blogspot.com/
 .. _`development bug/feature tracker`: https://bugs.pypy.org
-.. _here: http://tismerysoft.de/pypy/irc-logs/pypy
+.. _here: http://www.tismer.com/pypy/irc-logs/pypy/
 .. _`Mercurial commit mailing list`: http://mail.python.org/mailman/listinfo/pypy-commit
 .. _`development mailing list`: http://mail.python.org/mailman/listinfo/pypy-dev
 .. _`FAQ`: faq.html
diff --git a/pypy/doc/stackless.rst b/pypy/doc/stackless.rst
--- a/pypy/doc/stackless.rst
+++ b/pypy/doc/stackless.rst
@@ -211,6 +211,9 @@
 
 .. __: `recursion depth limit`_
 
+We also do not include any of the recent API additions to Stackless
+Python, like ``set_atomic()``.  Contributions welcome.
+
 
 Recursion depth limit
 +++++++++++++++++++++
diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst
--- a/pypy/doc/whatsnew-head.rst
+++ b/pypy/doc/whatsnew-head.rst
@@ -105,3 +105,25 @@
 
 .. branch: stdlib-2.7.6
 Update stdlib to v2.7.6
+
+.. branch: virtual-raw-store-load
+Support for virtualizing raw_store/raw_load operations
+
+.. branch: refactor-buffer-api
+Separate the interp-level buffer API from the buffer type exposed to
+app-level.  The `Buffer` class is now used by `W_MemoryView` and
+`W_Buffer`, which is not present in Python 3.  Previously `W_Buffer` was
+an alias to `Buffer`, which was wrappable itself.
+
+.. branch: improve-consecutive-dict-lookups
+Improve the situation when dict lookups of the same key are performed in a chain
+
+.. branch: add_PyErr_SetFromErrnoWithFilenameObject_try_2
+.. branch: test_SetFromErrnoWithFilename_NULL
+.. branch: test_SetFromErrnoWithFilename__tweaks
+
+.. branch: refactor_PyErr_SetFromErrnoWithFilename
+Add support for PyErr_SetFromErrnoWithFilenameObject to cpyext
+
+.. branch: win32-fixes4
+fix more tests for win32
diff --git a/pypy/interpreter/app_main.py b/pypy/interpreter/app_main.py
--- a/pypy/interpreter/app_main.py
+++ b/pypy/interpreter/app_main.py
@@ -700,7 +700,7 @@
 
 def setup_bootstrap_path(executable):
     """
-    Try to to as little as possible and to have the stdlib in sys.path. In
+    Try to do as little as possible and to have the stdlib in sys.path. In
     particular, we cannot use any unicode at this point, because lots of
     unicode operations require to be able to import encodings.
     """
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -193,6 +193,15 @@
     def immutable_unique_id(self, space):
         return None
 
+    def buffer_w(self, space):
+        w_impl = space.lookup(self, '__buffer__')
+        if w_impl is not None:
+            w_result = space.get_and_call_function(w_impl, self)
+            if space.isinstance_w(w_result, space.w_memoryview):
+                return w_result.buffer_w(space)
+        raise oefmt(space.w_TypeError,
+                    "'%T' does not support the buffer interface", self)
+
     def bytes_w(self, space):
         self._typed_unwrap_error(space, "bytes")
 
@@ -436,14 +445,12 @@
     def getbuiltinmodule(self, name, force_init=False):
         w_name = self.wrap(name)
         w_modules = self.sys.get('modules')
-        try:
-            w_mod = self.getitem(w_modules, w_name)
-        except OperationError, e:
-            if not e.match(self, self.w_KeyError):
-                raise
-        else:
-            if not force_init:
-                return w_mod
+        if not force_init:
+            try:
+                return self.getitem(w_modules, w_name)
+            except OperationError, e:
+                if not e.match(self, self.w_KeyError):
+                    raise
 
         # If the module is a builtin but not yet imported,
         # retrieve it and initialize it
@@ -454,13 +461,13 @@
                         "getbuiltinmodule() called with non-builtin module %s",
                         name)
         else:
-            # Add the module to sys.modules
-            self.setitem(w_modules, w_name, w_mod)
-
-            # And initialize it
+            # Initialize the module
             from pypy.interpreter.module import Module
             if isinstance(w_mod, Module):
                 w_mod.init(self)
+
+            # Add the module to sys.modules
+            self.setitem(w_modules, w_name, w_mod)
             return w_mod
 
     def get_builtinmodule_to_install(self):
@@ -1307,10 +1314,7 @@
                                            'to unsigned int'))
 
     def buffer_w(self, w_obj):
-        # returns a Buffer instance
-        from pypy.interpreter.buffer import Buffer
-        w_buffer = self.buffer(w_obj)
-        return self.interp_w(Buffer, w_buffer)
+        return w_obj.buffer_w(self)
 
     def rwbuffer_w(self, w_obj):
         # returns a RWBuffer instance
@@ -1727,7 +1731,6 @@
     ('set',             'set',       3, ['__set__']),
     ('delete',          'delete',    2, ['__delete__']),
     ('userdel',         'del',       1, ['__del__']),
-    ('buffer',          'buffer',    1, ['__buffer__']),   # see buffer.py
 ]
 
 ObjSpace.BuiltinModuleTable = [
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -1,32 +1,12 @@
 """
 Buffer protocol support.
 """
+from rpython.rlib.objectmodel import import_from_mixin
 
-# The implementation of the buffer protocol.  The basic idea is that we
-# can ask any app-level object for a 'buffer' view on it, by calling its
-# __buffer__() special method.  It should return a wrapped instance of a
-# subclass of the Buffer class defined below.  Note that __buffer__() is
-# a PyPy-only extension to the Python language, made necessary by the
-# fact that it's not natural in PyPy to hack an interp-level-only
-# interface.
 
-# In normal usage, the convenience method space.buffer_w() should be
-# used to get directly a Buffer instance.  Doing so also gives you for
-# free the typecheck that __buffer__() really returned a wrapped Buffer.
-
-import operator
-from pypy.interpreter.baseobjspace import W_Root
-from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.error import OperationError
-from rpython.rlib.objectmodel import compute_hash, import_from_mixin
-from rpython.rlib.rstring import StringBuilder
-
-
-class Buffer(W_Root):
-    """Abstract base class for memory views."""
-
-    __slots__ = ()     # no extra slot here
+class Buffer(object):
+    """Abstract base class for buffers."""
+    __slots__ = []
 
     def getlength(self):
         raise NotImplementedError
@@ -50,93 +30,10 @@
     def is_writable(self):
         return False
 
-    # __________ app-level support __________
-
-    def descr_len(self, space):
-        return space.wrap(self.getlength())
-
-    def descr_getitem(self, space, w_index):
-        start, stop, step, size = space.decode_index4(w_index, self.getlength())
-        if step == 0:  # index only
-            return space.wrapbytes(self.getitem(start))
-        res = self.getslice(start, stop, step, size)
-        return space.wrapbytes(res)
-
-    @unwrap_spec(newstring='bufferstr')
-    def descr_setitem(self, space, w_index, newstring):
-        if not isinstance(self, RWBuffer):
-            raise OperationError(space.w_TypeError,
-                                 space.wrap("buffer is read-only"))
-        start, stop, step, size = space.decode_index4(w_index, self.getlength())
-        if step == 0:  # index only
-            if len(newstring) != 1:
-                msg = 'buffer[index]=x: x must be a single character'
-                raise OperationError(space.w_TypeError, space.wrap(msg))
-            char = newstring[0]   # annotator hint
-            self.setitem(start, char)
-        elif step == 1:
-            if len(newstring) != size:
-                msg = "right operand length must match slice length"
-                raise OperationError(space.w_ValueError, space.wrap(msg))
-            self.setslice(start, newstring)
-        else:
-            raise OperationError(space.w_ValueError,
-                                 space.wrap("buffer object does not support"
-                                            " slicing with a step"))
-
-    def descr__buffer__(self, space):
-        return space.wrap(self)
-
-    def descr_str(self, space):
-        return space.wrap(self.as_str())
-
-    @unwrap_spec(other='bufferstr')
-    def descr_add(self, space, other):
-        return space.wrapbytes(self.as_str() + other)
-
-    def _make_descr__cmp(name):
-        def descr__cmp(self, space, w_other):
-            if not isinstance(w_other, Buffer):
-                return space.w_NotImplemented
-            # xxx not the most efficient implementation
-            str1 = self.as_str()
-            str2 = w_other.as_str()
-            return space.wrap(getattr(operator, name)(str1, str2))
-        descr__cmp.func_name = name
-        return descr__cmp
-
-    descr_eq = _make_descr__cmp('eq')
-    descr_ne = _make_descr__cmp('ne')
-    descr_lt = _make_descr__cmp('lt')
-    descr_le = _make_descr__cmp('le')
-    descr_gt = _make_descr__cmp('gt')
-    descr_ge = _make_descr__cmp('ge')
-
-    def descr_hash(self, space):
-        return space.wrap(compute_hash(self.as_str()))
-
-    def descr_mul(self, space, w_times):
-        # xxx not the most efficient implementation
-        w_string = space.wrapbytes(self.as_str())
-        # use the __mul__ method instead of space.mul() so that we
-        # return NotImplemented instead of raising a TypeError
-        return space.call_method(w_string, '__mul__', w_times)
-
-    def descr_repr(self, space):
-        if isinstance(self, RWBuffer):
-            info = 'read-write buffer'
-        else:
-            info = 'read-only buffer'
-        addrstring = self.getaddrstring(space)
-
-        return space.wrap("<%s for 0x%s, size %d>" %
-                          (info, addrstring, self.getlength()))
-
 
 class RWBuffer(Buffer):
-    """Abstract base class for read-write memory views."""
-
-    __slots__ = ()     # no extra slot here
+    """Abstract base class for read-write buffers."""
+    __slots__ = []
 
     def is_writable(self):
         return True
@@ -151,76 +48,8 @@
             self.setitem(start + i, string[i])
 
 
- at unwrap_spec(offset=int, size=int)
-def descr_buffer__new__(space, w_subtype, w_object, offset=0, size=-1):
-    # w_subtype can only be exactly 'buffer' for now
-    if not space.is_w(w_subtype, space.gettypefor(Buffer)):
-        raise OperationError(space.w_TypeError,
-                             space.wrap("argument 1 must be 'buffer'"))
-
-    if space.isinstance_w(w_object, space.w_unicode):
-        # unicode objects support the old buffer interface
-        # but not the new buffer interface (change in python  2.7)
-        from rpython.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE
-        unistr = space.unicode_w(w_object)
-        builder = StringBuilder(len(unistr) * UNICODE_SIZE)
-        for unich in unistr:
-            pack_unichar(unich, builder)
-        from pypy.interpreter.buffer import StringBuffer
-        w_buffer = space.wrap(StringBuffer(builder.build()))
-    else:
-        w_buffer = space.buffer(w_object)
-
-    buffer = space.interp_w(Buffer, w_buffer)    # type-check
-    if offset == 0 and size == -1:
-        return w_buffer
-    # handle buffer slices
-    if offset < 0:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("offset must be zero or positive"))
-    if size < -1:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("size must be zero or positive"))
-    if isinstance(buffer, RWBuffer):
-        buffer = RWSubBuffer(buffer, offset, size)
-    else:
-        buffer = SubBuffer(buffer, offset, size)
-    return space.wrap(buffer)
-
-
-Buffer.typedef = TypeDef(
-    "buffer",
-    __doc__ = """\
-buffer(object [, offset[, size]])
-
-Create a new buffer object which references the given object.
-The buffer will reference a slice of the target object from the
-start of the object (or at the specified offset). The slice will
-extend to the end of the target object (or with the specified size).
-""",
-    __new__ = interp2app(descr_buffer__new__),
-    __len__ = interp2app(Buffer.descr_len),
-    __getitem__ = interp2app(Buffer.descr_getitem),
-    __setitem__ = interp2app(Buffer.descr_setitem),
-    __buffer__ = interp2app(Buffer.descr__buffer__),
-    __str__ = interp2app(Buffer.descr_str),
-    __add__ = interp2app(Buffer.descr_add),
-    __eq__ = interp2app(Buffer.descr_eq),
-    __ne__ = interp2app(Buffer.descr_ne),
-    __lt__ = interp2app(Buffer.descr_lt),
-    __le__ = interp2app(Buffer.descr_le),
-    __gt__ = interp2app(Buffer.descr_gt),
-    __ge__ = interp2app(Buffer.descr_ge),
-    __hash__ = interp2app(Buffer.descr_hash),
-    __mul__ = interp2app(Buffer.descr_mul),
-    __rmul__ = interp2app(Buffer.descr_mul),
-    __repr__ = interp2app(Buffer.descr_repr),
-)
-Buffer.typedef.acceptable_as_base_class = False
-
-# ____________________________________________________________
-
 class StringBuffer(Buffer):
+    __slots__ = ['value']
 
     def __init__(self, value):
         self.value = value
@@ -241,49 +70,12 @@
             assert 0 <= start <= stop
             return self.value[start:stop]
         return "".join([self.value[start + i*step] for i in xrange(size)])
-
-
-class StringLikeBuffer(Buffer):
-    """For app-level objects that already have a string-like interface
-    with __len__ and a __getitem__ that returns characters or (with
-    slicing) substrings."""
-    # XXX this is inefficient, it should only be used temporarily
-
-    def __init__(self, space, w_obj):
-        self.space = space
-        self.w_obj = w_obj
-
-    def getlength(self):
-        space = self.space
-        return space.len_w(self.w_obj)
-
-    def getitem(self, index):
-        space = self.space
-        w_value = space.getitem(self.w_obj, space.wrap(index))
-        try:
-            return chr(space.int_w(w_value))
-        except OperationError as e:
-            if not e.match(space, space.w_TypeError):
-                raise
-        s = space.bytes_w(w_value)
-        if len(s) != 1:
-            raise OperationError(space.w_ValueError,
-                                 space.wrap("single byte expected, got string"))
-        char = s[0]   # annotator hint
-        return char
-
-    def getslice(self, start, stop, step, size):
-        space = self.space
-        if step != 1:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "buffer object does not support slicing with a step"))
-        s = space.str_w(space.getslice(self.w_obj, space.wrap(start),
-                                                   space.wrap(stop)))
-        return s
-
 # ____________________________________________________________
 
+
 class SubBufferMixin(object):
+    _attrs_ = ['buffer', 'offset', 'size']
+
     def __init__(self, buffer, offset, size):
         self.buffer = buffer
         self.offset = offset
@@ -305,11 +97,14 @@
         if start == stop:
             return ''     # otherwise, adding self.offset might make them
                           # out of bounds
-        return self.buffer.getslice(self.offset + start, self.offset + stop, step, size)
+        return self.buffer.getslice(self.offset + start, self.offset + stop,
+                                    step, size)
+
 
 class SubBuffer(Buffer):
     import_from_mixin(SubBufferMixin)
 
+
 class RWSubBuffer(RWBuffer):
     import_from_mixin(SubBufferMixin)
 
diff --git a/pypy/interpreter/test/test_buffer.py b/pypy/interpreter/test/test_buffer.py
--- a/pypy/interpreter/test/test_buffer.py
+++ b/pypy/interpreter/test/test_buffer.py
@@ -1,31 +1,28 @@
 import py
-from pypy.interpreter.buffer import Buffer
 from rpython.tool.udir import udir
 
 testdir = udir.ensure('test_buffer', dir=1)
 
 
 class TestBuffer:
-
     def test_buffer_w(self):
         space = self.space
         w_hello = space.wrapbytes('hello world')
         buf = space.buffer_w(w_hello)
-        assert isinstance(buf, Buffer)
         assert buf.getlength() == 11
         assert buf.as_str() == 'hello world'
         assert buf.getslice(1, 6, 1, 5) == 'ello '
-        assert space.buffer_w(space.wrap(buf)) is buf
+        assert space.buffer_w(space.newbuffer(buf)) is buf
         assert space.bufferstr_w(w_hello) == 'hello world'
-        assert space.bufferstr_w(space.buffer(w_hello)) == 'hello world'
+        assert space.bufferstr_w(space.newbuffer(space.buffer_w(w_hello))) == 'hello world'
         space.raises_w(space.w_TypeError, space.buffer_w, space.wrap(5))
-        e = space.raises_w(space.w_TypeError, space.buffer, space.wrap(5))
+        e = space.raises_w(space.w_TypeError, space.buffer_w, space.wrap(5))
         message = space.unwrap(e.value.get_w_value(space))
         assert "'int' does not support the buffer interface" == message
 
     def test_file_write(self):
         space = self.space
-        w_buffer = space.buffer(space.wrapbytes('hello world'))
+        w_buffer = space.newbuffer(space.buffer_w(space.wrapbytes('hello world')))
         filename = str(testdir.join('test_file_write'))
         space.appexec([w_buffer, space.wrap(filename)], """(buffer, filename):
             f = open(filename, 'wb')
@@ -37,4 +34,4 @@
         f.close()
         assert data == 'hello world'
 
-# Note: some app-level tests for buffer are in module/__builtin__/test/.
+# Note: some app-level tests for buffer are in objspace/std/test/test_memoryview.py.
diff --git a/pypy/module/__builtin__/__init__.py b/pypy/module/__builtin__/__init__.py
--- a/pypy/module/__builtin__/__init__.py
+++ b/pypy/module/__builtin__/__init__.py
@@ -29,14 +29,10 @@
 
     interpleveldefs = {
         # constants
+        '__debug__'     : '(space.w_True)',      # XXX
         'None'          : '(space.w_None)',
         'False'         : '(space.w_False)',
         'True'          : '(space.w_True)',
-        '__debug__'     : '(space.w_True)',      # XXX
-        'type'          : '(space.w_type)',
-        'object'        : '(space.w_object)',
-        'memoryview'    : 'interp_memoryview.W_MemoryView',
-
         'open'          : 'state.get(space).w_open',
 
         # interp-level function definitions
diff --git a/pypy/module/__builtin__/interp_memoryview.py b/pypy/module/__builtin__/interp_memoryview.py
deleted file mode 100644
--- a/pypy/module/__builtin__/interp_memoryview.py
+++ /dev/null
@@ -1,198 +0,0 @@
-"""
-Implementation of the 'buffer' and 'memoryview' types.
-"""
-from pypy.interpreter.baseobjspace import W_Root
-from pypy.interpreter import buffer
-from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.interpreter.error import OperationError
-import operator
-
-class W_MemoryView(W_Root):
-    """Implement the built-in 'memoryview' type as a thin wrapper around
-    an interp-level buffer.
-    """
-
-    def __init__(self, buf):
-        assert isinstance(buf, buffer.Buffer)
-        self.buf = buf
-
-    def _make_descr__cmp(name):
-        def descr__cmp(self, space, w_other):
-            if self.buf is None:
-                return space.wrap(getattr(operator, name)(self, w_other))
-            if isinstance(w_other, W_MemoryView):
-                # xxx not the most efficient implementation
-                str1 = self.as_str()
-                str2 = w_other.as_str()
-                return space.wrap(getattr(operator, name)(str1, str2))
-
-            try:
-                w_buf = space.buffer(w_other)
-            except OperationError, e:
-                if not e.match(space, space.w_TypeError):
-                    raise
-                return space.w_NotImplemented
-            else:
-                str1 = self.as_str()
-                str2 = space.buffer_w(w_buf).as_str()
-                return space.wrap(getattr(operator, name)(str1, str2))
-        descr__cmp.func_name = name
-        return descr__cmp
-
-    descr_eq = _make_descr__cmp('eq')
-    descr_ne = _make_descr__cmp('ne')
-
-    def as_str(self):
-        return self.buf.as_str()
-
-    def getlength(self):
-        return self.buf.getlength()
-
-    def getslice(self, start, stop):
-        if start < 0:
-            start = 0
-        size = stop - start
-        if size < 0:
-            size = 0
-        buf = self.buf
-        if isinstance(buf, buffer.RWBuffer):
-            buf = buffer.RWSubBuffer(buf, start, size)
-        else:
-            buf = buffer.SubBuffer(buf, start, size)
-        return W_MemoryView(buf)
-
-    def descr_buffer(self, space):
-        """
-        Note that memoryview() is very inconsistent in CPython: it does not
-        support the buffer interface but does support the new buffer
-        interface: as a result, it is possible to pass memoryview to
-        e.g. socket.send() but not to file.write().  For simplicity and
-        consistency, in PyPy memoryview DOES support buffer(), which means
-        that it is accepted in more places than CPython.
-        """
-        self._check_released(space)
-        return space.wrap(self.buf)
-
-    def descr_tobytes(self, space):
-        self._check_released(space)
-        return space.wrapbytes(self.as_str())
-
-    def descr_tolist(self, space):
-        self._check_released(space)
-        buf = self.buf
-        result = []
-        for i in range(buf.getlength()):
-            result.append(space.wrap(ord(buf.getitem(i))))
-        return space.newlist(result)
-
-    def descr_getitem(self, space, w_index):
-        self._check_released(space)
-        start, stop, step = space.decode_index(w_index, self.getlength())
-        if step == 0:  # index only
-            return space.wrapbytes(self.buf.getitem(start))
-        elif step == 1:
-            res = self.getslice(start, stop)
-            return space.wrap(res)
-        else:
-            raise OperationError(space.w_ValueError,
-                space.wrap("memoryview object does not support"
-                           " slicing with a step"))
-
-    @unwrap_spec(newstring='bufferstr')
-    def descr_setitem(self, space, w_index, newstring):
-        self._check_released(space)
-        buf = self.buf
-        if isinstance(buf, buffer.RWBuffer):
-            buf.descr_setitem(space, w_index, newstring)
-        else:
-            raise OperationError(space.w_TypeError,
-                                 space.wrap("cannot modify read-only memory"))
-
-    def descr_len(self, space):
-        self._check_released(space)
-        return self.buf.descr_len(space)
-
-    def w_get_format(self, space):
-        self._check_released(space)
-        return space.wrap("B")
-
-    def w_get_itemsize(self, space):
-        self._check_released(space)
-        return space.wrap(1)
-
-    def w_get_ndim(self, space):
-        self._check_released(space)
-        return space.wrap(1)
-
-    def w_is_readonly(self, space):
-        self._check_released(space)
-        return space.wrap(not isinstance(self.buf, buffer.RWBuffer))
-
-    def w_get_shape(self, space):
-        self._check_released(space)
-        return space.newtuple([space.wrap(self.getlength())])
-
-    def w_get_strides(self, space):
-        self._check_released(space)
-        return space.newtuple([space.wrap(1)])
-
-    def w_get_suboffsets(self, space):
-        self._check_released(space)
-        # I've never seen anyone filling this field
-        return space.w_None
-
-    def descr_repr(self, space):
-        if self.buf is None:
-            return self.getrepr(space, u'released memory')
-        else:
-            return self.getrepr(space, u'memory')
-
-    def descr_release(self, space):
-        self.buf = None
-
-    def _check_released(self, space):
-        if self.buf is None:
-            raise OperationError(space.w_ValueError, space.wrap(
-                    "operation forbidden on released memoryview object"))
-
-    def descr_enter(self, space):
-        self._check_released(space)
-        return self
-
-    def descr_exit(self, space, __args__):
-        self.buf = None
-        return space.w_None
-
-
-def descr_new(space, w_subtype, w_object):
-    memoryview = W_MemoryView(space.buffer(w_object))
-    return space.wrap(memoryview)
-
-W_MemoryView.typedef = TypeDef(
-    "memoryview",
-    __doc__ = """\
-Create a new memoryview object which references the given object.
-""",
-    __new__ = interp2app(descr_new),
-    __buffer__  = interp2app(W_MemoryView.descr_buffer),
-    __eq__      = interp2app(W_MemoryView.descr_eq),
-    __getitem__ = interp2app(W_MemoryView.descr_getitem),
-    __len__     = interp2app(W_MemoryView.descr_len),
-    __ne__      = interp2app(W_MemoryView.descr_ne),
-    __setitem__ = interp2app(W_MemoryView.descr_setitem),
-    __repr__    = interp2app(W_MemoryView.descr_repr),
-    __enter__   = interp2app(W_MemoryView.descr_enter),
-    __exit__    = interp2app(W_MemoryView.descr_exit),
-    tobytes     = interp2app(W_MemoryView.descr_tobytes),
-    tolist      = interp2app(W_MemoryView.descr_tolist),
-    release     = interp2app(W_MemoryView.descr_release),
-    format      = GetSetProperty(W_MemoryView.w_get_format),
-    itemsize    = GetSetProperty(W_MemoryView.w_get_itemsize),
-    ndim        = GetSetProperty(W_MemoryView.w_get_ndim),
-    readonly    = GetSetProperty(W_MemoryView.w_is_readonly),
-    shape       = GetSetProperty(W_MemoryView.w_get_shape),
-    strides     = GetSetProperty(W_MemoryView.w_get_strides),
-    suboffsets  = GetSetProperty(W_MemoryView.w_get_suboffsets),
-    )
-W_MemoryView.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/__builtin__/test/test_builtin.py b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -25,6 +25,13 @@
         else:
             cls.w_safe_runtimerror = cls.space.wrap(sys.version_info < (2, 6))
 
+    def test_builtin_names(self):
+        import builtins as __builtin__
+        assert __builtin__.bytes is bytes
+        assert __builtin__.dict is dict
+        assert __builtin__.memoryview is memoryview
+        assert not hasattr(__builtin__, 'buffer')
+
     def test_bytes_alias(self):
         assert bytes is not str
         assert isinstance(eval("b'hi'"), bytes)
diff --git a/pypy/module/__pypy__/bytebuffer.py b/pypy/module/__pypy__/bytebuffer.py
--- a/pypy/module/__pypy__/bytebuffer.py
+++ b/pypy/module/__pypy__/bytebuffer.py
@@ -7,7 +7,6 @@
 
 
 class ByteBuffer(RWBuffer):
-
     def __init__(self, len):
         self.data = ['\x00'] * len
 
@@ -23,4 +22,4 @@
 
 @unwrap_spec(length=int)
 def bytebuffer(space, length):
-    return space.wrap(ByteBuffer(length))
+    return space.newbuffer(ByteBuffer(length))
diff --git a/pypy/module/__pypy__/interp_builders.py b/pypy/module/__pypy__/interp_builders.py
--- a/pypy/module/__pypy__/interp_builders.py
+++ b/pypy/module/__pypy__/interp_builders.py
@@ -4,7 +4,6 @@
 from pypy.interpreter.typedef import TypeDef
 from rpython.rlib.rstring import UnicodeBuilder, StringBuilder
 from rpython.tool.sourcetools import func_with_new_name
-from rpython.rlib import jit
 
 
 def create_builder(name, strtype, builder_cls):
diff --git a/pypy/module/_cffi_backend/cbuffer.py b/pypy/module/_cffi_backend/cbuffer.py
--- a/pypy/module/_cffi_backend/cbuffer.py
+++ b/pypy/module/_cffi_backend/cbuffer.py
@@ -4,6 +4,7 @@
 from pypy.interpreter.gateway import unwrap_spec, interp2app
 from pypy.interpreter.typedef import TypeDef, make_weakref_descr
 from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray
+from pypy.objspace.std.memoryview import _buffer_setitem
 
 from rpython.rtyper.annlowlevel import llstr
 from rpython.rtyper.lltypesystem import rffi
@@ -39,26 +40,30 @@
         copy_string_to_raw(llstr(string), raw_cdata, 0, len(string))
 
 
+# Override the typedef to narrow down the interface that's exposed to app-level
+
 class MiniBuffer(W_Root):
-    # a different subclass of W_Root for the MiniBuffer, because we
-    # want a slightly different (simplified) API at the level of Python.
-
     def __init__(self, buffer, keepalive=None):
         self.buffer = buffer
         self.keepalive = keepalive
 
+    def buffer_w(self, space):
+        return self.buffer
+
     def descr_len(self, space):
-        return self.buffer.descr_len(space)
+        return space.wrap(self.buffer.getlength())
 
     def descr_getitem(self, space, w_index):
-        return self.buffer.descr_getitem(space, w_index)
+        start, stop, step, size = space.decode_index4(w_index,
+                                                      self.buffer.getlength())
+        if step == 0:
+            return space.wrapbytes(self.buffer.getitem(start))
+        res = self.buffer.getslice(start, stop, step, size)
+        return space.wrapbytes(res)
 
     @unwrap_spec(newstring='bufferstr')
     def descr_setitem(self, space, w_index, newstring):
-        self.buffer.descr_setitem(space, w_index, newstring)
-
-    def descr__buffer__(self, space):
-        return self.buffer.descr__buffer__(space)
+        _buffer_setitem(space, self.buffer, w_index, newstring)
 
 
 MiniBuffer.typedef = TypeDef(
@@ -67,7 +72,6 @@
     __len__ = interp2app(MiniBuffer.descr_len),
     __getitem__ = interp2app(MiniBuffer.descr_getitem),
     __setitem__ = interp2app(MiniBuffer.descr_setitem),
-    __buffer__ = interp2app(MiniBuffer.descr__buffer__),
     __weakref__ = make_weakref_descr(MiniBuffer),
     )
 MiniBuffer.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/_cffi_backend/ctypestruct.py b/pypy/module/_cffi_backend/ctypestruct.py
--- a/pypy/module/_cffi_backend/ctypestruct.py
+++ b/pypy/module/_cffi_backend/ctypestruct.py
@@ -80,7 +80,6 @@
         return (cfield.ctype, cfield.offset)
 
     def _copy_from_same(self, cdata, w_ob):
-        space = self.space
         if isinstance(w_ob, cdataobj.W_CData):
             if w_ob.ctype is self and self.size >= 0:
                 misc._raw_memcopy(w_ob._cdata, cdata, self.size)
diff --git a/pypy/module/_cffi_backend/handle.py b/pypy/module/_cffi_backend/handle.py
--- a/pypy/module/_cffi_backend/handle.py
+++ b/pypy/module/_cffi_backend/handle.py
@@ -1,4 +1,3 @@
-import weakref
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.module._cffi_backend import ctypeobj, ctypeptr, cdataobj
diff --git a/pypy/module/_cffi_backend/misc.py b/pypy/module/_cffi_backend/misc.py
--- a/pypy/module/_cffi_backend/misc.py
+++ b/pypy/module/_cffi_backend/misc.py
@@ -4,7 +4,7 @@
 
 from rpython.rlib import jit
 from rpython.rlib.objectmodel import keepalive_until_here, specialize
-from rpython.rlib.rarithmetic import r_uint, r_ulonglong, is_signed_integer_type
+from rpython.rlib.rarithmetic import r_uint, r_ulonglong
 from rpython.rlib.unroll import unrolling_iterable
 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
diff --git a/pypy/module/_codecs/test/test_codecs.py b/pypy/module/_codecs/test/test_codecs.py
--- a/pypy/module/_codecs/test/test_codecs.py
+++ b/pypy/module/_codecs/test/test_codecs.py
@@ -1,3 +1,5 @@
+import sys
+
 class AppTestCodecs:
     spaceconfig = {
         "usemodules": ['unicodedata', 'struct', 'binascii'],
@@ -138,7 +140,9 @@
 
 
 class AppTestPartialEvaluation:
-    spaceconfig = dict(usemodules=('array',))
+    spaceconfig = dict(usemodules=['array',])
+    if sys.platform == 'win32':
+        spaceconfig['usemodules'].append('_winreg')
 
     def test_partial_utf8(self):
         import _codecs
@@ -753,9 +757,25 @@
         import sys
         if sys.platform != 'win32':
             return
-        assert 'test'.encode('mbcs') == b'test'
-        assert 'caf\xe9'.encode('mbcs') == b'caf\xe9'
-        raises(UnicodeEncodeError, '\u040a'.encode, 'mbcs')
-        raises(UnicodeEncodeError,
-               "-\u5171\u0141\u2661\u0363\uDC80".encode, 'mbcs')
-        assert b'cafx\e9'.decode('mbcs') == 'cafx\e9'
+        toencode = u'caf\xe9', b'caf\xe9'
+        try:
+            # test for non-latin1 codepage, more general test needed
+            import _winreg
+            key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
+                        r'System\CurrentControlSet\Control\Nls\CodePage')
+            if _winreg.QueryValueEx(key, 'ACP')[0] == u'1255':  # non-latin1
+                toencode = u'caf\xbf',b'caf\xbf'
+        except:
+            assert False, 'cannot test mbcs on this windows system, check code page'
+        assert u'test'.encode('mbcs') == b'test'
+        assert toencode[0].encode('mbcs') == toencode[1]
+        assert u'\u040a'.encode('mbcs') == b'?'  # some cyrillic letter
+        assert b'cafx\e9'.decode('mbcs') == u'cafx\e9'
+
+    def test_handler_string_result(self):
+        import _codecs
+        def f(exc):
+            return (b'foo', exc.end)
+        _codecs.register_error("test.test_codecs_not_a_string", f)
+        result = '\u1234'.encode('ascii', 'test.test_codecs_not_a_string')
+        assert result == b'foo'
diff --git a/pypy/module/_io/interp_bufferedio.py b/pypy/module/_io/interp_bufferedio.py
--- a/pypy/module/_io/interp_bufferedio.py
+++ b/pypy/module/_io/interp_bufferedio.py
@@ -521,7 +521,7 @@
 
     def _raw_read(self, space, buffer, start, length):
         length = intmask(length)
-        w_buf = space.wrap(RawBuffer(buffer, start, length))
+        w_buf = space.newbuffer(RawBuffer(buffer, start, length))
         while True:
             try:
                 w_size = space.call_method(self.w_raw, "readinto", w_buf)
diff --git a/pypy/module/_io/interp_bytesio.py b/pypy/module/_io/interp_bytesio.py
--- a/pypy/module/_io/interp_bytesio.py
+++ b/pypy/module/_io/interp_bytesio.py
@@ -7,6 +7,7 @@
 from rpython.rlib.rarithmetic import r_longlong
 from pypy.module._io.interp_bufferedio import W_BufferedIOBase
 from pypy.module._io.interp_iobase import convert_size
+from pypy.objspace.std.memoryview import W_MemoryView
 import sys
 
 
@@ -121,7 +122,7 @@
         return space.wrap(size)
 
     def getbuffer_w(self, space):
-        return space.wrap(BytesIOBuffer(self))
+        return space.wrap(W_MemoryView(BytesIOBuffer(self)))
 
     def getvalue_w(self, space):
         self._check_closed(space)
diff --git a/pypy/module/_minimal_curses/fficurses.py b/pypy/module/_minimal_curses/fficurses.py
--- a/pypy/module/_minimal_curses/fficurses.py
+++ b/pypy/module/_minimal_curses/fficurses.py
@@ -1,4 +1,3 @@
-
 """ The ffi for rpython, need to be imported for side effects
 """
 
@@ -8,8 +7,6 @@
 from rpython.rtyper.extfunc import register_external
 from pypy.module._minimal_curses import interp_curses
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
-from sys import platform
-import os.path
 
 # We cannot trust ncurses5-config, it's broken in various ways in
 # various versions.  For example it might not list -ltinfo even though
diff --git a/pypy/module/_multiprocessing/test/test_connection.py b/pypy/module/_multiprocessing/test/test_connection.py
--- a/pypy/module/_multiprocessing/test/test_connection.py
+++ b/pypy/module/_multiprocessing/test/test_connection.py
@@ -9,8 +9,12 @@
 
 class AppTestBufferTooShort:
     spaceconfig = {'usemodules': ['_multiprocessing', 'thread', 'signal',
-                                  'select', 'fcntl', 'struct',
-                                  'binascii']}
+                                  'select', 'struct', 'binascii']}
+    if sys.platform == 'win32':
+        spaceconfig['usemodules'].append('_rawffi')
+    else:
+        spaceconfig['usemodules'].append('fcntl')
+
 
     def setup_class(cls):
         if cls.runappdirect:
@@ -75,6 +79,8 @@
             'itertools', '_socket', 'binascii',
         ]
     }
+    if sys.platform == 'win32':
+        spaceconfig['usemodules'].append('_rawffi')
 
     def setup_class(cls):
         if sys.platform != "win32":
@@ -86,7 +92,6 @@
             # just for multiprocessing to import correctly on Windows
             w_modules = space.sys.get('modules')
             space.setitem(w_modules, space.wrap('msvcrt'), space.sys)
-            space.setitem(w_modules, space.wrap('_subprocess'), space.sys)
         else:
             import _multiprocessing
 
@@ -100,9 +105,12 @@
     spaceconfig = {
         "usemodules": [
             '_multiprocessing', 'thread', 'signal', 'struct', 'array',
-            '_socket', 'binascii', 'select', 'fcntl',
-        ]
+            '_socket', 'binascii', 'select' ]
     }
+    if sys.platform == 'win32':
+        spaceconfig['usemodules'].append('_rawffi')
+    else:
+        spaceconfig['usemodules'].append('fcntl')
 
     def setup_class(cls):
         cls.w_connections = cls.space.newlist([])
diff --git a/pypy/module/_pickle_support/maker.py b/pypy/module/_pickle_support/maker.py
--- a/pypy/module/_pickle_support/maker.py
+++ b/pypy/module/_pickle_support/maker.py
@@ -3,7 +3,6 @@
 from pypy.interpreter.pycode import PyCode
 from pypy.interpreter.function import Function, Method
 from pypy.interpreter.module import Module
-from pypy.interpreter.pyframe import PyFrame
 from pypy.interpreter.pytraceback import PyTraceback
 from pypy.interpreter.generator import GeneratorIteratorWithDel
 from rpython.rlib.objectmodel import instantiate
diff --git a/pypy/module/_pypyjson/interp_decoder.py b/pypy/module/_pypyjson/interp_decoder.py
--- a/pypy/module/_pypyjson/interp_decoder.py
+++ b/pypy/module/_pypyjson/interp_decoder.py
@@ -1,13 +1,10 @@
 import sys
-import math
 from rpython.rlib.rstring import StringBuilder
 from rpython.rlib.objectmodel import specialize
 from rpython.rlib import rfloat, runicode
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError, oefmt
-from pypy.interpreter.gateway import unwrap_spec
 from pypy.interpreter import unicodehelper
-from rpython.rtyper.annlowlevel import llstr, hlunicode
 
 OVF_DIGITS = len(str(sys.maxint))
 
@@ -30,7 +27,7 @@
 
     Internally it's implemented at the level of low-level helpers, to avoid
     the extra copy we would need if we take the actual slice first.
-    
+
     No bound checking is done, use carefully.
     """
     from rpython.rtyper.annlowlevel import llstr, hlunicode
@@ -226,7 +223,6 @@
     def decode_array(self, i):
         w_list = self.space.newlist([])
         start = i
-        count = 0
         i = self.skip_whitespace(start)
         if self.ll_chars[i] == ']':
             self.pos = i+1
diff --git a/pypy/module/_rawffi/array.py b/pypy/module/_rawffi/array.py
--- a/pypy/module/_rawffi/array.py
+++ b/pypy/module/_rawffi/array.py
@@ -207,7 +207,6 @@
     __setitem__ = interp2app(W_ArrayInstance.descr_setitem),
     __getitem__ = interp2app(W_ArrayInstance.descr_getitem),
     __len__     = interp2app(W_ArrayInstance.getlength),
-    __buffer__  = interp2app(W_ArrayInstance.descr_buffer),
     buffer      = GetSetProperty(W_ArrayInstance.getbuffer),
     shape       = interp_attrproperty('shape', W_ArrayInstance),
     free        = interp2app(W_ArrayInstance.free),
@@ -232,7 +231,6 @@
     __setitem__ = interp2app(W_ArrayInstance.descr_setitem),
     __getitem__ = interp2app(W_ArrayInstance.descr_getitem),
     __len__     = interp2app(W_ArrayInstance.getlength),
-    __buffer__  = interp2app(W_ArrayInstance.descr_buffer),
     buffer      = GetSetProperty(W_ArrayInstance.getbuffer),
     shape       = interp_attrproperty('shape', W_ArrayInstance),
     byptr       = interp2app(W_ArrayInstance.byptr),
diff --git a/pypy/module/_rawffi/callback.py b/pypy/module/_rawffi/callback.py
--- a/pypy/module/_rawffi/callback.py
+++ b/pypy/module/_rawffi/callback.py
@@ -2,7 +2,7 @@
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from rpython.rtyper.lltypesystem import lltype, rffi
-from pypy.module._rawffi.interp_rawffi import read_ptr, write_ptr
+from pypy.module._rawffi.interp_rawffi import write_ptr
 from pypy.module._rawffi.structure import W_Structure
 from pypy.module._rawffi.interp_rawffi import (W_DataInstance, letter2tp,
      unwrap_value, unpack_argshapes, got_libffi_error)
diff --git a/pypy/module/_rawffi/interp_rawffi.py b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -359,9 +359,9 @@
         lltype.free(self.ll_buffer, flavor='raw')
         self.ll_buffer = lltype.nullptr(rffi.VOIDP.TO)
 
-    def descr_buffer(self, space):
+    def buffer_w(self, space):
         from pypy.module._rawffi.buffer import RawFFIBuffer
-        return space.wrap(RawFFIBuffer(self))
+        return RawFFIBuffer(self)
 
     def getrawsize(self):
         raise NotImplementedError("abstract base class")
diff --git a/pypy/module/_rawffi/structure.py b/pypy/module/_rawffi/structure.py
--- a/pypy/module/_rawffi/structure.py
+++ b/pypy/module/_rawffi/structure.py
@@ -15,7 +15,7 @@
 from pypy.module._rawffi.interp_rawffi import size_alignment
 from pypy.module._rawffi.interp_rawffi import read_ptr, write_ptr
 from rpython.rlib import clibffi, rgc
-from rpython.rlib.rarithmetic import intmask, signedtype, widen, r_uint, \
+from rpython.rlib.rarithmetic import intmask, signedtype, r_uint, \
     r_ulonglong
 from rpython.rtyper.lltypesystem import lltype, rffi
 
@@ -364,7 +364,6 @@
     __repr__    = interp2app(W_StructureInstance.descr_repr),
     __getattr__ = interp2app(W_StructureInstance.getattr),
     __setattr__ = interp2app(W_StructureInstance.setattr),
-    __buffer__  = interp2app(W_StructureInstance.descr_buffer),
     buffer      = GetSetProperty(W_StructureInstance.getbuffer),
     free        = interp2app(W_StructureInstance.free),
     shape       = interp_attrproperty('shape', W_StructureInstance),
@@ -387,7 +386,6 @@
     __repr__    = interp2app(W_StructureInstance.descr_repr),
     __getattr__ = interp2app(W_StructureInstance.getattr),
     __setattr__ = interp2app(W_StructureInstance.setattr),
-    __buffer__  = interp2app(W_StructureInstance.descr_buffer),
     buffer      = GetSetProperty(W_StructureInstance.getbuffer),
     shape       = interp_attrproperty('shape', W_StructureInstance),
     byptr       = interp2app(W_StructureInstance.byptr),
diff --git a/pypy/module/_rawffi/tracker.py b/pypy/module/_rawffi/tracker.py
--- a/pypy/module/_rawffi/tracker.py
+++ b/pypy/module/_rawffi/tracker.py
@@ -1,4 +1,3 @@
-
 """ The file that keeps track about freed/kept-alive objects allocated
 by _rawffi. Used for debugging ctypes
 """
diff --git a/pypy/module/_socket/interp_socket.py b/pypy/module/_socket/interp_socket.py
--- a/pypy/module/_socket/interp_socket.py
+++ b/pypy/module/_socket/interp_socket.py
@@ -69,10 +69,7 @@
         else:                 flowinfo = 0
         if len(pieces_w) > 3: scope_id = space.uint_w(pieces_w[3])
         else:                 scope_id = 0
-        if flowinfo < 0 or flowinfo > 0xfffff:
-            raise OperationError(space.w_OverflowError, space.wrap(
-                "flowinfo must be 0-1048575."))
-        flowinfo = rffi.cast(lltype.Unsigned, flowinfo)
+        flowinfo = make_unsigned_flowinfo(space, flowinfo)
         a = addr.lock(_c.sockaddr_in6)
         rffi.setintfield(a, 'c_sin6_port', rsocket.htons(port))
         rffi.setintfield(a, 'c_sin6_flowinfo', rsocket.htonl(flowinfo))
@@ -101,10 +98,7 @@
         else:                 flowinfo = 0
         if len(pieces_w) > 3: scope_id = space.uint_w(pieces_w[3])
         else:                 scope_id = 0
-        if flowinfo < 0 or flowinfo > 0xfffff:
-            raise OperationError(space.w_OverflowError, space.wrap(
-                "flowinfo must be 0-1048575."))
-        flowinfo = rffi.cast(lltype.Unsigned, flowinfo)
+        flowinfo = make_unsigned_flowinfo(space, flowinfo)
         return rsocket.INET6Address(host, port, flowinfo, scope_id)
     if rsocket.HAS_AF_UNIX and family == rsocket.AF_UNIX:
         return rsocket.UNIXAddress(space.str_w(w_address))
@@ -116,10 +110,16 @@
 # XXX Hack to seperate rpython and pypy
 def make_ushort_port(space, port):
     if port < 0 or port > 0xffff:
-        raise OperationError(space.w_ValueError, space.wrap(
+        raise OperationError(space.w_OverflowError, space.wrap(
             "port must be 0-65535."))
     return rffi.cast(rffi.USHORT, port)
 
+def make_unsigned_flowinfo(space, flowinfo):
+    if flowinfo < 0 or flowinfo > 0xfffff:
+        raise OperationError(space.w_OverflowError, space.wrap(
+            "flowinfo must be 0-1048575."))
+    return rffi.cast(lltype.Unsigned, flowinfo)
+
 # XXX Hack to seperate rpython and pypy
 def ipaddr_from_object(space, w_sockaddr):
     host = space.str_w(space.getitem(w_sockaddr, space.wrap(0)))
diff --git a/pypy/module/_socket/test/test_sock_app.py b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -391,7 +391,7 @@
         name = s.getpeername() # Will raise socket.error if not connected
         assert name[1] == 80
         s.close()
-    
+
     def test_socket_connect_ex(self):
         import _socket
         s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM, 0)
@@ -417,8 +417,13 @@
     def test_bigport(self):
         import _socket
         s = _socket.socket()
-        raises(ValueError, s.connect, ("localhost", 1000000))
-        raises(ValueError, s.connect, ("localhost", -1))
+        exc = raises(OverflowError, s.connect, ("localhost", -1))
+        assert "port must be 0-65535." in str(exc.value)
+        exc = raises(OverflowError, s.connect, ("localhost", 1000000))
+        assert "port must be 0-65535." in str(exc.value)
+        s = _socket.socket(_socket.AF_INET6)
+        exc = raises(OverflowError, s.connect, ("::1", 1234, 1048576))
+        assert "flowinfo must be 0-1048575." in str(exc.value)
 
     def test_NtoH(self):
         import sys
@@ -466,6 +471,13 @@
         import socket
         s = socket.socket()
 
+    def test_subclass(self):
+        from _socket import socket
+        class MySock(socket):
+            blah = 123
+        s = MySock()
+        assert s.blah == 123
+
     def test_getsetsockopt(self):
         import _socket as socket
         import struct
@@ -524,7 +536,8 @@
             skip("GAIError - probably no connection: %s" % str(ex.args))
         assert s.send(memoryview(b'')) == 0
         assert s.sendall(memoryview(b'')) is None
-        raises(TypeError, s.send, '')
+        exc = raises(TypeError, s.send, '')
+        assert str(exc.value) == "'str' does not support the buffer interface"
         raises(TypeError, s.sendall, '')
         s.close()
         s = _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM, 0)
@@ -587,11 +600,11 @@
 
 
 class AppTestSocketTCP:
+    HOST = 'localhost'
+
     def setup_class(cls):
         cls.space = space
 
-    HOST = 'localhost'
-
     def setup_method(self, method):
         w_HOST = space.wrap(self.HOST)
         self.w_serv = space.appexec([w_HOST],
@@ -602,6 +615,7 @@
             serv.listen(1)
             return serv
             ''')
+
     def teardown_method(self, method):
         if hasattr(self, 'w_serv'):
             space.appexec([self.w_serv], '(serv): serv.close()')
@@ -622,7 +636,7 @@
         raises(error, raise_error)
 
     def test_recv_send_timeout(self):
-        from _socket import socket, timeout
+        from _socket import socket, timeout, SOL_SOCKET, SO_RCVBUF, SO_SNDBUF
         cli = socket()
         cli.connect(self.serv.getsockname())
         fileno, addr = self.serv._accept()
@@ -643,6 +657,9 @@
         assert count is None
         buf = t.recv(1)
         assert buf == b'?'
+        # speed up filling the buffers
+        t.setsockopt(SOL_SOCKET, SO_RCVBUF, 4096)
+        cli.setsockopt(SOL_SOCKET, SO_SNDBUF, 4096)
         # test send() timeout
         count = 0
         try:
@@ -671,7 +688,7 @@
         conn = socket.socket(fileno=fileno)
         buf = memoryview(MSG)
         conn.send(buf)
-        buf = array.array('b', b' '*1024)
+        buf = array.array('b', b' ' * 1024)
         nbytes = cli.recv_into(buf)
         assert nbytes == len(MSG)
         msg = buf.tobytes()[:len(MSG)]
@@ -687,7 +704,7 @@
         conn = socket.socket(fileno=fileno)
         buf = memoryview(MSG)
         conn.send(buf)
-        buf = array.array('b', b' '*1024)
+        buf = array.array('b', b' ' * 1024)
         nbytes, addr = cli.recvfrom_into(buf)
         assert nbytes == len(MSG)
         msg = buf.tobytes()[:len(MSG)]
@@ -698,6 +715,7 @@
         cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         assert cli.family == socket.AF_INET
 
+
 class AppTestErrno:
     def setup_class(cls):
         cls.space = space
diff --git a/pypy/module/_ssl/interp_ssl.py b/pypy/module/_ssl/interp_ssl.py
--- a/pypy/module/_ssl/interp_ssl.py
+++ b/pypy/module/_ssl/interp_ssl.py
@@ -37,7 +37,7 @@
 SOCKET_HAS_TIMED_OUT, SOCKET_HAS_BEEN_CLOSED = 2, 3
 SOCKET_TOO_LARGE_FOR_SELECT, SOCKET_OPERATION_OK = 4, 5
 
-HAVE_RPOLL = True  # Even win32 has rpoll.poll
+HAVE_RPOLL = 'poll' in dir(rpoll)
 
 constants = {}
 constants["SSL_ERROR_ZERO_RETURN"] = PY_SSL_ERROR_ZERO_RETURN
diff --git a/pypy/module/_ssl/test/test_ssl.py b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -1,7 +1,7 @@
 from rpython.tool.udir import udir
 
 class AppTestSSL:
-    spaceconfig = dict(usemodules=('_ssl', '_socket', 'binascii'))
+    spaceconfig = dict(usemodules=('_ssl', '_socket', 'binascii', 'thread'))
 
     def setup_class(cls):
         import os
diff --git a/pypy/module/_ssl/thread_lock.py b/pypy/module/_ssl/thread_lock.py
--- a/pypy/module/_ssl/thread_lock.py
+++ b/pypy/module/_ssl/thread_lock.py
@@ -1,4 +1,5 @@
-from rpython.rlib.ropenssl import *
+from rpython.rlib import rthread
+from rpython.rlib.ropenssl import libraries
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 
@@ -22,7 +23,6 @@
 # without caring about the GIL.
 
 separate_module_source = """
-
 #include <openssl/crypto.h>
 
 static unsigned int _ssl_locks_count = 0;
@@ -62,13 +62,12 @@
 }
 """
 
-from rpython.rlib import rthread
-
 eci = rthread.eci.merge(ExternalCompilationInfo(
     separate_module_sources=[separate_module_source],
     post_include_bits=[
         "int _PyPy_SSL_SetupThreads(void);"],
     export_symbols=['_PyPy_SSL_SetupThreads'],
+    libraries = libraries,
 ))
 
 _PyPy_SSL_SetupThreads = rffi.llexternal('_PyPy_SSL_SetupThreads',
diff --git a/pypy/module/array/interp_array.py b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -40,16 +40,19 @@
 
             if len(__args__.arguments_w) > 0:
                 w_initializer = __args__.arguments_w[0]
-                if space.lookup(w_initializer, '__buffer__') is not None:
-                    if isinstance(w_initializer, W_ArrayBase):
-                        a.extend(w_initializer, True)
-                    else:
-                        a.descr_frombytes(space,
-                                          space.bufferstr_w(w_initializer))
+                if isinstance(w_initializer, W_ArrayBase):
+                    a.extend(w_initializer, True)
                 elif space.type(w_initializer) is space.w_list:
                     a.descr_fromlist(space, w_initializer)
                 else:
-                    a.extend(w_initializer, True)
+                    try:
+                        buf = space.bufferstr_w(w_initializer)
+                    except OperationError as e:
+                        if not e.match(space, space.w_TypeError):
+                            raise
+                        a.extend(w_initializer, True)
+                    else:
+                        a.descr_frombytes(space, buf)
             break
     else:
         msg = 'bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)'
@@ -135,6 +138,9 @@
         self.len = 0
         self.allocated = 0
 
+    def buffer_w(self, space):
+        return ArrayBuffer(self)
+
     def descr_append(self, space, w_x):
         """ append(x)
 
@@ -505,9 +511,6 @@
     def descr_iter(self, space):
         return space.wrap(ArrayIterator(self))
 
-    def descr_buffer(self, space):
-        return space.wrap(ArrayBuffer(self))
-
     def descr_repr(self, space):
         if self.len == 0:
             return space.wrap("array('%s')" % self.typecode)
@@ -544,7 +547,6 @@
     __radd__ = interp2app(W_ArrayBase.descr_radd),
     __rmul__ = interp2app(W_ArrayBase.descr_rmul),
 
-    __buffer__ = interp2app(W_ArrayBase.descr_buffer),
     __iter__ = interp2app(W_ArrayBase.descr_iter),
     __repr__ = interp2app(W_ArrayBase.descr_repr),
 
diff --git a/pypy/module/array/test/test_array.py b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -1,25 +1,8 @@
 import sys
-import py
-import py.test
-
-
-## class AppTestSimpleArray:
-##     spaceconfig = dict(usemodules=('array',))
-##     def setup_class(cls):
-##         cls.w_simple_array = cls.space.appexec([], """():
-##             import array
-##             return array.simple_array
-##         """)
-
-##     def test_simple(self):
-##         a = self.simple_array(10)
-##         a[5] = 7.42
-##         assert a[5] == 7.42
+import pytest
 
 
 class BaseArrayTests:
-
-
     def test_ctor(self):
         assert len(self.array('i')) == 0
 
@@ -545,7 +528,6 @@
         assert not a > 2*a
         assert not a >= 2*a
 
-
     def test_reduce(self):
         import pickle
         a = self.array('i', [1, 2, 3])
@@ -779,7 +761,6 @@
 
         assert img[3, 25] == 3 * 9
 
-
     def test_override_from(self):
         class mya(self.array):
             def fromlist(self, lst):
@@ -862,41 +843,41 @@
 
     def test_assign_object_with_special_methods(self):
         from array import array
-        
+
         class Num(object):
             def __float__(self):
                 return 5.25
-                
+
             def __int__(self):
                 return 7
-                
+
         class NotNum(object):
             pass
-        
+
         class Silly(object):
             def __float__(self):
                 return None
-                
+
             def __int__(self):
-                return None         
+                return None
 
         class OldNum:
             def __float__(self):
                 return 6.25
-                
+
             def __int__(self):
                 return 8
-                
+
         class OldNotNum:
             pass
-        
+
         class OldSilly:
             def __float__(self):
                 return None
-                
+
             def __int__(self):
                 return None
-                
+
         for tc in 'bBhHiIlL':
             a = array(tc, [0])
             raises(TypeError, a.__setitem__, 0, 1.0)
@@ -914,7 +895,7 @@
             a = array(tc, [0])
             a[0] = 1.0
             a[0] = 1
-            a[0] = Num()        
+            a[0] = Num()
             assert a[0] == 5.25
             raises(TypeError, a.__setitem__, NotNum())
             a[0] = OldNum()
@@ -922,11 +903,15 @@
             raises(TypeError, a.__setitem__, OldNotNum())
             raises(TypeError, a.__setitem__, Silly())
             raises(TypeError, a.__setitem__, OldSilly())
-            
+
         a = array('u', 'hi')
         a[0] = 'b'
         assert a[0] == 'b'
-        
+
+        a = array('u', u'hi')
+        a[0] = u'b'
+        assert a[0] == u'b'
+
     def test_bytearray(self):
         a = self.array('u', 'hi')
         b = self.array('u')
@@ -940,15 +925,13 @@
         assert repr(a) == "array('u', {!r})".format(s)
         assert eval(repr(a), {'array': self.array}) == a
 
-
 class DontTestCPythonsOwnArray(BaseArrayTests):
-
     def setup_class(cls):
         import array
         cls.array = array.array
         import struct
         cls.struct = struct
-        cls.tempfile = str(py.test.ensuretemp('array').join('tmpfile'))
+        cls.tempfile = str(pytest.ensuretemp('array').join('tmpfile'))
         cls.maxint = sys.maxint
 
 
@@ -961,7 +944,7 @@
             return array.array
         """)
         cls.w_tempfile = cls.space.wrap(
-            str(py.test.ensuretemp('array').join('tmpfile')))
+            str(pytest.ensuretemp('array').join('tmpfile')))
         cls.w_maxint = cls.space.wrap(sys.maxint)
 
     def test_buffer_info(self):
@@ -1028,11 +1011,11 @@
 
     def test_getitem_only_ints(self):
         class MyInt(object):
-          def __init__(self, x):
-            self.x = x
+            def __init__(self, x):
+                self.x = x
 
-          def __int__(self):
-            return self.x
+            def __int__(self):
+                return self.x
 
         a = self.array('i', [1, 2, 3, 4, 5, 6])
         raises(TypeError, "a[MyInt(0)]")
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -22,9 +22,9 @@
 from pypy.interpreter.nestedscope import Cell
 from pypy.interpreter.module import Module
 from pypy.interpreter.function import StaticMethod


More information about the pypy-commit mailing list