[Distutils] setuptools >= 20.2 may break applications using pyparsing

Sebastian Krause sebastian at realpath.org
Mon May 9 08:37:32 EDT 2016


Hi,

I'm developing an application that uses pyparsing and after upgrading 
setuptools to the newest version I noticed some tests failing. In my 
main parser module I define an alias for the ParseBaseException which I 
then use in other parts of the application to catch exceptions:

# definition of the ParseException
ParseException = pyparsing.ParseBaseException

# importing this alias in another module
from ...filterreader.parser import ParseException

Now my tests were failing because the ParseException was never actually 
caught. Some investigation by comparing the id() of the objects showed 
that the ParseException alias was no longer the same object as 
pyparsing.ParseBaseException. This was because the module "pyparsing" at 
the time of the alias definition was not the same "pyparsing" module 
which is later used for parsing. Looking at sys.module I can see that I 
have two pyparsing modules:

pyparsing: <module 'pyparsing' from 
'D:\\Project\\python34.zip\\pyparsing.pyc'>
pkg_resources.extern.pyparsing: <module 'pyparsing' from 
'D:\\Project\\python34.zip\\pyparsing.pyc'>

At the time of the alias definition id(pyparsing) is equal to the id() 
of pkg_resources.extern.pyparsing. When I later import pyparsing I get 
the other module. This whole problem only happens when I use the 
application packaged by cx_Freeze, so maybe some kind of race condition 
happens when importing from a ZIP file. I'm using 64 bit Python 3.4.4 on 
Windows.

The first version of setuptools where I can see this problem is 20.2, 
until 20.1 everything is fine. Looking at the source I can see that 
starting with 20.2 setuptools also includes its own pyparsing copy, so 
most likely that change is related to my problem.

Is there a simple way in which I can guarantee that there will only ever 
be a single "pyparsing" module in my application? Of course I could just 
stop using the alias and use the pyparsing exceptions directly, but I 
feel a bit uneasy when a module just changes its identity at some point 
between imports.

Sebastian


More information about the Distutils-SIG mailing list