[IronPython] Patching sys.modules to hide CLR 'modules'
dinov at exchange.microsoft.com
Tue Mar 20 20:44:08 CET 2007
The .NET namespaces don't live in sys.modules instead they live in their own hierarchy which is implemented as a "ReflectedPackage" class. The ReflectedPackage is checked after we fail to find the module in sys.modules so if you to stick something into sys.modules you'll take precedence over the namespaces.
If you want to delegate back to the normal .NET namespaces you'll want to import the namespace first, then patch sys.modules, and have your module delegate back.
import System as S
def __init__(self, backing):
self.backing = backing
def __getattr__(self, name):
# your interception here
return getattr(self.backing, name)
sys.modules['System'] = myclass(S)
import System.Drawing # myclass.__getattr__ is called for Drawing
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of William Reade
Sent: Tuesday, March 20, 2007 12:10 PM
To: Discussion of IronPython
Subject: [IronPython] Patching sys.modules to hide CLR 'modules'
I recently wanted to do as described in the subject line. However, it
seems that when I import (say) System.Drawing, there is no detectable
change to sys.modules, and so I cannot temporarily replace it with my
own module in order to substitute simpler functionality for testing
purposes*. Is this correct? If so, can I work around it, and what
exciting pitfalls will I walk into if I try?
* That is to say, I cannot monkey-patch anything in sys.modules that
didn't come from IronPython.
william at resolversystems.com
Office address: 17a Clerkenwell Road, London EC1M 5RD, UK
Registered address: 843 Finchley Road, London NW11 8NA, UK
Resolver Systems Limited is registered in England and Wales as company number 5467329.
users mailing list
users at lists.ironpython.com
More information about the Ironpython-users