From: Stephen Hansen on
On 6/15/10 10:25 PM, John Nagle wrote:
> On 6/15/2010 9:33 PM, Stephen Hansen wrote:
>> Replacing the module with a class in sys.modules is really the only way
>> to fake the behavior.
>
> OK, working on this. I can make a module make itself into a
> fake class, but can't yet do it to other modules from outside.

Can you show a bit of code that you're doing? I've never had much
trouble with it: but I'm not sure what you mean by that final statement.

You're attempting to turn "other" modules into faux-modules-as-classes?
I don't think you can do that successfully. The whole faux-module
strategy only works when the module is designed around that principle, I
think.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/

From: Michele Simionato on
On Jun 16, 7:25 am, John Nagle <na...(a)animats.com> wrote:

>     OK, working on this.  I can make a module make itself into a
> fake class, but can't yet do it to other modules from outside.
>
>                                         John Nagle


I think you can with something like

import module
sys.modules[module.__name__] = FakeModule(vars(module))
From: Christian Heimes on
> There aren't any; modules do not follow the class object protocol. They
> are simple types with a __dict__ (which you can't change, either, so no
> replacing it with a dict that implements __setattr__).

You are wrong, my friend. :)

Modules follow the new style class and instance protocol. Modules aren't
classes but instances of the internal module type. Since you can't
overwrite magic methods on instances of new style classes you can't
overwrite __setattr__ on module level, too.

Christian

From: Lie Ryan on
On 06/16/10 12:43, John Nagle wrote:
> Is it possible to override "__setattr__" of a module? I
> want to capture changes to global variables for debug purposes.
>
> None of the following seem to have any effect.
>
> modu.__setattr__ = myfn
>
> setattr(modu, "__setattr__", myfn)
>
> delattr(modu, "__setattr__")
>
> John Nagle

I doubt this is what you wanted, but...

import itertools
module = type(itertools)

class FakeModule(module):
def __init__(self, module):
super(FakeModule, self).__init__(module.__name__, module.__doc__)
self.__module = module
def __getattr__(self, attr):
return getattr(self.__module, attr)
def __setattr__(self, attr, val):
print self, attr, val
super(FakeModule, self).__setattr__(attr, val)


From: Steven D'Aprano on
On Tue, 15 Jun 2010 20:34:47 -0700, Michele Simionato wrote:

> On Jun 16, 4:43 am, John Nagle <na...(a)animats.com> wrote:
>>    Is it possible to override "__setattr__" of a module?  I
>> want to capture changes to global variables for debug purposes.
[...]
> There is a dirty trick which involves fiddling with sys.modules. For
> instance:
[snip example of installing a class in sys.modules]

I'm not sure that this is a dirty trick. Isn't it officially supported?
If I recall correctly, Python used to insist that modules in sys.modules
were actual module objects, and that restriction was lifted deliberately
to allow clever tricks like this. Or am I thinking of something else?


--
Steven