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

View File

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

View File

@ -2,22 +2,24 @@
import example
print "ICONST =", example.ICONST, "(should be 42)"
print "FCONST =", example.FCONST, "(should be 2.1828)"
print "CCONST =", example.CCONST, "(should be 'x')"
print "CCONST2 =", example.CCONST2, "(this should be on a new line)"
print "SCONST =", example.SCONST, "(should be 'Hello World')"
print "SCONST2 =", example.SCONST2, "(should be '\"Hello World\"')"
print "EXPR =", example.EXPR, "(should be 48.5484)"
print "iconst =", example.iconst, "(should be 37)"
print "fconst =", example.fconst, "(should be 3.14)"
print("ICONST = %s (should be 42)" % example.ICONST)
print("FCONST = %s (should be 2.1828)" % example.FCONST)
print("CCONST = %s (should be 'x')" % example.CCONST)
print("CCONST2 = %s (this should be on a new line)" % example.CCONST2)
print("SCONST = %s (should be 'Hello World')" % example.SCONST)
print("SCONST2 = %s (should be '\"Hello World\"')" % example.SCONST2)
print("EXPR = %s (should be 48.5484)" % example.EXPR)
print("iconst = %s (should be 37)" % example.iconst)
print("fconst = %s (should be 3.14)" % example.fconst)
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:
print "EXTERN isn't defined (good)"
print("EXTERN isn't defined (good)")
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:
print "FOO isn't defined (good)"
print("FOO isn't defined (good)")

View File

@ -7,15 +7,15 @@ import example
x = 42
y = 105
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
# Output its current value
print "Foo = ", example.cvar.Foo
print("Foo = %s" % example.cvar.Foo)
# Change its value
example.cvar.Foo = 3.1415926
# 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
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
print "Creating some objects:"
print("Creating some objects:")
c = example.MakeCircle(10)
print " Created circle", c
print(" Created circle %s" % c)
s = example.MakeSquare(10)
print " Created square", s
print(" Created square %s" % s)
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]:
print " ", o
print " area = ", o.area()
print " perimeter = ", o.perimeter()
print(" %s" % o)
print(" area = %s" % o.area())
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
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 -----
# Print out the value of some enums
print "*** color ***"
print " RED =", example.RED
print " BLUE =", example.BLUE
print " GREEN =", example.GREEN
print("*** color ***")
print(" RED = %s" % example.RED)
print(" BLUE = %s" % example.BLUE)
print(" GREEN = %s" % example.GREEN)
print "\n*** Foo::speed ***"
print " Foo_IMPULSE =", example.Foo.IMPULSE
print " Foo_WARP =", example.Foo.WARP
print " Foo_LUDICROUS =", example.Foo.LUDICROUS
print("\n*** Foo::speed ***")
print(" Foo_IMPULSE = %s" % example.Foo.IMPULSE)
print(" Foo_WARP = %s" % example.Foo.WARP)
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.BLUE, example.Foo.WARP)
example.enum_test(example.GREEN, example.Foo.LUDICROUS)
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.enum_test(example.Foo.IMPULSE)

View File

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

View File

@ -2,44 +2,44 @@
import example
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)
q = example.intQueue(10)
print "Inserting items into intQueue"
print("Inserting items into intQueue")
print type(example.FullError)
print(type(example.FullError))
try:
for i in range(0, 100):
q.enqueue(i)
except example.FullError, e:
print "Maxsize is", e.maxsize
except example.FullError as e:
print("Maxsize is %s" % e.maxsize)
print "Removing items"
print("Removing items")
try:
while 1:
while True:
q.dequeue()
except example.EmptyError, e:
except example.EmptyError as e:
pass
q = example.doubleQueue(1000)
print "Inserting items into doubleQueue"
print("Inserting items into doubleQueue")
try:
for i in range(0, 10000):
q.enqueue(i * 1.5)
except example.FullError, e:
print "Maxsize is", e.maxsize
except example.FullError as e:
print("Maxsize is %s" % e.maxsize)
print "Removing items"
print("Removing items")
try:
while 1:
while True:
q.dequeue()
except example.EmptyError, e:
except example.EmptyError as e:
pass

View File

