Skip to content

Commit 6c00c14

Browse files
author
Victor Stinner
committed
Issue python#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.
1 parent 6951157 commit 6c00c14

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

Doc/c-api/module.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,24 @@ There are only a few functions special to module objects.
6464

6565
.. cfunction:: char* PyModule_GetFilename(PyObject *module)
6666

67+
Similar to :cfunc:`PyModule_GetFilenameObject` but return the filename
68+
encoded to 'utf-8'.
69+
70+
.. deprecated:: 3.2
71+
:cfunc:`PyModule_GetFilename` raises :ctype:`UnicodeEncodeError` on
72+
unencodable filenames, use :cfunc:`PyModule_GetFilenameObject` instead.
73+
74+
75+
.. cfunction:: PyObject* PyModule_GetFilenameObject(PyObject *module)
76+
6777
.. index::
6878
single: __file__ (module attribute)
6979
single: SystemError (built-in exception)
7080

7181
Return the name of the file from which *module* was loaded using *module*'s
72-
:attr:`__file__` attribute. If this is not defined, or if it is not a string,
73-
raise :exc:`SystemError` and return *NULL*.
82+
:attr:`__file__` attribute. If this is not defined, or if it is not a
83+
unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return
84+
a reference to a :ctype:`PyUnicodeObject`.
7485

7586

7687
.. cfunction:: void* PyModule_GetState(PyObject *module)

Include/moduleobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ PyAPI_FUNC(PyObject *) PyModule_New(const char *);
1616
PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *);
1717
PyAPI_FUNC(const char *) PyModule_GetName(PyObject *);
1818
PyAPI_FUNC(const char *) PyModule_GetFilename(PyObject *);
19+
PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *);
1920
PyAPI_FUNC(void) _PyModule_Clear(PyObject *);
2021
PyAPI_FUNC(struct PyModuleDef*) PyModule_GetDef(PyObject*);
2122
PyAPI_FUNC(void*) PyModule_GetState(PyObject*);

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #9425: Create PyModule_GetFilenameObject() function to get the filename
16+
as a unicode object, instead of a byte string. Function needed to support
17+
unencodable filenames. Deprecate PyModule_GetFilename() in favor on the new
18+
function.
19+
1520
- Issue #8063: Call _PyGILState_Init() earlier in Py_InitializeEx().
1621

1722
- Issue #9612: The set object is now 64-bit clean under Windows.

Objects/moduleobject.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ PyModule_GetName(PyObject *m)
188188
return _PyUnicode_AsString(nameobj);
189189
}
190190

191-
static PyObject*
192-
module_getfilename(PyObject *m)
191+
PyObject*
192+
PyModule_GetFilenameObject(PyObject *m)
193193
{
194194
PyObject *d;
195195
PyObject *fileobj;
@@ -205,17 +205,21 @@ module_getfilename(PyObject *m)
205205
PyErr_SetString(PyExc_SystemError, "module filename missing");
206206
return NULL;
207207
}
208+
Py_INCREF(fileobj);
208209
return fileobj;
209210
}
210211

211212
const char *
212213
PyModule_GetFilename(PyObject *m)
213214
{
214215
PyObject *fileobj;
215-
fileobj = module_getfilename(m);
216+
char *utf8;
217+
fileobj = PyModule_GetFilenameObject(m);
216218
if (fileobj == NULL)
217219
return NULL;
218-
return _PyUnicode_AsString(fileobj);
220+
utf8 = _PyUnicode_AsString(fileobj);
221+
Py_DECREF(fileobj);
222+
return utf8;
219223
}
220224

221225
PyModuleDef*
@@ -346,19 +350,21 @@ static PyObject *
346350
module_repr(PyModuleObject *m)
347351
{
348352
const char *name;
349-
PyObject *filename;
353+
PyObject *filename, *repr;
350354

351355
name = PyModule_GetName((PyObject *)m);
352356
if (name == NULL) {
353357
PyErr_Clear();
354358
name = "?";
355359
}
356-
filename = module_getfilename((PyObject *)m);
360+
filename = PyModule_GetFilenameObject((PyObject *)m);
357361
if (filename == NULL) {
358362
PyErr_Clear();
359363
return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
360364
}
361-
return PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename);
365+
repr = PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename);
366+
Py_DECREF(filename);
367+
return repr;
362368
}
363369

364370
static int

0 commit comments

Comments
 (0)