Modify examples to be both Python 2 and 3 compatible

For removing dependency on 2to3
This commit is contained in:
William S Fulton 2020-08-15 16:46:01 +01:00
commit 89bee6a7fa
37 changed files with 368 additions and 388 deletions

View file

@ -11,7 +11,7 @@ class PyCallback(example.Callback):
example.Callback.__init__(self) example.Callback.__init__(self)
def run(self): def run(self):
print "PyCallback.run()" print("PyCallback.run()")
# Create an Caller instance # Create an Caller instance
@ -20,8 +20,8 @@ caller = example.Caller()
# Add a simple C++ callback (caller owns the callback, so # Add a simple C++ callback (caller owns the callback, so
# we disown it first by clearing the .thisown flag). # we disown it first by clearing the .thisown flag).
print "Adding and calling a normal C++ callback" print("Adding and calling a normal C++ callback")
print "----------------------------------------" print("----------------------------------------")
callback = example.Callback() callback = example.Callback()
callback.thisown = 0 callback.thisown = 0
@ -29,9 +29,9 @@ caller.setCallback(callback)
caller.call() caller.call()
caller.delCallback() caller.delCallback()
print print("")
print "Adding and calling a Python callback" print("Adding and calling a Python callback")
print "------------------------------------" print("------------------------------------")
# Add a Python callback (caller owns the callback, so we # Add a Python callback (caller owns the callback, so we
# disown it first by calling __disown__). # disown it first by calling __disown__).
@ -40,9 +40,9 @@ caller.setCallback(PyCallback().__disown__())
caller.call() caller.call()
caller.delCallback() caller.delCallback()
print print("")
print "Adding and calling another Python callback" print("Adding and calling another Python callback")
print "------------------------------------------" print("------------------------------------------")
# Let's do the same but use the weak reference this time. # Let's do the same but use the weak reference this time.
@ -53,5 +53,5 @@ caller.delCallback()
# All done. # All done.
print print("")
print "python exit" print("python exit")

View file

@ -7,15 +7,15 @@ import example
# ----- Object creation ----- # ----- Object creation -----
print "Creating some objects:" print("Creating some objects:")
c = example.Circle(10) c = example.Circle(10)
print " Created circle", c print(" Created circle %s" % c)
s = example.Square(10) s = example.Square(10)
print " Created square", s print(" Created square %s" % s)
# ----- Access a static member ----- # ----- Access a static member -----
print "\nA total of", example.cvar.Shape_nshapes, "shapes were created" print("\nA total of %d shapes were created" % example.cvar.Shape_nshapes)
# ----- Member data access ----- # ----- Member data access -----
@ -27,25 +27,25 @@ c.y = 30
s.x = -10 s.x = -10
s.y = 5 s.y = 5
print "\nHere is their current position:" print("\nHere is their current position:")
print " Circle = (%f, %f)" % (c.x, c.y) print(" Circle = (%f, %f)" % (c.x, c.y))
print " Square = (%f, %f)" % (s.x, s.y) print(" Square = (%f, %f)" % (s.x, s.y))
# ----- Call some methods ----- # ----- Call some methods -----
print "\nHere are some properties of the shapes:" print("\nHere are some properties of the shapes:")
for o in [c, s]: for o in [c, s]:
print " ", o print(" %s" % o)
print " area = ", o.area() print(" area = %s" % o.area())
print " perimeter = ", o.perimeter() print(" perimeter = %s" % o.perimeter())
# prevent o from holding a reference to the last object looked at # prevent o from holding a reference to the last object looked at
o = None o = None
print "\nGuess I'll clean up now" print("\nGuess I'll clean up now")
# Note: this invokes the virtual destructor # Note: this invokes the virtual destructor
del c del c
del s del s
print example.cvar.Shape_nshapes, "shapes remain" print("%d shapes remain" % example.cvar.Shape_nshapes)
print "Goodbye" print("Goodbye")

View file

@ -2,22 +2,24 @@
import example import example
print "ICONST =", example.ICONST, "(should be 42)" print("ICONST = %s (should be 42)" % example.ICONST)
print "FCONST =", example.FCONST, "(should be 2.1828)" print("FCONST = %s (should be 2.1828)" % example.FCONST)
print "CCONST =", example.CCONST, "(should be 'x')" print("CCONST = %s (should be 'x')" % example.CCONST)
print "CCONST2 =", example.CCONST2, "(this should be on a new line)" print("CCONST2 = %s (this should be on a new line)" % example.CCONST2)
print "SCONST =", example.SCONST, "(should be 'Hello World')" print("SCONST = %s (should be 'Hello World')" % example.SCONST)
print "SCONST2 =", example.SCONST2, "(should be '\"Hello World\"')" print("SCONST2 = %s (should be '\"Hello World\"')" % example.SCONST2)
print "EXPR =", example.EXPR, "(should be 48.5484)" print("EXPR = %s (should be 48.5484)" % example.EXPR)
print "iconst =", example.iconst, "(should be 37)" print("iconst = %s (should be 37)" % example.iconst)
print "fconst =", example.fconst, "(should be 3.14)" print("fconst = %s (should be 3.14)" % example.fconst)
try: try:
print "EXTERN = ", example.EXTERN, "(Arg! This shouldn't print anything)" x = example.EXTERN
print("%s (Arg! This shouldn't print anything)" % x)
except AttributeError: except AttributeError:
print "EXTERN isn't defined (good)" print("EXTERN isn't defined (good)")
try: try:
print "FOO = ", example.FOO, "(Arg! This shouldn't print anything)" x = example.FOO
print("%s (Arg! This shouldn't print anything)" % x)
except AttributeError: except AttributeError:
print "FOO isn't defined (good)" print("FOO isn't defined (good)")

View file

@ -7,15 +7,15 @@ import example
x = 42 x = 42
y = 105 y = 105
g = example.gcd(x, y) g = example.gcd(x, y)
print "The gcd of %d and %d is %d" % (x, y, g) print("The gcd of %d and %d is %d" % (x, y, g))
# Manipulate the Foo global variable # Manipulate the Foo global variable
# Output its current value # Output its current value
print "Foo = ", example.cvar.Foo print("Foo = %s" % example.cvar.Foo)
# Change its value # Change its value
example.cvar.Foo = 3.1415926 example.cvar.Foo = 3.1415926
# See if the change took effect # See if the change took effect
print "Foo = ", example.cvar.Foo print("Foo = %s" % example.cvar.Foo)

View file

