Exemple #1
0
def test_lazy():
    mlab = l.LazyImport('matplotlib.mlab')
    # repr for mlab should be <module 'matplotlib.mlab' will be lazily loaded>
    assert 'lazily loaded' in repr(mlab)
    # accessing mlab's attribute will cause an import of mlab
    npt.assert_equal(mlab.dist(1969, 2011), 42.0)
    # now mlab should be of class LoadedLazyImport an repr(mlab) should be
    # <module 'matplotlib.mlab' from # '.../matplotlib/mlab.pyc>
    assert 'lazily loaded' not in repr(mlab)
Exemple #2
0
def test_lazy():
    mlab = l.LazyImport('matplotlib.mlab')
    # repr for mlab should be <module 'matplotlib.mlab' will be lazily loaded>
    assert 'lazily loaded' in repr(mlab)
    # accessing mlab's attribute will cause an import of mlab
    npt.assert_(
        np.all(
            mlab.detrend_mean(np.array([1, 2, 3])) == np.array([-1., 0., 1.])))
    # now mlab should be of class LoadedLazyImport an repr(mlab) should be
    # <module 'matplotlib.mlab' from # '.../matplotlib/mlab.pyc>
    assert 'lazily loaded' not in repr(mlab)
Exemple #3
0
def test_lazy_noreload():
    "Reloading of lazy modules causes ImportError"
    mod = l.LazyImport('sys')
    # accessing module dictionary will trigger an import
    len(mod.__dict__)
    # do not use named tuple feature for Python 2.6 compatibility
    major, minor = sys.version_info[:2]
    if major == 2:
        with pytest.raises(ImportError) as e_info:
            reload(mod)
    elif major == 3:
        import imp
        with pytest.raises(ImportError) as e_info:
            imp.reload(mod)
Exemple #4
0
def test_lazy_reload():
    f = file('baz.py', 'w')
    f.write("def foo(): return 42")
    f.close()
    b = l.LazyImport('baz')
    assert b.foo()==42
    f = file('baz.py', 'w')
    f.write("def bar(): return 0x42")
    f.flush()
    os.fsync(f)
    f.close()
    import time
    time.sleep(1)
    os.utime('baz.py', None)
    reload(b)
    os.remove('baz.py')
    assert b.bar()==0x42
Exemple #5
0
def test_lazy_noreload():
    "Reloading of lazy modules causes ImportError"
    mod = l.LazyImport('sys')
    # accessing module dictionary will trigger an import
    len(mod.__dict__)
    npt.assert_raises(ImportError, reload, mod)