[Pyrex] Unused variables

David M. Cooke cookedm at physics.mcmaster.ca
Fri Dec 2 00:36:35 CET 2005


Jim Fulton <jim at zope.com> writes:

> I'm having a problem with my first Pyrex project. Pyrex is generating
> code with unused labels and variables and with static declarations for
> things that aren't defined.  I wasn't sure if this was normal, so
> I tried building the demos and got much the same behavior:
>
> jim at ds9:~/src/python/Pyrex-0.9.3.1/Demos$ /usr/local/python/2.4/bin/python Setup .py build_ext --inplace
> running build_ext
> building 'primes' extension
> creating build
> creating build/temp.linux-i686-2.4
> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPI C -I/usr/local/python/2.4/include/python2.4 -c primes.c -o build/temp.linux-i686 -2.4/primes.o
> primes.c: In function `__pyx_f_6primes_primes':
> primes.c:137: warning: label `__pyx_L4' defined but not used
> primes.c:108: warning: label `__pyx_L6' defined but not used
> primes.c:98: warning: label `__pyx_L5' defined but not used
> primes.c:89: warning: label `__pyx_L3' defined but not used
> primes.c: At top level:
> primes.c:12: warning: `__Pyx_UnpackItem' declared `static' but never defined

[snip]

> Is the generation of unused variables and declarations for unused and
> undefined variables normal?  Are there any plans to fix this?  While this
> seems to just generate warnings, it sure would be nice to avoid them.

see my patches at
http://lists.copyleft.no/pipermail/pyrex/2005-October/001452.html
and its followup,
http://lists.copyleft.no/pipermail/pyrex/2005-October/001453.html

The labels were relatively easy, as they're all forward jumps. One has
to be emitted only if the corresponding goto has already been emitted.

These patches don't handle the "routine declared static but not used",
though. For that, I think the code generation should be done in such a
way that you can go back and declare needed routines. Simply using a
few cStringIOs instead of a real file, split by section, could do
this. Then, you'd keep track of which utitlity functions are used, and
at the end you could output them, and their declarations, at the right
places in the file.

For the mtrand module in scipy, which is written using Pyrex, I've
whipped up this script to remove unused utility functions. With the
above patches, and the below postprocessing, we have one warning (an
unused variable in a tp_free slot, I think), which I've just ignored
for now. This also fixes up a miscast Pyrex was doing that we were
having trouble with.

#!/usr/bin/env python
import sys
import re
import os

unused_internal_funcs = ['__Pyx_PrintItem',
                         '__Pyx_PrintNewline',
                         '__Pyx_ReRaise',
                         '__Pyx_GetExcValue',
                         '__Pyx_ArgTypeTest',
                         '__Pyx_TypeTest',
                         '__Pyx_SetVtable',
                         '__Pyx_GetVtable',
                         '__Pyx_CreateClass']

if __name__ == '__main__':
    os.system('pyrexc mtrand.pyx')
    mtrand_c = open('mtrand.c', 'r')
    processed = open('mtrand_pp.c', 'w')
    unused_funcs_str = '(' + '|'.join(unused_internal_funcs) + ')'
    uifpat = re.compile(r'static \w+ \*?'+unused_funcs_str+r'.*/\*proto\*/')
    for linenum, line in enumerate(mtrand_c):
        m = re.match(r'^(\s+arrayObject\w*\s*=\s*[(])[(]PyObject\s*[*][)]',
                     line)
        if m:
            line = '%s(PyArrayObject *)%s' % (m.group(1), line[m.end():])
        m = uifpat.match(line)
        if m:
            line = ''
        m = re.search(unused_funcs_str, line)
        if m:
            print >>sys.stderr, \
                "%s was declared unused, but is used at line %d" % (m.group(),
                                                                    linenum+1)
        processed.write(line)
    mtrand_c.close()
    processed.close()
    os.rename('mtrand_pp.c', 'mtrand.c')


-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca



More information about the Pyrex mailing list