diff --git a/mypyc/lib-rt/exc_ops.c b/mypyc/lib-rt/exc_ops.c index 85498420d2af..c2a220d280ee 100644 --- a/mypyc/lib-rt/exc_ops.c +++ b/mypyc/lib-rt/exc_ops.c @@ -123,11 +123,11 @@ static PyObject *CPy_GetTypeName(PyObject *type) { PyObject *module = NULL, *name = NULL; PyObject *full = NULL; - module = PyObject_GetAttrString(type, "__module__"); + module = PyObject_GetAttr(type, mypyc_interned_str.__module__); if (!module || !PyUnicode_Check(module)) { goto out; } - name = PyObject_GetAttrString(type, "__qualname__"); + name = PyObject_GetAttr(type, mypyc_interned_str.__qualname__); if (!name || !PyUnicode_Check(name)) { goto out; } diff --git a/mypyc/lib-rt/librt_strings.c b/mypyc/lib-rt/librt_strings.c index d97d0f571f5b..1acaefa77fef 100644 --- a/mypyc/lib-rt/librt_strings.c +++ b/mypyc/lib-rt/librt_strings.c @@ -449,5 +449,6 @@ static PyModuleDef librt_strings_module = { PyMODINIT_FUNC PyInit_strings(void) { + intern_strings(); return PyModuleDef_Init(&librt_strings_module); } diff --git a/mypyc/lib-rt/misc_ops.c b/mypyc/lib-rt/misc_ops.c index 38472b33291f..3683f6e5d406 100644 --- a/mypyc/lib-rt/misc_ops.c +++ b/mypyc/lib-rt/misc_ops.c @@ -107,7 +107,7 @@ static bool _CPy_IsSafeMetaClass(PyTypeObject *metaclass) { // manage to work with TypingMeta and its friends. if (metaclass == &PyType_Type) return true; - PyObject *module = PyObject_GetAttrString((PyObject *)metaclass, "__module__"); + PyObject *module = PyObject_GetAttr((PyObject *)metaclass, mypyc_interned_str.__module__); if (!module) { PyErr_Clear(); return false; @@ -242,7 +242,7 @@ PyObject *CPyType_FromTemplate(PyObject *template, sizeof(PyTypeObject) - sizeof(PyVarObject)); if (bases != orig_bases) { - if (PyObject_SetAttrString((PyObject *)t, "__orig_bases__", orig_bases) < 0) + if (PyObject_SetAttr((PyObject *)t, mypyc_interned_str.__orig_bases__, orig_bases) < 0) goto error; } @@ -285,7 +285,7 @@ PyObject *CPyType_FromTemplate(PyObject *template, // Reject anything that would give us a nontrivial __slots__, // because the layout will conflict - slots = PyObject_GetAttrString((PyObject *)t, "__slots__"); + slots = PyObject_GetAttr((PyObject *)t, mypyc_interned_str.__slots__); if (slots) { // don't fail on an empty __slots__ int is_true = PyObject_IsTrue(slots); @@ -298,7 +298,7 @@ PyObject *CPyType_FromTemplate(PyObject *template, PyErr_Clear(); } - if (PyObject_SetAttrString((PyObject *)t, "__module__", modname) < 0) + if (PyObject_SetAttr((PyObject *)t, mypyc_interned_str.__module__, modname) < 0) goto error; if (init_subclass((PyTypeObject *)t, NULL)) @@ -458,7 +458,7 @@ CPyPickle_GetState(PyObject *obj) { PyObject *attrs = NULL, *state = NULL; - attrs = PyObject_GetAttrString((PyObject *)Py_TYPE(obj), "__mypyc_attrs__"); + attrs = PyObject_GetAttr((PyObject *)Py_TYPE(obj), mypyc_interned_str.__mypyc_attrs__); if (!attrs) { goto fail; } @@ -734,7 +734,7 @@ int CPyStatics_Initialize(PyObject **statics, // Call super(type(self), self) PyObject * CPy_Super(PyObject *builtins, PyObject *self) { - PyObject *super_type = PyObject_GetAttrString(builtins, "super"); + PyObject *super_type = PyObject_GetAttr(builtins, mypyc_interned_str.super); if (!super_type) return NULL; PyObject *result = PyObject_CallFunctionObjArgs( @@ -889,7 +889,7 @@ CPy_CallReverseOpMethod(PyObject *left, PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, PyObject *cls, PyObject *func) { - PyObject *registry = PyObject_GetAttrString(singledispatch_func, "registry"); + PyObject *registry = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.registry); PyObject *register_func = NULL; PyObject *typing = NULL; PyObject *get_type_hints = NULL; @@ -902,7 +902,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, // passed a class // bind cls to the first argument so that register gets called again with both the // class and the function - register_func = PyObject_GetAttrString(singledispatch_func, "register"); + register_func = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.register_); if (register_func == NULL) goto fail; return PyMethod_New(register_func, cls); } @@ -923,7 +923,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, func = cls; typing = PyImport_ImportModule("typing"); if (typing == NULL) goto fail; - get_type_hints = PyObject_GetAttrString(typing, "get_type_hints"); + get_type_hints = PyObject_GetAttr(typing, mypyc_interned_str.get_type_hints); type_hints = PyObject_CallOneArg(get_type_hints, func); PyObject *argname; @@ -944,7 +944,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, } // clear the cache so we consider the newly added function when dispatching - PyObject *dispatch_cache = PyObject_GetAttrString(singledispatch_func, "dispatch_cache"); + PyObject *dispatch_cache = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.dispatch_cache); if (dispatch_cache == NULL) goto fail; PyDict_Clear(dispatch_cache); diff --git a/mypyc/lib-rt/static_data.c b/mypyc/lib-rt/static_data.c index 1fc93330bdc1..cc503181dccd 100644 --- a/mypyc/lib-rt/static_data.c +++ b/mypyc/lib-rt/static_data.c @@ -17,8 +17,13 @@ mypyc_interned_str_struct mypyc_interned_str; int intern_strings(void) { INTERN_STRING(__init_subclass__, "__init_subclass__"); + INTERN_STRING(__module__, "__module__"); INTERN_STRING(__mro_entries__, "__mro_entries__"); + INTERN_STRING(__mypyc_attrs__, "__mypyc_attrs__"); INTERN_STRING(__name__, "__name__"); + INTERN_STRING(__orig_bases__, "__orig_bases__"); + INTERN_STRING(__qualname__, "__qualname__"); + INTERN_STRING(__slots__, "__slots__"); INTERN_STRING(__radd__, "__radd__"); INTERN_STRING(__rsub__, "__rsub__"); INTERN_STRING(__rmul__, "__rmul__"); @@ -42,12 +47,17 @@ intern_strings(void) { INTERN_STRING(clear, "clear"); INTERN_STRING(close_, "close"); INTERN_STRING(copy, "copy"); + INTERN_STRING(dispatch_cache, "dispatch_cache"); + INTERN_STRING(get_type_hints, "get_type_hints"); INTERN_STRING(keys, "keys"); INTERN_STRING(items, "items"); INTERN_STRING(join, "join"); + INTERN_STRING(register_, "register"); + INTERN_STRING(registry, "registry"); INTERN_STRING(send, "send"); INTERN_STRING(setdefault, "setdefault"); INTERN_STRING(startswith, "startswith"); + INTERN_STRING(super, "super"); INTERN_STRING(throw_, "throw"); INTERN_STRING(translate, "translate"); INTERN_STRING(update, "update"); diff --git a/mypyc/lib-rt/static_data.h b/mypyc/lib-rt/static_data.h index f779504b6c20..c177979d9eb0 100644 --- a/mypyc/lib-rt/static_data.h +++ b/mypyc/lib-rt/static_data.h @@ -13,7 +13,12 @@ int intern_strings(void); typedef struct mypyc_interned_str_struct { PyObject *__init_subclass__; + PyObject *__module__; PyObject *__mro_entries__; + PyObject *__mypyc_attrs__; + PyObject *__orig_bases__; + PyObject *__qualname__; + PyObject *__slots__; PyObject *__name__; PyObject *__radd__; PyObject *__rsub__; @@ -38,12 +43,17 @@ typedef struct mypyc_interned_str_struct { PyObject *clear; PyObject *close_; PyObject *copy; + PyObject *dispatch_cache; + PyObject *get_type_hints; PyObject *keys; PyObject *items; PyObject *join; + PyObject *register_; + PyObject *registry; PyObject *send; PyObject *setdefault; PyObject *startswith; + PyObject *super; PyObject *throw_; PyObject *translate; PyObject *update;