Skip to content

Commit

Permalink
Enforce import strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartarchibald committed Apr 20, 2021
1 parent baa912b commit 98f733f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
13 changes: 4 additions & 9 deletions numba/core/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ def init(self):
# Initialize NRT runtime
rtsys.initialize(self)

# Initialize additional implementations
import numba.cpython.unicode
import numba.cpython.charseq
import numba.typed.dictimpl
import numba.experimental.function_type
import numba.experimental.jitclass

# Add lower_extension attribute
self.lower_extensions = {}
from numba.parfors.parfor_lowering import _lower_parfor_parallel
Expand All @@ -76,11 +69,13 @@ def init(self):
def load_additional_registries(self):
# Add implementations that work via import
from numba.cpython import (slicing, tupleobj, enumimpl, hashing, heapq,
iterators, numbers, rangeobj)
iterators, numbers, rangeobj, unicode,
charseq)
from numba.core import optional
from numba.misc import gdb_hook, literal
from numba.np import linalg, polynomial, arraymath
from numba.typed import typeddict
from numba.typed import typeddict, dictimpl
from numba.experimental import function_type, jitclass

try:
from numba.np import npdatetime
Expand Down
78 changes: 66 additions & 12 deletions numba/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ def test_laziness(self):
Importing top-level numba features should not import too many modules.
"""
# A heuristic set of modules that shouldn't be imported immediately
blacklist = ['cffi',
'distutils',
'numba.cuda',
'numba.cpython.mathimpl',
'numba.cpython.randomimpl',
'numba.tests',
'numba.core.typing.collections',
'numba.core.typing.listdecl',
'numba.core.typing.npdatetime',
]
banlist = ['cffi',
'distutils',
'numba.cuda',
'numba.cpython.mathimpl',
'numba.cpython.randomimpl',
'numba.tests',
'numba.core.typing.collections',
'numba.core.typing.listdecl',
'numba.core.typing.npdatetime',
]
# Sanity check the modules still exist...
for mod in blacklist:
for mod in banlist:
if mod not in ('cffi',):
__import__(mod)

Expand All @@ -51,9 +51,63 @@ def test_laziness(self):

out, _ = self.run_in_subproc(code)
modlist = set(eval(out.strip()))
unexpected = set(blacklist) & set(modlist)
unexpected = set(banlist) & set(modlist)
self.assertFalse(unexpected, "some modules unexpectedly imported")

def test_no_impl_import(self):
"""
Tests that importing jit does not trigger import of modules containing
lowering implementations that would likely install things in the
builtins registry and have side effects impacting other targets
"""
# None of these modules should be imported through the process of
# doing 'import numba' or 'from numba import njit'
banlist = ['numba.cpython.slicing',
'numba.cpython.tupleobj',
'numba.cpython.enumimpl',
'numba.cpython.hashing',
'numba.cpython.heapq',
'numba.cpython.iterators',
'numba.cpython.numbers',
'numba.cpython.rangeobj',
'numba.cpython.cmathimpl',
'numba.cpython.mathimpl',
'numba.cpython.printimpl',
'numba.cpython.randomimpl',
'numba.core.optional',
'numba.misc.gdb_hook',
'numba.misc.literal',
'numba.misc.cffiimpl',
'numba.np.linalg',
'numba.np.polynomial',
'numba.np.arraymath',
'numba.np.npdatetime',
'numba.np.npyimpl',
'numba.typed.typeddict',
'numba.typed.typedlist',
'numba.experimental.jitclass.base',]

code1 = """if 1:
import sys
import numba
print(list(sys.modules))
"""

code2 = """if 1:
import sys
from numba import njit
@njit
def foo():
pass
print(list(sys.modules))
"""

for code in (code1, code2):
out, _ = self.run_in_subproc(code)
modlist = set(eval(out.strip()))
unexpected = set(banlist) & set(modlist)
self.assertFalse(unexpected, "some modules unexpectedly imported")

def test_no_accidental_warnings(self):
# checks that importing Numba isn't accidentally triggering warnings due
# to e.g. deprecated use of import locations from Python's stdlib
Expand Down

0 comments on commit 98f733f

Please sign in to comment.