[IronPython] Issues with Silly Module Tricks

Dino Viehland dinov at exchange.microsoft.com
Wed Nov 28 19:49:55 CET 2007

Thanks for the bug report.  There's actually a few issues here:

        types.ModuleType is actually SystemState (you can see this by doing import clr; clr.GetClrType(types.ModuleType))
        ScriptModule (the type we use to represent module's currently) is currently sealed, so you can't inherit from it if you were getting the right type
        We don't support __getattr__ / __getattribute__ on types that implement ICustomMembers (which SystemState does).

If you want the nitty gritty details:

The 1st problem is a long-lived problem where we do type impersonation on the SystemState to make it look like a module.  SystemState then implements ICustomMembers so that arbitrary things can be get/set on it which brings us to the 3rd problem.  Before v2.0 is done I suspect that this is going to change and "sys" will be just a normal module.  ICustomMembers will also be going away and get replaced with operators used for lookup - the operators will actually compose correctly and your __getattr__ should get called.

The 2nd problem is currently a DLR problem.  Currently we have a concept of modules in the DLR but that's going away soon.  When that happens ScriptModule (in the most recent code it's not ScriptScope) will be PythonModule and we'll enable inheritance again.

I've opened bug #14147 (http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=14147) to track the issue until all of this gets fixed but unfortunately it's going to take a little time.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeff Hardy
Sent: Tuesday, November 27, 2007 7:40 PM
To: Discussion of IronPython
Subject: [IronPython] Issues with Silly Module Tricks

The following code is adapted from the Pygments library:

import types

class _automodule(types.ModuleType):
    """Automatically import lexers."""

    def __getattr__(self, name):
        return "blah"

import sys
oldmod = sys.modules['automod_test']
newmod = _automodule('automod_test')
sys.modules['automod_test'] = newmod
del newmod.newmod, newmod.oldmod, newmod.sys, newmod.types

When run, I get the following results:
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import automod_test
>>> automod_test.blah

IronPython console: IronPython 2.0A6 (2.0.11102.00) on .NET 2.0.50727.312
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import automod_test
>>> automod_test.blah
Traceback (most recent call last):
  File , line 0, in ##268
AttributeError: 'module' object has no attribute 'blah'

I'm not sure whether the issue is with replacing sys.modules, or if it
is related to the __getattr__ issues that were reported a while back.
Either way, it prevents Pygments from getting past even the first
round of tests.

Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list