avoid import short-circuiting

Robert Kern robert.kern at gmail.com
Fri Mar 16 21:22:05 EDT 2012


On 3/16/12 11:14 PM, Andrea Crotti wrote:

> Very nice thanks, here it is
> class ImportMock:
>
>     def _my_import(self, *args, **kwargs):
>         self.ls.append(args[0])
>         self.orig(*args, **kwargs)

There's a bug here. You need to return the module object you got from calling 
self.orig(). By the way, you really should follow my example of getting the 
.__name__ from the module object instead of the argument in order to properly 
account for relative imports inside packages. __import__() will be passed the 
relative name, not the fully-qualified name.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list