views:

112

answers:

2

I'm trying to use a third-party lib (docutils) on Google App Engine and have a problem with this code (in docutils):

try:
    import pwd
    do stuff
except ImportError:
    do other stuff

I want the import to fail, as it will on the actual GAE server, but the problem is that it doesn't fail on my development box (ubuntu). How to make it fail, given that the import is not in my own code?

+4  A: 

In your testing framework, before you cause docutils to be imported, you can perform this setup task:

import __builtin__
self.savimport = __builtin__.__import__
def myimport(name, *a):
  if name=='pwd': raise ImportError
  return self.savimport(name, *a)
__builtin__.__import__ = myimport

and of course in teardown put things back to normal:

__builtin__.__import__ = self.savimport

Explanation: all import operations go through __builtin__.__import__, and you can reassign that name to have such operations use your own code (alternatives such as import hooks are better for such purposes as performing import from non-filesystem sources, but for purposes such as yours, overriding __builtin__.__import__, as you see above, affords truly simple code).

Alex Martelli
Thanks for the quick response, that looks like it should do the trick.
jerd
Unfortunately this didn't work - perhaps it's an AppEngine thing? In any case, Thomas Wouters' method did what I wanted. All the best.
jerd
It wouldn't work in App Engine -- but then in App Engine `import pwd` just fails, so you don't need this to work in App Engine itself, but rather _outside_ of it when you're simulating it for testing. Still, Thomas's answer is simpler and shorter, which makes it more fit for purpose, so I agree w/your choice (and I'm going to +1 his A -- remember to come back and do the same when your reputation grows enough to allow it!-).
Alex Martelli
`import pwd` fails in App Engine production, but not in the App Engine development server - I think you were mislead by the use of the word 'fail' in my post, I'm not in any particular testing framework here, this is code which I *know* will fail on App Engine but refuses to fail when running within `dev_appserver.py`. I'm speculating 'an App Engine thing' with regard to `__builtin__` in part because I'm also having problems with the `gettext` underscore - `gettext` also manipulates the __builtin__ dict, but code which works outside of dev_appserver.py doesn't work inside (no attribute '_').
jerd
+2  A: 

Even easier than messing with __import__ is just inserting None in the sys.modules dict:

>>> import sys
>>> sys.modules['pwd'] = None
>>> import pwd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pwd
Thomas Wouters
And I realized the hidden features of Python question was missing this one, so I added it there, as well: http://stackoverflow.com/questions/101268/hidden-features-of-python/2259080#2259080
Thomas Wouters
Perfect. That worked. Thanks for replying.
jerd