diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst index 2c7faf0d2e3..40acc4e424c 100644 --- a/Doc/c-api/module.rst +++ b/Doc/c-api/module.rst @@ -64,13 +64,24 @@ There are only a few functions special to module objects. .. 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:: single: __file__ (module attribute) single: SystemError (built-in exception) 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, - raise :exc:`SystemError` and return *NULL*. + :attr:`__file__` attribute. If this is not defined, or if it is not a + unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return + a reference to a :ctype:`PyUnicodeObject`. .. cfunction:: void* PyModule_GetState(PyObject *module) diff --git a/Include/moduleobject.h b/Include/moduleobject.h index 42eac82e950..376a525d1c3 100644 --- a/Include/moduleobject.h +++ b/Include/moduleobject.h @@ -16,6 +16,7 @@ PyAPI_FUNC(PyObject *) PyModule_New(const char *); PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *); PyAPI_FUNC(const char *) PyModule_GetName(PyObject *); PyAPI_FUNC(const char *) PyModule_GetFilename(PyObject *); +PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *); PyAPI_FUNC(void) _PyModule_Clear(PyObject *); PyAPI_FUNC(struct PyModuleDef*) PyModule_GetDef(PyObject*); PyAPI_FUNC(void*) PyModule_GetState(PyObject*); diff --git a/Misc/NEWS b/Misc/NEWS index 586aa7e0e7e..8a1d414c097 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 2? 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 #9612: The set object is now 64-bit clean under Windows. diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 7b8e1b63c4a..3a952610282 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -188,8 +188,8 @@ PyModule_GetName(PyObject *m) return _PyUnicode_AsString(nameobj); } -static PyObject* -module_getfilename(PyObject *m) +PyObject* +PyModule_GetFilenameObject(PyObject *m) { PyObject *d; PyObject *fileobj; @@ -205,6 +205,7 @@ module_getfilename(PyObject *m) PyErr_SetString(PyExc_SystemError, "module filename missing"); return NULL; } + Py_INCREF(fileobj); return fileobj; } @@ -212,10 +213,13 @@ const char * PyModule_GetFilename(PyObject *m) { PyObject *fileobj; - fileobj = module_getfilename(m); + char *utf8; + fileobj = PyModule_GetFilenameObject(m); if (fileobj == NULL) return NULL; - return _PyUnicode_AsString(fileobj); + utf8 = _PyUnicode_AsString(fileobj); + Py_DECREF(fileobj); + return utf8; } PyModuleDef* @@ -346,19 +350,21 @@ static PyObject * module_repr(PyModuleObject *m) { const char *name; - PyObject *filename; + PyObject *filename, *repr; name = PyModule_GetName((PyObject *)m); if (name == NULL) { PyErr_Clear(); name = "?"; } - filename = module_getfilename((PyObject *)m); + filename = PyModule_GetFilenameObject((PyObject *)m); if (filename == NULL) { PyErr_Clear(); return PyUnicode_FromFormat("", name); } - return PyUnicode_FromFormat("", name, filename); + repr = PyUnicode_FromFormat("", name, filename); + Py_DECREF(filename); + return repr; } static int