[Python-checkins] r82699 - python/branches/import_unicode/Python/import.c
victor.stinner
python-checkins at python.org
Fri Jul 9 01:33:03 CEST 2010
Author: victor.stinner
Date: Fri Jul 9 01:33:03 2010
New Revision: 82699
Log:
create fopen_unicode()
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:33:03 2010
@@ -136,6 +136,13 @@
{0, 0}
};
+/* Forward */
+static FILE* fopen_unicode(PyObject *unicode, const char *mode);
+#ifdef HAVE_STAT
+static int stat_unicode(PyObject *unicode, struct stat *statbuf);
+static int find_init_module(char *);
+#endif
+
/* Initialize things */
@@ -1061,14 +1068,8 @@
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");
+ fp = fopen_unicode(cpathobj, "rb");
if (fp == NULL)
return NULL;
magic = PyMarshal_ReadLongFromFile(fp);
@@ -1411,7 +1412,6 @@
static PyObject *
get_sourcefile(PyObject *fileobj)
{
- char *py;
PyObject *pyobj;
Py_UNICODE *file;
Py_ssize_t len;
@@ -1448,11 +1448,7 @@
return NULL;
}
- /* FIXME: use PyUnicode_EncodeFSDefault() */
- py = _PyUnicode_AsString(pyobj);
- if (py == NULL)
- return NULL;
- if (stat(py, &statbuf) == 0 &&
+ if (stat_unicode(pyobj, &statbuf) == 0 &&
S_ISREG(statbuf.st_mode)) {
return pyobj;
}
@@ -1631,8 +1627,6 @@
#endif
static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
-static int find_init_module(char *); /* Forward */
-static int stat_unicode(PyObject *unicode, struct stat *statbuf);
static struct filedescr importhookdescr = {"", "", IMP_HOOK};
static struct filedescr *
@@ -1887,20 +1881,23 @@
}
#endif /* PYOS_OS2 */
strcpy(buf+len, fdp->suffix);
+ unicode = PyUnicode_DecodeFSDefault(buf);
if (Py_VerboseFlag > 1)
- PySys_FormatStderr("# trying %s\n", buf);
+ PySys_FormatStderr("# trying %U\n", unicode);
filemode = fdp->mode;
if (filemode[0] == 'U')
filemode = "r" PY_STDIOTEXTMODE;
- fp = fopen(buf, filemode);
+ fp = fopen_unicode(unicode, filemode);
if (fp != NULL) {
- if (case_ok(buf, len, namelen, name))
+ if (case_ok(buf, len, namelen, name)) {
+ Py_DECREF(unicode);
break;
- else { /* continue search */
+ } else { /* continue search */
fclose(fp);
fp = NULL;
}
}
+ Py_DECREF(unicode);
#if defined(PYOS_OS2)
/* restore the saved snapshot */
strcpy(buf, saved_buf);
@@ -2108,11 +2105,22 @@
#endif
}
+static FILE*
+fopen_unicode(PyObject *unicode, const char *mode)
+{
+ /* FIXME: use PyUnicode_EncodeFSDefault() */
+ char *pathstr = _PyUnicode_AsString(unicode);
+ if (pathstr == NULL)
+ return NULL;
+ return fopen(pathstr, mode);
+}
+
#ifdef HAVE_STAT
static int
stat_unicode(PyObject *unicode, struct stat *statbuf)
{
+ /* FIXME: use PyUnicode_EncodeFSDefault() */
char *pathstr = _PyUnicode_AsString(unicode);
if (pathstr == NULL)
return 1;
@@ -2127,6 +2135,7 @@
size_t i = save_len;
char *pname; /* pointer to start of __init__ */
struct stat statbuf;
+ PyObject *unicode;
/* For calling case_ok(buf, len, namelen, name):
* /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
@@ -2141,26 +2150,32 @@
buf[i++] = SEP;
pname = buf + i;
strcpy(pname, "__init__.py");
- if (stat(buf, &statbuf) == 0) {
+ unicode = PyUnicode_DecodeFSDefault(buf);
+ if (stat_unicode(unicode, &statbuf) == 0) {
if (case_ok(buf,
save_len + 9, /* len("/__init__") */
8, /* len("__init__") */
pname)) {
buf[save_len] = '\0';
+ Py_DECREF(unicode);
return 1;
}
}
+ Py_DECREF(unicode);
i += strlen(pname);
strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
- if (stat(buf, &statbuf) == 0) {
+ unicode = PyUnicode_DecodeFSDefault(buf);
+ if (stat_unicode(unicode, &statbuf) == 0) {
if (case_ok(buf,
save_len + 9, /* len("/__init__") */
8, /* len("__init__") */
pname)) {
buf[save_len] = '\0';
+ Py_DECREF(unicode);
return 1;
}
}
+ Py_DECREF(unicode);
buf[save_len] = '\0';
return 0;
}
@@ -3445,15 +3460,10 @@
get_file(PyObject *pathobj, PyObject *fob, char *mode)
{
FILE *fp;
- char *pathname;
if (mode[0] == 'U')
mode = "r" PY_STDIOTEXTMODE;
if (fob == NULL) {
- /* FIXME: don't use _PyUnicode_AsString */
- pathname = _PyUnicode_AsString(pathobj);
- if (pathname == NULL)
- return NULL;
- fp = fopen(pathname, mode);
+ fp = fopen_unicode(pathobj, mode);
}
else {
int fd = PyObject_AsFileDescriptor(fob);
More information about the Python-checkins
mailing list