]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #25856: The __module__ attribute of extension classes and functions
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 9 Sep 2016 21:53:02 +0000 (00:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 9 Sep 2016 21:53:02 +0000 (00:53 +0300)
now is interned. This leads to more compact pickle data with protocol 4.

Misc/NEWS
Objects/typeobject.c

index 194325609d93dcbe35da2193b434c7cd663284ca..c47c4bf5b0a99ef813806ec09a9ddc1c8b8f9059 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 beta 1
 Core and Builtins
 -----------------
 
+- Issue #25856: The __module__ attribute of extension classes and functions
+  now is interned. This leads to more compact pickle data with protocol 4.
+
 - Issue #27213: Rework CALL_FUNCTION* opcodes to produce shorter and more
   efficient bytecode. Patch by Demur Rumed, design by Serhiy Storchaka,
   reviewed by Serhiy Storchaka and Victor Stinner.
index df0481dd58e2ef271f4d60bb971064ed8ea5bc59..2675a4897d1f68acc7c51f88ead7400b37e00eb0 100644 (file)
@@ -454,27 +454,30 @@ type_set_qualname(PyTypeObject *type, PyObject *value, void *context)
 static PyObject *
 type_module(PyTypeObject *type, void *context)
 {
-    char *s;
+    PyObject *mod;
 
     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
-        PyObject *mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__);
-        if (!mod) {
+        mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__);
+        if (mod == NULL) {
             PyErr_Format(PyExc_AttributeError, "__module__");
-            return 0;
+            return NULL;
         }
         Py_INCREF(mod);
-        return mod;
     }
     else {
-        PyObject *name;
-        s = strrchr(type->tp_name, '.');
-        if (s != NULL)
-            return PyUnicode_FromStringAndSize(
+        const char *s = strrchr(type->tp_name, '.');
+        if (s != NULL) {
+            mod = PyUnicode_FromStringAndSize(
                 type->tp_name, (Py_ssize_t)(s - type->tp_name));
-        name = _PyUnicode_FromId(&PyId_builtins);
-        Py_XINCREF(name);
-        return name;
+            if (mod != NULL)
+                PyUnicode_InternInPlace(&mod);
+        }
+        else {
+            mod = _PyUnicode_FromId(&PyId_builtins);
+            Py_XINCREF(mod);
+        }
     }
+    return mod;
 }
 
 static int