Index: Python/import.c =================================================================== --- Python/import.c (revision 80170) +++ Python/import.c (working copy) @@ -2492,8 +2492,24 @@ hasit = PyObject_HasAttr(mod, item); if (!hasit) { char *subname = PyString_AS_STRING(item); + size_t j, subnamelen = PyString_GET_SIZE(item); PyObject *submod; char *p; + /* We must reject an empty name. As a hack, we bump the + length to 1 so that the loop will balk on the + trailing \0. */ + if (subnamelen == 0) + subnamelen = 1; + /* subname must be an identifier */ + for (j = 0, p = subname; j < subnamelen; j++, p++) { + if (!(j == 0 ? isalpha(*p) : isalnum(*p)) + && *p != '_') { + /* don't raise an exception to stay + backwards compatible */ + Py_DECREF(item); + return 1; + } + } if (buflen + strlen(subname) >= MAXPATHLEN) { PyErr_SetString(PyExc_ValueError, "Module name too long");