Skip to content

Commit

Permalink
Merge pull request #374 from jdeschenes/python-3
Browse files Browse the repository at this point in the history
Python 3 Compatibility Fixes
  • Loading branch information
mdickinson committed Dec 18, 2018
2 parents 01bf50e + cdabe5a commit 6208f63
Show file tree
Hide file tree
Showing 118 changed files with 948 additions and 706 deletions.
4 changes: 2 additions & 2 deletions docs/source/_extensions/refactordoc/base_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#------------------------------------------------------------------------------
import re

from definition_items import DefinitionItem
from line_functions import is_empty, get_indent, fix_backspace, NEW_LINE
from .definition_items import DefinitionItem
from .line_functions import is_empty, get_indent, fix_backspace, NEW_LINE


underline_regex = re.compile(r'\s*\S+\s*\Z')
Expand Down
6 changes: 3 additions & 3 deletions docs/source/_extensions/refactordoc/class_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# Copyright (c) 2011, Enthought, Inc.
# All rights reserved.
#------------------------------------------------------------------------------
from base_doc import BaseDoc
from line_functions import get_indent, replace_at, add_indent
from definition_items import (MethodItem, AttributeItem, TableLineItem,
from .base_doc import BaseDoc
from .line_functions import get_indent, replace_at, add_indent
from .definition_items import (MethodItem, AttributeItem, TableLineItem,
max_attribute_length, max_attribute_index,
ListItem)

Expand Down
2 changes: 1 addition & 1 deletion docs/source/_extensions/refactordoc/definition_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import collections
import re

from line_functions import (add_indent, fix_star, trim_indent, NEW_LINE,
from .line_functions import (add_indent, fix_star, trim_indent, NEW_LINE,
fix_trailing_underscore)

header_regex = re.compile(r'\s:\s?')
Expand Down
6 changes: 3 additions & 3 deletions docs/source/_extensions/refactordoc/function_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# Copyright (c) 2011, Enthought, Inc.
# All rights reserved.
#------------------------------------------------------------------------------
from base_doc import BaseDoc
from line_functions import get_indent, add_indent
from definition_items import ArgumentItem, ListItem
from .base_doc import BaseDoc
from .line_functions import get_indent, add_indent
from .definition_items import ArgumentItem, ListItem


class FunctionDoc(BaseDoc):
Expand Down
18 changes: 11 additions & 7 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#
# All configuration values have a default value; values that are commented out
# serve to show the default value.

import sys
from __future__ import print_function
import io
import os
import sys

# The docset build will use slightly different formatting rules
BUILD_DOCSET = bool(os.environ.get('BUILD_DOCSET'))
Expand Down Expand Up @@ -113,8 +114,8 @@ def __name__(self):
(mod_name, DocMock(mocked_name=mod_name)) for mod_name in MOCK_MODULES)

# Report on what was mocked.
print 'mocking modules {0} and types {1}'.format(
MOCK_MODULES, [mocked[1] for mocked in MOCK_TYPES])
print('mocking modules {0} and types {1}'.format(
MOCK_MODULES, [mocked[1] for mocked in MOCK_TYPES]))

mock_modules()

Expand Down Expand Up @@ -143,9 +144,12 @@ def __name__(self):

# The default replacements for |version| and |release|, also used in various
# other places throughout the built documents.
d = {}
execfile(os.path.join('..', '..', 'traits', '__init__.py'), d)
version = release = d['__version__']
version_info = {}
traits_init_path = os.path.join('..', '..', 'traits', '__init__.py')
with io.open(traits_init_path, "r", encoding="utf-8") as version_module:
version_code = compile(version_module.read(), "__init__.py", "exec")
exec(version_code, version_info)
version = release = version_info['__version__']

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
Expand Down
8 changes: 4 additions & 4 deletions docs/source/traits_user_manual/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ specifying only the wildcard character for the attribute name::
File "all_wildcard.py", line 33, in <module>
bill.age = 'middle age'
File "c:\wrk\src\lib\enthought\traits\\trait_handlers.py", line 163, in error
raise TraitError, ( object, name, self.info(), value )
raise TraitError( object, name, self.info(), value )
TraitError: The 'age' trait of a Person instance must be an integer, but a value
of 'middle age' <type 'str'> was specified.
"""
Expand Down Expand Up @@ -915,9 +915,9 @@ interface and be open to extensions by adaptation as follows:
# about adaptation.
lines = printable.get_formatted_text(n_cols=20)

print '-- Start document --'
print '\n'.join(lines)
print '-- End of document -\n'
print('-- Start document --')
print('\n'.join(lines))
print('-- End of document -\n')

class TextDocument(HasTraits):
""" A text document. """
Expand Down
14 changes: 7 additions & 7 deletions docs/source/traits_user_manual/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,16 @@ value in the dictionary corresponding to the value assigned. For example::

>>> import mapped
>>> my_shape1 = mapped.GraphicShape()
>>> print my_shape1.line_color, my_shape1.fill_color
>>> print(my_shape1.line_color, my_shape1.fill_color)
black red
>>> print my_shape1.line_color_, my_shape1.fill_color_
>>> print(my_shape1.line_color_, my_shape1.fill_color_)
(0.0, 0.0, 0.0, 1.0) (1.0, 0.0, 0.0, 1.0)
>>> my_shape2 = mapped.GraphicShape()
>>> my_shape2.line_color = 'blue'
>>> my_shape2.fill_color = 'green'
>>> print my_shape2.line_color, my_shape2.fill_color
>>> print(my_shape2.line_color, my_shape2.fill_color)
blue green
>>> print my_shape2.line_color_, my_shape2.fill_color_
>>> print(my_shape2.line_color_, my_shape2.fill_color_)
(0.0, 0.0, 1.0, 1.0) (0.0, 1.0, 0.0, 1.0)

This example shows how a mapped trait can be used to create a user-friendly
Expand Down Expand Up @@ -449,10 +449,10 @@ For example::
...
>>> alf = Alien()
>>> alf.heads = 'o'
>>> print alf.heads
>>> print(alf.heads)
one
>>> alf.heads = 'tw'
>>> print alf.heads
>>> print(alf.heads)
two
>>> alf.heads = 't' # Error, not a unique prefix
Traceback (most recent call last):
Expand Down Expand Up @@ -557,7 +557,7 @@ than a complete sentence::
File "test.py", line 25, in ?
odd_stuff.very_odd = 0
File "C:\wrk\src\lib\enthought\traits\traits.py", line 1119, in validate
raise TraitError, excp
raise TraitError(excp)
traits.traits.TraitError: The 'very_odd' trait of an AnOddClass instance
must be **a positive odd integer** or -10 <= an integer <= -1, but a value
of 0 <type 'int'> was specified.
Expand Down
12 changes: 6 additions & 6 deletions docs/source/traits_user_manual/deferring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ DelegatesTo object. Consider the following example::
>>> tony = Parent(first_name='Anthony', last_name='Jones')
>>> alice = Parent(first_name='Alice', last_name='Smith')
>>> sally = Child( first_name='Sally', father=tony, mother=alice)
>>> print sally.last_name
>>> print(sally.last_name)
Jones
>>> sally.last_name = 'Cooper' # Updates delegatee
>>> print tony.last_name
>>> print(tony.last_name)
Cooper
>>> sally.last_name = sally.mother # ERR: string expected
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\src\trunk\enthought\traits\trait_handlers.py", line
163, in error
raise TraitError, ( object, name, self.info(), value )
raise TraitError( object, name, self.info(), value )
traits.trait_errors.TraitError: The 'last_name' trait of a
Parent instance must be a string, but a value of <__main__.Parent object at
0x014D6D80> <class '__main__.Parent'> was specified.
Expand Down Expand Up @@ -178,7 +178,7 @@ PrototypedFrom::
>>> maria = Parent(first_name = 'Maria', family_name = 'Gonzalez',\
... favorite_first_name = 'Tomas', child_allowance = 10.0 )
>>> nino = Child( father=fred, mother=maria )
>>> print '%s %s gets $%.2f for allowance' % (nino.first_name, \ ... nino.last_name, nino.allowance)
>>> print('%s %s gets $%.2f for allowance' % (nino.first_name, \ ... nino.last_name, nino.allowance))
Tomas Lopez gets $5.00 for allowance
"""

Expand Down Expand Up @@ -236,7 +236,7 @@ example::
last_name = Str

def _last_name_changed(self, new):
print "Parent's last name changed to %s." % new
print("Parent's last name changed to %s." % new)

class Child ( HasTraits ):

Expand All @@ -245,7 +245,7 @@ example::
last_name = PrototypedFrom( 'father' )

def _last_name_changed(self, new):
print "Child's last name changed to %s." % new
print("Child's last name changed to %s." % new)

"""
>>> dad = Parent( first_name='William', last_name='Chase' )
Expand Down
52 changes: 26 additions & 26 deletions docs/source/traits_user_manual/defining.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ casting traits::
traits.trait_errors.TraitError: The 'weight' trait of a Person instance
must be a float, but a value of '180' <type 'str'> was specified.
>>> bill.cweight = '180' # OK, cast to float('180')
>>> print bill.cweight
>>> print(bill.cweight)
180.0
>>>

Expand Down Expand Up @@ -536,7 +536,7 @@ it is also a valid value for assignment.
>>> hats = InventoryItem()
>>> hats.name = 'Stetson'

>>> print '%s: %s' % (hats.name, hats.stock)
>>> print('%s: %s' % (hats.name, hats.stock))
Stetson: None

>>> hats.stock = 2 # OK
Expand Down Expand Up @@ -742,32 +742,32 @@ the metadata attribute::

t = Test()

print t.trait( 'i' ).default # 99
print t.trait( 'i' ).default_kind # value
print t.trait( 'i' ).inner_traits # ()
print t.trait( 'i' ).is_trait_type( Int ) # True
print t.trait( 'i' ).is_trait_type( Float ) # False
print(t.trait( 'i' ).default) # 99
print(t.trait( 'i' ).default_kind) # value
print(t.trait( 'i' ).inner_traits) # ()
print(t.trait( 'i' ).is_trait_type( Int )) # True
print(t.trait( 'i' ).is_trait_type( Float )) # False

print t.trait( 'lf' ).default # []
print t.trait( 'lf' ).default_kind # list
print t.trait( 'lf' ).inner_traits
print(t.trait( 'lf' ).default) # []
print(t.trait( 'lf' ).default_kind) # list
print(t.trait( 'lf' ).inner_traits)
# (<traits.traits.CTrait object at 0x01B24138>,)
print t.trait( 'lf' ).is_trait_type( List ) # True
print t.trait( 'lf' ).is_trait_type( TraitType ) # True
print t.trait( 'lf' ).is_trait_type( Float ) # False
print t.trait( 'lf' ).inner_traits[0].is_trait_type( Float ) # True

print t.trait( 'foo' ).default # <undefined>
print t.trait( 'foo' ).default_kind # factory
print t.trait( 'foo' ).inner_traits # ()
print t.trait( 'foo' ).is_trait_type( Instance ) # True
print t.trait( 'foo' ).is_trait_type( List ) # False

print t.trait( 'any' ).default # [1, 2, 3]
print t.trait( 'any' ).default_kind # list
print t.trait( 'any' ).inner_traits # ()
print t.trait( 'any' ).is_trait_type( Any ) # True
print t.trait( 'any' ).is_trait_type( List ) # False
print(t.trait( 'lf' ).is_trait_type( List )) # True
print(t.trait( 'lf' ).is_trait_type( TraitType )) # True
print(t.trait( 'lf' ).is_trait_type( Float )) # False
print(t.trait( 'lf' ).inner_traits[0].is_trait_type( Float )) # True

print(t.trait( 'foo' ).default) # <undefined>
print(t.trait( 'foo' ).default_kind) # factory
print(t.trait( 'foo' ).inner_traits) # ()
print(t.trait( 'foo' ).is_trait_type( Instance )) # True
print(t.trait( 'foo' ).is_trait_type( List )) # False

print(t.trait( 'any' ).default) # [1, 2, 3]
print(t.trait( 'any' ).default_kind) # list
print(t.trait( 'any' ).inner_traits) # ()
print(t.trait( 'any' ).is_trait_type( Any )) # True
print(t.trait( 'any' ).is_trait_type( List )) # False

.. rubric:: Footnotes
.. [2] Most callable predefined traits are classes, but a few are functions.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/traits_user_manual/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ package. These features are elaborated in the rest of this guide.

# NOTIFICATION: This method is called when 'age' changes:
def _age_changed ( self, old, new ):
print 'Age changed from %s to %s ' % ( old, new )
print('Age changed from %s to %s ' % ( old, new ))

# Set up the example:
joe = Parent()
Expand All @@ -100,7 +100,7 @@ package. These features are elaborated in the rest of this guide.
moe.father = joe

# DELEGATION in action:
print "Moe's last name is %s " % moe.last_name
print("Moe's last name is %s " % moe.last_name)
# Result:
# Moe's last name is Johnson

Expand Down
16 changes: 8 additions & 8 deletions docs/source/traits_user_manual/notification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ some aspect of the extended trait name syntax in the name specifier.
class Department( HasTraits ):
employees = List(Employee)

def a_handler(): print "A handler"
def b_handler(): print "B handler"
def c_handler(): print "C handler"
def a_handler(): print("A handler")
def b_handler(): print("B handler")
def c_handler(): print("C handler")

fred = Employee()
mary = Employee()
Expand All @@ -377,9 +377,9 @@ some aspect of the extended trait name syntax in the name specifier.
# c_handler is called if 'employees' or its membership change:
dept.on_trait_change( c_handler, 'employees[]' )

print "Changing list items"
print("Changing list items")
dept.employees[1] = donna # Calls B and C
print "Replacing list"
print("Replacing list")
dept.employees = [donna] # Calls A and C

.. index:: notification; static
Expand Down Expand Up @@ -502,13 +502,13 @@ illustrated in the following example::
bmi = Float(0.0)

def _weight_kg_changed(self, old, new):
print 'weight_kg changed from %s to %s ' % (old, new)
print('weight_kg changed from %s to %s ' % (old, new))
if self.height_m != 0.0:
self.bmi = self.weight_kg / (self.height_m**2)

def _anytrait_changed(self, name, old, new):
print 'The %s trait changed from %s to %s ' \
% (name, old, new)
print('The %s trait changed from %s to %s ' \
% (name, old, new))
"""
>>> bob = Person()
>>> bob.height_m = 1.75
Expand Down
2 changes: 1 addition & 1 deletion docs/source/traits_user_manual/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ please follow the logic in the example below::

# We can now use the mock in our tests.
my_class.add_number(42)
print my_class.add_to_number.call_args_list
print(my_class.add_to_number.call_args_list)

.. note::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# package

#--[Imports]-------------------------------------------------------------------
from __future__ import print_function
from traits.api import Delegate, HasTraits, Instance, Int, Str


Expand All @@ -29,7 +30,7 @@ class Child(HasTraits):

# NOTIFICATION: This method is called when 'age' changes:
def _age_changed(self, old, new):
print 'Age changed from %s to %s ' % (old, new)
print('Age changed from %s to %s ' % (old, new))

#--[Example*]------------------------------------------------------------------

Expand All @@ -40,7 +41,7 @@ def _age_changed(self, old, new):
moe.father = joe

# DELEGATION in action:
print "Moe's last name is %s " % moe.last_name
print("Moe's last name is %s " % moe.last_name)
# Result:
# Moe's last name is Johnson

Expand Down
7 changes: 4 additions & 3 deletions examples/tutorials/doc_examples/examples/all_wildcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# all_wildcard.py --- Example of trait attribute wildcard rules

#--[Imports]-------------------------------------------------------------------
from __future__ import print_function
from traits.api import Any, Str, Int, HasTraits, TraitError


Expand Down Expand Up @@ -32,11 +33,11 @@ class Person(HasTraits):
bill.age = 49

# This should generate an error (must be an Int):
print 'Attempting to assign a string to an Int trait object...\n'
print('Attempting to assign a string to an Int trait object...\n')
try:
bill.age = 'middle age'
except TraitError, c:
print 'TraitError: ', c, '\n'
except TraitError as c:
print('TraitError: ', c, '\n')

# Display the final results:
bill.print_traits()

0 comments on commit 6208f63

Please sign in to comment.