Author: Armin Rigo <ar...@tunes.org>
Branch: py3.5
Changeset: r88276:007445aad81a
Date: 2016-11-09 16:50 +0100
http://bitbucket.org/pypy/pypy/changeset/007445aad81a/

Log:    merge heads

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1068,9 +1068,6 @@
         from pypy.objspace.std.listobject import make_empty_list_with_size
         return make_empty_list_with_size(self, sizehint)
 
-    def wrap_fsdecoded(self, x):
-        return self.fsdecode(self.newbytes(x))
-
     @jit.unroll_safe
     def exception_match(self, w_exc_type, w_check_class):
         """Checks if the given exception type matches 'w_check_class'."""
@@ -1680,6 +1677,9 @@
             w_obj = self.fsdecode(w_obj)
         return self.unicode0_w(w_obj)
 
+    def wrap_fsdecoded(self, x):
+        return self.fsdecode(self.newbytes(x))
+
     def bool_w(self, w_obj):
         # Unwraps a bool, also accepting an int for compatibility.
         # For cases where you need to accept bools and ints and nothing
diff --git a/pypy/interpreter/test/test_fsencode.py 
b/pypy/interpreter/test/test_fsencode.py
new file mode 100644
--- /dev/null
+++ b/pypy/interpreter/test/test_fsencode.py
@@ -0,0 +1,80 @@
+import sys
+from pypy.interpreter.error import OperationError
+
+class BaseFSEncodeTest:
+
+    def setup_class(cls):
+        space = cls.space
+        cls.testfn_unencodable = get_unencodable()
+        cls.w_testfn_unencodable = space.wrap(cls.testfn_unencodable)
+        cls.special_char = get_special_char()
+        cls.w_special_char = space.wrap(cls.special_char)
+
+def get_unencodable():
+    """Copy of the stdlib's support.TESTFN_UNENCODABLE:
+
+    A filename (py3k str type) that should *not* be able to be encoded
+    by the filesystem encoding (in strict mode). It can be None if we
+    cannot generate such filename.
+    """
+    testfn_unencodable = None
+    testfn = u'test_tmp'
+
+    if sys.platform == 'win32':
+        testfn_unencodable = testfn + u"-\u5171\u0141\u2661\u0363\uDC80"
+    elif sys.platform != 'darwin':
+        try:
+            '\xff'.decode(sys.getfilesystemencoding())
+        except UnicodeDecodeError:
+            testfn_unencodable = testfn + u'-\udcff'
+    return testfn_unencodable
+
+def get_special_char():
+    """Copy of the stdlib's test_imp.test_issue5604 special_char:
+
+    A non-ascii filename (py3k str type) that *should* be able to be
+    encoded by the filesystem encoding (in strict mode). It can be None
+    if we cannot generate such filename.
+    """
+    fsenc = sys.getfilesystemencoding()
+    # covers utf-8 and Windows ANSI code pages one non-space symbol from
+    # every page (http://en.wikipedia.org/wiki/Code_page)
+    known_locales = {
+        'utf-8' : b'\xc3\xa4',
+        'cp1250' : b'\x8C',
+        'cp1251' : b'\xc0',
+        'cp1252' : b'\xc0',
+        'cp1253' : b'\xc1',
+        'cp1254' : b'\xc0',
+        'cp1255' : b'\xe0',
+        'cp1256' : b'\xe0',
+        'cp1257' : b'\xc0',
+        'cp1258' : b'\xc0',
+        }
+
+    if sys.platform == 'darwin':
+        # Mac OS X uses the Normal Form D decomposition
+        # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
+        special_char = b'a\xcc\x88'
+    else:
+        special_char = known_locales.get(fsenc)
+
+    if special_char:
+        return special_char.decode(fsenc)
+
+class TestFSEncode(BaseFSEncodeTest):
+    def test_fsencode_fsdecode(self):
+        space = self.space
+        strs = [u"/home/bar/baz", u"c:\\"]
+        if self.special_char:
+            strs.append(self.special_char)
+        for st in strs:
+            # check roundtrip
+            w_st = space.newunicode(st)
+            w_enc = space.fsencode(w_st)
+            w_st2 = space.fsdecode(w_enc)
+            assert space.eq_w(w_st, w_st2)
+            assert space.fsdecode_w(w_enc) == st
+
+            assert space.fsencode_w(w_enc) == space.bytes_w(w_enc)
+            assert space.eq_w(space.wrap_fsdecoded(space.bytes_w(w_enc)), 
w_st2)
diff --git a/pypy/module/_cffi_backend/cffi1_module.py 
b/pypy/module/_cffi_backend/cffi1_module.py
--- a/pypy/module/_cffi_backend/cffi1_module.py
+++ b/pypy/module/_cffi_backend/cffi1_module.py
@@ -42,7 +42,7 @@
     w_name = space.wrap(name)
     module = Module(space, w_name)
     if path is not None:
