diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-09-09 21:53:02 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-09-09 21:53:02 (GMT) |
commit | 7c19affdceb6796322fb5046e3b231ddd8baa404 (patch) | |
tree | 305cf99dd804ce6cff1dbf4cd8954b65c25635d6 /Objects | |
parent | ae8b69c410b83d92c6c35bbe342c3c2e92be6aa1 (diff) | |
download | cpython-7c19affdceb6796322fb5046e3b231ddd8baa404.zip cpython-7c19affdceb6796322fb5046e3b231ddd8baa404.tar.gz cpython-7c19affdceb6796322fb5046e3b231ddd8baa404.tar.bz2 |
Issue #25856: The __module__ attribute of extension classes and functions
now is interned. This leads to more compact pickle data with protocol 4.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index df0481d..2675a48 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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 |