diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-02-23 00:21:43 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-02-23 00:21:43 (GMT) |
commit | bd475115c4fb0dda55f3d1f4443bff58abd1203f (patch) | |
tree | 5d8ebf7b06289fe9bbe5e8381185bd308d20bb2a /Objects/moduleobject.c | |
parent | 501c09a754bc329ed9b3d0c1be0c51ccd6687c6c (diff) | |
download | cpython-bd475115c4fb0dda55f3d1f4443bff58abd1203f.zip cpython-bd475115c4fb0dda55f3d1f4443bff58abd1203f.tar.gz cpython-bd475115c4fb0dda55f3d1f4443bff58abd1203f.tar.bz2 |
Issue #3080: Add PyModule_GetNameObject()
repr(module) uses %R to format module name and filenames, instead of '%s' and
'%U', so surrogates from undecodable bytes in a filename (PEP 383) are escaped.
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r-- | Objects/moduleobject.c | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index f31b5da..103ac83 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -169,24 +169,35 @@ PyModule_GetDict(PyObject *m) return d; } -const char * -PyModule_GetName(PyObject *m) +PyObject* +PyModule_GetNameObject(PyObject *m) { PyObject *d; - PyObject *nameobj; + PyObject *name; if (!PyModule_Check(m)) { PyErr_BadArgument(); return NULL; } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || - (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || - !PyUnicode_Check(nameobj)) + (name = PyDict_GetItemString(d, "__name__")) == NULL || + !PyUnicode_Check(name)) { PyErr_SetString(PyExc_SystemError, "nameless module"); return NULL; } - return _PyUnicode_AsString(nameobj); + Py_INCREF(name); + return name; +} + +const char * +PyModule_GetName(PyObject *m) +{ + PyObject *name = PyModule_GetNameObject(m); + if (name == NULL) + return NULL; + Py_DECREF(name); /* module dict has still a reference */ + return _PyUnicode_AsString(name); } PyObject* @@ -219,7 +230,7 @@ PyModule_GetFilename(PyObject *m) if (fileobj == NULL) return NULL; utf8 = _PyUnicode_AsString(fileobj); - Py_DECREF(fileobj); + Py_DECREF(fileobj); /* module dict has still a reference */ return utf8; } @@ -347,21 +358,25 @@ module_dealloc(PyModuleObject *m) static PyObject * module_repr(PyModuleObject *m) { - const char *name; - PyObject *filename, *repr; + PyObject *name, *filename, *repr; - name = PyModule_GetName((PyObject *)m); + name = PyModule_GetNameObject((PyObject *)m); if (name == NULL) { PyErr_Clear(); - name = "?"; + name = PyUnicode_FromStringAndSize("?", 1); + if (name == NULL) + return NULL; } filename = PyModule_GetFilenameObject((PyObject *)m); if (filename == NULL) { PyErr_Clear(); - return PyUnicode_FromFormat("<module '%s' (built-in)>", name); + repr = PyUnicode_FromFormat("<module %R (built-in)>", name); + } + else { + repr = PyUnicode_FromFormat("<module %R from %R>", name, filename); + Py_DECREF(filename); } - repr = PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename); - Py_DECREF(filename); + Py_DECREF(name); return repr; } |