Skip to content

Commit e360db9

Browse files
authored
Merge pull request #1045 from enthought/remove-python-version-checks
Remove sys.version_info checks
2 parents 2043a17 + 42a17bd commit e360db9

File tree

10 files changed

+35
-116
lines changed

10 files changed

+35
-116
lines changed

integrationtests/mayavi/common.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,7 @@ def run_standalone(self):
529529
g.start_event_loop()
530530
if self.exception_info is not None:
531531
type, value, tb = self.exception_info
532-
if sys.version_info[0] > 2:
533-
raise type(value).with_traceback(tb)
534-
else:
535-
raise type(value)
532+
raise type(value).with_traceback(tb)
536533

537534
def run(self):
538535
"""This starts everything up and runs the test. Call main to

mayavi/sources/vtk_data_source.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,14 @@ def __get_pure_state__(self):
164164
data = self.data
165165
if data is not None:
166166
sdata = write_dataset_to_string(data)
167-
if sys.version_info[0] > 2:
168-
z = gzip_string(sdata.encode('ascii'))
169-
else:
170-
z = gzip_string(sdata)
167+
z = gzip_string(sdata.encode('ascii'))
171168
d['data'] = z
172169
return d
173170

174171
def __set_pure_state__(self, state):
175172
z = state.data
176173
if z is not None:
177-
if sys.version_info[0] > 2:
178-
d = gunzip_string(z).decode('ascii')
179-
else:
180-
d = gunzip_string(z)
174+
d = gunzip_string(z).decode('ascii')
181175
r = tvtk.DataSetReader(read_from_input_string=1,
182176
input_string=d)
183177
warn = r.global_warning_display

mayavi/tools/camera.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424
from .engine_manager import get_engine
2525

2626

27-
if sys.version_info[0] > 2:
28-
string_types = (str,)
29-
else:
30-
string_types = (basestring,)
31-
32-
3327
def world_to_display(x, y, z, figure=None):
3428
""" Converts 3D world coordinates to screenshot pixel coordinates.
3529
@@ -285,14 +279,14 @@ def view(azimuth=None, elevation=None, distance=None, focalpoint=None,
285279
# bounds.
286280
bounds = np.array(ren.compute_visible_prop_bounds())
287281
if distance is not None and not (
288-
isinstance(distance, string_types) and distance == 'auto'):
282+
isinstance(distance, str) and distance == 'auto'):
289283
r = distance
290284
else:
291285
r = max(bounds[1::2] - bounds[::2]) * 2.0
292286

293287
cen = (bounds[1::2] + bounds[::2]) * 0.5
294288
if focalpoint is not None and not (
295-
isinstance(focalpoint, string_types) and focalpoint == 'auto'):
289+
isinstance(focalpoint, str) and focalpoint == 'auto'):
296290
cen = np.asarray(focalpoint)
297291

298292
# Find camera position.

tvtk/array_handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050

5151
BASE_REFERENCE_COUNT = vtk.vtkObject().GetReferenceCount()
5252

53-
if sys.version_info[0] > 2:
54-
unicode = str
55-
5653

5754
def getbuffer(array):
5855
return getattr(numpy, 'getbuffer', memoryview)(array)
@@ -713,7 +710,7 @@ def convert_array(arr, vtk_typ=None):
713710

714711
def is_array_sig(s):
715712
"""Given a signature, return if the signature has an array."""
716-
if not isinstance(s, (unicode, str)):
713+
if not isinstance(s, str):
717714
return False
718715
arr_types = ['Array', 'vtkPoints', 'vtkIdList']
719716
for i in arr_types:

tvtk/class_tree.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
import sys
1212

13-
if sys.version_info[0] > 2:
14-
import builtins
15-
else:
16-
import __builtin__ as builtins
13+
import builtins
1714

1815

1916
class TreeNode:

tvtk/code_gen.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ def _write_wrapper_class(self, node, tvtk_name):
220220
# The only reason this method is separate is to generate code
221221
# for an individual class when debugging.
222222
fname = camel2enthought(tvtk_name) + '.py'
223-
if sys.version_info[0] > 2:
224-
out = open(os.path.join(self.out_dir, fname), 'w', encoding='utf-8')
225-
else:
226-
out = open(os.path.join(self.out_dir, fname), 'w')
223+
out = open(os.path.join(self.out_dir, fname), 'w', encoding='utf-8')
227224
self.wrap_gen.generate_code(node, out)
228225
out.close()
229226

tvtk/tests/test_class_tree.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@
33
# Copyright (c) 2004, Enthought, Inc.
44

55
"""Tests class_tree.py. Uses the vtk module to test the code. Also
6-
tests if the tree generation works for the __builtin__ module.
6+
tests if the tree generation works for the builtins module.
77
88
"""
9-
9+
import builtins
1010
import sys
1111
import unittest
1212
from contextlib import contextmanager
1313

1414
from tvtk import class_tree
1515

1616
import vtk
17-
if sys.version_info[0] > 2:
18-
import builtins as __builtin__
19-
PY_VER = 3
20-
else:
21-
import __builtin__
22-
PY_VER = 2
2317

2418
# This computation can be expensive, so we cache it.
2519
_cache = class_tree.ClassTree(vtk)
@@ -42,14 +36,14 @@ def setUp(self):
4236
@contextmanager
4337
def _remove_loader_from_builtin(self):
4438
self._loader = None
45-
if hasattr(__builtin__, '__loader__'):
46-
self._loader = __builtin__.__loader__
47-
del __builtin__.__loader__
39+
if hasattr(builtins, '__loader__'):
40+
self._loader = builtins.__loader__
41+
del builtins.__loader__
4842
try:
4943
yield
5044
finally:
5145
if self._loader:
52-
__builtin__.__loader__ = self._loader
46+
builtins.__loader__ = self._loader
5347

5448
def test_basic_vtk(self):
5549
"""Basic tests for the VTK module."""
@@ -76,16 +70,11 @@ def test_basic_vtk(self):
7670
expect = ['object', 'vtkColor3', 'vtkColor4', 'vtkDenseArray',
7771
'vtkQuaternion', 'vtkRect',
7872
'vtkSparseArray', 'vtkTuple',
79-
'vtkTypedArray', 'vtkVariantStrictWeakOrderKey',
80-
'vtkVector', 'vtkVector2', 'vtkVector3']
81-
if PY_VER == 3:
82-
expect.remove('vtkVariantStrictWeakOrderKey')
73+
'vtkTypedArray','vtkVector',
74+
'vtkVector2', 'vtkVector3']
8375
else:
8476
self.assertGreaterEqual(vtk_major_version, 8)
85-
if PY_VER == 3:
86-
expect = ['object']
87-
else:
88-
expect = ['object', 'vtkVariantStrictWeakOrderKey']
77+
expect = ['object']
8978
self.assertEqual(names, expect)
9079
elif (hasattr(vtk, 'vtkVector')):
9180
self.assertEqual(len(t.tree[0]), 11)
@@ -133,9 +122,9 @@ def _get_ancestors(klass):
133122
bases.extend(_get_ancestors(base))
134123
return bases
135124

136-
# Simple __builtin__ test.
125+
# Simple builtins test.
137126
with self._remove_loader_from_builtin():
138-
t = class_tree.ClassTree(__builtin__)
127+
t = class_tree.ClassTree(builtins)
139128
t.create()
140129
n = t.get_node('TabError')
141130
bases = [x.__name__ for x in _get_ancestors(TabError)]
@@ -176,12 +165,12 @@ def test_tree(self):
176165
self.assertEqual(n.level, level)
177166

178167
def test_builtin(self):
179-
"""Check if tree structure for __builtin__ works."""
168+
"""Check if tree structure for builtins works."""
180169

181170
# This tests to see if the tree structure generation works for
182-
# the __builtin__ module.
171+
# the builtins module.
183172
with self._remove_loader_from_builtin():
184-
t = class_tree.ClassTree(__builtin__)
173+
t = class_tree.ClassTree(builtins)
185174
t.create()
186175
self.t = t
187176
self.test_parent_child()

tvtk/tests/test_tvtk.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
# Only used for testing.
4545
from tvtk.tvtk_classes import tvtk_helper
4646

47-
PY_VER = sys.version_info[0]
48-
if PY_VER > 2:
49-
long = int
50-
5147

5248
def mysum(arr):
5349
val = arr
@@ -134,10 +130,7 @@ def f(self):
134130
r = tvtk.XMLDataReader()
135131
self.assertEqual(r.f(), 'f')
136132
if len(vtk.vtkObjectBase.__bases__) > 0:
137-
if vtk_major_version == 7 and PY_VER < 3:
138-
expect = ()
139-
else:
140-
expect = (object,)
133+
expect = (object,)
141134
else:
142135
expect = tuple()
143136

@@ -337,19 +330,14 @@ def test_property(self):
337330
self.assertEqual(p.color, (0.5, 0.5, 0.5))
338331

