test_deepreload.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. """Test suite for the deepreload module."""
  3. #-----------------------------------------------------------------------------
  4. # Imports
  5. #-----------------------------------------------------------------------------
  6. import os
  7. import nose.tools as nt
  8. from IPython.testing import decorators as dec
  9. from IPython.utils.py3compat import builtin_mod_name, PY3
  10. from IPython.utils.syspathcontext import prepended_to_syspath
  11. from IPython.utils.tempdir import TemporaryDirectory
  12. from IPython.lib.deepreload import reload as dreload
  13. #-----------------------------------------------------------------------------
  14. # Test functions begin
  15. #-----------------------------------------------------------------------------
  16. @dec.skipif_not_numpy
  17. def test_deepreload_numpy():
  18. "Test that NumPy can be deep reloaded."
  19. import numpy
  20. # TODO: Find a way to exclude all standard library modules from reloading.
  21. exclude = [
  22. # Standard exclusions:
  23. 'sys', 'os.path', builtin_mod_name, '__main__',
  24. # Test-related exclusions:
  25. 'unittest', 'UserDict', '_collections_abc', 'tokenize',
  26. 'collections', 'collections.abc',
  27. 'importlib', 'importlib.machinery', '_imp',
  28. 'importlib._bootstrap', 'importlib._bootstrap_external',
  29. '_frozen_importlib', '_frozen_importlib_external',
  30. ]
  31. dreload(numpy, exclude=exclude)
  32. def test_deepreload():
  33. "Test that dreload does deep reloads and skips excluded modules."
  34. with TemporaryDirectory() as tmpdir:
  35. with prepended_to_syspath(tmpdir):
  36. with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
  37. f.write("class Object(object):\n pass\n")
  38. with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
  39. f.write("import A\n")
  40. import A
  41. import B
  42. # Test that A is not reloaded.
  43. obj = A.Object()
  44. dreload(B, exclude=['A'])
  45. nt.assert_true(isinstance(obj, A.Object))
  46. # Test that A is reloaded.
  47. obj = A.Object()
  48. dreload(B)
  49. nt.assert_false(isinstance(obj, A.Object))