[Python-checkins] r82689 - python/branches/import_unicode/Python/import.c

victor.stinner python-checkins at python.org
Fri Jul 9 01:32:48 CEST 2010


Author: victor.stinner
Date: Fri Jul  9 01:32:48 2010
New Revision: 82689

Log:
check_compiled_module() and read_compiled_module() use objects

Modified:
   python/branches/import_unicode/Python/import.c

Modified: python/branches/import_unicode/Python/import.c
==============================================================================
--- python/branches/import_unicode/Python/import.c	(original)
+++ python/branches/import_unicode/Python/import.c	Fri Jul  9 01:32:48 2010
@@ -1055,11 +1055,17 @@
    Doesn't set an exception. */
 
 static FILE *
-check_compiled_module(char *pathname, time_t mtime, char *cpathname)
+check_compiled_module(PyObject *pathobj, time_t mtime, PyObject *cpathobj)
 {
     FILE *fp;
     long magic;
     long pyc_mtime;
+    char *cpathname;
+
+    /* FIXME: use PyUnicode_EncodeFSDefault() */
+    cpathname = _PyUnicode_AsString(cpathobj);
+    if (cpathname == NULL)
+        return NULL;
 
     fp = fopen(cpathname, "rb");
     if (fp == NULL)
@@ -1067,19 +1073,19 @@
     magic = PyMarshal_ReadLongFromFile(fp);
     if (magic != pyc_magic) {
         if (Py_VerboseFlag)
-            PySys_WriteStderr("# %s has bad magic\n", cpathname);
+            PySys_FormatStderr("# %U has bad magic\n", cpathobj);
         fclose(fp);
         return NULL;
     }
     pyc_mtime = PyMarshal_ReadLongFromFile(fp);
     if (pyc_mtime != mtime) {
         if (Py_VerboseFlag)
-            PySys_WriteStderr("# %s has bad mtime\n", cpathname);
+            PySys_FormatStderr("# %s has bad mtime\n", cpathobj);
         fclose(fp);
         return NULL;
     }
     if (Py_VerboseFlag)
-        PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
+        PySys_FormatStderr("# %U matches %U\n", cpathobj, pathobj);
     return fp;
 }
 
@@ -1087,7 +1093,7 @@
 /* Read a code object from a file and check it for validity */
 
 static PyCodeObject *
-read_compiled_module(char *cpathname, FILE *fp)
+read_compiled_module(PyObject *cpathobj, FILE *fp)
 {
     PyObject *co;
 
@@ -1096,7 +1102,7 @@
         return NULL;
     if (!PyCode_Check(co)) {
         PyErr_Format(PyExc_ImportError,
-                     "Non-code object in %.200s", cpathname);
+                     "Non-code object in %U", cpathobj);
         Py_DECREF(co);
         return NULL;
     }
@@ -1113,19 +1119,15 @@
     long magic;
     PyCodeObject *co;
     PyObject *m;
-    char *cpathname;
-
-    /* FIXME: don't use _PyUnicode_AsString */
-    cpathname = _PyUnicode_AsString(cpathobj);
 
     magic = PyMarshal_ReadLongFromFile(fp);
     if (magic != pyc_magic) {
         PyErr_Format(PyExc_ImportError,
-                     "Bad magic number in %.200s", cpathname);
+                     "Bad magic number in %U", cpathobj);
         return NULL;
     }
     (void) PyMarshal_ReadLongFromFile(fp);
-    co = read_compiled_module(cpathname, fp);
+    co = read_compiled_module(cpathobj, fp);
     if (co == NULL)
         return NULL;
     if (Py_VerboseFlag)
@@ -1325,17 +1327,11 @@
 {
     struct stat st;
     FILE *fpc;
-    char *pathname;
     char *cpathname;
     PyObject *cpathobj;
     PyCodeObject *co;
     PyObject *m;
 
-    /* FIXME: use PyUnicode_EncodeFSDefault() */
-    pathname = _PyUnicode_AsString(pathobj);
-    if (pathname == NULL)
-        return NULL;
-
     if (fstat(fileno(fp), &st) != 0) {
         PyErr_Format(PyExc_RuntimeError,
                      "unable to get file status from '%U'",
@@ -1361,7 +1357,7 @@
             Py_DECREF(cpathobj);
             return NULL;
         }
-        fpc = check_compiled_module(pathname, st.st_mtime, cpathname);
+        fpc = check_compiled_module(pathobj, st.st_mtime, cpathobj);
     }
     else {
         if (PyErr_Occurred())
@@ -1370,7 +1366,7 @@
         fpc = NULL;
     }
     if (fpc) {
-        co = read_compiled_module(cpathname, fpc);
+        co = read_compiled_module(cpathobj, fpc);
         fclose(fpc);
         if (co == NULL) {
             Py_XDECREF(cpathobj);


More information about the Python-checkins mailing list