from _imp import get_magic, get_tag
# Can (probably) move to importlib
from _imp import get_suffixes
-# Should be re-implemented here (and mostly deprecated)
-from _imp import NullImporter
from importlib._bootstrap import _new_module as new_module
from importlib._bootstrap import _cache_from_source as cache_from_source
return os.path.join(head, base_filename + _bootstrap.SOURCE_SUFFIXES[0])
+class NullImporter:
+
+ """Null import object."""
+
+ def __init__(self, path):
+ if path == '':
+ raise ImportError('empty pathname', path='')
+ elif os.path.isdir(path):
+ raise ImportError('existing directory', path=path)
+
+ def find_module(self, fullname):
+ """Always returns None."""
+ return None
+
+
class _HackedGetData:
"""Compatibiilty support for 'file' arguments of various load_*()
PyObject *v, *path_hooks = NULL;
int err = 0;
- if (PyType_Ready(&PyNullImporter_Type) < 0)
- goto error;
-
/* adding sys.path_hooks and sys.path_importer_cache */
v = PyList_New(0);
if (v == NULL)
error:
PyErr_Print();
Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
- "path_importer_cache, or NullImporter failed"
- );
+ "or path_importer_cache");
}
Py_DECREF(path_hooks);
}
error:
PyErr_Print();
- Py_FatalError("initializing zipimport or NullImporter failed");
+ Py_FatalError("initializing zipimport failed");
}
/* Locking primitives to prevent parallel imports of the same module
return err;
}
-typedef struct {
- PyObject_HEAD
-} NullImporter;
-
-static int
-NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
-{
-#ifndef MS_WINDOWS
- PyObject *path;
- struct stat statbuf;
- int rv;
-
- if (!_PyArg_NoKeywords("NullImporter()", kwds))
- return -1;
-
- if (!PyArg_ParseTuple(args, "O&:NullImporter",
- PyUnicode_FSConverter, &path))
- return -1;
-
- if (PyBytes_GET_SIZE(path) == 0) {
- Py_DECREF(path);
- PyErr_SetString(PyExc_ImportError, "empty pathname");
- return -1;
- }
-
- rv = stat(PyBytes_AS_STRING(path), &statbuf);
- Py_DECREF(path);
- if (rv == 0) {
- /* it exists */
- if (S_ISDIR(statbuf.st_mode)) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError, "existing directory");
- return -1;
- }
- }
-#else /* MS_WINDOWS */
- PyObject *pathobj;
- DWORD rv;
- wchar_t *path;
-
- if (!_PyArg_NoKeywords("NullImporter()", kwds))
- return -1;
-
- if (!PyArg_ParseTuple(args, "U:NullImporter",
- &pathobj))
- return -1;
-
- if (PyUnicode_GET_LENGTH(pathobj) == 0) {
- PyErr_SetString(PyExc_ImportError, "empty pathname");
- return -1;
- }
-
- path = PyUnicode_AsWideCharString(pathobj, NULL);
- if (path == NULL)
- return -1;
- /* see issue1293 and issue3677:
- * stat() on Windows doesn't recognise paths like
- * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
- */
- rv = GetFileAttributesW(path);
- PyMem_Free(path);
- if (rv != INVALID_FILE_ATTRIBUTES) {
- /* it exists */
- if (rv & FILE_ATTRIBUTE_DIRECTORY) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError, "existing directory");
- return -1;
- }
- }
-#endif
- return 0;
-}
-
-static PyObject *
-NullImporter_find_module(NullImporter *self, PyObject *args)
-{
- Py_RETURN_NONE;
-}
-
-static PyMethodDef NullImporter_methods[] = {
- {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
- "Always return None"
- },
- {NULL} /* Sentinel */
-};
-
-
-PyTypeObject PyNullImporter_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "imp.NullImporter", /*tp_name*/
- sizeof(NullImporter), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash */
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- "Null importer object", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- NullImporter_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)NullImporter_init, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew /* tp_new */
-};
-
static struct PyModuleDef impmodule = {
PyModuleDef_HEAD_INIT,
"_imp",
{
PyObject *m, *d;
- if (PyType_Ready(&PyNullImporter_Type) < 0)
- return NULL;
-
m = PyModule_Create(&impmodule);
if (m == NULL)
goto failure;
if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
- Py_INCREF(&PyNullImporter_Type);
- PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
return m;
failure:
Py_XDECREF(m);