diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2012-06-16 01:22:05 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2012-06-16 01:22:05 (GMT) |
commit | 81c39a88a4c692a1110d9b1e4ae05410cf34f4c8 (patch) | |
tree | 293b507bab206ef355d1912527f4cdb7cfef3cba /Python/import.c | |
parent | ca439eecea1f293d6954cc5cb562199480b74c17 (diff) | |
download | cpython-81c39a88a4c692a1110d9b1e4ae05410cf34f4c8.zip cpython-81c39a88a4c692a1110d9b1e4ae05410cf34f4c8.tar.gz cpython-81c39a88a4c692a1110d9b1e4ae05410cf34f4c8.tar.bz2 |
get_sourcefile(): use PyUnicode_READ() to avoid the creation of a temporary
Py_UCS4 buffer
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/Python/import.c b/Python/import.c index 98d9dc5..701a6e9 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1008,23 +1008,25 @@ static PyObject * get_sourcefile(PyObject *filename) { Py_ssize_t len; - Py_UCS4 *fileuni; PyObject *py; struct stat statbuf; int err; + void *data; + unsigned int kind; len = PyUnicode_GET_LENGTH(filename); if (len == 0) Py_RETURN_NONE; /* don't match *.pyc or *.pyo? */ - fileuni = PyUnicode_AsUCS4Copy(filename); - if (!fileuni) - return NULL; + data = PyUnicode_DATA(filename); + kind = PyUnicode_KIND(filename); if (len < 5 - || fileuni[len-4] != '.' - || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P') - || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y')) + || PyUnicode_READ(kind, data, len-4) != '.' + || (PyUnicode_READ(kind, data, len-3) != 'p' + && PyUnicode_READ(kind, data, len-3) != 'P') + || (PyUnicode_READ(kind, data, len-2) != 'y' + && PyUnicode_READ(kind, data, len-2) != 'Y')) goto unchanged; /* Start by trying to turn PEP 3147 path into source path. If that @@ -1034,7 +1036,7 @@ get_sourcefile(PyObject *filename) py = make_source_pathname(filename); if (py == NULL) { PyErr_Clear(); - py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1); + py = PyUnicode_Substring(filename, 0, len - 1); } if (py == NULL) goto error; @@ -1042,17 +1044,14 @@ get_sourcefile(PyObject *filename) err = _Py_stat(py, &statbuf); if (err == -2) goto error; - if (err == 0 && S_ISREG(statbuf.st_mode)) { - PyMem_Free(fileuni); + if (err == 0 && S_ISREG(statbuf.st_mode)) return py; - } Py_DECREF(py); goto unchanged; error: PyErr_Clear(); unchanged: - PyMem_Free(fileuni); Py_INCREF(filename); return filename; } |