<!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>RE: [Pyrex] Total newbie needs code review</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>Chris,</FONT>
</P>
<P><FONT SIZE=2>What pyrex means by "Obtaining char * from temporary Python value" is that you haven't created a char pointer variable to hold the string value. When this happens pyrex needs to create a temporary variable to hold the value returned by the Python/C API call PyString_AsString. The way to remove this warning from your code is to be explicit in your conversions:</FONT></P>
<P><FONT SIZE=2>cdef void doStuff(char * stuff)</FONT>
<BR> <FONT SIZE=2>sprintf("this is stuff: %s\n", stuff)</FONT>
</P>
<BR>
<P><FONT SIZE=2>cdef char * cStuff</FONT>
<BR><FONT SIZE=2>pyStuff = "this is stuff"</FONT>
</P>
<P><FONT SIZE=2>doStuff(pyStuff) # implicit</FONT>
</P>
<BR>
<P><FONT SIZE=2>cStuff = pyStuff</FONT>
<BR><FONT SIZE=2>doStuff(cStuff) # explicit</FONT>
</P>
<BR>
<P><FONT SIZE=2>As i mentioned before the assignment of pyStuff to cStuff or the implicit function call both obtain a pointer that refers to the internal buffer of the python object not a copy, so it mustn't be modified. An example of a modifying function:</FONT></P>
<P><FONT SIZE=2>cdef void modifyArg(char * stuff)</FONT>
<BR> <FONT SIZE=2>strcat(stuff, " and more stuff!")</FONT>
</P>
<P><FONT SIZE=2>So if you are passing in the pointer reference as an argument to a function you must be totally sure that the function does not modify it in _any_ way. If you can't be sure malloc a char pointer to the size of the string and strcpy the contents of the pystring into it:</FONT></P>
<P><FONT SIZE=2>cdef char * copy</FONT>
<BR><FONT SIZE=2>pyString = "a python string!"</FONT>
</P>
<P><FONT SIZE=2># malloc char pointer; len + 1 for null terminator</FONT>
<BR><FONT SIZE=2>copy = <char *>PyMem_Malloc(len(pyString) * sizeof(char) + 1)</FONT>
</P>
<P><FONT SIZE=2># this implicit conversion is fine since strcpy does not modify the src</FONT>
<BR><FONT SIZE=2>strcpy(copy, pyString)</FONT>
</P>
<P><FONT SIZE=2># do something with copy that modifies it</FONT>
<BR><FONT SIZE=2>modifyArg(copy)</FONT>
</P>
<P><FONT SIZE=2># free memory</FONT>
<BR><FONT SIZE=2>PyMem_Free(copy)</FONT>
</P>
<P><FONT SIZE=2>In the above example I have used the PyMem_* routines which are defined in the python header file. Have a look in the Python/C API reference for a definition and description of them. Please ensure that whatever memory you malloc you free, since the python interpreter will not automatically free memory allocated outside the interpreter.</FONT></P>
<P><FONT SIZE=2>Regards,</FONT>
</P>
<P><FONT SIZE=2>Grant M.</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 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>