<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2657.73">
<TITLE>FW: Newbie question &amp; Error Found in Tutorial</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Vanitha,</FONT>
</P>

<P><FONT SIZE=2>Let me start by reposting the offending code:</FONT>
</P>

<P><FONT SIZE=2>cdef class Foo:</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>&quot;&quot;&quot;A foo, like any other.</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>Create one with Foo(s), where s is the name to assign to your foo.&quot;&quot;&quot;</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>cdef char *name</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>def __init__(self, name):</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>self.name = name</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>def__repr__(self):</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>return &quot;A Foo named %s.&quot; % (self.name)</FONT>
</P>

<P><FONT SIZE=2>The problem with this code lies in the fact that self.name is a c variable and as such doesn't appear to Python's garbage collector. First I'll give you a bit of background: when you are assigning a python string to a char pointer it actually sets the char pointer to the memory containing the python string -&gt; it does not create a copy of it. So if you have a char pointer that references a Python string that has been garbage collected you're pointing to invalid memory.</FONT></P>

<P><FONT SIZE=2>The above fact is demonstrated by what you are experiencing:</FONT>
</P>

<P><FONT SIZE=2>&gt;&gt;&gt; import foo</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f = foo.Foo('Vanitha')</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named stdout.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named &lt;stdin&gt;.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named global.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
</P>

<P><FONT SIZE=2>Because the memory 'Vanitha' points to is garbage collected after line 2 the memory it references is now occupied by something else. I'll explain what happens on line 2 in more detail:</FONT></P>

<P><FONT SIZE=2>&gt;&gt;&gt; f = foo.Foo('Vanitha')</FONT>
</P>

<P><FONT SIZE=2>What this does in Python is the following (briefly without ref count information):</FONT>
</P>

<P><FONT SIZE=2>1) String constant 'Vanitha' is turned into a Python string object and is passed to the Foo constructor</FONT>
<BR><FONT SIZE=2>2) a Foo object is created</FONT>
<BR><FONT SIZE=2>3) in the __init__ method the string object is bound to the local variable 'name'</FONT>
<BR><FONT SIZE=2>4) self.name (a char pointer) is set to point to the memory occupied by the Python string bound to 'name'</FONT>
<BR><FONT SIZE=2>5) local binding to 'name' removed</FONT>
<BR><FONT SIZE=2>5) back in the main statement, the foo object is bound to a local variable f</FONT>
<BR><FONT SIZE=2>6) since there are no longer any references to the string 'Vanitha' the python garbage collector will at some point free the Python object and the memory it occupied will be reclaimed.</FONT></P>

<P><FONT SIZE=2>This has the effect that once the string 'Vanitha' is garbage collected the char pointer points to something else. It is simply fortuitous that this doesn't result in a seg fault. To further demonstrate this consider the following code snippet:</FONT></P>

<P><FONT SIZE=2>&gt;&gt;&gt; import foo</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; name = 'Vanitha'</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f = foo.Foo(name)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named Vanitha.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named Vanitha.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named Vanitha.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named Vanitha.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; </FONT>
</P>

<P><FONT SIZE=2>In this instance we don't see the error because we are binding the 'Vanitha' string to a local variable called name. Since there is still a reference to it the char pointer doesn't become invalid. If we then type:</FONT></P>

<P><FONT SIZE=2>&gt;&gt;&gt; del name</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>A Foo named &lt;stdin&gt;.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; </FONT>
</P>

<P><FONT SIZE=2>We see the problem manifested again because 'del name' removes the reference to name and it is then garbage collected. So if you are wanting to store python string data in c variables persistently then you will need to allocate enough memory to hold the string and then strcpy the python object. The solution is as follows:</FONT></P>

<P><FONT SIZE=2>&quot;&quot;&quot;A simple extension module example&quot;&quot;&quot;</FONT>
</P>

<P><FONT SIZE=2>ctypedef long size_t</FONT>
</P>

<P><FONT SIZE=2>cdef extern from &quot;Python.h&quot;:</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; void* PyMem_Malloc(size_t n)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; void PyMem_Free(void* mem)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; object PyErr_NoMemory()</FONT>
</P>

