summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-29 16:50:03 (GMT)
committerBrett Cannon <brett@python.org>2012-04-29 16:50:03 (GMT)
commitacf85cd1314e498b1736677a1e08cfddc956c4ac (patch)
treedfd63a6562c931a4d02770927a589f4b41c35ebb
parente383e82e0484aed79f2c78516e3f223345408d4b (diff)
downloadcpython-acf85cd1314e498b1736677a1e08cfddc956c4ac.zip
cpython-acf85cd1314e498b1736677a1e08cfddc956c4ac.tar.gz
cpython-acf85cd1314e498b1736677a1e08cfddc956c4ac.tar.bz2
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
-rw-r--r--Lib/imp.py17
-rw-r--r--Lib/test/test_sys.py3
-rw-r--r--Python/import.c141
3 files changed, 17 insertions, 144 deletions
diff --git a/Lib/imp.py b/Lib/imp.py
index 0388d08..2abd7af 100644
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -14,8 +14,6 @@ from _imp import (lock_held, acquire_lock, release_lock, reload,
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
@@ -60,6 +58,21 @@ def source_from_cache(path):
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_*()
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 0f93610..2b9abfc 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -889,9 +889,6 @@ class SizeofTest(unittest.TestCase):
# _ast.AST
import _ast
check(_ast.AST(), size(h + 'P'))
- # imp.NullImporter
- import imp
- check(imp.NullImporter(self.file.name), size(h + ''))
try:
raise TypeError
except TypeError:
diff --git a/Python/import.c b/Python/import.c
index a47e988..0580f60 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -205,9 +205,6 @@ _PyImportHooks_Init(void)
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)
@@ -231,8 +228,7 @@ _PyImportHooks_Init(void)
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);
}
@@ -284,7 +280,7 @@ _PyImportZip_Init(void)
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
@@ -2244,134 +2240,6 @@ setint(PyObject *d, char *name, int value)
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",
@@ -2389,9 +2257,6 @@ PyInit_imp(void)
{
PyObject *m, *d;
- if (PyType_Ready(&PyNullImporter_Type) < 0)
- return NULL;
-
m = PyModule_Create(&impmodule);
if (m == NULL)
goto failure;
@@ -2410,8 +2275,6 @@ PyInit_imp(void)
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);