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. |
| 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.
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. |
jwiggins
left a comment
There was a problem hiding this comment.
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 alistordictaround 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
| '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.
Why not just list(__line_style_trait_values.keys())?
There was a problem hiding this comment.
It runs once when the module is imported, so I wouldn't worry about it
|
|
||
| import math | ||
|
|
||
| import six |
|
|
||
| if event_type == 'character': | ||
| key = unicode(event.text()) | ||
| key = six.u(event.text()) |
There was a problem hiding this comment.
key = six.text_type(event.text())
|
|
||
| import warnings | ||
|
|
||
| import six |
| 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.
Just use a regular dictionary comprehension:
self.pathOps = {k: v for k, v in six.iteritems(wx.GraphicsPath.__dict__) if k.startswith("Add")}
| 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.
Why the Image as PilImage import?
There was a problem hiding this comment.
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.
Ugh, yep. I see the NameError now... Carry on.
| font_map = {'Arial': 'Helvetica', | ||
| } | ||
| import _fontdata | ||
|
|
There was a problem hiding this comment.
A few lines below, it is being imported. font_map is also defined a bit below.
There was a problem hiding this comment.
That's just sloppy... Carry on.
|
|
||
| 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.
What's the reason for the new keyword arg?
| raise TraitError, ( object, name, 'a font descriptor string', | ||
| repr( value ) ) | ||
| raise TraitError(( object, name, 'a font descriptor string', | ||
| repr( value ) )) |
| # with builtins) in the auto-generated SWIG files like | ||
| # kiva/agg/agg.py. | ||
| use_2to3_exclude_fixers=['lib2to3.fixes.fix_imports'], | ||
| use_2to3=False, |
|
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.
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.
| 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.
The function just won't work. It is asking for two variables vtk_obj and eventname but they are never defined.
|
|
||
| def _window_paint(self, event): | ||
| "Do a GUI toolkit specific screen update" | ||
| # FIXME: THIS IS A BUG |
There was a problem hiding this comment.
A bit of the same... The function requires a variable union_bounds but it never defined.
| 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) |
|
|
||
| if event_type == 'character': | ||
| key = unicode(self.control.key_sym) | ||
| key = six.u(self.control.key_sym) |
| '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)) |
| @unittest.skipIf(six.PY3, reason="Crashes on python 3. See GH #95.") | ||
| 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.
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?
|
|
||
| if event_type == 'character': | ||
| key = unicode(event.text()) | ||
| key = six.u(event.text()) |
|
|
||
| import warnings | ||
|
|
||
| import six |
| 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")) |
| 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.
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. |
jwiggins
left a comment
There was a problem hiding this comment.
It looks like there are only some import cleanups needed now. This is otherwise ready to merge!
| from __future__ import with_statement | ||
|
|
||
| import six | ||
|
|
| """ | ||
|
|
||
| import six | ||
|
|
| from six import StringIO | ||
| import xml.etree.cElementTree as etree | ||
|
|
||
| import six |
| @@ -1,5 +1,7 @@ | |||
| import unittest | |||
|
|
|||
| import six | |||
| import copy | ||
| import unittest | ||
|
|
||
| import six |
|
|
||
| import sys, string, struct, re, os.path | ||
|
|
||
| import six |
| # marker_enum_map[enum] = string | ||
| # | ||
| #---------------------------------------------------------------------------- | ||
| import six |
| 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.
This was already imported at the top of this module.
| import tempfile | ||
| from contextlib import contextmanager | ||
|
|
||
| from six.moves import StringIO |
There was a problem hiding this comment.
This import appears to be unused
| #------------------------------------------------------------------------------- | ||
| # Imports: | ||
| #------------------------------------------------------------------------------- | ||
| import six |
* 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? |
| print ' red = ', | ||
| print(self.stops[i], end=" ") | ||
| print() | ||
| print(' red = ', end=" " |
There was a problem hiding this comment.
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.