Issue #9425: Create PyModule_GetFilenameObject() function

... to get the filename as a unicode object, instead of a byte string. Function
needed to support unencodable filenames. Deprecate PyModule_GetFilename() in
favor on the new function.
This commit is contained in:
Victor Stinner 2010-08-17 23:37:11 +00:00
parent 6951157475
commit 6c00c1464f
4 changed files with 32 additions and 9 deletions

View File

@ -64,13 +64,24 @@ There are only a few functions special to module objects.
.. cfunction:: char* PyModule_GetFilename(PyObject *module) .. cfunction:: char* PyModule_GetFilename(PyObject *module)
Similar to :cfunc:`PyModule_GetFilenameObject` but return the filename
encoded to 'utf-8'.
.. deprecated:: 3.2
:cfunc:`PyModule_GetFilename` raises :ctype:`UnicodeEncodeError` on
unencodable filenames, use :cfunc:`PyModule_GetFilenameObject` instead.
.. cfunction:: PyObject* PyModule_GetFilenameObject(PyObject *module)
.. index:: .. index::
single: __file__ (module attribute) single: __file__ (module attribute)
single: SystemError (built-in exception) single: SystemError (built-in exception)
Return the name of the file from which *module* was loaded using *module*'s Return the name of the file from which *module* was loaded using *module*'s
:attr:`__file__` attribute. If this is not defined, or if it is not a string, :attr:`__file__` attribute. If this is not defined, or if it is not a
raise :exc:`SystemError` and return *NULL*. unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return
a reference to a :ctype:`PyUnicodeObject`.
.. cfunction:: void* PyModule_GetState(PyObject *module) .. cfunction:: void* PyModule_GetState(PyObject *module)

View File

@ -16,6 +16,7 @@ PyAPI_FUNC(PyObject *) PyModule_New(const char *);
PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *); PyAPI_FUNC(PyObject *) PyModule_GetDict(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(void) _PyModule_Clear(PyObject *); PyAPI_FUNC(void) _PyModule_Clear(PyObject *);
PyAPI_FUNC(struct PyModuleDef*) PyModule_GetDef(PyObject*); PyAPI_FUNC(struct PyModuleDef*) PyModule_GetDef(PyObject*);
PyAPI_FUNC(void*) PyModule_GetState(PyObject*); PyAPI_FUNC(void*) PyModule_GetState(PyObject*);

View File

@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 2?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #9425: Create PyModule_GetFilenameObject() function to get the filename
as a unicode object, instead of a byte string. Function needed to support
unencodable filenames. Deprecate PyModule_GetFilename() in favor on the new
function.
- Issue #8063: Call _PyGILState_Init() earlier in Py_InitializeEx(). - Issue #8063: Call _PyGILState_Init() earlier in Py_InitializeEx().
- Issue #9612: The set object is now 64-bit clean under Windows. - Issue #9612: The set object is now 64-bit clean under Windows.

View File

@ -188,8 +188,8 @@ PyModule_GetName(PyObject *m)
return _PyUnicode_AsString(nameobj); return _PyUnicode_AsString(nameobj);
} }
static PyObject* PyObject*
module_getfilename(PyObject *m) PyModule_GetFilenameObject(PyObject *m)
{ {
PyObject *d; PyObject *d;
PyObject *fileobj; PyObject *fileobj;
@ -205,6 +205,7 @@ module_getfilename(PyObject *m)
PyErr_SetString(PyExc_SystemError, "module filename missing"); PyErr_SetString(PyExc_SystemError, "module filename missing");
return NULL; return NULL;
} }
Py_INCREF(fileobj);
return fileobj; return fileobj;
} }
@ -212,10 +213,13 @@ const char *
PyModule_GetFilename(PyObject *m) PyModule_GetFilename(PyObject *m)
{ {
PyObject *fileobj; PyObject *fileobj;
fileobj = module_getfilename(m); char *utf8;
fileobj = PyModule_GetFilenameObject(m);
if (fileobj == NULL) if (fileobj == NULL)
return NULL; return NULL;
return _PyUnicode_AsString(fileobj); utf8 = _PyUnicode_AsString(fileobj);
Py_DECREF(fileobj);
return utf8;
} }
PyModuleDef* PyModuleDef*
@ -346,19 +350,21 @@ static PyObject *
module_repr(PyModuleObject *m) module_repr(PyModuleObject *m)
{ {
const char *name; const char *name;
PyObject *filename; PyObject *filename, *repr;
name = PyModule_GetName((PyObject *)m); name = PyModule_GetName((PyObject *)m);
if (name == NULL) { if (name == NULL) {
PyErr_Clear(); PyErr_Clear();
name = "?"; name = "?";
} }
filename = module_getfilename((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); return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
} }
return PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename); repr = PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename);
Py_DECREF(filename);
return repr;
} }
static int static int