Skip to content

Commit 074cb7b

Browse files
committed
_PyLazyImport_New improvements
1 parent 6f16938 commit 074cb7b

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

Lib/test/test_import/test_lazy_imports.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ def test_lazy_import_type_exposed(self):
204204
self.assertHasAttr(types, 'LazyImportType')
205205
self.assertEqual(types.LazyImportType.__name__, 'lazy_import')
206206

207+
def test_lazy_import_type_invalid_name(self):
208+
"""passing invalid name to lazy imports should raise a TypeError"""
209+
with self.assertRaises(TypeError) as cm:
210+
types.LazyImportType({}, None)
211+
207212
def test_lazy_import_type_invalid_fromlist_type(self):
208213
"""LazyImportType should reject invalid fromlist types."""
209214
# fromlist must be None, a string, or a tuple - not an int

Objects/lazyimportobject.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
#define PyLazyImportObject_CAST(op) ((PyLazyImportObject *)(op))
1212

1313
PyObject *
14-
_PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
14+
_PyLazyImport_New(PyObject *builtins, PyObject *name, PyObject *fromlist)
1515
{
1616
PyLazyImportObject *m;
17-
if (!from || !PyUnicode_Check(from)) {
18-
PyErr_BadArgument();
17+
if (!name || !PyUnicode_Check(name)) {
18+
PyErr_SetString(PyExc_TypeError, "expected str for name");
1919
return NULL;
2020
}
21-
if (attr == Py_None || attr == NULL) {
22-
attr = NULL;
21+
if (fromlist == Py_None || fromlist == NULL) {
22+
fromlist = NULL;
2323
}
24-
else if (!PyUnicode_Check(attr) && !PyTuple_Check(attr)) {
24+
else if (!PyUnicode_Check(fromlist) && !PyTuple_Check(fromlist)) {
2525
PyErr_SetString(PyExc_TypeError,
2626
"lazy_import: fromlist must be None, a string, or a tuple");
2727
return NULL;
@@ -31,8 +31,8 @@ _PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
3131
return NULL;
3232
}
3333
m->lz_builtins = Py_XNewRef(builtins);
34-
m->lz_from = Py_NewRef(from);
35-
m->lz_attr = Py_XNewRef(attr);
34+
m->lz_from = Py_NewRef(name);
35+
m->lz_attr = Py_XNewRef(fromlist);
3636

3737
/* Capture frame information for the original import location */
3838
m->lz_code = NULL;

0 commit comments

Comments
 (0)