I've been trying this for almost two hours with no luck.
I have a module that looks like this:
try: from zope.component import queryUtility
Later in code:
try: queryUtility(foo) except NameError:
Any ideas?
EDIT:
Alex's suggestion doesn't seem to work:
>>> import __builtin__ >>> realimport = __builtin__.__import__ >>> def fakeimport(name, *args, **kw): ... if name == 'zope.component': ... raise ImportError ... realimport(name, *args, **kw) ... >>> __builtin__.__import__ = fakeimport
When running tests:
aatiis@aiur ~/work/ao.shorturl $ ./bin/test --coverage . Running zope.testing.testrunner.layer.UnitTests tests: Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds. Error in test /home/aatiis/work/ao.shorturl/src/ao/shorturl/shorturl.txt Traceback (most recent call last): File "/usr/lib64/python2.5/unittest.py", line 260, in run testMethod() File "/usr/lib64/python2.5/doctest.py", line 2123, in runTest test, out=new.write, clear_globs=False) File "/usr/lib64/python2.5/doctest.py", line 1361, in run return self.__run(test, compileflags, out) File "/usr/lib64/python2.5/doctest.py", line 1282, in __run exc_info) File "/usr/lib64/python2.5/doctest.py", line 1148, in report_unexpected_exception 'Exception raised:\n' + _indent(_exception_traceback(exc_info))) File "/usr/lib64/python2.5/doctest.py", line 1163, in _failure_header out.append(_indent(source)) File "/usr/lib64/python2.5/doctest.py", line 224, in _indent return re.sub('(?m)^(?!$)', indent*' ', s) File "/usr/lib64/python2.5/re.py", line 150, in sub return _compile(pattern, 0).sub(repl, string, count) File "/usr/lib64/python2.5/re.py", line 239, in _compile p = sre_compile.compile(pattern, flags) File "/usr/lib64/python2.5/sre_compile.py", line 507, in compile p = sre_parse.parse(p, flags) AttributeError: 'NoneType' object has no attribute 'parse' Error in test BaseShortUrlHandler (ao.shorturl) Traceback (most recent call last): File "/usr/lib64/python2.5/unittest.py", line 260, in run testMethod() File "/usr/lib64/python2.5/doctest.py", line 2123, in runTest test, out=new.write, clear_globs=False) File "/usr/lib64/python2.5/doctest.py", line 1351, in run self.debugger = _OutputRedirectingPdb(save_stdout) File "/usr/lib64/python2.5/doctest.py", line 324, in __init__ pdb.Pdb.__init__(self, stdout=out) File "/usr/lib64/python2.5/pdb.py", line 57, in __init__ cmd.Cmd.__init__(self, completekey, stdin, stdout) File "/usr/lib64/python2.5/cmd.py", line 90, in __init__ import sys File "<doctest shorturl.txt[10]>", line 4, in fakeimport NameError: global name 'realimport' is not defined
However, it works when I run the same code from the python interactive console.
MORE EDIT:
I use zope.testing and the test file shorturl.txt , which has all the tests specific to this part of my module. First, I import the module with zope.component to demonstrate and test normal use. The absence of zope.* Packages is considered edge, so I will test it later. So, I have to reload() my module, after zope.* Was unavailable, somehow.
So far, I have even tried using tempfile.mktempdir() and empty zope/__init__.py and zope/component/__init__.py files in tempdir, then inserting tempdir in sys.path[0] and removing the old zope.* Packages from sys.modules .
Does not work.
EVEN MORE EDITING:
In the meantime, I tried this:
>>> class NoZope(object): ... def find_module(self, fullname, path): ... if fullname.startswith('zope'): ... raise ImportError ... >>> import sys >>> sys.path.insert(0, NoZope())
And it works well for the test case namespace (= for all imports in shorturl.txt ), but it fails in my main ao.shorturl module. Even when I reload() . Any idea why?
>>> import zope
Importing zope.interfaces raises ImportError , so it does not fall into the part where I import zope.component , but it remains in the ao.shorturl namespace . Why?
>>> ao.shorturl.zope.component # why?! <module ...>