[Python-checkins] cpython: Issue #25856: The __module__ attribute of extension classes and functions

serhiy.storchaka python-checkins at python.org
Fri Sep 9 17:55:19 EDT 2016


https://hg.python.org/cpython/rev/861ddad3e0c1
changeset:   103470:861ddad3e0c1
parent:      103467:a25c39873d93
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Sep 10 00:53:02 2016 +0300
summary:
  Issue #25856: The __module__ attribute of extension classes and functions
now is interned. This leads to more compact pickle data with protocol 4.

files:
  Misc/NEWS            |   3 +++
  Objects/typeobject.c |  29 ++++++++++++++++-------------
  2 files changed, 19 insertions(+), 13 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -454,27 +454,30 @@
 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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list