<?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>Ctrl-C &#187; Software</title>
	<atom:link href="http://ctrl-c.us/blog/archives/category/computers/software/feed" rel="self" type="application/rss+xml" />
	<link>http://ctrl-c.us/blog</link>
	<description>a friendly interruption</description>
	<lastBuildDate>Fri, 28 May 2010 11:49:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding &#8216;addremove&#8217; to Git</title>
		<link>http://ctrl-c.us/blog/archives/849</link>
		<comments>http://ctrl-c.us/blog/archives/849#comments</comments>
		<pubDate>Fri, 09 Apr 2010 21:57:38 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[addremove]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[hg]]></category>
		<category><![CDATA[mercurial]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=849</guid>
		<description><![CDATA[I&#8217;m a happy git user, but mercurial has a nifty command with no git equivalent: hg addremove. This command removes deleted files from the repository and also adds new files in the current directory. For me, at least, it&#8217;s a useful command that I&#8217;d like to have. Of course, git is nothing if not flexible, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a happy git user, but mercurial has a nifty command with no git equivalent: <code>hg addremove</code>. This command removes deleted files from the repository and also adds new files in the current directory. For me, at least, it&#8217;s a useful command that I&#8217;d like to have. Of course, git is nothing if not flexible, so let&#8217;s add this command! I&#8217;m using a suggestion for a script that I found <a href="http://ionrails.com/2009/08/07/git-commands-adding-and-committing-cheatsheet/">here</a>. Add the following lines to your <code>$HOME/.gitconfig</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>alias<span style="">&#93;</span></span>
    <span style="color: #000099;">addremove</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #000066; font-weight:bold;"><span style="color: #660066;"> !git add . &amp;&amp; git ls-files --deleted | xargs --no-run-if-empty git rm</span></span></pre></div></div>

<p>Tada&mdash;<code>git addremove</code> puts new files in the staging area and also removes deleted files. Now you don&#8217;t have to pay attention to your hipster friends who try to tell you how great mercurial is.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/849/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gmail: Please Bold the Hidden Labels Button when there are Unread Messages</title>
		<link>http://ctrl-c.us/blog/archives/643</link>
		<comments>http://ctrl-c.us/blog/archives/643#comments</comments>
		<pubDate>Tue, 21 Jul 2009 18:41:56 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[gmail labels]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=643</guid>
		<description><![CDATA[I love Gmail. It does everything I could possibly want an email client (web or desktop) to do. All of my non-junk email accounts are Gmail or are forwarded to Gmail accounts. That said, occasionally something occurs to me that would make my Gmail experience a little bit better. Sometimes these crop up as Labs [...]]]></description>
			<content:encoded><![CDATA[<p>I love Gmail. It does everything I could possibly want an email client (web or desktop) to do. All of my non-junk email accounts are Gmail or are forwarded to Gmail accounts. That said, occasionally something occurs to me that would make my Gmail experience a little bit better. Sometimes these crop up as Labs features, which I think is great, and is part of why I enjoy using Gmail so much.<br />
<span id="more-643"></span></p>
<div id="attachment_644" class="wp-caption alignright" style="width: 191px"><img class="size-full wp-image-644 " src="http://ctrl-c.us/blog/wp-content/uploads/2009/07/gmail-hidden-labels.jpg" alt="What if this were bold?" width="181" height="488" /><p class="wp-caption-text">What if this were bold?</p></div>
<p>There was a fairly recent change to Gmail, where infrequently used labels could be hidden. It lets you keep your important labels in the left sidebar, and if you have 10 more hidden labels you click a little button that says &#8220;10 more&#8221; to show a menu with all of these labels. Chats, Sent Mail, Drafts, Starred, All Mail, Spam, and Trash also behave like normal labels for this purpose. This is a nice feature, in my opinion. I usually don&#8217;t care about how many spam messages I have, so I can tuck it away from my normal view. More importantly, I use a lot of filters (50 or 60) to organize incoming mail under labels (I have 32 at the moment), but  most of these only occasionally receive mail and it&#8217;s nice to have them hidden too.</p>
<p>When a label has unread messages in, it turns bold. For me, this used to mean that to see if I had any new mail, I looked both at the top of the inbox and scanned the labels for a bold one. Now, to see if any of my hidden labels have new messages, I have to click the button to show these labels. The fix, in my opinion, is to turn the &#8220;25 more&#8221; text bold when hidden labels have unread messages (see picture). I know it&#8217;s small, but this would save compulsive email checkers like myself a fair amount of annoyance. (Yes, I&#8217;m aware that I can just move all my labels out of this menu, but I like this feature too much to stop using it because of this.)</p>
<p>A small caveat here is that if you move the Spam label into this menu, you will always have unread messages (unless you don&#8217;t get spam, or mark it as read). Spam could be ignored for the purposes of bolding the button, but this might be considered unexpected behavior. Regardless, I&#8217;m sure the smart people on the Gmail team could figure something out.</p>
<p>I couldn&#8217;t find anyone else talking about this. This may be due to poor Googling on my part, or perhaps nobody else really cares. Hopefully other people have thought about it and we can get the attention of somebody at Google.</p>
<p><strong>Edit:</strong> I found another person&#8217;s idea for a solution to this problem <a href="http://www.google.com/support/forum/p/gmail/thread?tid=1e61cf53d192cc1d&amp;hl=en">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/643/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Thoughts on Scala</title>
		<link>http://ctrl-c.us/blog/archives/589</link>
		<comments>http://ctrl-c.us/blog/archives/589#comments</comments>
		<pubDate>Tue, 14 Jul 2009 19:31:36 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=589</guid>
		<description><![CDATA[Over the past week or so I&#8217;ve been looking into the Scala programming language. If you aren&#8217;t familiar with it, Scala is one of a group of new-ish languages including Groovy, Clojure, and Nice (as well as new implementations of preexisting languages like Jython, JRuby, and Rhino) that run on the JVM (either interpreted or [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past week or so I&#8217;ve been looking into the <a href="http://www.scala-lang.org/">Scala</a> programming language. If you aren&#8217;t familiar with it, Scala is one of a group of new-ish languages including <a href="http://groovy.codehaus.org/">Groovy</a>, <a href="http://clojure.org/">Clojure</a>, and <a href="http://nice.sourceforge.net/">Nice</a> (as well as new implementations of preexisting languages like <a href="http://www.jython.org/">Jython</a>, <a href="http://jruby.codehaus.org/">JRuby</a>, and <a href="http://www.mozilla.org/rhino/">Rhino</a>) that run on the JVM (either interpreted or compiled to Java bytecode). Over perhaps the past decade the JVM has been increasingly seen as an attractive target platform for language development for several reasons:</p>
<ul>
<li> Implementation in Java instead of C</li>
<li> Features like garbage collection, portability, and a huge standard library come for free</li>
<li> Languages benefit from advances and optimizations in the JVM</li>
</ul>
<p>(This is part of a trend pointed out as Prediction #4 in <a href="http://steve.yegge.googlepages.com/ten-predictions">a list of 10 predictions</a> about software Steve Yegge made about 5 years ago.) In fact, targeting a virtual machine has almost become the <em>only</em> way to implement new, fancy languages with reasonable performance, portability, and implementation time.</p>
<p><span id="more-589"></span></p>
<p>Scala has an interesting mix of features. Some are taken from functional programming such as higher-order and anonymous functions, closures, partial function applications, and algebraic data types. It has pure OO (everything is an object, even functions) with mixins to compose functionality. Probably the most distinguishing feature of Scala is that it is statically typed (with some pretty nice type inference). For all this, it has a fairly small syntax (significantly smaller than Java, for instance).</p>
<p>As implied by the name, Scala tries to be as scalable as possible. Here are a couple of ways this is done (and I&#8217;m sure there are many others that I haven&#8217;t seen). Scala programs can be interpreted like scripts: if <code>hello.scala</code> contains</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;OHAI&quot;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>then this can be run by</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ scala hello.scala
OHAI</pre></div></div>

<p>They can also be compiled to class files. If <code>hello.scala</code> instead contains</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> HelloApp <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>arg<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;OHAI&quot;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>then we can do</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ scalac hello.scala
$ scala HelloApp
OHAI</pre></div></div>

<p>What really makes Scala scalable, though, is how its small but powerful syntax allows creation of new constructs that almost look like new syntax itself. In <a href="http://ftp.heanet.ie/mirrors/fosdem-video/2009/maintracks/scala.xvid.avi">a great talk</a> by Martin Odersky, the creator of Scala, he presents three nice examples of features that can be concisely added to Scala due to its extensibility: the <code>using</code> keyword, <code>break</code> and <code>continue</code>, and Erlang-style actors (the latter two are included in the Scala library).</p>
<p>I&#8217;m just getting started with Scala, but I&#8217;ve been very happy with its thorough documentation and the large collection of documents available to help learn the language. For instance, I have found Martin Odersky&#8217;s <a href="http://www.scala-lang.org/docu/files/ScalaByExample.pdf">Scala By Example</a> to be a great introduction. It&#8217;s nice to see a language that makes it so easy for beginners to figure out what&#8217;s going on (I&#8217;m looking at you, <a href="http://cobra-language.com/">Cobra</a>). I&#8217;m also having a good time reading <a href="http://www.amazon.com/Programming-Scala-Comprehensive-Step-step/dp/0981531601/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1247598681&amp;sr=8-1">&#8220;Programming in Scala&#8221;</a>, the comprehensive and extremely well-written language guide. Not only does it explain Scala very well, it is easy to read and includes tidbits such as this (<code>var</code>s are mutable variables; <code>val</code>s are immutable):</p>
<blockquote><p>If you’re coming from an imperative background, such as Java, C++, or C#, you may think of <code>var</code> as a regular variable and <code>val</code> as a special kind of variable. On the other hand, if you’re coming from a functional background, such as Haskell, OCaml, or Erlang, you might think of <code>val</code> as a regular variable and <code>var</code> as akin to blasphemy.</p></blockquote>
<p>Scala has some traction in the software community. The most well-known example, of course, is Twitter; last year they switched their message queuing from Rails to Scala, in their continuing quest to turn millions of dollars of venture capital into the hippest code possible. (Also there was a bit of a case of their backend exploding when things started heating up; it seems a bit telling that they switched to from Ruby to a language that has the first two syllables of &#8217;scalable&#8217; in its name.) There is also a Rails-like web framework for Scala, <a href="http://liftweb.net/">Lift</a>.</p>
<p>Given my great initial impression, I can only hope that Scala becomes more widespread. I might post more thoughts on Scala quirks and features (as well as my improved vim indent file) later on.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/589/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://ftp.heanet.ie/mirrors/fosdem-video/2009/maintracks/scala.xvid.avi" length="185676474" type="video/x-msvideo" />
		</item>
		<item>
		<title>Keyboard Remapping in Windows XP</title>
		<link>http://ctrl-c.us/blog/archives/302</link>
		<comments>http://ctrl-c.us/blog/archives/302#comments</comments>
		<pubDate>Tue, 02 Jun 2009 17:36:55 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=302</guid>
		<description><![CDATA[When I found that I had to use Windows for work, the first thing I did was to install gvim and my usual host of plugins. The next thing was to remap caps lock and escape. I switch these when I&#8217;m using Linux because I don&#8217;t really ever use caps lock, but I use escape [...]]]></description>
			<content:encoded><![CDATA[<p>When I found that I had to use Windows for work, the first thing I did was to install <a href="http://www.vim.org/download.php">gvim</a> and my usual host of plugins. The next thing was to remap caps lock and escape. I switch these when I&#8217;m using Linux because I don&#8217;t really ever use caps lock, but I use escape all the time (especially in vim). In Linux it&#8217;s a simple <code>.Xmodmap</code> entry:</p>
<p><span id="more-302"></span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">remove Lock = Caps_Lock
add Lock = Escape
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock</pre></div></div>

<p>In Windows, it&#8217;s a <a href="http://www.usnetizen.com/fix_capslock.php">bit trickier</a>. You have to add a registry key, and look up the hex codes for the escape and caps lock keys. I found a lot of premade registry files for switching other keys, but apparently I&#8217;m the only person who swaps escape with caps lock. Anyway, you just need to paste the following in a file, name it something with a <code>.reg</code> extension, and then double-click it to import the key into the registry.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Windows Registry Editor Version 5.00
&nbsp;
[HKEY_CURRENT_USER\Keyboard Layout]
&quot;Scancode Map&quot;=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,01,00,01,00,3a,00,00,00,00,00</pre></div></div>

<p>This only applies the change to the current user, and you have to log out and log back in for the changes to take effect. You have to change the <code>[HKEY_CURRENT_USER\Keyboard Layout]</code> to <code>[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]</code> if you want it to apply to all users.</p>
<p>While you&#8217;re at it, you can find a small utility to allow for alt + left-click window dragging and alt + right-click resizing <a href="http://corz.org/windows/software/accessories/KDE-resizing-moving-for-XP-or-Vista.php">here</a> and another for background window scrolling <a href="http://www.thefreewindows.com/?p=1387">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/302/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interfacing C and Python 3 using SWIG</title>
		<link>http://ctrl-c.us/blog/archives/99</link>
		<comments>http://ctrl-c.us/blog/archives/99#comments</comments>
		<pubDate>Mon, 27 Apr 2009 09:06:17 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[swig]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/?p=99</guid>
		<description><![CDATA[I like Python, and I use it for almost all of my personal projects and whenever I can get away with it in school. Sometimes, however, (rarely, these days) you&#8217;re looking for raw speed. Python, alas, is not the fastest kid on the block, and at these times you might have to turn to a [...]]]></description>
			<content:encoded><![CDATA[<p>I like Python, and I use it for almost all of my personal projects and whenever I can get away with it in school. Sometimes, however, (rarely, these days) you&#8217;re looking for raw speed. Python, alas, is not the fastest kid on the block, and at these times you might have to turn to a different language to give you the necessary speed. (Another option you might consider first is <a title="Psyco" href="http://psyco.sourceforge.net/" target="_blank">Psyco</a>.) In these situations it is often the case that only particular operations or functions really have to be fast. Even if you want to write the whole piece of software in C, you might like to use Python to write the front end. There are a lot of different ways to do this, but my favorite is <a title="Simplified Wrapper and Interface Generator" href="http://www.swig.org/" target="_blank">SWIG</a>, because it&#8217;s easy and I&#8217;m lazy.</p>
<p><span id="more-99"></span>SWIG is a great tool that supports a host of high-level languages; I&#8217;ve only ever used it with Python. At the other end, you get your choice of C and C++—crappy languages aren&#8217;t really my thing, so this tutorial will be about C. I&#8217;m going to use Python 3 because all the cool kids live on the bleeding edge. This requires SWIG 1.3.37 or better; if you&#8217;re on Ubuntu like me, you&#8217;re going to have to go grab it from the website and compile it yourself (I&#8217;m using 1.3.39). Also, remember that you need the header files for python installed (python3-dev on Ubuntu). If you&#8217;re too lazy to copy-paste, you can download the files <a href='http://ctrl-c.us/wordpress/wp-content/uploads/2009/04/swig_intro.tar.gz'>here</a>.</p>
<p>We&#8217;ll start out with the obligatory hello world program just to test that SWIG is set up correctly. First make a C function that prints the text and name it <code>hello.c</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we&#8217;ll get SWIG to generate a wrapper for us so that we can call it in Python. Make a file called hello.i and put this in it (my code highlighting isn&#8217;t feeling very confident about SWIG files, sorry):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>module hello
<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">void</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>This file is the interface file, and it tells SWIG what functions and data structures are going to be shared between C and Python. Now we&#8217;ll have SWIG process the interface file to make a wrapper for a python module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ swig <span style="color: #660033;">-python</span> hello.i</pre></div></div>

<p>This should generate two files: <code>hello.py</code> and <code>hello_wrap.c</code>. Compile the C files:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-c</span> <span style="color: #660033;">-fPIC</span> hello_wrap.c hello.c -I<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>python3.0 -I<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python3.0</pre></div></div>

<p>Make sure that the direcotories are correct. Unfortunately, gcc generates a warning when I compile, which is bothersome as I&#8217;m a <code>-Werror</code> kind of guy. The <code>-fPIC</code> flag is necessary, because we&#8217;re making a shared library the python module is going to use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ld</span> <span style="color: #660033;">-shared</span> hello.o hello_wrap.o <span style="color: #660033;">-o</span> _hello.so</pre></div></div>

<p>If that works, you are ready to fire up Python and try it. At a Python3 prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> hello <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> hello<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
Hello, World<span style="color: #66cc66;">!</span></pre></div></div>

<p>Easy, right? Now I&#8217;ll show you some of the features that I most frequently use. First, we&#8217;ll pass some data through by wrapping a C function to multiply integers. Make a file called <code>swig_intro.h</code>, and put in the following declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include&lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> mult<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then in <code>swig_intro.c</code> we can write</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;swig_intro.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> mult<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> a <span style="color: #339933;">*</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, the interface file, <code>swig_intro.i</code>, will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>module swig_intro
<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">#include &quot;swig_intro.h&quot;</span>
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> mult<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To make things easier, let&#8217;s write a makefile for this little project:</p>

<div class="wp_syntax"><div class="code"><pre class="make" style="font-family:monospace;">TARGET     <span style="color: #004400;">=</span> swig_intro
CC         <span style="color: #004400;">=</span> gcc
CFLAGS     <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>g <span style="color: #004400;">-</span>fPIC <span style="color: #004400;">-</span>Wall <span style="color: #004400;">-</span>Wextra
HEADERS    <span style="color: #004400;">=</span> swig_intro<span style="color: #004400;">.</span>h
SRCS       <span style="color: #004400;">=</span> swig_intro<span style="color: #004400;">.</span>c
INCLUDES   <span style="color: #004400;">=</span>
INTERFACE  <span style="color: #004400;">=</span> swig_intro<span style="color: #004400;">.</span>i
SWIG       <span style="color: #004400;">=</span> swig
&nbsp;
LDSHARED   <span style="color: #004400;">=</span> ld
CSHARED    <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>shared
&nbsp;
OBJS       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>SRCS<span style="color: #004400;">:.</span>c<span style="color: #004400;">=.</span>o<span style="color: #004400;">&#41;</span>
&nbsp;
IWRAP      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>INTERFACE<span style="color: #004400;">:.</span>i<span style="color: #004400;">=</span>_wrap<span style="color: #004400;">.</span>i<span style="color: #004400;">&#41;</span>
ISRCS      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>IWRAP<span style="color: #004400;">:.</span>i<span style="color: #004400;">=.</span>c<span style="color: #004400;">&#41;</span>
IOBJS      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>IWRAP<span style="color: #004400;">:.</span>i<span style="color: #004400;">=.</span>o<span style="color: #004400;">&#41;</span>
&nbsp;
PYTHON_INCLUDE<span style="color: #004400;">=</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span><span style="color: #004400;">/</span>python3<span style="color: #004400;">.</span>0 <span style="color: #004400;">-</span>I<span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span>lib<span style="color: #004400;">/</span>python3<span style="color: #004400;">.</span>0
PYTHON_SO     <span style="color: #004400;">=</span> <span style="color: #004400;">.</span>so
&nbsp;
all<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SRCS</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SWIG</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>python <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INTERFACE</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CCSHARED</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ISRCS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HEADERS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SRCS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INCLUDES</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PYTHON_INCLUDE</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LDSHARED</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CSHARED</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">IOBJS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o _<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PYTHON_SO</span><span style="color: #004400;">&#41;</span>
&nbsp;
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*</span>_wrap<span style="color: #004400;">*</span> <span style="color: #004400;">*</span>~ <span style="color: #004400;">.</span>~<span style="color: #004400;">*</span> <span style="color: #004400;">*.</span>pyc
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*.</span>o <span style="color: #004400;">*.</span>so <span style="color: #004400;">*.</span>so</pre></div></div>

<p>If that works, you can launch a Python3 shell (from now on I&#8217;ll assume you do the import first):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> swig_intro <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> mult<span style="color: black;">&#40;</span>-<span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
-<span style="color: #ff4500;">50</span></pre></div></div>

<p>Behind the scenes, SWIG uses something called typemaps to specify how each type is from Python to C and vice versa. In many cases, such as this, we don&#8217;t need to worry about that at all, because SWIG has default typemaps for all the fundamental datatypes. For example, you might wonder what SWIG does with a C struct. Go ahead and add this definition to the header file and the interface file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> Person <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>first_name<span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>last_name<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> birth_year<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> Person<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> print_person<span style="color: #009900;">&#40;</span>Person <span style="color: #339933;">*</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and put the following function into the <code>.c</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> print_person<span style="color: #009900;">&#40;</span>Person <span style="color: #339933;">*</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>p<span style="color: #339933;">-&gt;</span>first_name <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>p<span style="color: #339933;">-&gt;</span>last_name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;The person is not properly defined.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s %s was born in %d.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>first_name<span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>last_name<span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>birth_year<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now recompile and try this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> p = Person<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p.<span style="color: black;">first_name</span> = <span style="color: #483d8b;">&quot;Paul&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p.<span style="color: black;">last_name</span> = <span style="color: #483d8b;">&quot;Black&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p.<span style="color: black;">birth_year</span> = <span style="color: #ff4500;">1977</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> print_person<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
Paul Black was born <span style="color: #ff7700;font-weight:bold;">in</span> 1977.</pre></div></div>

<p>As you can see, the the struct Person was wrapped in a very simple Python class with the three public data fields <code>first_name</code>, <code>last_name</code>, and <code>birth_year</code>.</p>
<p>Next, let&#8217;s see how to use typemaps ourselves to pass a more complex structure from Python to C. We&#8217;re going to make a (rather contrived) C function that will return the product of a list of integers. We&#8217;ll do this by turning a Python list into a C struct that specifies an int array and a size. Add the following code to the header and interface files:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> IntList <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> size<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> IntList<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> product<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the <code>.c</code> file, add the function:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> product<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>l<span style="color: #339933;">-&gt;</span>size <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #993333;">int</span> ret_val <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ret_val <span style="color: #339933;">*=</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> ret_val<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we need to define the typemap in the <code>.i</code> file. This isn&#8217;t too difficult; we just make use of several functions from the Python C API:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span> IntList <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyList_Check<span style="color: #009900;">&#40;</span>$input<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PyErr_SetString<span style="color: #009900;">&#40;</span>PyExc_TypeError<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Not a list.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #993333;">int</span> size <span style="color: #339933;">=</span> PyList_Size<span style="color: #009900;">&#40;</span>$input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #0000dd;">1</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>IntList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>list <span style="color: #339933;">=</span> list<span style="color: #339933;">;</span>
    $<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>size <span style="color: #339933;">=</span> size<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PyObject <span style="color: #339933;">*</span>item <span style="color: #339933;">=</span> PyList_GetItem<span style="color: #009900;">&#40;</span>$input<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyInt_Check<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            PyErr_SetString<span style="color: #009900;">&#40;</span>PyExc_TypeError<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;List item is not an integer.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            free<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            free<span style="color: #009900;">&#40;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> PyInt_AsLong<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>freearg<span style="color: #009900;">&#41;</span> IntList <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This also illustrates how to tell SWIG to free your objects and how to handle errors. Now we can see it in action:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> swig_intro <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> product<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
<span style="color: #008000;">TypeError</span>: Not a <span style="color: #008000;">list</span>.
<span style="color: #66cc66;">&gt;&gt;&gt;</span> product<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;five&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
<span style="color: #008000;">TypeError</span>: List item <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> an integer.
<span style="color: #66cc66;">&gt;&gt;&gt;</span> product<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">7</span>,<span style="color: #ff4500;">9</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">945</span></pre></div></div>

<p>As you would expect, transferring data the other way is similar. For an example, let&#8217;s make a function that takes a list and returns a dictionary mapping the list elements to their squares. Add these definitions to the header and interface files:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> IntMap <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> size<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list1<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list2<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> IntMap<span style="color: #339933;">;</span>
&nbsp;
IntMap <span style="color: #339933;">*</span>square<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The actual C function will take a pointer to an <code>IntList</code> and return a pointer to an <code>IntMap</code>, allocating space for a second list as needed. The conversion to a <code>dict</code> will be specified with an out typemap. In the <code>.c</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">IntMap <span style="color: #339933;">*</span>square<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    IntMap <span style="color: #339933;">*</span>ret_map <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IntMap <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>IntMap<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ret_map<span style="color: #339933;">-&gt;</span>size <span style="color: #339933;">=</span> l<span style="color: #339933;">-&gt;</span>size<span style="color: #339933;">;</span>
    ret_map<span style="color: #339933;">-&gt;</span>list1 <span style="color: #339933;">=</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #339933;">;</span>
    ret_map<span style="color: #339933;">-&gt;</span>list2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> l<span style="color: #339933;">-&gt;</span>size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ret_map<span style="color: #339933;">-&gt;</span>list2<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> ret_map<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Add the typemap to the <code>.i</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span> IntMap <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    $result <span style="color: #339933;">=</span> PyDict_New<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PyObject <span style="color: #339933;">*</span>key <span style="color: #339933;">=</span> PyInt_FromLong<span style="color: #009900;">&#40;</span>$<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>list1<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        PyObject <span style="color: #339933;">*</span>val <span style="color: #339933;">=</span> PyInt_FromLong<span style="color: #009900;">&#40;</span>$<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>list2<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        PyDict_SetItem<span style="color: #009900;">&#40;</span>$result<span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>freearg<span style="color: #009900;">&#41;</span> IntMap <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntMap <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>list2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntMap <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And we&#8217;re done!</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> square<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,-<span style="color: #ff4500;">12</span>,<span style="color: #ff4500;">60000</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#123;</span><span style="color: #ff4500;">60000</span>: <span style="color: #ff4500;">3600000000</span>, <span style="color: #ff4500;">1</span>: <span style="color: #ff4500;">1</span>, -<span style="color: #ff4500;">12</span>: <span style="color: #ff4500;">144</span><span style="color: black;">&#125;</span></pre></div></div>

<p>I&#8217;ve only shown some of the more useful capabilities of SWIG that I find myself using a lot. For more information, consult the official <a href="http://www.swig.org/Doc1.3/Python.html">SWIG docs</a>. Unfortunately, they&#8217;re not entirely complete, so it is sometime easier to get answers by just browsing the built-in SWIG interface files to see how the typemaps work.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/99/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>64-bit Flash Player!</title>
		<link>http://ctrl-c.us/blog/archives/92</link>
		<comments>http://ctrl-c.us/blog/archives/92#comments</comments>
		<pubDate>Mon, 17 Nov 2008 06:47:39 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/?p=92</guid>
		<description><![CDATA[Today Adobe is releasing an alpha version of a 64-bit Flash player for Linux. I cannot explain how happy this makes me. I have been dealing with shitty 32-bit Flash players running on 32-bit emulation in 64-bit browsers in Linux for about the past five years, and it has caused me nothing but endless annoyance [...]]]></description>
			<content:encoded><![CDATA[<p>Today <a title="Press release" href="http://news.cnet.com/8301-1001_3-10097931-92.html" target="_blank">Adobe is releasing an alpha version of a 64-bit Flash player for Linux</a>. I cannot explain how happy this makes me. I have been dealing with shitty 32-bit Flash players running on 32-bit emulation in 64-bit browsers in Linux for about the past five years, and it has caused me nothing but endless annoyance and frustration. Half the time I have to refresh a page a few times because Flash keeps dying, and even when it manages to play it&#8217;s a complete coin toss as to whether there will be sound. With my current setup I basically have to close Amarok to play a Flash video if I want to hear sound. Adobe is years late to the party with this one, but better late than never, right?</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/92/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Facebook Chat, Making My Life More Complicated</title>
		<link>http://ctrl-c.us/blog/archives/9</link>
		<comments>http://ctrl-c.us/blog/archives/9#comments</comments>
		<pubDate>Mon, 11 Aug 2008 07:00:00 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/index.php?page=articles&amp;article=facebook_chat_making</guid>
		<description><![CDATA[When Facebook added the chat feature a few months ago, I found it to be entirely unnecessary. All the people with whom I wished to chat were already in my MSN, AIM, and GoogleTalk contact lists, which are all tied together by Pidgin.

(As an interesting and only slightly related aside, the Facebook chat was implemented [...]]]></description>
			<content:encoded><![CDATA[<p>When Facebook added the chat feature a few months ago, I found it to be entirely unnecessary. All the people with whom I wished to chat were already in my MSN, AIM, and GoogleTalk contact lists, which are all tied together by Pidgin.</p>
<p><span id="more-9"></span></p>
<p>(As an interesting and only slightly related aside, the Facebook chat was implemented on an open-source framework called <a href="http://incubator.apache.org/thrift/">Thrift</a> developed in-house by Facebook engineers, and is in part written in Erlang. The <a href="http://www.facebook.com/note.php?note_id=14218138919&amp;id=9445547199&amp;index=2">developers&#8217; notes on this</a> are an interesting read.)</p>
<p>Unfortunately, I have since found a handful of Facebook friends who like to communicate using the chat feature, but not using conventional chat. I don&#8217;t usually keep a Facebook window open, so I don&#8217;t usually get to talk with them this way.</p>
<p>I had a similar problem a long time ago when Google first added chat to GMail. I don&#8217;t like using my browser to chat (because, among other reasons, a dedicated client is a lot better at it). As soon as I was able to add Google chat to Pidgin, though, I started using it daily.</p>
<p>Today, though I found a similar solution for my Facebook dilemma: <a href="http://code.google.com/p/pidgin-facebookchat/">a Facebook Chat plugin for Pidgin</a>. It is supposed to be able to integrate my Facebook chat buddies and status, among other things. Sounds great!</p>
<p>…except that it makes Pidgin segfault for me. Oops. Maybe after a few patches?</p>
<p><strong>Update:</strong> How about a Pidgin plugin for Twitter? I would totally use Twitter if I didn&#8217;t have to go to a website to use it (I don&#8217;t have SMS on my phone).</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/9/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
