mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
get_sourcefile(): use PyUnicode_READ() to avoid the creation of a temporary
Py_UCS4 buffer
This commit is contained in:
parent
ca439eecea
commit
81c39a88a4
1 changed files with 11 additions and 12 deletions
|
@ -1008,23 +1008,25 @@ static PyObject *
|
||||||
get_sourcefile(PyObject *filename)
|
get_sourcefile(PyObject *filename)
|
||||||
{
|
{
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
Py_UCS4 *fileuni;
|
|
||||||
PyObject *py;
|
PyObject *py;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
int err;
|
int err;
|
||||||
|
void *data;
|
||||||
|
unsigned int kind;
|
||||||
|
|
||||||
len = PyUnicode_GET_LENGTH(filename);
|
len = PyUnicode_GET_LENGTH(filename);
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
|
|
||||||
/* don't match *.pyc or *.pyo? */
|
/* don't match *.pyc or *.pyo? */
|
||||||
fileuni = PyUnicode_AsUCS4Copy(filename);
|
data = PyUnicode_DATA(filename);
|
||||||
if (!fileuni)
|
kind = PyUnicode_KIND(filename);
|
||||||
return NULL;
|
|
||||||
if (len < 5
|
if (len < 5
|
||||||
|| fileuni[len-4] != '.'
|
|| PyUnicode_READ(kind, data, len-4) != '.'
|
||||||
|| (fileuni[len-3] != 'p' && fileuni[len-3] != 'P')
|
|| (PyUnicode_READ(kind, data, len-3) != 'p'
|
||||||
|| (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y'))
|
&& PyUnicode_READ(kind, data, len-3) != 'P')
|
||||||
|
|| (PyUnicode_READ(kind, data, len-2) != 'y'
|
||||||
|
&& PyUnicode_READ(kind, data, len-2) != 'Y'))
|
||||||
goto unchanged;
|
goto unchanged;
|
||||||
|
|
||||||
/* Start by trying to turn PEP 3147 path into source path. If that
|
/* 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);
|
py = make_source_pathname(filename);
|
||||||
if (py == NULL) {
|
if (py == NULL) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1);
|
py = PyUnicode_Substring(filename, 0, len - 1);
|
||||||
}
|
}
|
||||||
if (py == NULL)
|
if (py == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
|
@ -1042,17 +1044,14 @@ get_sourcefile(PyObject *filename)
|
||||||
err = _Py_stat(py, &statbuf);
|
err = _Py_stat(py, &statbuf);
|
||||||
if (err == -2)
|
if (err == -2)
|
||||||
goto error;
|
goto error;
|
||||||
if (err == 0 && S_ISREG(statbuf.st_mode)) {
|
if (err == 0 && S_ISREG(statbuf.st_mode))
|
||||||
PyMem_Free(fileuni);
|
|
||||||
return py;
|
return py;
|
||||||
}
|
|
||||||
Py_DECREF(py);
|
Py_DECREF(py);
|
||||||
goto unchanged;
|
goto unchanged;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
unchanged:
|
unchanged:
|
||||||
PyMem_Free(fileuni);
|
|
||||||
Py_INCREF(filename);
|
Py_INCREF(filename);
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue