[pypy-commit] pypy default: Issue #2669

arigo pypy.commits at gmail.com
Thu Sep 28 01:58:40 EDT 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r92488:7ee8c3a718e5
Date: 2017-09-28 07:57 +0200
http://bitbucket.org/pypy/pypy/changeset/7ee8c3a718e5/

Log:	Issue #2669

	Make Py_FileSystemDefaultEncoding non-NULL in cpyext.

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
@@ -558,6 +558,7 @@
     'PyObject_GetBuffer', 'PyBuffer_Release',
     'PyBuffer_FromMemory', 'PyBuffer_FromReadWriteMemory', 'PyBuffer_FromObject',
     'PyBuffer_FromReadWriteObject', 'PyBuffer_New', 'PyBuffer_Type', '_Py_get_buffer_type',
+    '_Py_setfilesystemdefaultencoding',
 
     'PyCObject_FromVoidPtr', 'PyCObject_FromVoidPtrAndDesc', 'PyCObject_AsVoidPtr',
     'PyCObject_GetDesc', 'PyCObject_Import', 'PyCObject_SetVoidPtr',
@@ -1045,11 +1046,17 @@
     get_capsule_type = rffi.llexternal('_%s_get_capsule_type' % prefix,
                                        [], PyTypeObjectPtr,
                                        compilation_info=eci, _nowrapper=True)
+    setdefenc = rffi.llexternal('_%s_setfilesystemdefaultencoding' % prefix,
+                                [rffi.CCHARP], lltype.Void,
+                                compilation_info=eci, _nowrapper=True)
     def init_types(space):
         from pypy.module.cpyext.typeobject import py_type_ready
+        from pypy.module.sys.interp_encoding import getfilesystemencoding
         py_type_ready(space, get_buffer_type())
         py_type_ready(space, get_cobject_type())
         py_type_ready(space, get_capsule_type())
+        s = space.text_w(getfilesystemencoding(space))
+        setdefenc(rffi.str2charp(s, track_allocation=False))  # "leaks"
     INIT_FUNCTIONS.append(init_types)
     from pypy.module.posix.interp_posix import add_fork_hook
     _reinit_tls = rffi.llexternal('%sThread_ReInitTLS' % prefix, [],
diff --git a/pypy/module/cpyext/include/fileobject.h b/pypy/module/cpyext/include/fileobject.h
--- a/pypy/module/cpyext/include/fileobject.h
+++ b/pypy/module/cpyext/include/fileobject.h
@@ -1,1 +1,2 @@
-#define Py_FileSystemDefaultEncoding   NULL
+PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
+PyAPI_FUNC(void) _Py_setfilesystemdefaultencoding(const char *);
diff --git a/pypy/module/cpyext/src/missing.c b/pypy/module/cpyext/src/missing.c
--- a/pypy/module/cpyext/src/missing.c
+++ b/pypy/module/cpyext/src/missing.c
@@ -27,3 +27,7 @@
 int Py_Py3kWarningFlag = 0;
 int Py_HashRandomizationFlag = 0;
 
+const char *Py_FileSystemDefaultEncoding;  /* filled when cpyext is imported */
+void _Py_setfilesystemdefaultencoding(const char *enc) {
+    Py_FileSystemDefaultEncoding = enc;
+}
diff --git a/pypy/module/cpyext/test/test_fileobject.py b/pypy/module/cpyext/test/test_fileobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/test/test_fileobject.py
@@ -0,0 +1,13 @@
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+
+
+class AppTestFileObject(AppTestCpythonExtensionBase):
+    def test_defaultencoding(self):
+        import sys
+        module = self.import_extension('foo', [
+            ("defenc", "METH_NOARGS",
+             """
+                return PyString_FromString(Py_FileSystemDefaultEncoding);
+             """),
+            ])
+        assert module.defenc() == sys.getfilesystemencoding()


More information about the pypy-commit mailing list