[Python-checkins] r84168 - in python/branches/py3k: Doc/c-api/module.rst Include/moduleobject.h Misc/NEWS Objects/moduleobject.c

victor.stinner python-checkins at python.org
Wed Aug 18 01:37:15 CEST 2010


Author: victor.stinner
Date: Wed Aug 18 01:37:11 2010
New Revision: 84168

Log:
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.


Modified:
   python/branches/py3k/Doc/c-api/module.rst
   python/branches/py3k/Include/moduleobject.h
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/moduleobject.c

Modified: python/branches/py3k/Doc/c-api/module.rst
==============================================================================
--- python/branches/py3k/Doc/c-api/module.rst	(original)
+++ python/branches/py3k/Doc/c-api/module.rst	Wed Aug 18 01:37:11 2010
@@ -64,13 +64,24 @@
 
 .. 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)

Modified: python/branches/py3k/Include/moduleobject.h
==============================================================================
--- python/branches/py3k/Include/moduleobject.h	(original)
+++ python/branches/py3k/Include/moduleobject.h	Wed Aug 18 01:37:11 2010
@@ -16,6 +16,7 @@
 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*);

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Aug 18 01:37:11 2010
@@ -12,6 +12,11 @@
 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.

Modified: python/branches/py3k/Objects/moduleobject.c
==============================================================================
--- python/branches/py3k/Objects/moduleobject.c	(original)
+++ python/branches/py3k/Objects/moduleobject.c	Wed Aug 18 01:37:11 2010
@@ -188,8 +188,8 @@
     return _PyUnicode_AsString(nameobj);
 }
 
-static PyObject*
-module_getfilename(PyObject *m)
+PyObject*
+PyModule_GetFilenameObject(PyObject *m)
 {
     PyObject *d;
     PyObject *fileobj;
@@ -205,6 +205,7 @@
         PyErr_SetString(PyExc_SystemError, "module filename missing");
         return NULL;
     }
+    Py_INCREF(fileobj);
     return fileobj;
 }
 
@@ -212,10 +213,13 @@
 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 @@
 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


More information about the Python-checkins mailing list