<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>therning.org/ magnus &#187; ctypes</title>
	<atom:link href="http://therning.org/magnus/archives/tag/ctypes/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Mon, 09 Apr 2012 20:24:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ctypes and flexible (zero-length) arrays</title>
		<link>http://therning.org/magnus/archives/344</link>
		<comments>http://therning.org/magnus/archives/344#comments</comments>
		<pubDate>Wed, 09 Apr 2008 17:00:06 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=344</guid>
		<description><![CDATA[Dear lazyweb, I&#8217;ve been racking my brain trying to get information out of the following C struct in a nice way when using ctypes: struct Foo &#123; unsigned int length; char name&#91;&#93;; &#125;; I wrote a little C function to get instance of the struct easily into python: struct Foo * put_name&#40;char *name&#41; &#123; struct [...]]]></description>
			<content:encoded><![CDATA[<p>Dear lazyweb, I&#8217;ve been racking my brain trying to get information out of the following C struct in a nice way when using <a href="http://python.net/crew/theller/ctypes/">ctypes</a>:</p>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> Foo
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> length<span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> name<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>


<p>I wrote a little C function to get instance of the struct easily into python:</p>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> Foo <span style="color: #339933;">*</span>
put_name<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">struct</span> Foo <span style="color: #339933;">*</span>foo <span style="color: #339933;">=</span> malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> Foo<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> strlen<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    foo<span style="color: #339933;">-&gt;</span>length <span style="color: #339933;">=</span> strlen<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    memcpy<span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">-&gt;</span>name<span style="color: #339933;">,</span> name<span style="color: #339933;">,</span> foo<span style="color: #339933;">-&gt;</span>length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Now, how do I actually get the full contents of <code>Foo.name</code> in python?  The only way I could think of that actually works is to create a dummy-struct type in order to get the length out and then use it to dynamically create a new sub-class of <code>ctypes.Structure</code>, then create an instance based on the address of what was returned.  I think the following shows it pretty clearly:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> FOO<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
    _fields_ = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'length'</span>, c_uint<span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>, c_char <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
liblib = cdll.<span style="color: black;">LoadLibrary</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'./_build/liblib.so'</span><span style="color: black;">&#41;</span>
c_put_name = liblib.<span style="color: black;">put_name</span>
c_put_name.<span style="color: black;">argtypes</span> = <span style="color: black;">&#91;</span>c_char_p<span style="color: black;">&#93;</span>
c_put_name.<span style="color: black;">restype</span> = POINTER<span style="color: black;">&#40;</span>FOO<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> put_name<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
    f = c_put_name<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span>
    K = <span style="color: #008000;">type</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'FOO%s'</span> <span style="color: #66cc66;">%</span> f.<span style="color: black;">contents</span>.<span style="color: black;">length</span>, <span style="color: black;">&#40;</span>Structure,<span style="color: black;">&#41;</span>,
                        <span style="color: black;">&#123;</span><span style="color: #483d8b;">'_fields_'</span> : <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'length'</span>, c_uint<span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>, c_char <span style="color: #66cc66;">*</span> f.<span style="color: black;">contents</span>.<span style="color: black;">length</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> K.<span style="color: black;">from_address</span><span style="color: black;">&#40;</span>addressof<span style="color: black;">&#40;</span>f.<span style="color: black;">contents</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>


<p>I still think there ought to be some other way that &#8216;feels&#8217; nicer.  I mean the use of “short arrays” (declared as here with <code>[]</code>, or zero size as supported by GCC, or the more portable array of size one) seems to be common enough to warrant some support from ctypes, right?</p>

<p>Any suggestions for improvements?</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F344&amp;title=ctypes%20and%20flexible%20%28zero-length%29%20arrays" id="wpa2a_2">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/344/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