-        module.setdictvalue(space, '__file__', space.wrap(path))
+        module.setdictvalue(space, '__file__', space.wrap_fsdecoded(path))
     module.setdictvalue(space, 'ffi', space.wrap(ffi))
     module.setdictvalue(space, 'lib', space.wrap(lib))
     w_modules_dict = space.sys.get('modules')
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
@@ -1597,7 +1597,7 @@
             libssl_SSL_CTX_set_default_passwd_cb_userdata(
                 self.ctx, None)
 
-    @unwrap_spec(filepath=str)
+    @unwrap_spec(filepath='fsencode')
     def load_dh_params_w(self, space, filepath):
         bio = libssl_BIO_new_file(filepath, "r")
         if not bio:
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,6 +1,8 @@
 from rpython.tool.udir import udir
 import os
 
+from pypy.interpreter.test.test_fsencode import BaseFSEncodeTest
+
 class AppTestSSL:
     spaceconfig = dict(usemodules=('_ssl', '_socket', 'select', 'struct',
                                    'binascii', 'thread'))
@@ -341,10 +343,11 @@
             """)
 
 
-class AppTestContext:
+class AppTestContext(BaseFSEncodeTest):
     spaceconfig = dict(usemodules=('_ssl',))
 
     def setup_class(cls):
+        BaseFSEncodeTest.setup_class.im_func(cls)
         tmpfile = udir / "tmpfile.pem"
         tmpfile.write(SSL_CERTIFICATE + SSL_PRIVATE_KEY)
         cls.w_keycert = cls.space.wrap(str(tmpfile))
@@ -366,8 +369,14 @@
         tmpfile = udir / "python.org.pem"
         tmpfile.write(SVN_PYTHON_ORG_ROOT_CERT)
         cls.w_python_org_cert = cls.space.wrap(str(tmpfile))
-        cls.w_dh512 = cls.space.wrap(os.path.join(
-            os.path.dirname(__file__), 'dh512.pem'))
+        tmpfile = udir / cls.special_char
+        fn = os.path.join(
+            os.path.dirname(__file__), 'dh512.pem')
+        with file(fn) as f:
+            s = f.read()
+        tmpfile.write(s)
+        cls.w_dh512 = cls.space.wrap(fn)
+        cls.w_dh512special = cls.space.wrap(str(tmpfile))
 
     def test_load_cert_chain(self):
         import _ssl, errno
@@ -443,6 +452,9 @@
         exc = raises(IOError, ctx.load_dh_params, "inexistent.pem")
         assert exc.value.errno == errno.ENOENT
 
+        ctx = _ssl._SSLContext(_ssl.PROTOCOL_TLS)
+        ctx.load_dh_params(self.dh512special)
+
     def test_set_ecdh_curve(self):
         import _ssl
         ctx = _ssl._SSLContext(_ssl.PROTOCOL_TLS)
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
@@ -1511,7 +1511,7 @@
     from pypy.module._cffi_backend import cffi1_module
     cffi1_module.load_cffi1_module(space, name, path, initptr)
 
-@unwrap_spec(path=str, name=str)
+@unwrap_spec(path='fsencode', name=str)
 def load_extension_module(space, path, name):
     # note: this is used both to load CPython-API-style C extension
     # modules (cpyext) and to load CFFI-style extension modules
diff --git a/pypy/module/cpyext/modsupport.py b/pypy/module/cpyext/modsupport.py
--- a/pypy/module/cpyext/modsupport.py
+++ b/pypy/module/cpyext/modsupport.py
@@ -47,7 +47,7 @@
     state.package_context = None, None
 
     if f_path is not None:
-        dict_w = {'__file__': space.wrap(f_path)}
+        dict_w = {'__file__': space.wrap_fsdecoded(f_path)}
     else:
         dict_w = {}
     convert_method_defs(space, dict_w, methods, None, w_mod, modname)
diff --git a/pypy/module/imp/test/support.py b/pypy/module/imp/test/support.py
deleted file mode 100644
--- a/pypy/module/imp/test/support.py
+++ /dev/null
@@ -1,62 +0,0 @@
-import sys
-
-class BaseImportTest:
-
-    def setup_class(cls):
-        space = cls.space
-        cls.testfn_unencodable = get_unencodable()
-        cls.w_testfn_unencodable = space.wrap(cls.testfn_unencodable)
-        cls.special_char = get_special_char()
-        cls.w_special_char = space.wrap(cls.special_char)
-
-def get_unencodable():
-    """Copy of the stdlib's support.TESTFN_UNENCODABLE:
-
-    A filename (py3k str type) that should *not* be able to be encoded
-    by the filesystem encoding (in strict mode). It can be None if we
-    cannot generate such filename.
-    """
-    testfn_unencodable = None
-    testfn = u'test_tmp'
-
-    if sys.platform == 'win32':
-        testfn_unencodable = testfn + u"-\u5171\u0141\u2661\u0363\uDC80"
-    elif sys.platform != 'darwin':
-        try:
-            '\xff'.decode(sys.getfilesystemencoding())
-        except UnicodeDecodeError:
-            testfn_unencodable = testfn + u'-\udcff'
-    return testfn_unencodable
-
-def get_special_char():
-    """Copy of the stdlib's test_imp.test_issue5604 special_char:
-
-    A non-ascii filename (py3k str type) that *should* be able to be
-    encoded by the filesystem encoding (in strict mode). It can be None
-    if we cannot generate such filename.
-    """
-    fsenc = sys.getfilesystemencoding()
-    # covers utf-8 and Windows ANSI code pages one non-space symbol from
-    # every page (http://en.wikipedia.org/wiki/Code_page)
-    known_locales = {
-        'utf-8' : b'\xc3\xa4',
-        'cp1250' : b'\x8C',
-        'cp1251' : b'\xc0',
-        'cp1252' : b'\xc0',
-        'cp1253' : b'\xc1',
-        'cp1254' : b'\xc0',
-        'cp1255' : b'\xe0',
-        'cp1256' : b'\xe0',
-        'cp1257' : b'\xc0',
-        'cp1258' : b'\xc0',
-        }
-
-    if sys.platform == 'darwin':
-        # Mac OS X uses the Normal Form D decomposition
-        # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
-        special_char = b'a\xcc\x88'
-    else:
-        special_char = known_locales.get(fsenc)
-
-    if special_char:
-        return special_char.decode(fsenc)
diff --git a/pypy/module/imp/test/test_import.py 
b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -4,7 +4,7 @@
 from pypy.interpreter import gateway
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.pycode import PyCode
-from pypy.module.imp.test.support import BaseImportTest
+from pypy.interpreter.test.test_fsencode import BaseFSEncodeTest
 from rpython.tool.udir import udir
 from rpython.rlib import streamio
 from pypy.tool.option import make_config
@@ -177,13 +177,13 @@
     """)
 
 
-class AppTestImport(BaseImportTest):
+class AppTestImport(BaseFSEncodeTest):
     spaceconfig = {
         "usemodules": ['_md5', 'time', 'struct', '_pypyjson'],
     }
 
     def setup_class(cls):
-        BaseImportTest.setup_class.im_func(cls)
+        BaseFSEncodeTest.setup_class.im_func(cls)
         cls.w_runappdirect = cls.space.wrap(conftest.option.runappdirect)
         cls.w_saved_modules = _setup(cls)
         #XXX Compile class
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to