@ -2,4 +2,4 @@
import example import example
print "example.Foo.bar.__doc__ =", repr(example.Foo.bar.__doc__), "(Should be 'No comment')" print("example.Foo.bar.__doc__ = %s (Should be 'No comment')" % repr(example.Foo.bar.__doc__))

View file

@ -5,24 +5,24 @@
import example import example
print "Creating some objects:" print("Creating some objects:")
c = example.MakeCircle(10) c = example.MakeCircle(10)
print " Created circle", c print(" Created circle %s" % c)
s = example.MakeSquare(10) s = example.MakeSquare(10)
print " Created square", s print(" Created square %s" % s)
r = example.MakeRectangleInt(10, 20) r = example.MakeRectangleInt(10, 20)
print " Created rectangle", r print(" Created rectangle %s" % r)
print "\nHere are some properties of the shapes:" print("\nHere are some properties of the shapes:")
for o in [c, s, r]: for o in [c, s, r]:
print " ", o print(" %s" % o)
print " area = ", o.area() print(" area = %s" % o.area())
print " perimeter = ", o.perimeter() print(" perimeter = %s" % o.perimeter())
print "\nRunning pydoc, this is the equivalent to executing: pydoc -w ./example.py" print("\nRunning pydoc, this is the equivalent to executing: pydoc -w ./example.py")
import pydoc import pydoc
pydoc.writedoc("example") pydoc.writedoc("example")
print "Open example.html in your browser to view the generated python docs" print("Open example.html in your browser to view the generated python docs")

View file

@ -5,24 +5,24 @@ import example
# ----- Object creation ----- # ----- Object creation -----
# Print out the value of some enums # Print out the value of some enums
print "*** color ***" print("*** color ***")
print " RED =", example.RED print(" RED = %s" % example.RED)
print " BLUE =", example.BLUE print(" BLUE = %s" % example.BLUE)
print " GREEN =", example.GREEN print(" GREEN = %s" % example.GREEN)
print "\n*** Foo::speed ***" print("\n*** Foo::speed ***")
print " Foo_IMPULSE =", example.Foo.IMPULSE print(" Foo_IMPULSE = %s" % example.Foo.IMPULSE)
print " Foo_WARP =", example.Foo.WARP print(" Foo_WARP = %s" % example.Foo.WARP)
print " Foo_LUDICROUS =", example.Foo.LUDICROUS print(" Foo_LUDICROUS = %s" % example.Foo.LUDICROUS)
print "\nTesting use of enums with functions\n" print("\nTesting use of enums with functions\n")
example.enum_test(example.RED, example.Foo.IMPULSE) example.enum_test(example.RED, example.Foo.IMPULSE)
example.enum_test(example.BLUE, example.Foo.WARP) example.enum_test(example.BLUE, example.Foo.WARP)
example.enum_test(example.GREEN, example.Foo.LUDICROUS) example.enum_test(example.GREEN, example.Foo.LUDICROUS)
example.enum_test(1234, 5678) example.enum_test(1234, 5678)
print "\nTesting use of enum with class method" print("\nTesting use of enum with class method")
f = example.Foo() f = example.Foo()
f.enum_test(example.Foo.IMPULSE) f.enum_test(example.Foo.IMPULSE)

View file

@ -7,36 +7,36 @@ import example
t = example.Test() t = example.Test()
try: try:
t.unknown() t.unknown()
except RuntimeError, e: except RuntimeError as e:
print "incomplete type", e.args[0] print("incomplete type %s" % e.args[0])
try: try:
t.simple() t.simple()
except RuntimeError, e: except RuntimeError as e:
print e.args[0] print(e.args[0])
try: try:
t.message() t.message()
except RuntimeError, e: except RuntimeError as e:
print e.args[0] print(e.args[0])
if not example.is_python_builtin(): if not example.is_python_builtin():
try: try:
t.hosed() t.hosed()
except example.Exc, e: except example.Exc as e:
print e.code, e.msg print("%s %s" % (e.code, e.msg))
else: else:
try: try:
t.hosed() t.hosed()
except BaseException, e: except BaseException as e:
# Throwing builtin classes as exceptions not supported (-builtin # Throwing builtin classes as exceptions not supported (-builtin
# option) # option)
print e print(e)
for i in range(1, 4): for i in range(1, 4):
try: try:
t.multi(i) t.multi(i)
except RuntimeError, e: except RuntimeError as e:
print e.args[0] print(e.args[0])
except example.Exc, e: except example.Exc as e:
print e.code, e.msg print("%s %s" % (e.code, e.msg))

View file

@ -2,44 +2,44 @@
import example import example
if example.is_python_builtin(): if example.is_python_builtin():
print "Skipping example: -builtin option does not support %exceptionclass" print("Skipping example: -builtin option does not support %exceptionclass")
exit(0) exit(0)
q = example.intQueue(10) q = example.intQueue(10)
print "Inserting items into intQueue" print("Inserting items into intQueue")
print type(example.FullError) print(type(example.FullError))
try: try:
for i in range(0, 100): for i in range(0, 100):
q.enqueue(i) q.enqueue(i)
except example.FullError, e: except example.FullError as e:
print "Maxsize is", e.maxsize print("Maxsize is %s" % e.maxsize)
print "Removing items" print("Removing items")
try: try:
while 1: while True:
q.dequeue() q.dequeue()
except example.EmptyError, e: except example.EmptyError as e:
pass pass
q = example.doubleQueue(1000) q = example.doubleQueue(1000)
print "Inserting items into doubleQueue" print("Inserting items into doubleQueue")
try: try:
for i in range(0, 10000): for i in range(0, 10000):
q.enqueue(i * 1.5) q.enqueue(i * 1.5)
except example.FullError, e: except example.FullError as e:
print "Maxsize is", e.maxsize print("Maxsize is %s" % e.maxsize)
print "Removing items" print("Removing items")
try: try:
while 1: while True:
q.dequeue() q.dequeue()
except example.EmptyError, e: except example.EmptyError as e:
pass pass

View file

@ -21,9 +21,9 @@ class CEO(example.Manager):
# the director wrappers to call CEO.getPosition. # the director wrappers to call CEO.getPosition.
e = CEO("Alice") e = CEO("Alice")
print e.getName(), "is a", e.getPosition() print("%s is a %s" % (e.getName(), e.getPosition()))
print "Just call her \"%s\"" % e.getTitle() print("Just call her \"%s\"" % e.getTitle())
print "----------------------" print("----------------------")
# Create a new EmployeeList instance. This class does not have a C++ # Create a new EmployeeList instance. This class does not have a C++
@ -40,7 +40,7 @@ list = example.EmployeeList()
e = e.__disown__() e = e.__disown__()
list.addEmployee(e) list.addEmployee(e)
print "----------------------" print("----------------------")
# Now we access the first four items in list (three are C++ objects that # Now we access the first four items in list (three are C++ objects that
# EmployeeList's constructor adds, the last is our CEO). The virtual # EmployeeList's constructor adds, the last is our CEO). The virtual
@ -59,13 +59,13 @@ print "----------------------"
# passes down through the C++ director class to the Python implementation # passes down through the C++ director class to the Python implementation
# in CEO. All this routing takes place transparently. # in CEO. All this routing takes place transparently.
print "(position, title) for items 0-3:" print("(position, title) for items 0-3:")
print " %s, \"%s\"" % (list.get_item(0).getPosition(), list.get_item(0).getTitle()) print(" %s, \"%s\"" % (list.get_item(0).getPosition(), list.get_item(0).getTitle()))
print " %s, \"%s\"" % (list.get_item(1).getPosition(), list.get_item(1).getTitle()) print(" %s, \"%s\"" % (list.get_item(1).getPosition(), list.get_item(1).getTitle()))
print " %s, \"%s\"" % (list.get_item(2).getPosition(), list.get_item(2).getTitle()) print(" %s, \"%s\"" % (list.get_item(2).getPosition(), list.get_item(2).getTitle()))
print " %s, \"%s\"" % (list.get_item(3).getPosition(), list.get_item(3).getTitle()) print(" %s, \"%s\"" % (list.get_item(3).getPosition(), list.get_item(3).getTitle()))
print "----------------------" print("----------------------")
# Time to delete the EmployeeList, which will delete all the Employee* # Time to delete the EmployeeList, which will delete all the Employee*
# items it contains. The last item is our CEO, which gets destroyed as its # items it contains. The last item is our CEO, which gets destroyed as its
@ -75,8 +75,8 @@ print "----------------------"
# usual to destroy the object. # usual to destroy the object.
del list del list
print "----------------------" print("----------------------")
# All done. # All done.
print "python exit" print("python exit")

View file

@ -7,14 +7,14 @@ b = 42
# Now call our C function with a bunch of callbacks # Now call our C function with a bunch of callbacks
print "Trying some C callback functions" print("Trying some C callback functions")
print " a =", a print(" a = %s" % a)
print " b =", b print(" b = %s" % b)
print " ADD(a,b) =", example.do_op(a, b, example.ADD) print(" ADD(a,b) = %s" % example.do_op(a, b, example.ADD))
print " SUB(a,b) =", example.do_op(a, b, example.SUB) print(" SUB(a,b) = %s" % example.do_op(a, b, example.SUB))
print " MUL(a,b) =", example.do_op(a, b, example.MUL) print(" MUL(a,b) = %s" % example.do_op(a, b, example.MUL))
print "Here is what the C callback function objects look like in Python" print("Here is what the C callback function objects look like in Python")
print " ADD =", example.ADD print(" ADD = %s" % example.ADD)
print " SUB =", example.SUB print(" SUB = %s" % example.SUB)
print " MUL =", example.MUL print(" MUL = %s" % example.MUL)

View file

@ -7,18 +7,18 @@ b = 42
# Now call our C function with a bunch of callbacks # Now call our C function with a bunch of callbacks
print "Trying some C callback functions" print("Trying some C callback functions")
print " a =", a print(" a = %s" % a)
print " b =", b print(" b = %s" % b)
print " ADD(a,b) =", example.do_op(a, b, example.ADD) print(" ADD(a,b) = %s" % example.do_op(a, b, example.ADD))
print " SUB(a,b) =", example.do_op(a, b, example.SUB) print(" SUB(a,b) = %s" % example.do_op(a, b, example.SUB))
print " MUL(a,b) =", example.do_op(a, b, example.MUL) print(" MUL(a,b) = %s" % example.do_op(a, b, example.MUL))
print "Here is what the C callback function objects look like in Python" print("Here is what the C callback function objects look like in Python")
print " ADD =", example.ADD print(" ADD = %s" % example.ADD)
print " SUB =", example.SUB print(" SUB = %s" % example.SUB)
print " MUL =", example.MUL print(" MUL = %s" % example.MUL)
print "Call the functions directly..." print("Call the functions directly...")
print " add(a,b) =", example.add(a, b) print(" add(a,b) = %s" % example.add(a, b))
print " sub(a,b) =", example.sub(a, b) print(" sub(a,b) = %s" % example.sub(a, b))

View file

@ -12,5 +12,5 @@ for i in range(0, 100):
a(i) # Note: function call a(i) # Note: function call
b(math.sqrt(i)) # Note: function call b(math.sqrt(i)) # Note: function call
print a.result() print(a.result())
print b.result() print(b.result())

View file

