Discussion:
[Python.NET] Errors with Python.Net 2.0 SP 1
Nicolas Lelong
2008-01-24 10:54:58 UTC
Permalink
Hi,

I have the same problem here since this morning, as XP installed automatically the .NET 2.0 SP 1 update.

Unit test 'leaktest.py' illustrated the problem ::

H:\temp\pythonnet\pythonnet_orig\src\tests>..\..\python.exe leaktest.py
Running module leak check...
start: 16109568 end: 17137664 diff: +1028096

Running class leak check...

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Python.Runtime.CodeGenerator' threw an exception. --
-> System.InvalidCastException: Unable to cast object of type 'System.Reflection.Module' to type 'System.Reflection.Emit.ModuleBuilder'.
at System.Reflection.Emit.AssemblyBuilderData.GetInMemoryAssemblyModule()
at System.AppDomain.InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions, StackCrawlMark& stackMark, IEnumerable`1 unsafeAssemblyAttributes)
at System.AppDomain.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access)
at Python.Runtime.CodeGenerator..cctor() in H:\temp\pythonnet\pythonnet_orig\src\runtime\codegenerator.cs:line 37
--- End of inner exception stack trace ---
at Python.Runtime.DelegateManager.GetDispatcher(Type dtype) in H:\temp\pythonnet\pythonnet_orig\src\runtime\delegatemanager.cs:line 79
at Python.Runtime.DelegateManager.GetDelegate(Type dtype, IntPtr callable) in H:\temp\pythonnet\pythonnet_orig\src\runtime\delegatemanager.cs:line 168
at Python.Runtime.DelegateObject.tp_new(IntPtr tp, IntPtr args, IntPtr kw) in H:\temp\pythonnet\pythonnet_orig\src\runtime\delegateobject.cs:line 74
at e__NativeCall.Call_3(IntPtr , IntPtr , IntPtr , IntPtr )
at Python.Runtime.MetaType.tp_call(IntPtr tp, IntPtr args, IntPtr kw) in H:\temp\pythonnet\pythonnet_orig\src\runtime\metatype.cs:line 156
at Python.Runtime.Runtime.Py_Main(Int32 argc, String[] argv)
at Python.Runtime.PythonConsole.Main(String[] args) in H:\temp\pythonnet\pythonnet_orig\src\console\pythonconsole.cs:line 24

I think, for now, I will uninstall this SP1...

Cheers,

Nicolas.
Nicolas Lelong
2008-01-25 08:44:08 UTC
Permalink
After having setup a minimum repro case for a MSDN Forum (http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=2735385&SiteID=1), I came up with a simple workaround that suits me for now...

The problems comes for the AssemblyManager, it seems that on dynamic Assemblies, the AssemblyLoad event is called before the Assembly object is properly set up, and that the GetTypes() call issued in ScanAssembly messes up the internal state of the newly created Assembly....

As, dynamic assemblies are empty when created, there's no point is scanning them - at least, this is the case of assemblies created by Python.Runtime.CodeGenerator and Python.Runtime.NativeCall.

So, checking is an assembly is dynamic before calling ScanAssembly does the trick, see the following patch :

Index: pythonnet/src/runtime/assemblymanager.cs
===================================================================
--- pythonnet/src/runtime/assemblymanager.cs (révision 90)
+++ pythonnet/src/runtime/assemblymanager.cs (copie de travail)
@@ -87,7 +87,12 @@
static void AssemblyLoadHandler(Object ob, AssemblyLoadEventArgs args){
Assembly assembly = args.LoadedAssembly;
assemblies.Add(assembly);
- ScanAssembly(assembly);
+ // .NET v2.0 SP1 bug workaround ; ScanAssembly called on newly created DynamicAssembly causes problems
+ // only scan non-dynamic assemblies...
+ if ( !(assembly is System.Reflection.Emit.AssemblyBuilder) )
+ {
+ ScanAssembly(assembly);
+ }
}

It fixes the issue, as I'm quite new to .NET I may miss something obvious...

Any thoughts ?

Cheers,
Nicolas.
Feihong Hsu
2008-02-15 15:10:57 UTC
Permalink
Yes! That did the trick.

Thanks so much for tracking the problem down!

- Feihong
Post by Nicolas Lelong
After having setup a minimum repro case for a MSDN Forum
(http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=2735385&SiteID=1),
Post by Nicolas Lelong
I came up with a simple workaround that suits me for now...
The problems comes for the AssemblyManager, it seems that on
dynamic Assemblies, the AssemblyLoad event is called before the
Assembly object is properly set up, and that the GetTypes() call
issued in ScanAssembly messes up the internal state of the newly
created Assembly....
As, dynamic assemblies are empty when created, there's no point is
scanning them - at least, this is the case of assemblies created by
Python.Runtime.CodeGenerator and Python.Runtime.NativeCall.
So, checking is an assembly is dynamic before calling ScanAssembly
Index: pythonnet/src/runtime/assemblymanager.cs
===================================================================
--- pythonnet/src/runtime/assemblymanager.cs (révision 90)
+++ pythonnet/src/runtime/assemblymanager.cs (copie de travail)
@@ -87,7 +87,12 @@
static void AssemblyLoadHandler(Object ob,
AssemblyLoadEventArgs args){
Assembly assembly = args.LoadedAssembly;
assemblies.Add(assembly);
- ScanAssembly(assembly);
+ // .NET v2.0 SP1 bug workaround ; ScanAssembly called
on newly created DynamicAssembly causes problems
+ // only scan non-dynamic assemblies...
+ if ( !(assembly is
System.Reflection.Emit.AssemblyBuilder) )
+ {
+ ScanAssembly(assembly);
+ }
}
It fixes the issue, as I'm quite new to .NET I may miss something obvious...
Any thoughts ?
Cheers,
Nicolas.
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet

Loading...