@ -21,9 +21,9 @@ class CEO(example.Manager):
# the director wrappers to call CEO.getPosition.
e = CEO("Alice")
print e.getName(), "is a", e.getPosition()
print "Just call her \"%s\"" % e.getTitle()
print "----------------------"
print("%s is a %s" % (e.getName(), e.getPosition()))
print("Just call her \"%s\"" % e.getTitle())
print("----------------------")
# Create a new EmployeeList instance. This class does not have a C++
@ -40,7 +40,7 @@ list = example.EmployeeList()
e = e.__disown__()
list.addEmployee(e)
print "----------------------"
print("----------------------")
# 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
@ -59,13 +59,13 @@ print "----------------------"
# passes down through the C++ director class to the Python implementation
# 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(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(3).getPosition(), list.get_item(3).getTitle())
print "----------------------"
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(2).getPosition(), list.get_item(2).getTitle()))
print(" %s, \"%s\"" % (list.get_item(3).getPosition(), list.get_item(3).getTitle()))
print("----------------------")
# 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
@ -75,8 +75,8 @@ print "----------------------"
# usual to destroy the object.
del list
print "----------------------"
print("----------------------")
# 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
print "Trying some C callback functions"
print " a =", a
print " b =", b
print " ADD(a,b) =", example.do_op(a, b, example.ADD)
print " SUB(a,b) =", example.do_op(a, b, example.SUB)
print " MUL(a,b) =", example.do_op(a, b, example.MUL)
print("Trying some C callback functions")
print(" a = %s" % a)
print(" b = %s" % b)
print(" ADD(a,b) = %s" % example.do_op(a, b, example.ADD))
print(" SUB(a,b) = %s" % example.do_op(a, b, example.SUB))
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 " ADD =", example.ADD
print " SUB =", example.SUB
print " MUL =", example.MUL
print("Here is what the C callback function objects look like in Python")
print(" ADD = %s" % example.ADD)
print(" SUB = %s" % example.SUB)
print(" MUL = %s" % example.MUL)

View File

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

View File

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

View File

@ -6,26 +6,26 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw
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
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):
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)
if sys.version_info < (3, 0):
import py2.pkg2
print " Finished importing py2.pkg2"
print(" Finished importing py2.pkg2")
commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.foo"
run_except_on_windows(commandline)
else:
import py3.pkg2
print " Finished importing py3.pkg2"
print(" Finished importing py3.pkg2")
# commandline = sys.executable + " -m py3.pkg2.bar"
# run_except_on_windows(commandline)
# 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":
# Strange failures on windows/cygin/mingw
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
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):
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)
if sys.version_info < (3, 0):
import py2.pkg2
print " Finished importing py2.pkg2"
print(" Finished importing py2.pkg2")
commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline)
else:
import py3.pkg2
print " Finished importing py3.pkg2"
print(" Finished importing py3.pkg2")
# commandline = sys.executable + " -m py3.pkg2.bar"
# 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":
# Strange failures on windows/cygin/mingw
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
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):
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)
if sys.version_info < (3, 0):
import py2.pkg2
print " Finished importing py2.pkg2"
print(" Finished importing py2.pkg2")
commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline)
else:
import py3.pkg2
print " Finished importing py3.pkg2"
print(" Finished importing py3.pkg2")
# commandline = sys.executable + " -m py3.pkg2.bar"
# 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
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):
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.
# Can be fixed by using this in the interface file:
# %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)
import pkg1
print " Finished importing pkg1"
print(" Finished importing pkg1")
if pkg1.foofunction(123) != 1230:
raise RuntimeError("foofunction failed")
@ -23,4 +23,4 @@ fc = pkg1.FooClass()
if fc.foomethod(1) != 6:
raise RuntimeError("foomethod failed")
print " Finished testing pkg1"
print(" Finished testing pkg1")

View File

@ -5,14 +5,14 @@ import subprocess
import sys
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):
print " Not importing nstest as Python version is < 3.3"
print(" Not importing nstest as Python version is < 3.3")
sys.exit(0)
import nstest
print " Finished importing nstest"
print(" Finished importing nstest")
nstest.main()

View File

