Ticket #4693 (closed defect: fixed)

Opened 13 months ago

Last modified 13 months ago

Improve support for python3

Reported by: antonio Owned by: hobu
Priority: normal Milestone: 1.10.0
Component: PythonBindings Version: svn-trunk
Severity: normal Keywords:
Cc:

Description

There seems to be still some issue with python 3 support:

Iteration OGR Layers:

In [60]: for f in layer:
   ....:     print(f)
   ....:     
----------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/antonio/<ipython-input-60-57e8d963b9b0> in <module>()
----> 1 for f in layer:
      2     print(f)
      3 

TypeError: iter() returned non-iterator of type 'Layer'

GDAL sub-datasets:

In [63]: ds1.GetSubDatasets()
--------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/antonio/<ipython-input-63-674e513f6408> in <module>()
----> 1 ds1.GetSubDatasets()

/usr/lib/python3/dist-packages/osgeo/gdal.py in GetSubDatasets(self)
    685 
    686         i = 1
--> 687         while sd.has_key('SUBDATASET_'+str(i)+'_NAME'):
    688             sd_list.append( ( sd['SUBDATASET_'+str(i)+'_NAME'],
    689                               sd['SUBDATASET_'+str(i)+'_DESC'] ) )

AttributeError: 'dict' object has no attribute 'has_key'

sys.maxint is gone (and also a missing sys import)

In [64]: layer[0:1e500]
------------------------------------------------------
NameError                                 Traceback (most recent call last)
/home/antonio/<ipython-input-64-ef4e5319f766> in <module>()
----> 1 l[0:1e500]

/usr/lib/python3/dist-packages/osgeo/ogr.py in __getitem__(self, value)
   1533         if isinstance(value, slice):
   1534             output = []
-> 1535             if value.stop == sys.maxint:
   1536 
   1537 

NameError: global name 'sys' is not defined

Please find attached four patches that should fix issues described above. I would appreciate a lot the review of another developer before committing.

Also there are some remaining pyflakes warnings. I don't know how to address them exactly:

$ pyflakes "ogr.py"
ogr.py:760: undefined name 'OGRError'
ogr.py:781: redefinition of function 'DeleteLayer' from line 452

$ pyflakes "gdal.py"
gdal.py:770: redefinition of function 'WriteRaster' from line 733
gdal.py:823: redefinition of function 'BeginAsyncReader' from line 743
gdal.py:1089: local variable 'shape' is assigned to but never used

Attachments

0001-Fix-pyflakes-warnings.patch Download (5.8 KB) - added by antonio 13 months ago.
0002-Improve-compatibility-with-python3.patch Download (7.0 KB) - added by antonio 13 months ago.
0003-Do-not-shadow-python-builtins.patch Download (3.5 KB) - added by antonio 13 months ago.
0004-Automatically-run-2to3.patch Download (2.8 KB) - added by antonio 13 months ago.
2to3_osgeo.diff Download (5.4 KB) - added by antonio 13 months ago.

Change History

Changed 13 months ago by antonio

Changed 13 months ago by antonio

Changed 13 months ago by antonio

Changed 13 months ago by antonio

Changed 13 months ago by rouault

  • 0001-Fix-pyflakes-warnings.patch looks good. Please commit
  • 0002-Improve-compatibility-with-python3.patch : there's an update of a generated file (swig/python/osgeo/gdal.py), but not the matching change in the source file (swig/include/python/gdal_python.i). Otherwise looks good
  • 0003-Do-not-shadow-python-builtins.patch looks good. Please commit
  • 0004-Automatically-run-2to3.patch : I must confess I'm a bit afraid by 2to3 automatically running without human checking...

Changed 13 months ago by hobu

As long as 2to3 is only being run in the instance where the user is trying to install into 3, I don't see a problem, as this would still be better than doing nothing.

Changed 13 months ago by antonio

Hi Even, hi Howard, I tried reduce to the minimum the number of fixers used by 2to3. Probably some other one can be stripped out to reduce a little more the amount of changes that 2o3 performs automatically.

Probably it is possible to fix most of the issues without using 2to3, but it will surely need a lot of explicit checks on python version.

If you want I can write an alternative patch so that you can choose the one you like more.

Anyway the solution with 2to3 is by far better IMHO.

Changed 13 months ago by rouault

Antonio, please go ahead with the 2to3 stuff if you and Howard feel confident with that. It would be good if you could do a quick check of the changes they cause with the current state of the binding files, just to be sure. But, as the autotest suite mainly passes with python3, I would tend to think that there are not so many places where changes are acutally necessary.

Changed 13 months ago by antonio

Changed 13 months ago by antonio

  • status changed from new to closed
  • resolution set to fixed

Even, changes performed by 2to3 are in the attached 2to3_osgeo.diff.

Fixed in 24539, closing.

Changed 13 months ago by rouault

  • status changed from closed to reopened
  • resolution fixed deleted
  • milestone changed from 1.9.2 to 2.0.0

Reopening. r24539 breaks building with python3, whereas it worked before :

$ python3.2 setup.py build
Traceback (most recent call last):
  File "setup.py", line 75, in <module>
    from setuptools import setup
ImportError: No module named setuptools

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 83, in <module>
    from distutils.command.build_py import build_scripts_2to3 as build_scripts
ImportError: cannot import name build_scripts_2to3

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 86, in <module>
    from distutils.command.build_py import build_scripts
ImportError: cannot import name build_scripts

Changed 13 months ago by antonio

  • status changed from reopened to closed
  • resolution set to fixed

Thanks Even, it should be fixed in r24540.

Note: See TracTickets for help on using tickets.