I looked into the ctypes on mono problem a bit (MONO_LOG_LEVEL=debug mono ipy.exe is helpful here).  There seem to be three issues: <div><br></div><div>1) libdl.so is libdl.dylib on OSX and there is no dllmap that does this automatically on Mono.  It checks for libdl.so.dylib and a few other places but not libdl.dylib.  This can be fixed by creating a symbolic link or changing the DLLImport to libdl without the &quot;.so&quot;.  Unfortunately the latter approach breaks breaks linux support.  So I just created this symlink by hand, which is not ideal.<div>
<br></div><div>2) On linux, it seems that RTLD_NOW (2) should be passed to the dlopen call. (See <a href="http://www.mail-archive.com/mono-list@lists.ximian.com/msg30407.html">http://www.mail-archive.com/mono-list@lists.ximian.com/msg30407.html</a>).  Without this import ctypes works on OSX but not linux, with it things seem to work on both platforms.</div>
<div><br></div><div>3) LocalAlloc needs a posix equivalent.  I added a calloc(1, size), which seemed to do the trick.  I&#39;m not expert here, I assume some of the other public methods should be ported too?</div><div><br>
</div><div>With these changes basic libc calls, like abs(), work on windows, osx, and linux. Lots stuff doesn&#39;t work. Printf gives one character everywhere and segfaults on linux, but this seems to be the general state of ctypes support even on Windows.  Unfortunately fcntl through ctypes also doesn&#39;t seem to work, which is why I was looking into ctypes to start with.</div>
<div><br></div><div>Below are my changes to NativeFunctions.cs, which are a partial solution at best.</div><div><br></div><div>Tristan</div><div><br></div><div>---</div><div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div>
        // unix entry points, VM needs to map the filenames.</div><div>        [DllImport(&quot;libdl.so&quot;, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]</div><div>        private static extern IntPtr dlopen([In] string filename, [In] int flags);</div>
<div><br></div><div>        [DllImport(&quot;libdl.so&quot;, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]</div><div>        private static extern IntPtr dlsym([In] IntPtr handle, [In] string symbol);</div>
<div><br></div><div>        [DllImport(&quot;libdl.so&quot;)]</div><div>        private static extern IntPtr calloc(uint nelem, IntPtr size);</div><div><br></div><div>        // Required for Unix not OSX.</div><div>        private const int RTLD_NOW = 2;</div>
<div><br></div><div>        public static IntPtr LoadDLL(string filename, int flags) {</div><div>            if (Environment.OSVersion.Platform == PlatformID.Unix ||</div><div>                Environment.OSVersion.Platform == PlatformID.MacOSX) {</div>
<div>                return dlopen(filename, RTLD_NOW);</div><div>            }</div><div>            return LoadLibrary(filename);</div><div>        }</div><div><br></div><div><div>        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]</div>
<div>        public static IntPtr Calloc(IntPtr size) {</div><div>            if (Environment.OSVersion.Platform == PlatformID.Unix ||</div><div>                 Environment.OSVersion.Platform == PlatformID.MacOSX) {</div>
<div>                     return calloc(1, size);</div><div>            }</div><div>            return LocalAlloc(LMEM_ZEROINIT, size);</div><div>        }</div></div></span></div><div><br></div><div>---</div><div><br></div>
<div><br><br><div class="gmail_quote">On Tue, Jun 1, 2010 at 3:13 PM, Tristan Zajonc <span dir="ltr">&lt;<a href="mailto:tristanz@gmail.com">tristanz@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Both on OSX and Ubuntu it appears to fail on a call to PyDLL(None).  The problematic call is from the block:<div><br></div><div><div>if _<a href="http://os.name" target="_blank">os.name</a> in (&quot;nt&quot;, &quot;ce&quot;):</div>
<div>    pythonapi = PyDLL(&quot;python dll&quot;, None, _sys.dllhandle)</div>
<div>elif _sys.platform == &quot;cygwin&quot;:</div><div>    pythonapi = PyDLL(&quot;libpython%d.%d.dll&quot; % _sys.version_info[:2])</div><div>else:</div><div>    print &quot;DEBUG: NOT NT/CE OR CYGWIN.&quot;</div><div>

    pythonapi = PyDLL(None)</div><div><br></div><div>It goes to CDLL base class and dies exactly here:</div><div><br></div><div>if handle is None:</div><div><div>    print &quot;HANDLE: &quot; + str(self._name) + str(mode)  #prints None0</div>

<div>    self._handle = _dlopen(self._name, mode)</div><div>    print &quot;AFTER DLOPEN. DOESN&#39;T PRINT.&quot;</div></div><div><br></div><div>Hope this helps.</div><div><br></div><font color="#888888"><div>Tristan</div>
</font><div><div></div><div class="h5"><br><div class="gmail_quote">
On Tue, Jun 1, 2010 at 1:34 PM, Dino Viehland <span dir="ltr">&lt;<a href="mailto:dinov@microsoft.com" target="_blank">dinov@microsoft.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">








<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">Any chance you can put a print in any calls to dlopen in ctypes\__init__.py and see what we’re trying to load?</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt">From:</span></b><span style="font-size:10.0pt"> <a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a>]
<b>On Behalf Of </b>Tristan Zajonc<br>
<b>Sent:</b> Sunday, May 30, 2010 3:57 PM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> Re: [IronPython] ctypes on Mono</span></p>
</div>
</div><div><div></div><div>
<p class="MsoNormal"> </p>
<p class="MsoNormal">(I&#39;ll look into OSX issue, although am happy to hear a solution from other users)</p>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<p class="MsoNormal">I&#39;m not sure why I don&#39;t get line numbers.  If I pass -XExceptionDetail, I don&#39;t get much more:</p>
</div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<div>
<p class="MsoNormal">tristanz@aontic1:~$ ipy -X:ExceptionDetail       </p>
</div>
<div>
<p class="MsoNormal">IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.1433</p>
</div>
<div>
<p class="MsoNormal">Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</p>
</div>
<div>
<p class="MsoNormal">&gt;&gt;&gt; import ctypes</p>
</div>
<div>
<p class="MsoNormal">cannot load library </p>
</div>
<div>
<p class="MsoNormal">LoadLibrary at offset 0 in file:line:column &lt;filename unknown&gt;:0:0</p>
</div>
<div>
<p class="MsoNormal">dlopen at offset 0 in file:line:column &lt;filename unknown&gt;:0:0</p>
</div>
<div>
<p class="MsoNormal">Invoke at offset 0 in file:line:column &lt;filename unknown&gt;:0:0</p>
</div>
<div>
<p class="MsoNormal">  at IronPython.Modules.CTypes.LoadLibrary (System.String library, Int32 mode) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal">  at IronPython.Modules.CTypes.dlopen (System.String library, Int32 mode) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal">  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod*,object,object[],System.Exception&amp;)</p>
</div>
<div>
<p class="MsoNormal">  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in &lt;filename unknown&gt;:0 </p>



</div>
<div>
<p class="MsoNormal">OSError: IronPython.Runtime.Exceptions.OSException: cannot load library </p>
</div>
<div>
<p class="MsoNormal">  at IronPython.Modules.CTypes.LoadLibrary (System.String library, Int32 mode) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal">  at IronPython.Modules.CTypes.dlopen (System.String library, Int32 mode) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal">  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod*,object,object[],System.Exception&amp;)</p>
</div>
<div>
<p class="MsoNormal">  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in &lt;filename unknown&gt;:0 </p>



</div>
</div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<div>
<p class="MsoNormal"> </p>
<div>
<p class="MsoNormal">On Sun, May 30, 2010 at 6:24 PM, Dino Viehland &lt;<a href="mailto:dinov@microsoft.com" target="_blank">dinov@microsoft.com</a>&gt; wrote:</p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">On OS/X I believe you need to use Mono’s DllMap feature (<a href="http://www.mono-project.com/Config_DllMap" target="_blank">http://www.mono-project.com/Config_DllMap</a>)
 to map from the Linux library name (libdl.so) to the Mac OS/X library which exports dlopen.  I actually have no clue what exports it and I don’t see one listed over here:
<a href="http://gemma.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/dlopen.3.html" target="_blank">
http://gemma.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/dlopen.3.html</a> so hopefully someone else on the list knows.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">It looks like on Linux the –X:ExceptionDetail command line option is being passed which is preventing the stack trace from having any
 line number information - or maybe the stack trace is cut off?  Either way it’d be useful to see the line number which is calling dlopen to understand what library we are failing to load.  My guess would be that it’s trying to load the Python library but I’d
 think that would work fine (it looks like we pass in a null path in that case which should be alright for dlopen).</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt">From:</span></b><span style="font-size:10.0pt">
<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a>]
<b>On Behalf Of </b>Tristan Zajonc</span></p>
<div>
<p class="MsoNormal"><br>
<b>Sent:</b> Saturday, May 29, 2010 3:59 PM<br>
<b>To:</b> Discussion of IronPython</p>
</div>
<div>
<p class="MsoNormal"><b>Subject:</b> [IronPython] ctypes on Mono</p>
</div>
</div>
</div>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I&#39;m running the the latest IronPython 2.6.1 (.NET 2.0 version) with Mono.  I cannot import ctypes. </p>
<div>
<div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<p class="MsoNormal">Is this behavior expected?  Any solutions?  Here are the exact results:</p>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<p class="MsoNormal">On OSX:</p>
</div>
<div>
<div>
<p class="MsoNormal">IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.1433</p>
</div>
<div>
<p class="MsoNormal">Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</p>
</div>
<div>
<p class="MsoNormal">&gt;&gt;&gt; import ctypes</p>
</div>
<div>
<p class="MsoNormal">Traceback (most recent call last):</p>
</div>
<div>
<p class="MsoNormal">SystemError: libdl.so</p>
</div>
</div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<p class="MsoNormal">On Ubuntu (using trunk Mono):</p>
</div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<div>
<p class="MsoNormal">IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.1433</p>
</div>
<div>
<p class="MsoNormal">Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</p>
</div>
<div>
<p class="MsoNormal">&gt;&gt;&gt; import ctypes</p>
</div>
<div>
<p class="MsoNormal">Traceback (most recent call last):</p>
</div>
<div>
<p class="MsoNormal">OSError: IronPython.Runtime.Exceptions.OSException: cannot load library </p>
</div>
<div>
<p class="MsoNormal">  at IronPython.Modules.CTypes.LoadLibrary (System.String library, Int32 mode) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal">  at IronPython.Modules.CTypes.dlopen (System.String library, Int32 mode) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal">  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod*,object,object[],System.Exception&amp;)</p>
</div>
<div>
<p class="MsoNormal">  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo
 culture) [0x00000] in &lt;filename unknown&gt;:0 </p>
</div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<p class="MsoNormal"> </p>
</div>
<div>
<div>
<div>
<p class="MsoNormal">Best,</p>
</div>
<div>
<p class="MsoNormal">Tristan</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a></p>
</div>
<p class="MsoNormal"> </p>
</div>
</div>
</div></div></div>
</div>
</div>

<br>_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br></blockquote></div><br>
</div></div></div>
</blockquote></div><br></div></div>