@ -6,26 +6,26 @@ def run_except_on_windows(commandline, env=None):
if os.name != "nt" and sys.platform != "cygwin":
# Strange failures on windows/cygin/mingw
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
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):
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)
if sys.version_info < (3, 0):
import py2.pkg2.bar
print " Finished importing py2.pkg2.bar"
print(" Finished importing py2.pkg2.bar")
commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.pkg3.foo"
run_except_on_windows(commandline)
else:
import py3.pkg2.bar
print " Finished importing py3.pkg2.bar"
print(" Finished importing py3.pkg2.bar")
commandline = sys.executable + " -m py3.pkg2.bar"
run_except_on_windows(commandline)
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":
# Strange failures on windows/cygin/mingw
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
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):
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)
if sys.version_info < (3, 0):
import py2.pkg2.bar
print " Finished importing py2.pkg2.bar"
print(" Finished importing py2.pkg2.bar")
commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.pkg3.pkg4.foo"
run_except_on_windows(commandline)
else:
import py3.pkg2.bar
print " Finished importing py3.pkg2.bar"
print(" Finished importing py3.pkg2.bar")
commandline = sys.executable + " -m py3.pkg2.bar"
run_except_on_windows(commandline)
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":
# Strange failures on windows/cygin/mingw
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
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):
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)
if sys.version_info < (3, 0):
import py2.pkg2.bar
print " Finished importing py2.pkg2.bar"
print(" Finished importing py2.pkg2.bar")
commandline = sys.executable + " -m py2.pkg2.bar"
run_except_on_windows(commandline)
commandline = sys.executable + " -m py2.pkg2.pkg3.foo"
run_except_on_windows(commandline)
else:
import py3.pkg2.bar
print " Finished importing py3.pkg2.bar"
print(" Finished importing py3.pkg2.bar")
commandline = sys.executable + " -m py3.pkg2.bar"
run_except_on_windows(commandline)
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":
# Strange failures on windows/cygin/mingw
subprocess.check_call(commandline, env=env, shell=True)
print(" Finished running: " + commandline)
print((" Finished running: " + commandline))
# Test import of same modules from different packages
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
print " Finished importing pkg2.foo"
print(" Finished importing pkg2.foo")
var2 = pkg2.foo.Pkg2_Foo()
classname = str(type(var2))
if classname.find("pkg2.foo.Pkg2_Foo") == -1:
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"
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":
# Strange failures on windows/cygin/mingw
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__)))
print "Testing " + testname + " - %module(package=...) + python 'import' in __init__.py"
print("Testing " + testname + " - %module(package=...) + python 'import' in __init__.py")
import pkg1.pkg2.foo
print " Finished importing pkg1.pkg2.foo"
print(" Finished importing pkg1.pkg2.foo")
var2 = pkg1.pkg2.foo.Pkg2_Foo()
classname = str(type(var2))
if classname.find("pkg1.pkg2.foo.Pkg2_Foo") == -1:
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"
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":
# Strange failures on windows/cygin/mingw
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__)))
print "Testing " + testname + " - split modules"
print("Testing " + testname + " - split modules")
import pkg1.foo
print " Finished importing pkg1.foo"
print(" Finished importing pkg1.foo")
if not(pkg1.foo.count() == 3):
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":
# Strange failures on windows/cygin/mingw
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__)))
print "Testing " + testname + " - split modules"
print("Testing " + testname + " - split modules")
import pkg1.foo
print " Finished importing pkg1.foo"
print(" Finished importing pkg1.foo")
if not(pkg1.foo.count() == 3):
raise RuntimeError("test failed")

View File

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

View File

@ -7,14 +7,14 @@ import example
x = 42
y = 105
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
example.gcdmain(["gcdmain", "42", "105"])
# Call the count function
print example.count("Hello World", "l")
print(example.count("Hello World", "l"))
# 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)
b = example.Complex(-5, 10)
print "a =", a
print "b =", b
print("a = %s" % a)
print("b = %s" % b)
c = a + b
print "c =", c
print "a*b =", a * b
print "a-c =", a - c
print("c = %s" % c)
print("a*b = %s" % (a * b))
print("a-c = %s" % (a - c))
e = example.ComplexCopy(a - c)
print "e =", e
print("e = %s" % e)
# Big expression
f = ((a + b) * (c + b * e)) + (-a)
print "f =", f
print("f = %s" % f)

View File

