Skip to content

Commit 9fc7da2

Browse files
committed
Add test exercising that the appropriate error is raised when calling
lazy import within an except block. Also removed unused var.
1 parent b437afb commit 9fc7da2

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Lib/test/test_syntax.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,6 +3511,15 @@ def test_lazy_import_in_trystar_block(self):
35113511
pass
35123512
""", "lazy from ... import not allowed inside try/except blocks")
35133513

3514+
def test_lazy_import_in_except_block(self):
3515+
"""Test that lazy imports are not allowed inside except blocks."""
3516+
self._check_error("""\
3517+
try:
3518+
sys.modules # trigger the except block
3519+
except* Exception:
3520+
lazy import sys
3521+
""", "lazy import not allowed inside try/except blocks")
3522+
35143523
def test_lazy_import_in_function(self):
35153524
"""Test that lazy imports are not allowed inside functions."""
35163525
self._check_error("""\

Objects/lazyimportobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ lazy_import_repr(PyObject *op)
112112
static PyObject *
113113
lazy_import_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
114114
{
115-
PyTypeObject *base_tp = &PyLazyImport_Type;
116115
if (!_PyArg_NoKeywords("lazy_import", kwds)) {
117116
return NULL;
118117
}

0 commit comments

Comments
 (0)