Opened 12 years ago

Closed 12 years ago

#4693 closed defect (fixed)

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 (5)

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

Download all attachments as: .zip

Change History (12)

by antonio, 12 years ago

by antonio, 12 years ago

comment:1 by Even Rouault, 12 years ago

  • 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...

comment:2 by hobu, 12 years ago

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.

comment:3 by antonio, 12 years ago

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.

comment:4 by Even Rouault, 12 years ago

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.

by antonio, 12 years ago

Attachment: 2to3_osgeo.diff added

comment:5 by antonio, 12 years ago

Resolution: fixed
Status: newclosed

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

Fixed in 24539, closing.

comment:6 by Even Rouault, 12 years ago

Milestone: 1.9.22.0.0
Resolution: fixed
Status: closedreopened

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

comment:7 by antonio, 12 years ago

Resolution: fixed
Status: reopenedclosed

Thanks Even, it should be fixed in r24540.

Note: See TracTickets for help on using tickets.