Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21789

Re: avoid import short-circuiting

From Robert Kern <robert.kern@gmail.com>
Subject Re: avoid import short-circuiting
Date 2012-03-17 01:22 +0000
References <4F636F0D.3060006@gmail.com> <jjvsnj$v9d$1@dough.gmane.org> <4F63B8D4.8010103@gmail.com> <jk0eav$b5l$1@dough.gmane.org> <4F63C948.8020700@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.745.1331947337.3037.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: avoid import short-circuiting Robert Kern <robert.kern@gmail.com> - 2012-03-17 01:22 +0000

csiph-web