mirror of https://github.com/python/cpython
simplify and rewrite the zipimport part of 702009f3c0b1 a bit
This commit is contained in:
parent
209e04c201
commit
5ed7bd79df
|
@ -319,13 +319,20 @@ get_module_info(ZipImporter *self, PyObject *fullname)
|
||||||
return MI_NOT_FOUND;
|
return MI_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
fl_error,
|
||||||
|
fl_not_found,
|
||||||
|
fl_module_found,
|
||||||
|
fl_ns_found
|
||||||
|
} find_loader_result;
|
||||||
|
|
||||||
/* The guts of "find_loader" and "find_module". Return values:
|
/* The guts of "find_loader" and "find_module". Return values:
|
||||||
-1: error
|
-1: error
|
||||||
0: no loader or namespace portions found
|
0: no loader or namespace portions found
|
||||||
1: module/package found
|
1: module/package found
|
||||||
2: namespace portion found: *namespace_portion will point to the name
|
2: namespace portion found: *namespace_portion will point to the name
|
||||||
*/
|
*/
|
||||||
static int
|
static find_loader_result
|
||||||
find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
|
find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
|
||||||
{
|
{
|
||||||
enum zi_module_info mi;
|
enum zi_module_info mi;
|
||||||
|
@ -334,7 +341,7 @@ find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
|
||||||
|
|
||||||
mi = get_module_info(self, fullname);
|
mi = get_module_info(self, fullname);
|
||||||
if (mi == MI_ERROR)
|
if (mi == MI_ERROR)
|
||||||
return -1;
|
return fl_error;
|
||||||
if (mi == MI_NOT_FOUND) {
|
if (mi == MI_NOT_FOUND) {
|
||||||
/* Not a module or regular package. See if this is a directory, and
|
/* Not a module or regular package. See if this is a directory, and
|
||||||
therefore possibly a portion of a namespace package. */
|
therefore possibly a portion of a namespace package. */
|
||||||
|
@ -349,13 +356,13 @@ find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion)
|
||||||
self->archive, SEP,
|
self->archive, SEP,
|
||||||
self->prefix, fullname);
|
self->prefix, fullname);
|
||||||
if (*namespace_portion == NULL)
|
if (*namespace_portion == NULL)
|
||||||
return -1;
|
return fl_error;
|
||||||
return 2;
|
return fl_ns_found;
|
||||||
}
|
}
|
||||||
return 0;
|
return fl_not_found;
|
||||||
}
|
}
|
||||||
/* This is a module or package. */
|
/* This is a module or package. */
|
||||||
return 1;
|
return fl_module_found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -367,32 +374,26 @@ zipimporter_find_module(PyObject *obj, PyObject *args)
|
||||||
ZipImporter *self = (ZipImporter *)obj;
|
ZipImporter *self = (ZipImporter *)obj;
|
||||||
PyObject *path = NULL;
|
PyObject *path = NULL;
|
||||||
PyObject *fullname;
|
PyObject *fullname;
|
||||||
PyObject* namespace_portion = NULL;
|
PyObject *namespace_portion = NULL;
|
||||||
|
PyObject *result = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module",
|
if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
|
||||||
&fullname, &path))
|
return NULL;
|
||||||
goto error;
|
|
||||||
|
|
||||||
switch (find_loader(self, fullname, &namespace_portion)) {
|
switch (find_loader(self, fullname, &namespace_portion)) {
|
||||||
case -1: /* Error */
|
case fl_ns_found:
|
||||||
goto error;
|
/* A namespace portion is not allowed via find_module, so return None. */
|
||||||
case 0: /* Not found, return None */
|
|
||||||
Py_INCREF(Py_None);
|
|
||||||
return Py_None;
|
|
||||||
case 1: /* Return self */
|
|
||||||
Py_INCREF(self);
|
|
||||||
return (PyObject *)self;
|
|
||||||
case 2: /* A namespace portion, but not allowed via
|
|
||||||
find_module, so return None */
|
|
||||||
Py_DECREF(namespace_portion);
|
Py_DECREF(namespace_portion);
|
||||||
Py_INCREF(Py_None);
|
/* FALL THROUGH */
|
||||||
return Py_None;
|
case fl_error:
|
||||||
|
case fl_not_found:
|
||||||
|
result = Py_None;
|
||||||
|
break;
|
||||||
|
case fl_module_found:
|
||||||
|
result = (PyObject *)self;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
/* Can't get here. */
|
Py_XINCREF(result);
|
||||||
assert(0);
|
|
||||||
return NULL;
|
|
||||||
error:
|
|
||||||
Py_XDECREF(namespace_portion);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,35 +412,24 @@ zipimporter_find_loader(PyObject *obj, PyObject *args)
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
PyObject *namespace_portion = NULL;
|
PyObject *namespace_portion = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module",
|
if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path))
|
||||||
&fullname, &path))
|
return NULL;
|
||||||
goto error;
|
|
||||||
|
|
||||||
switch (find_loader(self, fullname, &namespace_portion)) {
|
switch (find_loader(self, fullname, &namespace_portion)) {
|
||||||
case -1: /* Error */
|
case fl_error:
|
||||||
goto error;
|
return NULL;
|
||||||
case 0: /* Not found, return (None, []) */
|
case fl_not_found: /* Not found, return (None, []) */
|
||||||
if (!(result = Py_BuildValue("O[]", Py_None)))
|
result = Py_BuildValue("O[]", Py_None);
|
||||||
goto error;
|
break;
|
||||||
return result;
|
case fl_module_found: /* Return (self, []) */
|
||||||
case 1: /* Return (self, []) */
|
result = Py_BuildValue("O[]", self);
|
||||||
if (!(result = Py_BuildValue("O[]", self)))
|
break;
|
||||||
goto error;
|
case fl_ns_found: /* Return (None, [namespace_portion]) */
|
||||||
return result;
|
result = Py_BuildValue("O[O]", Py_None, namespace_portion);
|
||||||
case 2: /* Return (None, [namespace_portion]) */
|
|
||||||
if (!(result = Py_BuildValue("O[O]", Py_None, namespace_portion)))
|
|
||||||
goto error;
|
|
||||||
Py_DECREF(namespace_portion);
|
Py_DECREF(namespace_portion);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/* Can't get here. */
|
return result;
|
||||||
assert(0);
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
error:
|
|
||||||
Py_XDECREF(namespace_portion);
|
|
||||||
Py_XDECREF(result);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load and return the module named by 'fullname'. */
|
/* Load and return the module named by 'fullname'. */
|
||||||
|
|
Loading…
Reference in New Issue