#!/usr/bin/env python #@+leo-ver=4 #@+node:@file hackpyrexc.py #@@first """ Quick/dirty to work around a bug in pyrex. Does a test compile of a pyrex-generated file, grabs the errors relating to pyrex-omitted decs, and inserts these decs into the c file. """ # set this here if you want so you don't have to keep specifying # it as an argument pyinclude = "/usr/include/python2.3" import sys, os, commands, re reSym = re.compile(".*(__pyx_k[0-9]+p).*") if len(sys.argv) not in [2,3]: print "Usage: %s pyrexgeneratedfile.c [/path/to/python/include]" % sys.argv[0] sys.exit(1) filename = sys.argv[1] if len(sys.argv) == 3: pyinclude = sys.argv[2] # do the test compile, gather errors testCompileCmd = "gcc -c -shared %s -I%s 2>&1|grep \"undeclared here\"" % (filename, pyinclude) lines = commands.getoutput(testCompileCmd).split("\n") # extract the symbols from the raw error lines finds = [reSym.findall(l) for l in lines] #print finds try: syms = [f[0] for f in finds] except: print finds syms = [] # convert into a set of the missing declarations decs = ["static PyObject *%s;\n" % sym for sym in syms] # and insert these decs into the .c file finals = [] found = 0 lines = file(filename).readlines() for line in lines: finals.append(line) if (not found) and line.startswith("#include \"Python.h\""): finals.extend(decs) # slot the decs in after the '#include python.h' found = 1 file(filename, "w").write("".join(finals)) #@-node:@file hackpyrexc.py #@-leo