<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent changes to 5: Possible bug in genericutil.cs (static Register method), and some support questions</title><link href="https://sourceforge.net/p/pythonnet/support-requests/5/" rel="alternate"/><link href="https://sourceforge.net/p/pythonnet/support-requests/5/feed.atom" rel="self"/><id>https://sourceforge.net/p/pythonnet/support-requests/5/</id><updated>2013-10-29T00:58:07.890000Z</updated><subtitle>Recent changes to 5: Possible bug in genericutil.cs (static Register method), and some support questions</subtitle><entry><title>Possible bug in genericutil.cs (static Register method), and some support questions</title><link href="https://sourceforge.net/p/pythonnet/support-requests/5/" rel="alternate"/><published>2013-10-29T00:58:07.890000Z</published><updated>2013-10-29T00:58:07.890000Z</updated><author><name>Dean Wittmann</name><uri>https://sourceforge.net/u/deanwittmann/</uri></author><id>https://sourceforge.net1a95a9c1b21a61392d8e43defce9063dcaea24cb</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;This is my first time trying Python for .NET and I must say that this is an excellent resource.  This is also my first time developing with Python, so I have a couple of questions at the end of this request.  I am, however, a very seasoned C# and C/C++ developer.&lt;/p&gt;
&lt;p&gt;For reference, I am using Visual Studio Ultimate 2012 (with Update 3) and the .Net 4.0 32-bit runtime, Python 2.7.5 through the latest Spyder and Python(x,y) downloads.   I downloaded the latest source snapshot for Python for .NET, and debugged through both the clrmodule.il and Python.Runtime source to find the cause of this error.  I'm not sure that the fix I put in is correct, but here is what I was seeing...&lt;/p&gt;
&lt;p&gt;After the following Python source line:&lt;/p&gt;
&lt;p&gt;import clr&lt;/p&gt;
&lt;p&gt;I was receiving the console output message:&lt;/p&gt;
&lt;p&gt;SystemError: dynamic module not initialized properly&lt;/p&gt;
&lt;p&gt;I have my very short Python source file (Test.py) and the Python for .NET dll, pdb, and clr.pyd in the same folder (along with my C# dll in the same folder).&lt;/p&gt;
&lt;p&gt;I traced the error all the way into the GenericUtil.Register(Type t) static method when it was trying to Register the EmptyArray`1 type name (By the way, this is the only type name that was trying to register with a Namepace == null)&lt;/p&gt;
&lt;p&gt;The EmptyArray`1 Type has the Namespace property (string) set to null.&lt;/p&gt;
&lt;p&gt;The code in the Register method that tries to get the Namespace value:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Namespace&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;nsmap&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;causes an ArgumentNullException when the key value (t.Namespace) is null.&lt;/p&gt;
&lt;p&gt;To fix the error, I just check for t.Namespace == null at the top of the method and immediately return from the method if it is null:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;   &lt;span class="c1"&gt;//====================================================================&lt;/span&gt;
    &lt;span class="c1"&gt;// Register a generic type that appears in a given namespace.&lt;/span&gt;
    &lt;span class="c1"&gt;//====================================================================&lt;/span&gt;

    &lt;span class="nx"&gt;internal&lt;/span&gt; &lt;span class="nx"&gt;static&lt;/span&gt; &lt;span class="bp"&gt;void&lt;/span&gt; &lt;span class="nb"&gt;Register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;Type&lt;/span&gt; &lt;span class="nb"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t.Namespace&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kt"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nsmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;mapping.TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t.Namespace&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;out&lt;/span&gt; &lt;span class="nx"&gt;nsmap&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nsmap&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kt"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;nsmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nx"&gt;mapping&lt;/span&gt;&lt;span class="err"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;t.Namespace&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; = nsmap;
        }
        string basename = t.Name;
        int tick = basename.IndexOf(&amp;quot;`&amp;quot;);
        if (tick &amp;gt; -1) {
            basename = basename.Substring(0, tick);
        }
        List&lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt; gnames = null;
        nsmap.TryGetValue(basename, out gnames);
        if (gnames == null) {
            gnames = new List&lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;();
            nsmap&lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;basename&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; = gnames;
        }
        gnames.Add(t.Name);
    }
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This fix allowed me to then successfully import and use the Python for .NET functionality.  I'm not sure how to get this fix into the source and/or binaries for general use.  So, I'll leave that up to you.&lt;/p&gt;
&lt;p&gt;I don't want to take too much of your time, but I do have some questions (and these are due to the fact that I'm not a Python developer, yet):&lt;/p&gt;
&lt;p&gt;Here is my simple Test.py code:&lt;/p&gt;
&lt;h1 id="-coding-utf-8-"&gt;-&lt;em&gt;- coding: utf-8 -&lt;/em&gt;-&lt;/h1&gt;
&lt;p&gt;"""&lt;br /&gt;
Created on Thu Oct 24 16:08:49 2013&lt;/p&gt;
&lt;p&gt;@author: dwittma&lt;br /&gt;
"""&lt;/p&gt;
&lt;p&gt;import clr&lt;/p&gt;
&lt;p&gt;from System import *&lt;/p&gt;
&lt;p&gt;clr.AddReference("CoreTypes")&lt;/p&gt;
&lt;p&gt;from QuantaSoft import ProcessedWriter&lt;/p&gt;
&lt;p&gt;print ProcessedWriter.&lt;strong&gt;doc&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;pr = ProcessedWriter.Overloads&lt;a class="" href='../"D:\MyPython\Data\Test.qlp",%20"Ch1,Ch2",%20False,%20"",%20None,%20False'&gt;String, String, Boolean, String, Nullable&lt;span&gt;&lt;span&gt;[DateTime]&lt;/span&gt;&lt;/span&gt;, Boolean&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;pr.Close()&lt;/p&gt;
&lt;p&gt;and here is the console output:&lt;/p&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;runfile('D:/MyPython/Test.py', wdir=r'D:/MyPython')&lt;br /&gt;
Void .ctor(System.String, Boolean)&lt;br /&gt;
Void .ctor(System.String, System.String, Boolean, System.String, System.Nullable`1&lt;span&gt;[System.DateTime]&lt;/span&gt;, Boolean)&lt;br /&gt;
Traceback (most recent call last):&lt;br /&gt;
  File "", line 1, in &lt;br /&gt;
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile&lt;br /&gt;
    execfile(filename, namespace)&lt;br /&gt;
  File "D:/MyPython/Test.py", line 18, in &lt;br /&gt;
    pr = ProcessedWriter.Overloads&lt;a class="" href='../"D:\MyPython\Data\Test.qlp",%20"Ch1,Ch2",%20False,%20"",%20None,%20False'&gt;String, String, Boolean, String, Nullable&lt;span&gt;&lt;span&gt;[DateTime]&lt;/span&gt;&lt;/span&gt;, Boolean&lt;/a&gt;&lt;br /&gt;
TypeError: no constructor matches given arguments&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;p&gt;My C# overloaded constructor looks like this:&lt;/p&gt;
&lt;p&gt;public ProcessedWriter(string fileName, string channelDescriptor, bool columnBasedAcquisitionOrder,&lt;br /&gt;
                       string fileComments = QLBFileConst.EmptyString, DateTime? dateTime = null, bool appendChecksum = false)&lt;/p&gt;
&lt;p&gt;My questions are:&lt;/p&gt;
&lt;p&gt;How do I handle the Nullable (or DateTime?) type and how to pass an equivalent Python null argument for this type?&lt;/p&gt;
&lt;p&gt;And, as you can see, I have optional defaulted parameters in the constructor.  How does Python for .NET handle these?  (i.e., how to specify that they are optional, or must I pass in all arguments, even if the parameters are defaulted and optional)&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;-Dean&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>