mirror of https://github.com/python/cpython.git
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.
This commit is contained in:
parent
501c09a754
commit
bd475115c4
|
@ -52,7 +52,7 @@ There are only a few functions special to module objects.
|
||||||
manipulate a module's :attr:`__dict__`.
|
manipulate a module's :attr:`__dict__`.
|
||||||
|
|
||||||
|
|
||||||
.. c:function:: char* PyModule_GetName(PyObject *module)
|
.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module)
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
single: __name__ (module attribute)
|
single: __name__ (module attribute)
|
||||||
|
@ -61,15 +61,13 @@ There are only a few functions special to module objects.
|
||||||
Return *module*'s :attr:`__name__` value. If the module does not provide one,
|
Return *module*'s :attr:`__name__` value. If the module does not provide one,
|
||||||
or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
|
or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
|
||||||
|
|
||||||
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
.. c:function:: char* PyModule_GetFilename(PyObject *module)
|
|
||||||
|
|
||||||
Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
|
.. c:function:: char* PyModule_GetName(PyObject *module)
|
||||||
encoded to 'utf-8'.
|
|
||||||
|
|
||||||
.. deprecated:: 3.2
|
Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to
|
||||||
:c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
|
``'utf-8'``.
|
||||||
unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
|
|
||||||
|
|
||||||
|
|
||||||
.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
|
.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
|
||||||
|
@ -86,6 +84,16 @@ There are only a few functions special to module objects.
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
|
|
||||||
|
|
||||||
|
.. c:function:: char* PyModule_GetFilename(PyObject *module)
|
||||||
|
|
||||||
|
Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
|
||||||
|
encoded to 'utf-8'.
|
||||||
|
|
||||||
|
.. deprecated:: 3.2
|
||||||
|
:c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
|
||||||
|
unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
|
||||||
|
|
||||||
|
|
||||||
.. c:function:: void* PyModule_GetState(PyObject *module)
|
.. c:function:: void* PyModule_GetState(PyObject *module)
|
||||||
|
|
||||||
Return the "state" of the module, that is, a pointer to the block of memory
|
Return the "state" of the module, that is, a pointer to the block of memory
|
||||||
|
|
|
@ -16,6 +16,7 @@ PyAPI_FUNC(PyObject *) PyModule_New(
|
||||||
const char *name /* UTF-8 encoded string */
|
const char *name /* UTF-8 encoded string */
|
||||||
);
|
);
|
||||||
PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *);
|
PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *);
|
||||||
|
PyAPI_FUNC(PyObject *) PyModule_GetNameObject(PyObject *);
|
||||||
PyAPI_FUNC(const char *) PyModule_GetName(PyObject *);
|
PyAPI_FUNC(const char *) PyModule_GetName(PyObject *);
|
||||||
PyAPI_FUNC(const char *) PyModule_GetFilename(PyObject *);
|
PyAPI_FUNC(const char *) PyModule_GetFilename(PyObject *);
|
||||||
PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *);
|
PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *);
|
||||||
|
|
|
@ -169,24 +169,35 @@ PyModule_GetDict(PyObject *m)
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
PyObject*
|
||||||
PyModule_GetName(PyObject *m)
|
PyModule_GetNameObject(PyObject *m)
|
||||||
{
|
{
|
||||||
PyObject *d;
|
PyObject *d;
|
||||||
PyObject *nameobj;
|
PyObject *name;
|
||||||
if (!PyModule_Check(m)) {
|
if (!PyModule_Check(m)) {
|
||||||
PyErr_BadArgument();
|
PyErr_BadArgument();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
d = ((PyModuleObject *)m)->md_dict;
|
d = ((PyModuleObject *)m)->md_dict;
|
||||||
if (d == NULL ||
|
if (d == NULL ||
|
||||||
(nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
|
(name = PyDict_GetItemString(d, "__name__")) == NULL ||
|
||||||
!PyUnicode_Check(nameobj))
|
!PyUnicode_Check(name))
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_SystemError, "nameless module");
|
PyErr_SetString(PyExc_SystemError, "nameless module");
|
||||||
return NULL;
|
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*
|
PyObject*
|
||||||
|
@ -219,7 +230,7 @@ PyModule_GetFilename(PyObject *m)
|
||||||
if (fileobj == NULL)
|
if (fileobj == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
utf8 = _PyUnicode_AsString(fileobj);
|
utf8 = _PyUnicode_AsString(fileobj);
|
||||||
Py_DECREF(fileobj);
|
Py_DECREF(fileobj); /* module dict has still a reference */
|
||||||
return utf8;
|
return utf8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,21 +358,25 @@ module_dealloc(PyModuleObject *m)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
module_repr(PyModuleObject *m)
|
module_repr(PyModuleObject *m)
|
||||||
{
|
{
|
||||||
const char *name;
|
PyObject *name, *filename, *repr;
|
||||||
PyObject *filename, *repr;
|
|
||||||
|
|
||||||
name = PyModule_GetName((PyObject *)m);
|
name = PyModule_GetNameObject((PyObject *)m);
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
name = "?";
|
name = PyUnicode_FromStringAndSize("?", 1);
|
||||||
|
if (name == NULL)
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
filename = PyModule_GetFilenameObject((PyObject *)m);
|
filename = PyModule_GetFilenameObject((PyObject *)m);
|
||||||
if (filename == NULL) {
|
if (filename == NULL) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
|
repr = PyUnicode_FromFormat("<module %R (built-in)>", name);
|
||||||
}
|
}
|
||||||
repr = PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename);
|
else {
|
||||||
Py_DECREF(filename);
|
repr = PyUnicode_FromFormat("<module %R from %R>", name, filename);
|
||||||
|
Py_DECREF(filename);
|
||||||
|
}
|
||||||
|
Py_DECREF(name);
|
||||||
return repr;
|
return repr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue