<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.gmailquote
        {mso-style-name:gmail_quote;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Forgive my non-C-ness (it&#8217;s been a long time since I wrote
a native module for Python), but aren&#8217;t you now buying into a major re-implementation
of all the native Python standard library into C#?&nbsp; <o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Assuming for a moment that few sane people will want to
re-compile them from source, it seems much easier to just use the current
C++/CLI compiler from MS (not MC++), as recommended on the Mono site: <a
href="http://www.mono-project.com/CPlusPlus">http://www.mono-project.com/CPlusPlus</a>
(#CodeGeneration)<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Then conditionally switch out the CRT references with a CLR
version:<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#ifdef CLR<o:p></o:p></span></p>

<p class=MsoNormal style='text-indent:.5in'><span style='font-size:11.0pt;
font-family:"Calibri","sans-serif";color:#1F497D'>Tasty, non-CRT happy-happy<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#else<o:p></o:p></span></p>

<p class=MsoNormal style='text-indent:.5in'><span style='font-size:11.0pt;
font-family:"Calibri","sans-serif";color:#1F497D'>Evil, CRT hurty-hurty<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>#endif<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>.. Then maybe re-submit back to CPython-land.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<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;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>
users-bounces@lists.ironpython.com [mailto:users-bounces@lists.ironpython.com] <b>On
Behalf Of </b>Curt Hagenlocher<br>
<b>Sent:</b> Wednesday, October 17, 2007 2:31 PM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> Re: [IronPython] [python] Re: Announcement: Project to get
someCPython C extensions running under IronPython<o:p></o:p></span></p>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<div>

<p class=MsoNormal><span class=gmailquote>On 10/17/07, <b>Michael Foord</b>
&lt;<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk
</a>&gt; wrote:</span> <o:p></o:p></p>

<p class=MsoNormal>I would be sad if we chose any approach that couldn't work
on Mono. :-(<o:p></o:p></p>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>I agree.&nbsp; Assuming this rules out MC++, I think the
next best approach is (as Paolo suggests) to try to shift as much of the work
to C# as possible.<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>Here's an architecture to think about. Consider the
following example for PySequence_Length:<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>In C:<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>typedef int
(__stdcall *PFN_iO)(PyObject *);</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>PFN_iO
PFN_PySequence_Length;</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>void
Set_PySequence_Length(PFN_iO callback)<br>
{<br>
&nbsp;&nbsp;&nbsp; PFN_PySequence_Length = callback;<br>
}<br>
int PySequence_Length(PyObject * object)<br>
{<br>
&nbsp;&nbsp;&nbsp; return PFN_PySequence_Length(object); <br>
}</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>In C#:<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>public delegate int
PFN_iO(IntPtr object);</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>[DllImport(&quot;extension.DLL&quot;)]<br>
public static extern void Set_PySequence_Length(PFN_iO callback);</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>public
static&nbsp;int PySequence_Length(IntPtr obj_in)</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>{</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;
object o = ObjectConverter(obj_in);</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;
// Do something with o</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;
return result;</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>}</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>public static void
SetApiFunctionsAtStartup()</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>{</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;
Set_PySequence_Length(PySequence_Length);</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span style='font-family:"Courier New"'>}</span><o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>Ignoring exception-handling for now, the idea is to keep
most of the implementation in C# and to have the C# code register callback
functions as actual implementations for the parts of the Python API that we're
going to duplicate. <o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>This is idea is dependent on a kind of &quot;global
registry&quot; of objects with representations on both sides of the
border.&nbsp; Such a registry will almost certainly be required in order to
translate between the two -- if for no other reason than to handle
reference-counting and/or garbage collection.&nbsp; Any PyObject being passed
across the boundary would then actually be some kind of index into the
registry. <o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>A &quot;PyTuple_New&quot; would call through the delegate
into C# where (say) a List&lt;object&gt; would be created and added to the
registry, with its unique identifer being passed back to the C caller as a
&quot;PyObject *&quot;. <o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>INCREF and DECREF on the C side would affect a reference
count in the registry. Once the DECREF takes the reference count down to zero,
the object is removed.&nbsp; At that point, it may very well still be a valid
CLR object in use by other parts of the program -- just not by the C extension.
<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>Objects actually defined in the C extension would be
required to use either PyObject_New or Py_NewReference as part of their
initialization in order for the proper fixups to happen.<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>What do you think?<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>&nbsp;<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>--<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal>Curt Hagenlocher<o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><a href="mailto:curt@hagenlocher.org">curt@hagenlocher.org</a><o:p></o:p></p>

</div>

</div>

</div>

</body>

</html>