-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python3 compatibility fixes #284
Conversation
* print should now be functions instead of statements * raises should now respect the new syntax * "<>" operator was replaced by "!="
* Fixed tuple unpacking issues * replaced has_key with "in" syntax * exec is now a function instead of a statement
* Fixed __metaclass__ compatibility using six.add_metaclass
* replaced xrange by six.moves.range
* Fixed tuple unpacking issues * Fixed izip issues using six
* Replaced self.assert_ with self.assertTrue
* Fixed several non-trivial list vs iterables in kiva
* Fixed several non-trivial list vs iterables in enable
* Fixed reduce function using six * Fixed a few other issues not seen previously
* fixed unicode and unichr calls
* Fixed StringIO with six
* Fixed imports * Fixed some StringIO that were missed in the initial pass * Some string fixes
* Fixed issues with basestring * fixed issues with long
* Some fixes with import *
Solved some issues while running unit test.
# Conflicts: # enable/savage/compliance/comparator.py # enable/savage/compliance/viewer.py # enable/savage/svg/attributes.py # enable/savage/svg/css/colour.py # enable/savage/svg/document.py # enable/savage/svg/tests/test_document.py # enable/savage/trait_defs/ui/qt4/svg_editor.py # kiva/agg/src/graphics_context.i # kiva/fonttools/fontTools/misc/textTools.py # kiva/fonttools/fontTools/ttLib/__init__.py # kiva/fonttools/fontTools/ttLib/macUtils.py # kiva/fonttools/fontTools/ttLib/sfnt.py # kiva/fonttools/fontTools/ttLib/tables/DefaultTable.py # kiva/fonttools/fontTools/ttLib/tables/__init__.py # kiva/fonttools/fontTools/ttLib/tables/_n_a_m_e.py # kiva/fonttools/font_manager.py # kiva/fonttools/misc/textTools.py # kiva/pdfmetrics.py # kiva/ps.py # kiva/quartz/setup.py # kiva/svg.py # kiva/tests/test_macport.py # setup.py
Codecov Report
@@ Coverage Diff @@
## master #284 +/- ##
==========================================
+ Coverage 33.26% 33.59% +0.32%
==========================================
Files 206 206
Lines 18572 18278 -294
Branches 2522 2407 -115
==========================================
- Hits 6178 6140 -38
+ Misses 12001 11745 -256
Partials 393 393
Continue to review full report at Codecov.
|
The failure doesn't have anything to do with the actual PR. Should probably resolved separately. |
The travis-ci failure has been resolved in #285. |
enable/base.py
Outdated
raise TraitError, ( object, name, 'a font descriptor string', | ||
repr( value ) ) | ||
raise TraitError(( object, name, 'a font descriptor string', | ||
repr( value ) )) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's one set of parentheses too many here.
raise TraitError(object, name, 'a font descriptor string', repr( value ))
I fixed the issue you found and another that I saw from scanning the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a quick pass through all the changes. Couple general comments:
- If code is already using
keys
,values
, oritems
, then I wouldn't worry about switching to aniter*
function fromsix
. Just wrap alist
ordict
around the places which don't expect an iterator and call it a day. - Try not to change the structure of the code. Our test coverage is not very good in this project and the minimal changes needed here are quite enough churn for a single pull request
enable/enable_traits.py
Outdated
@@ -75,7 +77,7 @@ | |||
'dot': array( [ 2.0, 2.0 ] ), | |||
'long dash': array( [ 9.0, 5.0 ] ) | |||
} | |||
__line_style_trait_map_keys = __line_style_trait_values.keys() | |||
__line_style_trait_map_keys = list(six.iterkeys(__line_style_trait_values)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just list(__line_style_trait_values.keys())
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For efficiency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It runs once when the module is imported, so I wouldn't worry about it
enable/gadgets/vu_meter.py
Outdated
@@ -1,6 +1,9 @@ | |||
|
|||
import math | |||
|
|||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
enable/qt4/base_window.py
Outdated
@@ -356,14 +358,14 @@ def _create_key_event(self, event_type, event): | |||
return None | |||
|
|||
if event_type == 'character': | |||
key = unicode(event.text()) | |||
key = six.u(event.text()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key = six.text_type(event.text())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
enable/qt4/constants.py
Outdated
@@ -13,6 +13,9 @@ | |||
|
|||
import warnings | |||
|
|||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
enable/savage/compliance/drawer.py
Outdated
@@ -43,7 +46,7 @@ def OnPaint(self, evt): | |||
class DrawFrame(wx.Frame): | |||
def __init__(self, parent, *args, **kwargs): | |||
wx.Frame.__init__(self, parent, *args, **kwargs) | |||
self.pathOps = dict((k,v) for (k,v) in wx.GraphicsPath.__dict__.iteritems() if k.startswith("Add")) | |||
self.pathOps = dict((k,v) for (k,v) in six.iteritems(wx.GraphicsPath.__dict__) if k.startswith("Add")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use a regular dictionary comprehension:
self.pathOps = {k: v for k, v in six.iteritems(wx.GraphicsPath.__dict__) if k.startswith("Add")}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@@ -199,7 +210,7 @@ def device_draw_image(self, img, rect): | |||
|
|||
Requires the Python Imaging Library (PIL). | |||
""" | |||
from kiva.compat import pilfromstring, piltostring | |||
from kiva.compat import pilfromstring, piltostring, Image as PilImage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the Image as PilImage
import?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in the function but never imported in the module.(Line 249). I can move the import at the top of the module, but that could have more important impact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, yep. I see the NameError
now... Carry on.
font_map = {'Arial': 'Helvetica', | ||
} | ||
import _fontdata | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few lines below, it is being imported. font_map is also defined a bit below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's just sloppy... Carry on.
kiva/tests/test_agg_drawing.py
Outdated
@@ -6,7 +6,7 @@ | |||
class TestAggDrawing(DrawingImageTester, unittest.TestCase): | |||
|
|||
def create_graphics_context(self, width, height): | |||
return GraphicsContext((width, height)) | |||
return GraphicsContext((width, height), pix_format="rgba32") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for the new keyword arg?
kiva/trait_defs/kiva_font_trait.py
Outdated
raise TraitError, ( object, name, 'a font descriptor string', | ||
repr( value ) ) | ||
raise TraitError(( object, name, 'a font descriptor string', | ||
repr( value ) )) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too many parentheses here
# with builtins) in the auto-generated SWIG files like | ||
# kiva/agg/agg.py. | ||
use_2to3_exclude_fixers=['lib2to3.fixes.fix_imports'], | ||
use_2to3=False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay 🎉
In general: Thanks!!! 💯 This is important work and we really appreciate you taking it on (here and in the other ETS libraries). I'm a bit swamped with other things right now, so I haven't had a chance to do anything more than read through these changes. Hopefully someone else can take a look... |
* Removed unused imports * Changed six.u to six.text_type * Replaced call with a dictionary comprehension in drawer.py * Fixed an an issue with a callable in svg_regex.py * Removed sm.zip in favour of the builtin function * Changed print("") to print() * Removed a pix_format keyword call. Still missing: * Corrections for itervalues, iterkeys and iteritems * Some clarifications for the comments that were written * `pdfmetrics.parseAFMFile`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few comments in the lines, I will need your input to move forward. As I said in the comments, I tried to keep the code structure to the minimum but there are few places where it simply isn't possible. I tried to fix a few bugs as I saw along the way.
As mentionned in my commit message,
- pdfmetrics will require a bit of a refactor
- I left out the iteritems, itervalues, iterkeys for later.
- Some of the comments I left out there, might better belong in github issues? Let me know.
@@ -281,6 +283,7 @@ def _vtk_mouse_wheel(self, vtk_obj, eventname): | |||
self._pass_event_to_vtk(vtk_obj, eventname) | |||
|
|||
def _create_key_event(self, vtk_event, event_type): | |||
# FIXME: THIS IS A BUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function just won't work. It is asking for two variables vtk_obj
and eventname
but they are never defined.
enable/wx/cairo.py
Outdated
@@ -25,6 +27,7 @@ def _create_gc(self, size, pix_format="bgra32"): | |||
|
|||
def _window_paint(self, event): | |||
"Do a GUI toolkit specific screen update" | |||
# FIXME: THIS IS A BUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of the same... The function requires a variable union_bounds but it never defined.
enable/vtk_backend/vtk_window.py
Outdated
else: | ||
key = KEY_MAP.get(self.control.key_sym, None) | ||
if key is None: | ||
key = unicode(self.control.key_sym) | ||
key = six.u(self.control.key_sym) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
enable/vtk_backend/vtk_window.py
Outdated
@@ -290,11 +293,11 @@ def _create_key_event(self, vtk_event, event_type): | |||
return self._pass_event_to_vtk(vtk_obj, eventname) | |||
|
|||
if event_type == 'character': | |||
key = unicode(self.control.key_sym) | |||
key = six.u(self.control.key_sym) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
enable/enable_traits.py
Outdated
@@ -75,7 +77,7 @@ | |||
'dot': array( [ 2.0, 2.0 ] ), | |||
'long dash': array( [ 9.0, 5.0 ] ) | |||
} | |||
__line_style_trait_map_keys = __line_style_trait_values.keys() | |||
__line_style_trait_map_keys = list(six.iterkeys(__line_style_trait_values)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For efficiency
def test_draw_24(self): | ||
gc = GraphicsContext((256, 128), pix_format='rgba32') | ||
gc = GraphicsContext((256, 128), pix_format='rgb24') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pix_format is probably wrong. This is a test_draw_24 and it is using rgba32. We might want to add another test to cover the normal case as well?
enable/qt4/base_window.py
Outdated
@@ -356,14 +358,14 @@ def _create_key_event(self, event_type, event): | |||
return None | |||
|
|||
if event_type == 'character': | |||
key = unicode(event.text()) | |||
key = six.u(event.text()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
enable/qt4/constants.py
Outdated
@@ -13,6 +13,9 @@ | |||
|
|||
import warnings | |||
|
|||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
enable/savage/compliance/drawer.py
Outdated
@@ -43,7 +46,7 @@ def OnPaint(self, evt): | |||
class DrawFrame(wx.Frame): | |||
def __init__(self, parent, *args, **kwargs): | |||
wx.Frame.__init__(self, parent, *args, **kwargs) | |||
self.pathOps = dict((k,v) for (k,v) in wx.GraphicsPath.__dict__.iteritems() if k.startswith("Add")) | |||
self.pathOps = dict((k,v) for (k,v) in six.iteritems(wx.GraphicsPath.__dict__) if k.startswith("Add")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
except ImportError, SystemExit: | ||
raise ImportError, "Unable to import a Savage backend for the %s " \ | ||
"toolkit." % ETSConfig.toolkit | ||
except (ImportError, SystemExit): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will raise a SyntaxError
otherwise
Thanks for the quick review by the way! |
* Added a test for the `test_draw_24` * Fixed an import error in cairo.py * Removed changes from tohtml.py * Removed reference of CP Trac #2111 in comments * Fixed issue in function parseAFMFile * Added very preliminary test for parseAFMFile
I have changed a few more things from @jwiggins comments. I think all comments except itervalues, iterkeys and iteritems have been answered. If not, please advise. I will make the last change tomorrow. |
# Conflicts: # enable/qt4/base_window.py # enable/wx/base_window.py
I think I have resolved all issues. Let me know if there is anything else to correct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there are only some import cleanups needed now. This is otherwise ready to merge!
enable/compass.py
Outdated
@@ -1,6 +1,8 @@ | |||
|
|||
from __future__ import with_statement | |||
|
|||
import six | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import is unused here.
enable/enable_traits.py
Outdated
@@ -2,6 +2,8 @@ | |||
Define the base Enable object traits | |||
""" | |||
|
|||
import six | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also unused here
enable/savage/compliance/viewer.py
Outdated
import os | ||
import time | ||
from six import StringIO | ||
import xml.etree.cElementTree as etree | ||
|
||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import
enable/tests/container_test_case.py
Outdated
@@ -1,5 +1,7 @@ | |||
import unittest | |||
|
|||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import
@@ -3,6 +3,8 @@ | |||
import copy | |||
import unittest | |||
|
|||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too
@@ -28,7 +28,7 @@ | |||
|
|||
|
|||
import sys, string, struct, re, os.path | |||
|
|||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import
kiva/agg/src/constants.i
Outdated
@@ -53,6 +53,7 @@ unsigned path_flags(unsigned c); | |||
# marker_enum_map[enum] = string | |||
# | |||
#---------------------------------------------------------------------------- | |||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import
kiva/cairo.py
Outdated
@@ -1292,8 +1293,9 @@ def font_metrics_provider(): | |||
from traits.api import false | |||
from chaco.api import ArrayPlotData, Plot, PlotGraphicsContext | |||
from chaco.example_support import COLOR_PALETTE | |||
import six.moves as sm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was already imported at the top of this module.
kiva/tests/test_pdfmetrics.py
Outdated
import tempfile | ||
from contextlib import contextmanager | ||
|
||
from six.moves import StringIO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import appears to be unused
@@ -18,6 +18,7 @@ | |||
#------------------------------------------------------------------------------- | |||
# Imports: | |||
#------------------------------------------------------------------------------- | |||
import six |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import
* Removed unused imports * added `from __future__ import print_function` in tohtml.py * fixed syntax error in vu_meter.py
Fixed that unused imports. There was another issue that went unnoticed in vu_meter that I fixed. |
I think I'm basically done reviewing this. Does anybody with fresh eyes want to take a look before it's merged? |
kiva/quartz/ABCGI.pyx
Outdated
print ' red = ', | ||
print(self.stops[i], end=" ") | ||
print() | ||
print(' red = ', end=" " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is missing its closing )
OK, so I finally had a chance to test this. Looks good, modulo the missing parenthesis which causes a build failure on macOS. |
I added the parenthesis. Building MacOS on travis should be done quite quickly so this doesn't slip through! |
Thanks for all the updates @jdeschenes! |
This is a fix to remove 2to3 when installing enable.