Skip to content
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

Merged
merged 33 commits into from
Aug 6, 2017
Merged

Conversation

jdeschenes
Copy link
Contributor

This is a fix to remove 2to3 when installing enable.

jmdeschenes and others added 22 commits September 7, 2016 09:55
* 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-io
Copy link

codecov-io commented Jul 25, 2017

Codecov Report

Merging #284 into master will increase coverage by 0.32%.
The diff coverage is 38.26%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
enable/tools/hover_tool.py 0% <ø> (ø) ⬆️
kiva/fonttools/afm.py 45.11% <ø> (ø) ⬆️
enable/wx/constants.py 0% <ø> (ø) ⬆️
enable/tools/api.py 0% <ø> (ø) ⬆️
enable/savage/trait_defs/ui/svg_button_editor.py 0% <0%> (ø) ⬆️
enable/scrollbar.py 0% <0%> (ø) ⬆️
enable/savage/compliance/comparator.py 0% <0%> (ø) ⬆️
enable/canvas.py 27.36% <0%> (ø) ⬆️
enable/vtk_backend/vtk_window.py 0% <0%> (ø) ⬆️
enable/tools/viewport_pan_tool.py 0% <0%> (ø) ⬆️
... and 76 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 73829e9...812b243. Read the comment docs.

@jdeschenes
Copy link
Contributor Author

The failure doesn't have anything to do with the actual PR. Should probably resolved separately.

@jdeschenes
Copy link
Contributor Author

jdeschenes commented Jul 25, 2017

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 ) ))
Copy link
Member

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

@jdeschenes
Copy link
Contributor Author

I fixed the issue you found and another that I saw from scanning the code.

Copy link
Member

@jwiggins jwiggins left a 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, or items, then I wouldn't worry about switching to an iter* function from six. Just wrap a list or dict 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

@@ -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))
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For efficiency

Copy link
Member

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

@@ -1,6 +1,9 @@

import math

import six
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -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())
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -13,6 +13,9 @@

import warnings

import six
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -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"))
Copy link
Member

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")}

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@@ -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")
Copy link
Member

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?

raise TraitError, ( object, name, 'a font descriptor string',
repr( value ) )
raise TraitError(( object, name, 'a font descriptor string',
repr( value ) ))
Copy link
Member

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay 🎉

@jwiggins
Copy link
Member

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`
Copy link
Contributor Author

@jdeschenes jdeschenes left a 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
Copy link
Contributor Author

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.

@@ -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
Copy link
Contributor Author

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.

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -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))
Copy link
Contributor Author

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')
Copy link
Contributor Author

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?

@@ -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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -13,6 +13,9 @@

import warnings

import six
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -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"))
Copy link
Contributor Author

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):
Copy link
Contributor Author

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

@jdeschenes
Copy link
Contributor Author

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
@jdeschenes
Copy link
Contributor Author

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.

@jdeschenes
Copy link
Contributor Author

I think I have resolved all issues. Let me know if there is anything else to correct.

Copy link
Member

@jwiggins jwiggins left a 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!

@@ -1,6 +1,8 @@

from __future__ import with_statement

import six

Copy link
Member

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.

@@ -2,6 +2,8 @@
Define the base Enable object traits
"""

import six

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also unused here

import os
import time
from six import StringIO
import xml.etree.cElementTree as etree

import six
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import

@@ -1,5 +1,7 @@
import unittest

import six
Copy link
Member

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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import

@@ -53,6 +53,7 @@ unsigned path_flags(unsigned c);
# marker_enum_map[enum] = string
#
#----------------------------------------------------------------------------
import six
Copy link
Member

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
Copy link
Member

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.

import tempfile
from contextlib import contextmanager

from six.moves import StringIO
Copy link
Member

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
Copy link
Member

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
@jdeschenes
Copy link
Contributor Author

Fixed that unused imports. There was another issue that went unnoticed in vu_meter that I fixed.

@jwiggins
Copy link
Member

jwiggins commented Aug 4, 2017

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=" "
Copy link
Member

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 )

@jwiggins
Copy link
Member

jwiggins commented Aug 5, 2017

OK, so I finally had a chance to test this. Looks good, modulo the missing parenthesis which causes a build failure on macOS.

@jdeschenes
Copy link
Contributor Author

I added the parenthesis. Building MacOS on travis should be done quite quickly so this doesn't slip through!

@jwiggins jwiggins merged commit 8cb8dd4 into enthought:master Aug 6, 2017
@jwiggins
Copy link
Member

jwiggins commented Aug 6, 2017

Thanks for all the updates @jdeschenes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants