Skip to content

Commit

Permalink
Merge pull request numba#6503 from stuartarchibald/wip/catch_errors_o…
Browse files Browse the repository at this point in the history
…n_import

Add test to ensure Numba imports without warnings.
  • Loading branch information
stuartarchibald authored Nov 18, 2020
2 parents 56f52a4 + 5d72107 commit 3b1e4ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ exclude =
numba/tests/test_extended_arg.py
numba/tests/test_alignment.py
numba/tests/test_multi3.py
numba/tests/test_import.py
numba/tests/test_overlap.py
numba/tests/test_array_attr.py
numba/tests/test_array_methods.py
Expand Down
54 changes: 35 additions & 19 deletions numba/tests/test_import.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import unittest
import subprocess
import sys

from numba.tests.support import TestCase
import unittest


class TestNumbaImport(TestCase):
"""
Test behaviour of importing Numba.
"""

def run_in_subproc(self, code, flags=None):
if flags is None:
flags = []
cmd = [sys.executable,] + flags + ["-c", code]
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = popen.communicate()
if popen.returncode != 0:
msg = "process failed with code %s: stderr follows\n%s\n"
raise AssertionError(msg % (popen.returncode, err.decode()))
return out, err

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',
]
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',
]
# Sanity check the modules still exist...
for mod in blacklist:
if mod not in ('cffi',):
Expand All @@ -38,13 +49,18 @@ def test_laziness(self):
print(list(sys.modules))
"""

popen = subprocess.Popen([sys.executable, "-c", code],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = popen.communicate()
if popen.returncode != 0:
raise AssertionError("process failed with code %s: stderr follows\n%s\n"
% (popen.returncode, err.decode()))

out, _ = self.run_in_subproc(code)
modlist = set(eval(out.strip()))
unexpected = set(blacklist) & 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
code = "import numba"
flags = ["-Werror",]
self.run_in_subproc(code, flags)


if __name__ == '__main__':
unittest.main()

0 comments on commit 3b1e4ab

Please sign in to comment.