Tag Archives: python

Python and SWIG: local variable ‘_mod’ referenced before assignment

I was doing some stuff that requires importing C++ libraries to use in Python, and that error came up.

I’m using Stackless, so I first thought that was the case. Eventually it turned out to be related to the build configuration of the program I was compiling, FIFE.

The thing here is, SWIG generated a wrapper for that library that had a bad. It was at the very top of the wrapper file (fife.py):

8  from sys import version_info
9  if version_info >= (2,6,0):
10     def swig_import_helper():
11         from os.path import dirname
12         import imp
13         fp = None
14         try:
15             fp, pathname, description = imp.find_module('_fife', [dirname(__file__)])
16         except ImportError:
17             import _fife
18             return _fife
19         if fp is not None:
20             try:
21                 _mod = imp.load_module('_fife', fp, pathname, description)
22             finally:
23                 fp.close()
24                 return _mod
25     _fife = swig_import_helper()
26     del swig_import_helper
27 else:
28     import _fife
29 del version_info

The bad thing here is that there’s no “except:” where it should be. When I added one on line 22, it caught an exception. Technically, the exception should be passed to a higher-level “try:” (on line 14). However:

1. “finally:” is executed in all cases, exception or no exception.
2. The upper-level “try:” doesn’t have a default exception handler.

I didn’t dig deeper into this, because I have no time at the moment.

Stackless-Python in Netkit VM

I needed to do this, since I’m running a x86_64 machine, and netkit emulates a 32-bit one. Thus external programs cannot be run inside Netkit, which, surprise, is also a security feature.

1. Install netkit:

# yaourt netkit

2. Use the second method described in the Netkit FAQ to update packages in the VM image:

# mkdir /mnt/nkft
# mount -o loop,offset=32768 $NETKIT_HOME/fs/netkit-fs /mnt/nkfs
# chroot /mnt/nkfs

3. Install the build tools:

# apt-get install gcc g++ make automake autoconf libc6-dev binutils

4. Then proceed with Stackless (there seems to be no Debian package yet). First, install dependencies:

# apt-get install bzip2 openssl

Then:

# wget http://www.stackless.com/binaries/stackless-262-export.tar.bz2
# tar xf stackless-262-export.tar.bz2
# cd stackless-2.6.2

;; I tried building specifying “–with-threads –enable-shared” to
;; configure and it didn’t work, with “PIC register ebx clobbered in asm”.
;; Adding “–stackless-fewerregisters” might help if
;; you really need that. Thanks to Lloyd Weehuizen.
# ./configure --prefix=/usr
# make
# make install

5. After all goes well, as it always does, press CTRL+D. Then:

# umount /mnt/nfks

Simple script to see file sizes

#!/usr/bin/python
# directory size - show it

import os, sys

if len(sys.argv) < 2:
    os.system("du -sh *")
    print "-----"
    os.system("du -sh .")
else:
    for arg in sys.argv[1:]:
        cmd = "du -sh \"%s\"" % arg
        os.system(cmd)