339332
def test_points_lookup(self):
340-
""" Test if points can be looked up with both int and long keys.
341-
Fixes GH Issue 173.
333+
""" Test if points can be looked up.
342334
"""
343335
points = tvtk.Points()
344336
points.insert_next_point((0, 1, 2))
345337
pt = points[0]
346338
self.assertEqual(pt, (0, 1, 2))
347-
ptl = points[long(0)]
348-
self.assertEqual(ptl, (0, 1, 2))
349339
get_pt = points.get_point(0)
350340
self.assertEqual(get_pt, (0, 1, 2))
351-
get_ptl = points.get_point(long(0))
352-
self.assertEqual(get_ptl, (0, 1, 2))
353341

354342
def test_cell_array(self):
355343
""" Test if cell array insertion updates number of cells.
@@ -503,13 +491,7 @@ def test_idlist(self):
503491
self.assertEqual(i, j)
504492
self.assertEqual(f[-1], 3)
505493
self.assertEqual(f[0], 0)
506-
if sys.version_info[0] > 2:
507-
self.assertEqual(repr(f), '[0, 1, 2, 3]')
508-
else:
509-
if type(f[0]) is long:
510-
self.assertEqual(repr(f), '[0L, 1L, 2L, 3L]')
511-
else:
512-
self.assertEqual(repr(f), '[0, 1, 2, 3]')
494+
self.assertEqual(repr(f), '[0, 1, 2, 3]')
513495
f.append(4)
514496
f.extend([5, 6])
515497
self.assertEqual(len(f), 7)
@@ -1053,12 +1035,6 @@ def test_import_tvtk_does_not_import_gui(self):
10531035
self.assertFalse('QtCore' in output)
10541036
self.assertFalse('wx' in output)
10551037

1056-
@unittest.skipIf(PY_VER > 2, "Irrelevant for python 3")
1057-
def test_unicode_traits(self):
1058-
reader = tvtk.DelimitedTextReader()
1059-
self.assertIsInstance(reader.unicode_record_delimiters, unicode)
1060-
self.assertIsInstance(reader.unicode_string_delimiters, unicode)
1061-
self.assertIsInstance(reader.unicode_field_delimiters, unicode)
10621038

10631039
if __name__ == "__main__":
10641040
unittest.main()

tvtk/util/qt_gradient_editor.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
ColorControlPoint, ChannelBase, FunctionControl, GradientEditorWidget
2020
)
2121

22-
PY3 = sys.version_info[0] > 2
2322

2423
##########################################################################
2524
# `QGradientControl` class.
@@ -163,17 +162,12 @@ def __init__(self, master=None, gradient_table=None, color_space=None,
163162
set_status_text: a callback used to set the status text
164163
when using the editor.
165164
"""
166-
if PY3:
167-
kw = dict(
168-
master=master, gradient_table=gradient_table,
169-
color_space=color_space, width=width,
170-
height=height
171-
)
172-
super().__init__(**kw)
173-
else:
174-
FunctionControl.__init__(self, master, gradient_table, color_space,
175-
width, height)
176-
QtGui.QWidget.__init__(self, parent=master)
165+
kw = dict(
166+
master=master, gradient_table=gradient_table,
167+
color_space=color_space, width=width,
168+
height=height
169+
)
170+
super().__init__(**kw)
177171
self.resize(width, height)
178172
self.setMinimumSize(100, 50)
179173

@@ -303,15 +297,10 @@ def __init__(self, master, vtk_table, on_change_color_table=None,
303297
'h', 's', 'v', 'r', 'g', 'b', 'a' separately
304298
specified creates different panels for each.
305299
"""
306-
if PY3:
307-
kw = dict(master=master, vtk_table=vtk_table,
308-
on_change_color_table=on_change_color_table,
309-
colors=colors)
310-
super().__init__(**kw)
311-
else:
312-
QtGui.QWidget.__init__(self, master)
313-
GradientEditorWidget.__init__(self, master, vtk_table,
314-
on_change_color_table, colors)
300+
kw = dict(master=master, vtk_table=vtk_table,
301+
on_change_color_table=on_change_color_table,
302+
colors=colors)
303+
super().__init__(**kw)
315304

316305
gradient_preview_width = self.gradient_preview_width
317306
gradient_preview_height = self.gradient_preview_height

tvtk/wrapper_gen.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
else:
3131
faulthandler.enable()
3232

33-
PY_VER = sys.version_info[0]
34-
3533

3634
def get_trait_def(value, **kwargs):
3735
""" Return the appropriate trait type, reformatted string and
@@ -78,10 +76,6 @@ def get_trait_def(value, **kwargs):
7876
number_map = {int: 'traits.Int',
7977
float: 'traits.Float'}
8078

81-
# In Python 2 there is long type
82-
if PY_VER < 3:
83-
number_map[long] = 'traits.Int'
84-
8579
if type_ in number_map:
8680
return number_map[type_], str(value), kwargs_code
8781

@@ -90,11 +84,6 @@ def get_trait_def(value, **kwargs):
9084
value = ''
9185
return 'traits.String', '{!r}'.format(value), kwargs_code
9286

93-
elif PY_VER < 3 and type_ is unicode:
94-
if value == u'\x00':
95-
value = u''
96-
return 'traits.Unicode', '{!r}'.format(value), kwargs_code
97-
9887
elif type_ in (tuple, list):
9988
shape = (len(value),)
10089
dtypes = set(type(element) for element in value)

0 commit comments

Comments
 (0)