[IronPython] [python] Re: Embedding CPython Messes with the IronPython 2 Import Mechanism

Dino Viehland dinov at exchange.microsoft.com
Sat Nov 3 00:12:18 CET 2007


I'm not sure what's going on here - I've pinged the CLR dev who owns this feature.  In some patches (at some point) post v2.0.50727.42 there were some changes made to Ref.Emit w.r.t. ModuleBuilder's vs. Modules - this looks like it could be a regression related to those changes.  But I don't understand the big picture here.

As a work around you might be able to run in -X:GenerateAsSnippets mode.  In v2.0 this results in about a 10% perf degrade (last time I measured at least, and this was against Pystone) but stops us from generating types to create modules.  So ultimately we won't create an assembly and won't go down this code path.  Of course, that may just be putting off the inevitable.

Another possiblity might be grepping Python.NET to see if they're doing anything weird w/ Module or Assembly objects, although I wouldn't know exactly what to look for.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
Sent: Friday, November 02, 2007 3:33 PM
To: Discussion of IronPython
Subject: Re: [IronPython] [python] Re: Embedding CPython Messes with the IronPython 2 Import Mechanism

You asked for it. :-)

IronPython console: IronPython 2.0A5 (2.0.11011.00) on .NET 2.0.50727.1378
Copyright (c) Microsoft Corporation. All rights reserved.
 >>> import clr
 >>> clr.AddReference('Python.Runtime')
 >>> from Python.Runtime import PythonEngine
 >>> engine = PythonEngine()
 >>> engine.Initialize()
 >>> import xyzz
Unable to cast object of type 'System.Reflection.Module' to type
'System.Reflect
ion.Emit.ModuleBuilder'.
   at System.Reflection.Emit.AssemblyBuilderData.GetInMemoryAssemblyModule()
   at System.AppDomain.InternalDefineDynamicAssembly(AssemblyName name,
Assembly
BuilderAccess access, String dir, Evidence evidence, PermissionSet
requiredPermi
ssions, PermissionSet optionalPermissions, PermissionSet
refusedPermissions, Sta
ckCrawlMark& stackMark, IEnumerable`1 unsafeAssemblyAttributes)
   at System.AppDomain.DefineDynamicAssembly(AssemblyName name,
AssemblyBuilderA
ccess access)
   at Microsoft.Scripting.Generation.AssemblyGen..ctor(String
moduleName, String
 outDir, String outFile, AssemblyGenAttributes generationAttributes,
PortableExe
cutableKinds peKind, ImageFileMachine machine)
   at
Microsoft.Scripting.Generation.StaticFieldModuleGenerator.CreateModuleAsse
mbly(ScriptCode scriptCode)
   at
Microsoft.Scripting.Generation.StaticFieldModuleGenerator.CreateSlotFactor
y(ScriptCode scriptCode)
   at
Microsoft.Scripting.Generation.OptimizedModuleGenerator.CreateStorageAlloc
ator(ScriptCode scriptCode)
   at
Microsoft.Scripting.Generation.OptimizedModuleGenerator.GenerateScriptMeth
ods()
   at
Microsoft.Scripting.Generation.OptimizedModuleGenerator.GenerateScope()
   at Microsoft.Scripting.ScriptDomainManager.CreateModule(String name,
ScriptMo
duleKind kind, Scope scope, ScriptCode[] scriptCodes)
   at Microsoft.Scripting.ScriptDomainManager.CompileModule(String name,
ScriptM
oduleKind kind, Scope scope, CompilerOptions options, ErrorSink
errorSink, Sourc
eUnit[] sourceUnits)
   at Microsoft.Scripting.ScriptDomainManager.CompileModule(String name,
SourceU
nit sourceUnit)
   at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext
context, Stri
ng name, String path)
   at IronPython.Runtime.Importer.ImportFromPath(CodeContext context,
String nam
e, String fullName, List path)
   at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context,
String
name)
   at IronPython.Runtime.Importer.ImportModule(CodeContext context,
String modNa
me, Boolean bottom)
   at IronPython.Runtime.Builtin.__import__(CodeContext context, String
name, Ob
ject globals, Object locals, Object fromList)
   at _stub_##2(Object[] , DynamicSite`6 , CodeContext , Object , String
, IAttr
ibutesCollection , IAttributesCollection , PythonTuple )
   at Microsoft.Scripting.Actions.DynamicSite`6.Invoke(CodeContext
context, T0 a
rg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
   at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext
context, Str
ing fullName)
   at ##18(Object[] , CodeContext )
   at Microsoft.Scripting.ScriptCode.Run(CodeContext codeContext,
Boolean tryEva
luate)
   at Microsoft.Scripting.ScriptCode.Run(ScriptModule module)
   at Microsoft.Scripting.Hosting.CompiledCode.Evaluate(IScriptModule
module)
   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteCommand(String
code, IScri
ptModule module)
   at Microsoft.Scripting.Shell.CommandLine.RunOneInteraction()
   at Microsoft.Scripting.Shell.CommandLine.TryInteractiveAction()
   at IronPython.Hosting.PythonCommandLine.TryInteractiveAction()
   at Microsoft.Scripting.Shell.CommandLine.RunInteractiveLoop()
TypeError: Unable to cast object of type 'System.Reflection.Module' to
type 'Sys
tem.Reflection.Emit.ModuleBuilder'.

 >>>


Dino Viehland wrote:
> Could you run with -X:ExceptionDetail and report back the stack trace?  That'll let us know where the exception is actually coming from.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Friday, November 02, 2007 3:25 PM
> To: Discussion of IronPython
> Subject: [IronPython] Embedding CPython Messes with the IronPython 2 Import Mechanism
>
> Hello Guys,
>
> Not sure what is going on here, nor whether it indicates a bug in
> IronPython, but it is certainly odd. Loading a CPython interpreter
> through the Python.NET assembly seems to change the IronPython (2a5)
> import machinery.
>
> I have a test module 'xyzz.py' that has an unused name 'wibble'.
> Unsurprisingly, attempting to import this into IronPython 2.0a5 raises a
> name error:
>
> IronPython console: IronPython 2.0A5 (2.0.11011.00) on .NET 2.0.50727.1378
> Copyright (c) Microsoft Corporation. All rights reserved.
>  >>> import xyzz
> Traceback (most recent call last):
>   File , line 0, in ##7
>   File , line 0, in _stub_##2
>   File C:\compile\cext\trunk\cext\xyzz.py, line 1, in Initialize
> NameError: name 'wibble' is not defined
>
>
> Executing the following code, (using the 'Python.Runtime' assembly from
> Python.NET with Python 2.4):
>
> IronPython console: IronPython 2.0A5 (2.0.11011.00) on .NET 2.0.50727.1378
> Copyright (c) Microsoft Corporation. All rights reserved.
>  >>> import clr
>  >>> clr.AddReference('Python.Runtime')
>  >>>
>  >>> from Python.Runtime import PythonEngine
>  >>> engine = PythonEngine()
>  >>> engine.Initialize()
>  >>>
>  >>> import xyzz
> Traceback (most recent call last):
>   File , line 0, in ##18
>   File , line 0, in _stub_##2
>   File mscorlib, line unknown, in DefineDynamicAssembly
>   File mscorlib, line unknown, in InternalDefineDynamicAssembly
> TypeError: Unable to cast object of type 'System.Reflection.Module' to
> type 'Sys
> tem.Reflection.Emit.ModuleBuilder'.
>
> Just importing the 'PythonEngine' isn't enough. The problem only happens
> if the PythonEngine is instantiated and initialized.
>
> All the best,
>
>
> Michael
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>

_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list