Skip to content

Commit 541db05

Browse files
Yhg1sencukou
andauthored
Apply batched suggestions from code review
Co-authored-by: T. Wouters <thomas@python.org> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 30be4e1 commit 541db05

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎Lib/test/test_lazy_import/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ def test_deleted_child(self):
485485
import xml
486486
del xml.dom
487487
assert not hasattr(xml, 'dom')
488+
lazy from xml import dom
489+
assert hasattr(xml, 'dom')
488490
""")
489491

490492
def test_cached_import_releases_builtins(self):

‎Objects/lazyimportobject.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ PyObject *
564564
_PyLazyImport_LoadChild(PyThreadState *tstate, PyObject *declaration,
565565
PyObject *name)
566566
{
567-
PyLazyImportObject *source = (PyLazyImportObject *)declaration;
567+
assert(PyLazyImport_CheckExact(declaration));
568+
PyLazyImportObject *source = PyLazyImportObject_CAST(declaration);
568569
PyObject *lz = _PyLazyImport_New(NULL, source->lz_builtins, name, NULL);
569570
Py_ssize_t end = PyUnicode_GET_LENGTH(name);
570571
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, end, 1);
@@ -587,7 +588,7 @@ _PyLazyImport_LoadChild(PyThreadState *tstate, PyObject *declaration,
587588
if (lz == NULL) {
588589
return NULL;
589590
}
590-
PyLazyImportObject *child = (PyLazyImportObject *)lz;
591+
PyLazyImportObject *child = PyLazyImportObject_CAST(lz);
591592
child->lz_code = (PyCodeObject *)Py_XNewRef(source->lz_code);
592593
child->lz_instr_offset = source->lz_instr_offset;
593594
PyObject *result = _PyImport_LoadLazyImportTstate(tstate, lz);
@@ -605,8 +606,9 @@ _PyLazyImport_LoadChild(PyThreadState *tstate, PyObject *declaration,
605606
int
606607
_PyLazyImport_IsActive(PyObject *declaration)
607608
{
609+
assert(PyLazyImport_CheckExact(declaration));
608610
return FT_ATOMIC_LOAD_INT_RELAXED(
609-
((PyLazyImportObject *)declaration)->lz_active);
611+
PyLazyImportObject_CAST(declaration)->lz_active);
610612
}
611613

612614
// Loading pkg.child can replace a placeholder in pkg.child with the module

0 commit comments

Comments
 (0)