From b03ca9766d5cbdf23854314e50275991d3a445c6 Mon Sep 17 00:00:00 2001 From: Larry Hastings Date: Tue, 27 Apr 2021 04:06:36 -0700 Subject: [PATCH] Two minor fixes for accessing a module's name. --- Lib/test/test_module.py | 2 +- Objects/moduleobject.c | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py index 1d44563579fd2f..a589f630118fd7 100644 --- a/Lib/test/test_module.py +++ b/Lib/test/test_module.py @@ -22,7 +22,7 @@ def test_uninitialized(self): # and __doc__ is None foo = ModuleType.__new__(ModuleType) self.assertTrue(foo.__dict__ is None) - self.assertRaises(SystemError, dir, foo) + self.assertRaises(TypeError, dir, foo) try: s = foo.__name__ self.fail("__name__ = %s" % repr(s)) diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index a6eb85bdc2a962..50e13ed0883968 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -478,7 +478,7 @@ PyModule_GetNameObject(PyObject *m) return NULL; } d = ((PyModuleObject *)m)->md_dict; - if (d == NULL || + if (d == NULL || !PyDict_Check(d) || (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL || !PyUnicode_Check(name)) { @@ -823,11 +823,7 @@ module_dir(PyObject *self, PyObject *args) } } else { - const char *name = PyModule_GetName(self); - if (name) - PyErr_Format(PyExc_TypeError, - "%.200s.__dict__ is not a dictionary", - name); + PyErr_Format(PyExc_TypeError, ".__dict__ is not a dictionary"); } }