@ -3,23 +3,23 @@
import example
# First create some objects using the pointer library.
print "Testing the pointer library"
print("Testing the pointer library")
a = example.new_intp()
b = example.new_intp()
c = example.new_intp()
example.intp_assign(a, 37)
example.intp_assign(b, 42)
print " a =", a
print " b =", b
print " c =", c
print(" a = %s" % a)
print(" b = %s" % b)
print(" c = %s" % c)
# Call the add() function with some pointers
example.add(a, b, c)
# Now get the result
r = example.intp_value(c)
print " 37 + 42 =", r
print(" 37 + 42 = %s" % r)
# Clean up the pointers
example.delete_intp(a)
@ -30,12 +30,12 @@ example.delete_intp(c)
# This should be much easier. Now how it is no longer
# necessary to manufacture pointers.
print "Trying the typemap library"
print("Trying the typemap library")
r = example.sub(37, 42)
print " 37 - 42 =", r
print(" 37 - 42 = %s" % r)
# Now try the version with multiple return values
print "Testing multiple return values"
print("Testing multiple return values")
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 -----
print "Creating some objects:"
print("Creating some objects:")
a = example.Vector(3, 4, 5)
b = example.Vector(10, 11, 12)
print " Created", a.cprint()
print " Created", b.cprint()
print(" Created %s" % a.cprint())
print(" Created %s" % b.cprint())
# ----- Call an overloaded operator -----
@ -21,9 +21,9 @@ print " Created", b.cprint()
#
# It returns a new allocated object.
print "Adding a+b"
print("Adding 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
del c
@ -31,9 +31,9 @@ del c
# ----- Create a vector array -----
# Note: Using the high-level interface here
print "Creating an array of vectors"
print("Creating an array of vectors")
va = example.VectorArray(10)
print " va = ", va
print(" va = %s" % va)
# ----- Set some values in the array -----
@ -45,17 +45,17 @@ va.set(2, example.addv(a, b))
# Get some values from the array
print "Getting some array values"
print("Getting some array values")
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
print "Making sure we don't leak memory."
for i in xrange(0, 1000000):
print("Making sure we don't leak memory.")
for i in range(0, 1000000):
c = va.get(i % 10)
# ----- Clean up -----
print "Cleaning up"
print("Cleaning up")
del va
del a

View File

@ -7,15 +7,15 @@ import example
x = 42
y = 105
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
# Output its current value
print "Foo = ", example.cvar.Foo
print("Foo = %s" % example.cvar.Foo)
# Change its value
example.cvar.Foo = 3.1415926
# 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 -----
print "Creating some objects:"
print("Creating some objects:")
cc = example.Circle(10)
c = example.ShapePtr(cc)
print " Created circle", c
print(" Created circle %s" % c)
ss = example.Square(10)
s = example.ShapePtr(ss)
print " Created square", s
print(" Created square %s" % s)
# ----- 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 -----
@ -29,19 +29,19 @@ c.y = 30
s.x = -10
s.y = 5
print "\nHere is their current position:"
print " Circle = (%f, %f)" % (c.x, c.y)
print " Square = (%f, %f)" % (s.x, s.y)
print("\nHere is their current position:")
print(" Circle = (%f, %f)" % (c.x, c.y))
print(" Square = (%f, %f)" % (s.x, s.y))
# ----- Call some methods -----
print "\nHere are some properties of the shapes:"
print("\nHere are some properties of the shapes:")
for o in [c, s]:
print " ", o
print " area = ", o.area()
print " perimeter = ", o.perimeter()
print(" %s" % o)
print(" area = %s" % o.area())
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
del c
@ -50,5 +50,5 @@ del cc
del ss
s = 3
print example.cvar.Shape_nshapes, "shapes remain"
print "Goodbye"
print("%d shapes remain" % example.cvar.Shape_nshapes)
print("Goodbye")

View File

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

View File

@ -4,32 +4,30 @@ import example
# 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>
v = example.IntVector(4)
for i in range(len(v)):
v[i] = i + 1
print example.average(v)
print(example.average(v))
# half will return a Python list.
# 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>
v = example.DoubleVector()
for i in [1, 2, 3, 4]:
v.append(i)
print example.half(v)
print(example.half(v))
# now halve a wrapped std::vector<double> in place
example.halve_in_place(v)
for i in range(len(v)):
print v[i], "; ",
print
print([i for i in v])

View File

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

View File

@ -22,51 +22,51 @@ example.cvar.name = "Bill"
# 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 "svar =", example.cvar.svar
print "lvar =", example.cvar.lvar
print "uivar =", example.cvar.uivar
print "usvar =", example.cvar.usvar
print "ulvar =", example.cvar.ulvar
print "scvar =", example.cvar.scvar
print "ucvar =", example.cvar.ucvar
print "fvar =", example.cvar.fvar
print "dvar =", example.cvar.dvar
print "cvar =", example.cvar.cvar
print "strvar =", example.cvar.strvar
print "cstrvar =", example.cvar.cstrvar
print "iptrvar =", example.cvar.iptrvar
print "name =", example.cvar.name
print "ptptr =", example.cvar.ptptr, example.Point_print(example.cvar.ptptr)
print "pt =", example.cvar.pt, example.Point_print(example.cvar.pt)
print("ivar = %s" % example.cvar.ivar)
print("svar = %s" % example.cvar.svar)
print("lvar = %s" % example.cvar.lvar)
print("uivar = %s" % example.cvar.uivar)
print("usvar = %s" % example.cvar.usvar)
print("ulvar = %s" % example.cvar.ulvar)
print("scvar = %s" % example.cvar.scvar)
print("ucvar = %s" % example.cvar.ucvar)
print("fvar = %s" % example.cvar.fvar)
print("dvar = %s" % example.cvar.dvar)
print("cvar = %s" % example.cvar.cvar)
print("strvar = %s" % example.cvar.strvar)
print("cstrvar = %s" % example.cvar.cstrvar)
print("iptrvar = %s" % example.cvar.iptrvar)
print("name = %s" % example.cvar.name)
print("ptptr = %s %s" % (example.cvar.ptptr, example.Point_print(example.cvar.ptptr)))
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()
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:
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:
print "Good."
print("Good.")
print " Trying to set 'status'"
print(" Trying to set 'status'")
try:
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:
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
print "The new value is"
print("The new value is")
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))