File: test_deepreload.py

package info (click to toggle)
ipython 5.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,428 kB
  • ctags: 4,941
  • sloc: python: 33,657; makefile: 170; sh: 139
file content (59 lines) | stat: -rw-r--r-- 2,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""Test suite for the deepreload module."""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os

import nose.tools as nt

from IPython.testing import decorators as dec
from IPython.utils.py3compat import builtin_mod_name, PY3
from IPython.utils.syspathcontext import prepended_to_syspath
from IPython.utils.tempdir import TemporaryDirectory
from IPython.lib.deepreload import reload as dreload

#-----------------------------------------------------------------------------
# Test functions begin
#-----------------------------------------------------------------------------

@dec.skipif_not_numpy
def test_deepreload_numpy():
    "Test that NumPy can be deep reloaded."
    import numpy
    # TODO: Find a way to exclude all standard library modules from reloading.
    exclude = [
        # Standard exclusions:
        'sys', 'os.path', builtin_mod_name, '__main__',
        # Test-related exclusions:
        'unittest', 'UserDict', '_collections_abc', 'tokenize',
        'collections', 'collections.abc',
        'importlib', 'importlib.machinery', '_imp',
        'importlib._bootstrap', 'importlib._bootstrap_external',
        '_frozen_importlib', '_frozen_importlib_external',
        ]

    dreload(numpy, exclude=exclude)

def test_deepreload():
    "Test that dreload does deep reloads and skips excluded modules."
    with TemporaryDirectory() as tmpdir:
        with prepended_to_syspath(tmpdir):
            with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
                f.write("class Object(object):\n    pass\n")
            with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
                f.write("import A\n")
            import A
            import B

            # Test that A is not reloaded.
            obj = A.Object()
            dreload(B, exclude=['A'])
            nt.assert_true(isinstance(obj, A.Object))

            # Test that A is reloaded.
            obj = A.Object()
            dreload(B)
            nt.assert_false(isinstance(obj, A.Object))