[gdal-dev] Possible SWIG Python bug

Even Rouault even.rouault at mines-paris.org
Sat Dec 19 10:47:02 EST 2009


Leonardo,

This part of swig/python/osgeo/__init__.py is in fact a copy & paste of 
code in osgeo/gdal.py, which is a SWIG generated file.
It was definitely a bug in SWIG 1.3.39 that was fixed in 1.3.40 and in 
that latter version, the generated code looks like :

            try:
                _mod = imp.load_module('_gdal', fp, pathname, description)
            finally:
                fp.close()
            return _mod

So, I've fixed similarly swig/python/osgeo/__init__.py in r18350 and now 
a more meaningful error is reported

Thanks for having reported that.

Best regards,

Even

> I'm working on creating an autotest for OGR GTM driver, I just started
> the work and I faced a little problem with the script run_all.py.
> I've already solved the problem, however, since I'm not a Python
> expert, I'm reporting the problem that I had. If it is not a bug,
> please excuse me.
>
> I've checked out and installed gdal from source. I've checked out
> autotest scripts, too.
> When I ran run_all.py I got the following message:
>
> Traceback (most recent call last):
>   File "run_all.py", line 30, in <module>
>     import gdaltest
>   File "pymod/gdaltest.py", line 35, in <module>
>     from osgeo import gdal
>   File "/usr/local/lib/python2.6/dist-packages/osgeo/__init__.py",
> line 21, in <module>
>     _gdal = swig_import_helper()
>   File "/usr/local/lib/python2.6/dist-packages/osgeo/__init__.py",
> line 20, in swig_import_helper
>     return _mod
> UnboundLocalError: local variable '_mod' referenced before assignment
>
> I took a look at the file
> /usr/local/lib/python2.6/dist-packages/osgeo/__init__.py and I saw
> that I was having an exception, but the error message was not giving
> me any hint. I think there is a bug in __init__.py code, because _mod
> is being used without being defined when the exception is raised.
>
>
> Take a look at these lines:
>
>             try:
>                 _mod = imp.load_module('_gdal', fp, pathname, description)
>             finally:
>                 fp.close()
>                 return _mod
>
> Is it really a bug? Is it possible to define _mod before the try and
> finally block?
>
> In time, I solved the problem by making LD_LIBRARY_PATH point to
> /user/local/lib. So I'm not having the exception anymore and the
> problem was solved.
>
> Thanks a lot
>   




More information about the gdal-dev mailing list