]> granicus.if.org Git - python/commitdiff
Issue #9425: Create PyModule_GetFilenameObject() function
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 17 Aug 2010 23:37:11 +0000 (23:37 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 17 Aug 2010 23:37:11 +0000 (23:37 +0000)
... 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.

Doc/c-api/module.rst
Include/moduleobject.h
Misc/NEWS
Objects/moduleobject.c

index 2c7faf0d2e3b15361b8adf344f774d581b800658..40acc4e424c3c4edc664e80802d267618bfd5475 100644 (file)
@@ -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)
index 42eac82e9502ea2b44f1520eefc029e46ad3252a..376a525d1c389d9b667d71183aa8f9595b62380b 100644 (file)
@@ -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*);
index 586aa7e0e7e34783355670b6222022f1f6a57629..8a1d414c0978b550227d4ba611c5b7e983276274 100644 (file)
--- 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.
index 7b8e1b63c4af76a53b97f17edf58bbcd7fb1900b..3a9526102825faa50756b89b6fd3776ea0055451 100644 (file)
@@ -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("<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