@ -1,15 +1,22 @@
# file: runme.py # file: runme.py
# Test various properties of classes defined in separate modules # Test various properties of classes defined in separate modules
import sys
print("Testing the %import directive")
print "Testing the %import directive"
import base import base
import foo import foo
import bar import bar
import spam import spam
def write_flush(s):
# Python 2/3 compatible write and flush
sys.stdout.write(s)
sys.stdout.flush()
# Create some objects # Create some objects
print "Creating some objects" print("Creating some objects")
a = base.Base() a = base.Base()
b = foo.Foo() b = foo.Foo()
@ -17,91 +24,74 @@ c = bar.Bar()
d = spam.Spam() d = spam.Spam()
# Try calling some methods # Try calling some methods
print "Testing some methods" print("Testing some methods")
print "",
print "Should see 'Base::A' ---> ", write_flush(" Should see 'Base::A' ---> ")
a.A() a.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
a.B() a.B()
print "Should see 'Foo::A' ---> ", write_flush(" Should see 'Foo::A' ---> ")
b.A() b.A()
print "Should see 'Foo::B' ---> ", write_flush(" Should see 'Foo::B' ---> ")
b.B() b.B()
print "Should see 'Bar::A' ---> ", write_flush(" Should see 'Bar::A' ---> ")
c.A() c.A()
print "Should see 'Bar::B' ---> ", write_flush(" Should see 'Bar::B' ---> ")
c.B() c.B()
print "Should see 'Spam::A' ---> ", write_flush(" Should see 'Spam::A' ---> ")
d.A() d.A()
print "Should see 'Spam::B' ---> ", write_flush(" Should see 'Spam::B' ---> ")
d.B() d.B()
# Try some casts # Try some casts
print "\nTesting some casts\n" print("\nTesting some casts\n")
print "",
x = a.toBase() x = a.toBase()
print "Should see 'Base::A' ---> ", write_flush(" Should see 'Base::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = b.toBase() x = b.toBase()
print "Should see 'Foo::A' ---> ", write_flush(" Should see 'Foo::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = c.toBase() x = c.toBase()
print "Should see 'Bar::A' ---> ", write_flush(" Should see 'Bar::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = d.toBase() x = d.toBase()
print "Should see 'Spam::A' ---> ", write_flush(" Should see 'Spam::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = d.toBar() x = d.toBar()
print "Should see 'Bar::B' ---> ", write_flush(" Should see 'Bar::B' ---> ")
x.B() x.B()
print "\nTesting some dynamic casts\n" print("\nTesting some dynamic casts\n")
x = d.toBase() x = d.toBase()
print " Spam -> Base -> Foo : ",
y = foo.Foo_fromBase(x) y = foo.Foo_fromBase(x)
if y: print(" Spam -> Base -> Foo : {} swig".format("bad" if y else "good"))
print "bad swig"
else:
print "good swig"
print " Spam -> Base -> Bar : ",
y = bar.Bar_fromBase(x) y = bar.Bar_fromBase(x)
if y: print(" Spam -> Base -> Bar : {} swig".format("good" if y else "bad"))
print "good swig"
else:
print "bad swig"
print " Spam -> Base -> Spam : ",
y = spam.Spam_fromBase(x) y = spam.Spam_fromBase(x)
if y: print(" Spam -> Base -> Spam : {} swig".format("good" if y else "bad"))
print "good swig"
else:
print "bad swig"
print " Foo -> Spam : ",
y = spam.Spam_fromBase(b) y = spam.Spam_fromBase(b)
if y: print(" Foo -> Spam : {} swig".format("bad" if y else "good"))
print "bad swig"
else:
print "good swig"

View file

@ -6,26 +6,26 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of modules content from within __init__.py # Test import of modules content from within __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py" print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
if sys.version_info < (2, 5): if sys.version_info < (2, 5):
print " Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'" print(" Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'")
sys.exit(0) sys.exit(0)
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
import py2.pkg2 import py2.pkg2
print " Finished importing py2.pkg2" print(" Finished importing py2.pkg2")
commandline = sys.executable + " -m py2.pkg2.bar" commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.foo" commandline = sys.executable + " -m py2.pkg2.foo"
run_except_on_windows(commandline) run_except_on_windows(commandline)
else: else:
import py3.pkg2 import py3.pkg2
print " Finished importing py3.pkg2" print(" Finished importing py3.pkg2")
# commandline = sys.executable + " -m py3.pkg2.bar" # commandline = sys.executable + " -m py3.pkg2.bar"
# run_except_on_windows(commandline) # run_except_on_windows(commandline)
# commandline = sys.executable + " -m py3.pkg2.foo" # commandline = sys.executable + " -m py3.pkg2.foo"

View file

@ -6,24 +6,24 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of modules content from within __init__.py # Test import of modules content from within __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py" print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
if sys.version_info < (2, 5): if sys.version_info < (2, 5):
print " Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'" print(" Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'")
sys.exit(0) sys.exit(0)
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
import py2.pkg2 import py2.pkg2
print " Finished importing py2.pkg2" print(" Finished importing py2.pkg2")
commandline = sys.executable + " -m py2.pkg2.bar" commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
else: else:
import py3.pkg2 import py3.pkg2
print " Finished importing py3.pkg2" print(" Finished importing py3.pkg2")
# commandline = sys.executable + " -m py3.pkg2.bar" # commandline = sys.executable + " -m py3.pkg2.bar"
# run_except_on_windows(commandline) # run_except_on_windows(commandline)

View file

@ -6,24 +6,24 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of modules content from within __init__.py # Test import of modules content from within __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py" print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
if sys.version_info < (2, 5): if sys.version_info < (2, 5):
print " Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'" print(" Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'")
sys.exit(0) sys.exit(0)
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
import py2.pkg2 import py2.pkg2
print " Finished importing py2.pkg2" print(" Finished importing py2.pkg2")
commandline = sys.executable + " -m py2.pkg2.bar" commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
else: else:
import py3.pkg2 import py3.pkg2
print " Finished importing py3.pkg2" print(" Finished importing py3.pkg2")
# commandline = sys.executable + " -m py3.pkg2.bar" # commandline = sys.executable + " -m py3.pkg2.bar"
# run_except_on_windows(commandline) # run_except_on_windows(commandline)

View file

@ -3,10 +3,10 @@ import sys
# Test import of a SWIG generated module renamed as the package's __init__.py # Test import of a SWIG generated module renamed as the package's __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - module renamed as __init__.py" print("Testing " + testname + " - module renamed as __init__.py")
if sys.version_info >= (3, 0, 0) and sys.version_info < (3, 3, 0): if sys.version_info >= (3, 0, 0) and sys.version_info < (3, 3, 0):
print " Not importing as Python version is >= 3.0 and < 3.3" print(" Not importing as Python version is >= 3.0 and < 3.3")
# Package detection does not work in these versions. # Package detection does not work in these versions.
# Can be fixed by using this in the interface file: # Can be fixed by using this in the interface file:
# %module(moduleimport="from . import $module") foo # without -builtin # %module(moduleimport="from . import $module") foo # without -builtin
@ -14,7 +14,7 @@ if sys.version_info >= (3, 0, 0) and sys.version_info < (3, 3, 0):
sys.exit(0) sys.exit(0)
import pkg1 import pkg1
print " Finished importing pkg1" print(" Finished importing pkg1")
if pkg1.foofunction(123) != 1230: if pkg1.foofunction(123) != 1230:
raise RuntimeError("foofunction failed") raise RuntimeError("foofunction failed")
@ -23,4 +23,4 @@ fc = pkg1.FooClass()
if fc.foomethod(1) != 6: if fc.foomethod(1) != 6:
raise RuntimeError("foomethod failed") raise RuntimeError("foomethod failed")
print " Finished testing pkg1" print(" Finished testing pkg1")

View file

@ -5,14 +5,14 @@ import subprocess
import sys import sys
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - namespace packages" print("Testing " + testname + " - namespace packages")
if sys.version_info < (3, 3, 0): if sys.version_info < (3, 3, 0):
print " Not importing nstest as Python version is < 3.3" print(" Not importing nstest as Python version is < 3.3")
sys.exit(0) sys.exit(0)
import nstest import nstest
print " Finished importing nstest" print(" Finished importing nstest")
nstest.main() nstest.main()

View file

@ -6,26 +6,26 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of modules content from within __init__.py # Test import of modules content from within __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) with -relativeimport" print("Testing " + testname + " - %module(package=...) with -relativeimport")
if sys.version_info < (2, 5): if sys.version_info < (2, 5):
print " Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'" print(" Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'")
sys.exit(0) sys.exit(0)
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
import py2.pkg2.bar import py2.pkg2.bar
print " Finished importing py2.pkg2.bar" print(" Finished importing py2.pkg2.bar")
commandline = sys.executable + " -m py2.pkg2.bar" commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.pkg3.foo" commandline = sys.executable + " -m py2.pkg2.pkg3.foo"
run_except_on_windows(commandline) run_except_on_windows(commandline)
else: else:
import py3.pkg2.bar import py3.pkg2.bar
print " Finished importing py3.pkg2.bar" print(" Finished importing py3.pkg2.bar")
commandline = sys.executable + " -m py3.pkg2.bar" commandline = sys.executable + " -m py3.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py3.pkg2.pkg3.foo" commandline = sys.executable + " -m py3.pkg2.pkg3.foo"

View file

@ -6,26 +6,26 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of modules content from within __init__.py # Test import of modules content from within __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py" print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
if sys.version_info < (2, 5): if sys.version_info < (2, 5):
print " Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'" print(" Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'")
sys.exit(0) sys.exit(0)
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
import py2.pkg2.bar import py2.pkg2.bar
print " Finished importing py2.pkg2.bar" print(" Finished importing py2.pkg2.bar")
commandline = sys.executable + " -m py2.pkg2.bar" commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.pkg3.pkg4.foo" commandline = sys.executable + " -m py2.pkg2.pkg3.pkg4.foo"
run_except_on_windows(commandline) run_except_on_windows(commandline)
else: else:
import py3.pkg2.bar import py3.pkg2.bar
print " Finished importing py3.pkg2.bar" print(" Finished importing py3.pkg2.bar")
commandline = sys.executable + " -m py3.pkg2.bar" commandline = sys.executable + " -m py3.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py3.pkg2.pkg3.pkg4.foo" commandline = sys.executable + " -m py3.pkg2.pkg3.pkg4.foo"

View file

@ -6,26 +6,26 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of modules content from within __init__.py # Test import of modules content from within __init__.py
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) with -relativeimport" print("Testing " + testname + " - %module(package=...) with -relativeimport")
if sys.version_info < (2, 5): if sys.version_info < (2, 5):
print " Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'" print(" Skipping test as Python version is < 2.5 and does not support relative import syntax: 'from . import x'")
sys.exit(0) sys.exit(0)
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
import py2.pkg2.bar import py2.pkg2.bar
print " Finished importing py2.pkg2.bar" print(" Finished importing py2.pkg2.bar")
commandline = sys.executable + " -m py2.pkg2.bar" commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.pkg3.foo" commandline = sys.executable + " -m py2.pkg2.pkg3.foo"
run_except_on_windows(commandline) run_except_on_windows(commandline)
else: else:
import py3.pkg2.bar import py3.pkg2.bar
print " Finished importing py3.pkg2.bar" print(" Finished importing py3.pkg2.bar")
commandline = sys.executable + " -m py3.pkg2.bar" commandline = sys.executable + " -m py3.pkg2.bar"
run_except_on_windows(commandline) run_except_on_windows(commandline)
commandline = sys.executable + " -m py3.pkg2.pkg3.foo" commandline = sys.executable + " -m py3.pkg2.pkg3.foo"

View file

@ -6,21 +6,21 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
# Test import of same modules from different packages # Test import of same modules from different packages
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py" print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
import pkg2.foo import pkg2.foo
print " Finished importing pkg2.foo" print(" Finished importing pkg2.foo")
var2 = pkg2.foo.Pkg2_Foo() var2 = pkg2.foo.Pkg2_Foo()
classname = str(type(var2)) classname = str(type(var2))
if classname.find("pkg2.foo.Pkg2_Foo") == -1: if classname.find("pkg2.foo.Pkg2_Foo") == -1:
raise RuntimeError("failed type checking: " + classname) raise RuntimeError("failed type checking: " + classname)
print " Successfully created object pkg2.foo.Pkg2_Foo" print(" Successfully created object pkg2.foo.Pkg2_Foo")
commandline = sys.executable + " -m pkg2.foo" commandline = sys.executable + " -m pkg2.foo"
run_except_on_windows(commandline) run_except_on_windows(commandline)

View file

@ -6,20 +6,20 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py" print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
import pkg1.pkg2.foo import pkg1.pkg2.foo
print " Finished importing pkg1.pkg2.foo" print(" Finished importing pkg1.pkg2.foo")
var2 = pkg1.pkg2.foo.Pkg2_Foo() var2 = pkg1.pkg2.foo.Pkg2_Foo()
classname = str(type(var2)) classname = str(type(var2))
if classname.find("pkg1.pkg2.foo.Pkg2_Foo") == -1: if classname.find("pkg1.pkg2.foo.Pkg2_Foo") == -1:
raise RuntimeError("failed type checking: " + classname) raise RuntimeError("failed type checking: " + classname)
print " Successfully created object pkg1.pkg2.foo.Pkg2_Foo" print(" Successfully created object pkg1.pkg2.foo.Pkg2_Foo")
commandline = sys.executable + " -m pkg1.pkg2.foo" commandline = sys.executable + " -m pkg1.pkg2.foo"
run_except_on_windows(commandline) run_except_on_windows(commandline)

View file

@ -6,14 +6,14 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - split modules" print("Testing " + testname + " - split modules")
import pkg1.foo import pkg1.foo
print " Finished importing pkg1.foo" print(" Finished importing pkg1.foo")
if not(pkg1.foo.count() == 3): if not(pkg1.foo.count() == 3):
raise RuntimeError("test failed") raise RuntimeError("test failed")

View file

@ -6,14 +6,14 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin": if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw # Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True) subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline) print((" Finished running: " + commandline))
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
print "Testing " + testname + " - split modules" print("Testing " + testname + " - split modules")
import pkg1.foo import pkg1.foo
print " Finished importing pkg1.foo" print(" Finished importing pkg1.foo")
if not(pkg1.foo.count() == 3): if not(pkg1.foo.count() == 3):
raise RuntimeError("test failed") raise RuntimeError("test failed")

View file

@ -1,15 +1,22 @@
# file: runme.py # file: runme.py
# Test various properties of classes defined in separate modules # Test various properties of classes defined in separate modules
import sys
print("Testing the %import directive with templates")
print "Testing the %import directive with templates"
import base import base
import foo import foo
import bar import bar
import spam import spam
def write_flush(s):
# Python 2/3 compatible write and flush
sys.stdout.write(s)
sys.stdout.flush()
# Create some objects # Create some objects
print "Creating some objects" print("Creating some objects")
a = base.intBase() a = base.intBase()
b = foo.intFoo() b = foo.intFoo()
@ -17,91 +24,74 @@ c = bar.intBar()
d = spam.intSpam() d = spam.intSpam()
# Try calling some methods # Try calling some methods
print "Testing some methods" print("Testing some methods")
print "",
print "Should see 'Base::A' ---> ", write_flush(" Should see 'Base::A' ---> ")
a.A() a.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
a.B() a.B()
print "Should see 'Foo::A' ---> ", write_flush(" Should see 'Foo::A' ---> ")
b.A() b.A()
print "Should see 'Foo::B' ---> ", write_flush(" Should see 'Foo::B' ---> ")
b.B() b.B()
print "Should see 'Bar::A' ---> ", write_flush(" Should see 'Bar::A' ---> ")
c.A() c.A()
print "Should see 'Bar::B' ---> ", write_flush(" Should see 'Bar::B' ---> ")
c.B() c.B()
print "Should see 'Spam::A' ---> ", write_flush(" Should see 'Spam::A' ---> ")
d.A() d.A()
print "Should see 'Spam::B' ---> ", write_flush(" Should see 'Spam::B' ---> ")
d.B() d.B()
# Try some casts # Try some casts
print "\nTesting some casts\n" print("\nTesting some casts\n")
print "",
x = a.toBase() x = a.toBase()
print "Should see 'Base::A' ---> ", write_flush(" Should see 'Base::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = b.toBase() x = b.toBase()
print "Should see 'Foo::A' ---> ", write_flush(" Should see 'Foo::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = c.toBase() x = c.toBase()
print "Should see 'Bar::A' ---> ", write_flush(" Should see 'Bar::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = d.toBase() x = d.toBase()
print "Should see 'Spam::A' ---> ", write_flush(" Should see 'Spam::A' ---> ")
x.A() x.A()
print "Should see 'Base::B' ---> ", write_flush(" Should see 'Base::B' ---> ")
x.B() x.B()
x = d.toBar() x = d.toBar()
print "Should see 'Bar::B' ---> ", write_flush(" Should see 'Bar::B' ---> ")
x.B() x.B()
print "\nTesting some dynamic casts\n" print("\nTesting some dynamic casts\n")
x = d.toBase() x = d.toBase()
print " Spam -> Base -> Foo : ",
y = foo.intFoo_fromBase(x) y = foo.intFoo_fromBase(x)
if y: print(" Spam -> Base -> Foo : {} swig".format("bad" if y else "good"))
print "bad swig"
else:
print "good swig"
print " Spam -> Base -> Bar : ",
y = bar.intBar_fromBase(x) y = bar.intBar_fromBase(x)
if y: print(" Spam -> Base -> Bar : {} swig".format("good" if y else "bad"))
print "good swig"
else:
print "bad swig"
print " Spam -> Base -> Spam : ",
y = spam.intSpam_fromBase(x) y = spam.intSpam_fromBase(x)
if y: print(" Spam -> Base -> Spam : {} swig".format("good" if y else "bad"))
print "good swig"
else:
print "bad swig"
print " Foo -> Spam : ",
y = spam.intSpam_fromBase(b) y = spam.intSpam_fromBase(b)
if y: print(" Foo -> Spam : {} swig".format("bad" if y else "good"))
print "bad swig"
else:
print "good swig"

View file

@ -7,14 +7,14 @@ import example
x = 42 x = 42
y = 105 y = 105
g = example.gcd(x, y) g = example.gcd(x, y)
print "The gcd of %d and %d is %d" % (x, y, g) print("The gcd of %d and %d is %d" % (x, y, g))
# Call the gcdmain() function # Call the gcdmain() function
example.gcdmain(["gcdmain", "42", "105"]) example.gcdmain(["gcdmain", "42", "105"])
# Call the count function # Call the count function
print example.count("Hello World", "l") print(example.count("Hello World", "l"))
# Call the capitalize function # Call the capitalize function
print example.capitalize("hello world") print(example.capitalize("hello world"))

View file

@ -4,17 +4,17 @@ import example
a = example.Complex(2, 3) a = example.Complex(2, 3)
b = example.Complex(-5, 10) b = example.Complex(-5, 10)
print "a =", a print("a = %s" % a)
print "b =", b print("b = %s" % b)
c = a + b c = a + b
print "c =", c print("c = %s" % c)
print "a*b =", a * b print("a*b = %s" % (a * b))
print "a-c =", a - c print("a-c = %s" % (a - c))
e = example.ComplexCopy(a - c) e = example.ComplexCopy(a - c)
print "e =", e print("e = %s" % e)
# Big expression # Big expression
f = ((a + b) * (c + b * e)) + (-a) f = ((a + b) * (c + b * e)) + (-a)
print "f =", f print("f = %s" % f)

View file

@ -3,23 +3,23 @@
import example import example
# First create some objects using the pointer library. # First create some objects using the pointer library.
print "Testing the pointer library" print("Testing the pointer library")
a = example.new_intp() a = example.new_intp()
b = example.new_intp() b = example.new_intp()
c = example.new_intp() c = example.new_intp()
example.intp_assign(a, 37) example.intp_assign(a, 37)
example.intp_assign(b, 42) example.intp_assign(b, 42)
print " a =", a print(" a = %s" % a)
print " b =", b print(" b = %s" % b)
print " c =", c print(" c = %s" % c)
# Call the add() function with some pointers # Call the add() function with some pointers
example.add(a, b, c) example.add(a, b, c)
# Now get the result # Now get the result
r = example.intp_value(c) r = example.intp_value(c)
print " 37 + 42 =", r print(" 37 + 42 = %s" % r)
# Clean up the pointers # Clean up the pointers
example.delete_intp(a) example.delete_intp(a)
@ -30,12 +30,12 @@ example.delete_intp(c)
# This should be much easier. Now how it is no longer # This should be much easier. Now how it is no longer
# necessary to manufacture pointers. # necessary to manufacture pointers.
print "Trying the typemap library" print("Trying the typemap library")
r = example.sub(37, 42) r = example.sub(37, 42)
print " 37 - 42 =", r print(" 37 - 42 = %s" % r)
# Now try the version with multiple return values # Now try the version with multiple return values
print "Testing multiple return values" print("Testing multiple return values")
q, r = example.divide(42, 37) q, r = example.divide(42, 37)
print " 42/37 = %d remainder %d" % (q, r) print(" 42/37 = %d remainder %d" % (q, r))

View file

@ -6,12 +6,12 @@ import example
# ----- Object creation ----- # ----- Object creation -----
print "Creating some objects:" print("Creating some objects:")
a = example.Vector(3, 4, 5) a = example.Vector(3, 4, 5)
b = example.Vector(10, 11, 12) b = example.Vector(10, 11, 12)
print " Created", a.cprint() print(" Created %s" % a.cprint())
print " Created", b.cprint() print(" Created %s" % b.cprint())
# ----- Call an overloaded operator ----- # ----- Call an overloaded operator -----
@ -21,9 +21,9 @@ print " Created", b.cprint()
# #
# It returns a new allocated object. # It returns a new allocated object.
print "Adding a+b" print("Adding a+b")
c = example.addv(a, b) c = example.addv(a, b)
print " a+b =", c.cprint() print(" a+b = %s" % c.cprint())
# Note: Unless we free the result, a memory leak will occur # Note: Unless we free the result, a memory leak will occur
del c del c
@ -31,9 +31,9 @@ del c
# ----- Create a vector array ----- # ----- Create a vector array -----
# Note: Using the high-level interface here # Note: Using the high-level interface here
print "Creating an array of vectors" print("Creating an array of vectors")
va = example.VectorArray(10) va = example.VectorArray(10)
print " va = ", va print(" va = %s" % va)
# ----- Set some values in the array ----- # ----- Set some values in the array -----
@ -45,17 +45,17 @@ va.set(2, example.addv(a, b))
# Get some values from the array # Get some values from the array
print "Getting some array values" print("Getting some array values")
for i in range(0, 5): for i in range(0, 5):
print " va(%d) = %s" % (i, va.get(i).cprint()) print(" va(%d) = %s" % (i, va.get(i).cprint()))
# Watch under resource meter to check on this # Watch under resource meter to check on this
print "Making sure we don't leak memory." print("Making sure we don't leak memory.")
for i in xrange(0, 1000000): for i in range(0, 1000000):
c = va.get(i % 10) c = va.get(i % 10)
# ----- Clean up ----- # ----- Clean up -----
print "Cleaning up" print("Cleaning up")
del va del va
del a del a

View file

@ -7,15 +7,15 @@ import example
x = 42 x = 42
y = 105 y = 105
g = example.gcd(x, y) g = example.gcd(x, y)
print "The gcd of %d and %d is %d" % (x, y, g) print("The gcd of %d and %d is %d" % (x, y, g))
# Manipulate the Foo global variable # Manipulate the Foo global variable
# Output its current value # Output its current value
print "Foo = ", example.cvar.Foo print("Foo = %s" % example.cvar.Foo)
# Change its value # Change its value
example.cvar.Foo = 3.1415926 example.cvar.Foo = 3.1415926
# See if the change took effect # See if the change took effect
print "Foo = ", example.cvar.Foo print("Foo = %s" % example.cvar.Foo)

View file

@ -7,17 +7,17 @@ import example
# ----- Object creation ----- # ----- Object creation -----
print "Creating some objects:" print("Creating some objects:")
cc = example.Circle(10) cc = example.Circle(10)
c = example.ShapePtr(cc) c = example.ShapePtr(cc)
print " Created circle", c print(" Created circle %s" % c)
ss = example.Square(10) ss = example.Square(10)
s = example.ShapePtr(ss) s = example.ShapePtr(ss)
print " Created square", s print(" Created square %s" % s)
# ----- Access a static member ----- # ----- Access a static member -----
print "\nA total of", example.cvar.Shape_nshapes, "shapes were created" print("\nA total of %s shapes were created" % example.cvar.Shape_nshapes)
# ----- Member data access ----- # ----- Member data access -----
@ -29,19 +29,19 @@ c.y = 30
s.x = -10 s.x = -10
s.y = 5 s.y = 5
print "\nHere is their current position:" print("\nHere is their current position:")
print " Circle = (%f, %f)" % (c.x, c.y) print(" Circle = (%f, %f)" % (c.x, c.y))
print " Square = (%f, %f)" % (s.x, s.y) print(" Square = (%f, %f)" % (s.x, s.y))
# ----- Call some methods ----- # ----- Call some methods -----
print "\nHere are some properties of the shapes:" print("\nHere are some properties of the shapes:")
for o in [c, s]: for o in [c, s]:
print " ", o print(" %s" % o)
print " area = ", o.area() print(" area = %s" % o.area())
print " perimeter = ", o.perimeter() print(" perimeter = %s" % o.perimeter())
print "\nGuess I'll clean up now" print("\nGuess I'll clean up now")
# Note: this invokes the virtual destructor # Note: this invokes the virtual destructor
del c del c
@ -50,5 +50,5 @@ del cc
del ss del ss
s = 3 s = 3
print example.cvar.Shape_nshapes, "shapes remain" print("%d shapes remain" % example.cvar.Shape_nshapes)
print "Goodbye" print("Goodbye")

View file

@ -11,45 +11,45 @@ dmap = {}
dmap["hello"] = 1.0 dmap["hello"] = 1.0
dmap["hi"] = 2.0 dmap["hi"] = 2.0
print dmap.items() print(list(dmap.items()))
print dmap.keys() print(list(dmap.keys()))
print dmap.values() print(list(dmap.values()))
print dmap print(dmap)
hmap = example.halfd(dmap) hmap = example.halfd(dmap)
dmap = hmap dmap = hmap
print dmap print(dmap)
for i in dmap.iterkeys(): for i in dmap.keys():
print "key", i print("key %s" % i)
for i in dmap.itervalues(): for i in dmap.values():
print "val", i print("val %s" % i)
for k, v in dmap.iteritems(): for k, v in dmap.items():
print "item", k, v print("item %s %s" % (k, v))
dmap = example.DoubleMap() dmap = example.DoubleMap()
dmap["hello"] = 1.0 dmap["hello"] = 1.0
dmap["hi"] = 2.0 dmap["hi"] = 2.0
for i in dmap.iterkeys(): for i in dmap.keys():
print "key", i print("key %s" % i)
for i in dmap.itervalues(): for i in dmap.values():
print "val", i print("val %s" % i)
for k, v in dmap.iteritems(): for k, v in dmap.items():
print "item", k, v print("item %s %s" % (k, v))
print dmap.items() print(list(dmap.items()))
print dmap.keys() print(list(dmap.keys()))
print dmap.values() print(list(dmap.values()))
hmap = example.halfd(dmap) hmap = example.halfd(dmap)
print hmap.keys() print(list(hmap.keys()))
print hmap.values() print(list(hmap.values()))
dmap = {} dmap = {}
@ -57,23 +57,23 @@ dmap["hello"] = 2
dmap["hi"] = 4 dmap["hi"] = 4
hmap = example.halfi(dmap) hmap = example.halfi(dmap)
print hmap print(hmap)
print hmap.keys() print(list(hmap.keys()))
print hmap.values() print(list(hmap.values()))
dmap = hmap dmap = hmap
for i in dmap.iterkeys(): for i in dmap.keys():
print "key", i print("key %s" % i)
for i in dmap.itervalues(): for i in dmap.values():
print "val", i print("val %s" % i)
for i in dmap.iteritems(): for i in dmap.items():
print "item", i print("item %s" % str(i))
for k, v in dmap.iteritems(): for k, v in dmap.items():
print "item", k, v print("item %s %s" % (k, v))
print dmap print(dmap)

View file

@ -4,32 +4,30 @@ import example
# Call average with a Python list... # Call average with a Python list...
print example.average([1, 2, 3, 4]) print(example.average([1, 2, 3, 4]))
# ... or a wrapped std::vector<int> # ... or a wrapped std::vector<int>
v = example.IntVector(4) v = example.IntVector(4)
for i in range(len(v)): for i in range(len(v)):
v[i] = i + 1 v[i] = i + 1
print example.average(v) print(example.average(v))
# half will return a Python list. # half will return a Python list.
# Call it with a Python tuple... # Call it with a Python tuple...
print example.half((1.0, 1.5, 2.0, 2.5, 3.0)) print(example.half((1.0, 1.5, 2.0, 2.5, 3.0)))
# ... or a wrapped std::vector<double> # ... or a wrapped std::vector<double>
v = example.DoubleVector() v = example.DoubleVector()
for i in [1, 2, 3, 4]: for i in [1, 2, 3, 4]:
v.append(i) v.append(i)
print example.half(v) print(example.half(v))
# now halve a wrapped std::vector<double> in place # now halve a wrapped std::vector<double> in place
example.halve_in_place(v) example.halve_in_place(v)
for i in range(len(v)): print([i for i in v])
print v[i], "; ",
print

View file

@ -3,8 +3,8 @@
import example import example
# Call some templated functions # Call some templated functions
print example.maxint(3, 7) print(example.maxint(3, 7))
print example.maxdouble(3.14, 2.18) print(example.maxdouble(3.14, 2.18))
# Create some class # Create some class
@ -21,12 +21,12 @@ sum = 0
for i in range(0, 100): for i in range(0, 100):
sum = sum + iv.getitem(i) sum = sum + iv.getitem(i)
print sum print(sum)
sum = 0.0 sum = 0.0
for i in range(0, 1000): for i in range(0, 1000):
sum = sum + dv.getitem(i) sum = sum + dv.getitem(i)
print sum print(sum)
del iv del iv
del dv del dv

View file

@ -22,51 +22,51 @@ example.cvar.name = "Bill"
# Now print out the values of the variables # Now print out the values of the variables
print "Variables (values printed from Python)" print("Variables (values printed from Python)")
print "ivar =", example.cvar.ivar print("ivar = %s" % example.cvar.ivar)
print "svar =", example.cvar.svar print("svar = %s" % example.cvar.svar)
print "lvar =", example.cvar.lvar print("lvar = %s" % example.cvar.lvar)
print "uivar =", example.cvar.uivar print("uivar = %s" % example.cvar.uivar)
print "usvar =", example.cvar.usvar print("usvar = %s" % example.cvar.usvar)
print "ulvar =", example.cvar.ulvar print("ulvar = %s" % example.cvar.ulvar)
print "scvar =", example.cvar.scvar print("scvar = %s" % example.cvar.scvar)
print "ucvar =", example.cvar.ucvar print("ucvar = %s" % example.cvar.ucvar)
print "fvar =", example.cvar.fvar print("fvar = %s" % example.cvar.fvar)
print "dvar =", example.cvar.dvar print("dvar = %s" % example.cvar.dvar)
print "cvar =", example.cvar.cvar print("cvar = %s" % example.cvar.cvar)
print "strvar =", example.cvar.strvar print("strvar = %s" % example.cvar.strvar)
print "cstrvar =", example.cvar.cstrvar print("cstrvar = %s" % example.cvar.cstrvar)
print "iptrvar =", example.cvar.iptrvar print("iptrvar = %s" % example.cvar.iptrvar)
print "name =", example.cvar.name print("name = %s" % example.cvar.name)
print "ptptr =", example.cvar.ptptr, example.Point_print(example.cvar.ptptr) print("ptptr = %s %s" % (example.cvar.ptptr, example.Point_print(example.cvar.ptptr)))
print "pt =", example.cvar.pt, example.Point_print(example.cvar.pt) print("pt = %s %s" % (example.cvar.pt, example.Point_print(example.cvar.pt)))
print "\nVariables (values printed from C)" print("\nVariables (values printed from C)")
example.print_vars() example.print_vars()
print "\nNow I'm going to try and modify some read only variables" print("\nNow I'm going to try and modify some read only variables")
print " Trying to set 'path'" print(" Trying to set 'path'")
try: try:
example.cvar.path = "Whoa!" example.cvar.path = "Whoa!"
print "Hey, what's going on?!?! This shouldn't work" print("Hey, what's going on?!?! This shouldn't work")
except Exception: except Exception:
print "Good." print("Good.")
print " Trying to set 'status'" print(" Trying to set 'status'")
try: try:
example.cvar.status = 0 example.cvar.status = 0
print "Hey, what's going on?!?! This shouldn't work" print("Hey, what's going on?!?! This shouldn't work")
except Exception: except Exception:
print "Good." print("Good.")
print "\nI'm going to try and update a structure variable.\n" print("\nI'm going to try and update a structure variable.\n")
example.cvar.pt = example.cvar.ptptr example.cvar.pt = example.cvar.ptptr
print "The new value is" print("The new value is")
example.pt_print() example.pt_print()
print "You should see the value", example.Point_print(example.cvar.ptptr) print("You should see the value %s" % example.Point_print(example.cvar.ptptr))