<P><FONT SIZE=2>cdef extern from &quot;string.h&quot;:</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; void* memset(void* mem, int setting, size_t memsize)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; char* strcpy (char* dest, char* src)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; char* strcat (char* dest, char* src)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; int strlen(char* str)</FONT>
</P>

<P><FONT SIZE=2>cdef class Foo:</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;A foo, like any other.</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; Create one with Foo(s), where s is the name to assign to your foo.&quot;&quot;&quot;</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; cdef char *name</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def __new__(self, name):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = NULL</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def __init__(self, name):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = &lt;char*&gt;PyMem_Malloc(len(name)+1)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not self.name:</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise MemoryError</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memset(self.name, 0x00, len(name)+1)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(self.name,name)</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def __repr__(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &quot;A Foo named %s.&quot; % (self.name)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; def __dealloc__(self):</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if self.name:</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyMem_Free(self.name)</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
</P>

<P><FONT SIZE=2>In any respect the original tutorial author was incorrect in his use of Pyrex. If any of this is unclear please ask, and it might be worth reading the Python/C API documentation if you are thinking of playing around with Pyrex (as well as the Pyrex doco ;)</FONT></P>

<P><FONT SIZE=2>Regards,</FONT>
</P>

<P><FONT SIZE=2>Grant M.</FONT>
</P>
<BR>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: vanitha@cs.wisc.edu [<A HREF="mailto:vanitha@cs.wisc.edu">mailto:vanitha@cs.wisc.edu</A>]</FONT>
<BR><FONT SIZE=2>Sent: 29 November 2005 14:29</FONT>
<BR><FONT SIZE=2>To: Grant McDonald</FONT>
<BR><FONT SIZE=2>Subject: RE: [Pyrex] Newbie question</FONT>
</P>
<BR>

<P><FONT SIZE=2>Hi Grant,</FONT>
<BR><FONT SIZE=2>Here is the foo.c file that was generated!</FONT>
</P>

<P><FONT SIZE=2>Thanks,</FONT>
</P>

<P><FONT SIZE=2>- Vanitha</FONT>
</P>

<P><FONT SIZE=2>&gt; Vanitha,</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; I'm not sure why this is occurring, but looking at the generated foo.c</FONT>
<BR><FONT SIZE=2>&gt; should make things a bit clearer. Can you send me your foo.c file? (Pyrex</FONT>
<BR><FONT SIZE=2>&gt; generates this from your pyx file) It should be in the same directory as</FONT>
<BR><FONT SIZE=2>&gt; foo.pyx.</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Thanks,</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Grant</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt; From: vanitha@cs.wisc.edu [<A HREF="mailto:vanitha@cs.wisc.edu">mailto:vanitha@cs.wisc.edu</A>]</FONT>
<BR><FONT SIZE=2>&gt; Sent: 28 November 2005 23:57</FONT>
<BR><FONT SIZE=2>&gt; To: Grant McDonald</FONT>
<BR><FONT SIZE=2>&gt; Subject: RE: [Pyrex] Newbie question</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Hi Grant:</FONT>
<BR><FONT SIZE=2>&gt; Inserting the space took care of the compilation but I don't the results I</FONT>
<BR><FONT SIZE=2>&gt; am expecting.</FONT>
<BR><FONT SIZE=2>&gt; I did exactly what was given in the following website:</FONT>
<BR><FONT SIZE=2>&gt; <A HREF="http://cfl-x.uwindsor.ca/graham/pyrex/" TARGET="_blank">http://cfl-x.uwindsor.ca/graham/pyrex/</A></FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; But here's what I get:</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Python 2.4.2 (#5, Oct 24 2005, 14:05:01)</FONT>
<BR><FONT SIZE=2>&gt; [GCC 3.4.4] on linux2</FONT>
<BR><FONT SIZE=2>&gt; Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; import foo</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; f = foo.Foo('Vanitha')</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; f.__class__</FONT>
<BR><FONT SIZE=2>&gt; &lt;type 'foo.Foo'&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; f</FONT>
<BR><FONT SIZE=2>&gt; A Foo named stdout.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; I should really be getting &quot;A Foo named Vanitha&quot; as my output. I'm not</FONT>
<BR><FONT SIZE=2>&gt; sure why this is happening. Also, what is f.__class__ and what does it</FONT>
<BR><FONT SIZE=2>&gt; accomplish?</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Thanks,</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; - Vanitha</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; Vanitha,</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; It's actually a simple error, on line ten you forgot to add a space</FONT>
<BR><FONT SIZE=2>&gt;&gt; after</FONT>
<BR><FONT SIZE=2>&gt;&gt; the def:</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; cdef class Foo:</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;A foo, like any other.</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; Create one with Foo(s), where s is the name to assign to your</FONT>
<BR><FONT SIZE=2>&gt;&gt; foo.&quot;&quot;&quot;</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; cdef char *name</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; def __init__(self, name):</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = name</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; def__repr__(self):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # &lt;-- this should be def</FONT>
<BR><FONT SIZE=2>&gt;&gt; __repr__(self):</FONT>
<BR><FONT SIZE=2>&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &quot;A Foo named %s.&quot; % (self.name)</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; Everything should work then. Let me know if you have any further</FONT>
<BR><FONT SIZE=2>&gt;&gt; questions</FONT>
<BR><FONT SIZE=2>&gt;&gt; as I'm more than happy to help out.</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; Grant</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt;&gt; From: vanitha@cs.wisc.edu [<A HREF="mailto:vanitha@cs.wisc.edu">mailto:vanitha@cs.wisc.edu</A>]</FONT>
<BR><FONT SIZE=2>&gt;&gt; Sent: 28 November 2005 13:57</FONT>
<BR><FONT SIZE=2>&gt;&gt; To: Grant McDonald</FONT>
<BR><FONT SIZE=2>&gt;&gt; Subject: RE: [Pyrex] Newbie question</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; Hi Grant,</FONT>
<BR><FONT SIZE=2>&gt;&gt; I've attached the files. Your help is much appreciated!</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; Thanks very much,</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt; - Vanitha</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Vanitha,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; That's a strange error, there's nothing in the source code of the</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; extension</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; that should cause this problem. It's actually the Pyrex compiler</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; complaining</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; about something in your source code (maybe line endings???).&nbsp; Can you</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; send</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; me your exact source files as an email attachment? I might be able to</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; figure</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; it out for you.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Regards,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Grant</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; From: vanitha@cs.wisc.edu [<A HREF="mailto:vanitha@cs.wisc.edu">mailto:vanitha@cs.wisc.edu</A>]</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Sent: 26 November 2005 22:57</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; To: Grant McDonald</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Subject: RE: [Pyrex] Newbie question</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Hi Grant,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Thanks! I did upgrade to 0.9.3.1 and I ran setup.py build_ext again and</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; I</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; get errors:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Here is the simple example (off the web) that I tried to run:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; * foo.pyx - a straightforward extension class written in Pyrex.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &quot;&quot;&quot;A simple extension module example&quot;&quot;&quot;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; cdef class Foo:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; &quot;&quot;&quot;A foo, like any other.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; Create one with Foo(s), where s is the name to assign to your</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; foo.&quot;&quot;&quot;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; cdef char *name</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; def __init__(self, name):</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = name</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; def __repr__(self):</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &quot;A Foo named %s.&quot; % (self.name)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; * setup.py - note the import of Pyrex.Distutils.build_ext, its</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; placement via the cmdclass argument, and the specification of .pyx</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; files (not .c files) in the ext_modules parameter.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; from distutils.core import setup</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; from distutils.extension import Extension</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; from Pyrex.Distutils import build_ext</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; setup(name='foo', ext_modules=[Extension(&quot;foo&quot;, [&quot;foo.pyx&quot;])],</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; &nbsp;&nbsp;&nbsp; cmdclass = {'build_ext': build_ext}</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; )</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; And here is the error I get:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; running build_ext</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; building 'foo' extension</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; /afs/cs.wisc.edu/u/v/a/vanitha/private/CS838/PyrexExamples/foo.pyx:10:1:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Executable statement not allowed here</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Any help will be much appreciated. I'm new to both Python and Pyrex :-(</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; Thanks,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt; - Vanitha</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Vanitha,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; This bug is an incompatibility between Pyrex 0.9.3 build_ext.py and</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Python</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; 2.4. distutils/build_ext.py For full details see:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; <A HREF="http://lists.copyleft.no/pipermail/pyrex/2004-December/001084.html" TARGET="_blank">http://lists.copyleft.no/pipermail/pyrex/2004-December/001084.html</A></FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; This is fixed in 0.9.3.1 (or you could apply the patch yourself if you</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; didn't want any other updates)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Grant M.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; From: pyrex-bounces@lists.copyleft.no</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; [<A HREF="mailto:pyrex-bounces@lists.copyleft.no">mailto:pyrex-bounces@lists.copyleft.no</A>]On Behalf Of</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; vanitha@cs.wisc.edu</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Sent: 26 November 2005 14:56</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; To: pyrex@lists.copyleft.no</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Subject: Re: [Pyrex] Newbie question</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Hi Grant:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; I'm using Pyrex 0.9.3 along with Python 2.4 and gcc on Linux.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; I think I'll have to upgrade to 0.9.3.1 to get the distutils patch.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Here is the whole error message anyways!</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; python setup.py build_ext --inplace</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; running build_ext</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; building 'foo' extension</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; Traceback (most recent call last):</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File &quot;setup.py&quot;, line 6, in ?</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; cmdclass = {'build_ext' : build_ext}</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; &quot;/afs/cs.wisc.edu/u/v/a/vanitha/lib/python2.4/distutils/core.py&quot;,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; line 149, in setup</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; dist.run_commands()</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; &quot;/afs/cs.wisc.edu/u/v/a/vanitha/lib/python2.4/distutils/dist.py&quot;,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; line 946, in run_commands</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; self.run_command(cmd)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; &quot;/afs/cs.wisc.edu/u/v/a/vanitha/lib/python2.4/distutils/dist.py&quot;,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; line 966, in run_command</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; cmd_obj.run()</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt; &quot;/afs/cs.wisc.edu/u/v/a/vanitha/lib/python2.4/distutils/command/build_ext.py</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; &quot;,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; line 279, in run</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; self.build_extensions()</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt; &quot;/afs/cs.wisc.edu/u/v/a/vanitha/lib/python2.4/distutils/command/build_ext.py</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; &quot;,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; line 405, in build_extensions</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; self.build_extension(ext)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp; File</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt; &quot;/afs/cs.wisc.edu/u/v/a/vanitha/lib/python2.4/distutils/command/build_ext.py</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; &quot;,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; line 442, in build_extension</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; sources = self.swig_sources(sources, ext)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&gt; TypeError: swig_sources() takes exactly 2 arguments (3 given)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Thanks,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; - Vanitha</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Original Message:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Vanitha,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; What version of Pyrex are you using? I built the sample extension you</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; provided without encountering an error with Pyrex 0.9.3.1, Python</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; 2.4.1</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; and</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; MSVC 7.0. The stack trace that you provided seems to be lacking the</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; final</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; error message because the line it terminates on is:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; ### distutils/command/build_ext.py ###</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; def build_extensions(self):</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # First, sanity-check the 'extensions' list</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.check_extensions_list(self.extensions)</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ext in self.extensions:</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.build_extension(ext)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # &lt;-- this is where</FONT>
<BR><FONT SIZE=2>&gt;&gt; your</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; stack trace terminates</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Are you able to repost your error and also some information about your</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; build</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; environment?</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Regards,</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Grant M.</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; _______________________________________________</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Pyrex mailing list</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; Pyrex@lists.copyleft.no</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt; <A HREF="http://lists.copyleft.no/mailman/listinfo/pyrex" TARGET="_blank">http://lists.copyleft.no/mailman/listinfo/pyrex</A></FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
</P>

</BODY>
<!--[object_id=#infocomp.com#]--><P><FONT face=Arial color=#808080 size=1>Important notice: This message is intended for the individual(s) and entity(s) addressed. The information contained in this transmission and any attached, may be confidential and may also be the subject of legal privilege, public interest immunity or legal professional privilege. Any review, retransmission, dissemination or other use of, taking of any action in reliance upon this information by person or entities other than the recipient is prohibited and requires authorization from the sender. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person) you may not copy or deliver this message to anyone. In such cases you should destroy this message and kindly notify the sender by reply email. </FONT></P>
<P><FONT face=Arial color=#808080 size=1>WARNING: Although Infocomp has taken reasonable precautions so that no viruses&nbsp;are present in this e-mail, the company cannot accept responsibility for any loss or damage arising from the use of e-mail attachments.</FONT></P></HTML>