comparison Python/import.c @ 68718:bdf5820f5a39

Issue #3080: Remove useless name buffer from find_module() Rename subname argument to name, and mark it as constant.
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 12 Mar 2011 09:26:54 -0500
parents d52f471fbbeb
children a4d797b9ff63
comparison
equal deleted inserted replaced
68717:d52f471fbbeb 68718:bdf5820f5a39
1426 return u; 1426 return u;
1427 } 1427 }
1428 1428
1429 /* Forward */ 1429 /* Forward */
1430 static PyObject *load_module(char *, FILE *, char *, int, PyObject *); 1430 static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
1431 static struct filedescr *find_module(char *, char *, PyObject *, 1431 static struct filedescr *find_module(char *, const char *, PyObject *,
1432 char *, size_t, FILE **, PyObject **); 1432 char *, size_t, FILE **, PyObject **);
1433 static struct _frozen * find_frozen(PyObject *); 1433 static struct _frozen * find_frozen(PyObject *);
1434 1434
1435 /* Load a package and return its module object WITH INCREMENTED 1435 /* Load a package and return its module object WITH INCREMENTED
1436 REFERENCE COUNT */ 1436 REFERENCE COUNT */
1592 #ifdef MS_COREDLL 1592 #ifdef MS_COREDLL
1593 extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **, 1593 extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
1594 PyObject **p_path); 1594 PyObject **p_path);
1595 #endif 1595 #endif
1596 1596
1597 static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *); 1597 static int case_ok(char *, Py_ssize_t, Py_ssize_t, const char *);
1598 static int find_init_module(char *); /* Forward */ 1598 static int find_init_module(char *); /* Forward */
1599 static struct filedescr importhookdescr = {"", "", IMP_HOOK}; 1599 static struct filedescr importhookdescr = {"", "", IMP_HOOK};
1600 1600
1601 /* Find a module: 1601 /* Find a module:
1602 1602
1622 1622
1623 By default, write an empty string into *buf, and *p_fp and *p_loader (if 1623 By default, write an empty string into *buf, and *p_fp and *p_loader (if
1624 set) are set to NULL. Eg. *buf is an empty string for a builtin package. */ 1624 set) are set to NULL. Eg. *buf is an empty string for a builtin package. */
1625 1625
1626 static struct filedescr * 1626 static struct filedescr *
1627 find_module(char *fullname, char *subname, PyObject *path, char *buf, 1627 find_module(char *fullname, const char *name, PyObject *path, char *buf,
1628 size_t buflen, FILE **p_fp, PyObject **p_loader) 1628 size_t buflen, FILE **p_fp, PyObject **p_loader)
1629 { 1629 {
1630 Py_ssize_t i, npath; 1630 Py_ssize_t i, npath;
1631 size_t len, namelen; 1631 size_t len, namelen;
1632 struct filedescr *fdp = NULL; 1632 struct filedescr *fdp = NULL;
1635 PyObject *path_hooks, *path_importer_cache; 1635 PyObject *path_hooks, *path_importer_cache;
1636 struct stat statbuf; 1636 struct stat statbuf;
1637 static struct filedescr fd_frozen = {"", "", PY_FROZEN}; 1637 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1638 static struct filedescr fd_builtin = {"", "", C_BUILTIN}; 1638 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1639 static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; 1639 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1640 char name[MAXPATHLEN+1];
1641 #if defined(PYOS_OS2) 1640 #if defined(PYOS_OS2)
1642 size_t saved_len; 1641 size_t saved_len;
1643 size_t saved_namelen; 1642 size_t saved_namelen;
1644 char *saved_buf = NULL; 1643 char *saved_buf = NULL;
1645 #endif 1644 #endif
1648 *buf = '\0'; 1647 *buf = '\0';
1649 *p_fp = NULL; 1648 *p_fp = NULL;
1650 if (p_loader != NULL) 1649 if (p_loader != NULL)
1651 *p_loader = NULL; 1650 *p_loader = NULL;
1652 1651
1653 if (strlen(subname) > MAXPATHLEN) { 1652 if (strlen(name) > MAXPATHLEN) {
1654 PyErr_SetString(PyExc_OverflowError, 1653 PyErr_SetString(PyExc_OverflowError,
1655 "module name is too long"); 1654 "module name is too long");
1656 return NULL; 1655 return NULL;
1657 } 1656 }
1658 strcpy(name, subname);
1659 1657
1660 /* sys.meta_path import hook */ 1658 /* sys.meta_path import hook */
1661 if (p_loader != NULL) { 1659 if (p_loader != NULL) {
1662 PyObject *meta_path; 1660 PyObject *meta_path;
1663 1661
1865 extension) 1863 extension)
1866 * so if the name is longer than that and its a 1864 * so if the name is longer than that and its a
1867 * dynamically loaded module we're going to try, 1865 * dynamically loaded module we're going to try,
1868 * truncate the name before trying 1866 * truncate the name before trying
1869 */ 1867 */
1870 if (strlen(subname) > 8) { 1868 if (strlen(name) > 8) {
1871 /* is this an attempt to load a C extension? */ 1869 /* is this an attempt to load a C extension? */
1872 const struct filedescr *scan; 1870 const struct filedescr *scan;
1873 scan = _PyImport_DynLoadFiletab; 1871 scan = _PyImport_DynLoadFiletab;
1874 while (scan->suffix != NULL) { 1872 while (scan->suffix != NULL) {
1875 if (!strcmp(scan->suffix, fdp->suffix)) 1873 if (!strcmp(scan->suffix, fdp->suffix))
1878 scan++; 1876 scan++;
1879 } 1877 }
1880 if (scan->suffix != NULL) { 1878 if (scan->suffix != NULL) {
1881 /* yes, so truncate the name */ 1879 /* yes, so truncate the name */
1882 namelen = 8; 1880 namelen = 8;
1883 len -= strlen(subname) - namelen; 1881 len -= strlen(name) - namelen;
1884 buf[len] = '\0'; 1882 buf[len] = '\0';
1885 } 1883 }
1886 } 1884 }
1887 #endif /* PYOS_OS2 */ 1885 #endif /* PYOS_OS2 */
1888 strcpy(buf+len, fdp->suffix); 1886 strcpy(buf+len, fdp->suffix);
1970 #define INCL_NOPMAPI 1968 #define INCL_NOPMAPI
1971 #include <os2.h> 1969 #include <os2.h>
1972 #endif 1970 #endif
1973 1971
1974 static int 1972 static int
1975 case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name) 1973 case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, const char *name)
1976 { 1974 {
1977 /* Pick a platform-specific implementation; the sequence of #if's here should 1975 /* Pick a platform-specific implementation; the sequence of #if's here should
1978 * match the sequence just above. 1976 * match the sequence just above.
1979 */ 1977 */
1980 1978