Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9af29d3

Browse files
committed
Rewrite find_module_path using unicode API.
1 parent 2cc0cc5 commit 9af29d3

1 file changed

Lines changed: 48 additions & 35 deletions

File tree

Python/import.c

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,11 +1743,11 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
17431743
PyObject *path_hooks, PyObject *path_importer_cache,
17441744
PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd)
17451745
{
1746-
Py_UCS4 buf[MAXPATHLEN+1];
1747-
PyObject *path_unicode, *filename;
1748-
Py_ssize_t len;
1746+
PyObject *path_unicode, *filename = NULL;
1747+
Py_ssize_t len, pos;
17491748
struct stat statbuf;
17501749
static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1750+
int result, addsep;
17511751

17521752
if (PyUnicode_Check(path)) {
17531753
Py_INCREF(path);
@@ -1766,15 +1766,10 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
17661766
return -1;
17671767

17681768
len = PyUnicode_GET_LENGTH(path_unicode);
1769-
if (!PyUnicode_AsUCS4(path_unicode, buf, Py_ARRAY_LENGTH(buf), 1)) {
1770-
Py_DECREF(path_unicode);
1771-
PyErr_Clear();
1772-
return 0;
1769+
if (PyUnicode_FindChar(path_unicode, 0, 0, len, 1) != -1) {
1770+
result = 0;
1771+
goto out; /* path contains '\0' */
17731772
}
1774-
Py_DECREF(path_unicode);
1775-
1776-
if (Py_UCS4_strlen(buf) != len)
1777-
return 0; /* path contains '\0' */
17781773

17791774
/* sys.path_hooks import hook */
17801775
if (p_loader != NULL) {
@@ -1784,43 +1779,54 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
17841779
importer = get_path_importer(path_importer_cache,
17851780
path_hooks, path);
17861781
if (importer == NULL) {
1787-
return -1;
1782+
result = -1;
1783+
goto out;
17881784
}
17891785
/* Note: importer is a borrowed reference */
17901786
if (importer != Py_None) {
17911787
PyObject *loader;
17921788
loader = _PyObject_CallMethodId(importer,
17931789
&PyId_find_module, "O", fullname);
1794-
if (loader == NULL)
1795-
return -1; /* error */
1790+
if (loader == NULL) {
1791+
result = -1; /* error */
1792+
goto out;
1793+
}
17961794
if (loader != Py_None) {
17971795
/* a loader was found */
17981796
*p_loader = loader;
17991797
*p_fd = &importhookdescr;
1800-
return 2;
1798+
result = 2;
1799+
goto out;
18011800
}
18021801
Py_DECREF(loader);
1803-
return 0;
1802+
result = 0;
1803+
goto out;
18041804
}
18051805
}
18061806
/* no hook was found, use builtin import */
18071807

1808-
if (len > 0 && buf[len-1] != SEP
1808+
addsep = 0;
1809+
if (len > 0 && PyUnicode_READ_CHAR(path_unicode, len-1) != SEP
18091810
#ifdef ALTSEP
1810-
&& buf[len-1] != ALTSEP
1811+
&& PyUnicode_READ_CHAR(path_unicode, len-1) != ALTSEP
18111812
#endif
18121813
)
1813-
buf[len++] = SEP;
1814-
if (!PyUnicode_AsUCS4(name, buf+len, Py_ARRAY_LENGTH(buf)-len, 1)) {
1815-
PyErr_Clear();
1816-
return 0;
1817-
}
1818-
len += PyUnicode_GET_LENGTH(name);
1819-
1820-
filename = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
1821-
buf, len);
1822-
if (filename == NULL)
1823-
return -1;
1814+
addsep = 1;
1815+
filename = PyUnicode_New(len + PyUnicode_GET_LENGTH(name) + addsep,
1816+
Py_MAX(PyUnicode_MAX_CHAR_VALUE(path_unicode),
1817+
PyUnicode_MAX_CHAR_VALUE(name)));
1818+
if (filename == NULL) {
1819+
result = -1;
1820+
goto out;
1821+
}
1822+
PyUnicode_CopyCharacters(filename, 0, path_unicode, 0, len);
1823+
pos = len;
1824+
if (addsep)
1825+
PyUnicode_WRITE(PyUnicode_KIND(filename),
1826+
PyUnicode_DATA(filename),
1827+
pos++, SEP);
1828+
PyUnicode_CopyCharacters(filename, pos, name, 0,
1829+
PyUnicode_GET_LENGTH(name));
18241830

18251831
/* Check for package import (buf holds a directory name,
18261832
and there's an __init__ module in that directory */
@@ -1832,30 +1838,37 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
18321838

18331839
match = case_ok(filename, 0, name);
18341840
if (match < 0) {
1835-
Py_DECREF(filename);
1836-
return -1;
1841+
result = -1;
1842+
goto out;
18371843
}
18381844
if (match) { /* case matches */
18391845
if (find_init_module(filename)) { /* and has __init__.py */
18401846
*p_path = filename;
1847+
filename = NULL;
18411848
*p_fd = &fd_package;
1842-
return 2;
1849+
result = 2;
1850+
goto out;
18431851
}
18441852
else {
18451853
int err;
18461854
err = PyErr_WarnFormat(PyExc_ImportWarning, 1,
18471855
"Not importing directory %R: missing __init__.py",
18481856
filename);
18491857
if (err) {
1850-
Py_DECREF(filename);
1851-
return -1;
1858+
result = -1;
1859+
goto out;
18521860
}
18531861
}
18541862
}
18551863
}
18561864
#endif
18571865
*p_path = filename;
1858-
return 1;
1866+
filename = NULL;
1867+
result = 1;
1868+
out:
1869+
Py_DECREF(path_unicode);
1870+
Py_XDECREF(filename);
1871+
return result;
18591872
}
18601873

18611874
/* Find a module in search_path_list. For each path, try

0 commit comments

Comments
 (0)