Skip to content

Commit 45802c6

Browse files
committed
python3 support
1 parent 0193e37 commit 45802c6

5 files changed

+109
-88
lines changed

example_basic.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# while the second version does the same with the memory state
44
# by symbol resolution with some additional access tests.
55
# A third version (commented out) shows how to write an executable.
6+
from __future__ import print_function
67

78
from tinycc import TinyCC
89
from ctypes import (CFUNCTYPE, c_int, c_void_p, Structure, c_ubyte,
@@ -77,16 +78,16 @@ def bytes(self, bytes):
7778
tcc = TinyCC()
7879

7980
# 1st version - simple direct run of main function
80-
print 'simple "run" state:'
81+
print('simple "run" state:')
8182
state = tcc.create_state('run')
8283
state.compile(C_CODE)
8384
result = state.run(['stuff', 'from', 'the', 'cmdline'])
84-
print 'result:', result
85+
print('result:', result)
8586

86-
print
87+
print()
8788

8889
# 2nd version - more complex run via symbols
89-
print '"memory" state with symbol access:'
90+
print('"memory" state with symbol access:')
9091
state = tcc.create_state() # defaults to 'memory'
9192
state.compile(C_CODE)
9293
state.relocate()
@@ -97,21 +98,21 @@ def bytes(self, bytes):
9798
value = state.get_symbol('value', c_int)
9899

99100
# alter test.bytes
100-
test.bytes = bytearray('Python was here...')
101+
test.bytes = bytearray(b'Python was here...')
101102

102103
# prepare main arguments
103-
arguments = ['this', 'is', 'more', 'interactive...']
104+
arguments = [b'this', b'is', b'more', b'interactive...']
104105
argc = len(arguments)
105106
argv = (POINTER(c_char) * argc)()
106107
argv[:] = [create_string_buffer(s) for s in arguments]
107108

108109
# call main
109110
result = main(argc, argv)
110-
print 'result:', result
111+
print('result:', result)
111112

112113
# read exported globals
113-
print 'global "test.bytes"', repr(test.bytes)
114-
print 'global "value":', repr(value)
114+
print('global "test.bytes"', repr(test.bytes))
115+
print('global "value":', repr(value))
115116

116117
# 3rd version - write an executable
117118
#state = tcc.create_state('exe')

example_functions.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
#
99
# NOTE: For trivial tasks the overhead of the ctype conversions
1010
# will let the C code run slower than a CPython equivalent.
11+
from __future__ import print_function
1112

1213
from tinycc import TinyCC, InlineGenerator
1314
from ctypes import c_int
1415

16+
try:
17+
range = xrange
18+
except NameError:
19+
pass
20+
1521
# create a generator object first
1622
gen = InlineGenerator()
1723

@@ -87,14 +93,14 @@ def c_runner_l(n):
8793

8894
def py_runner_r(n):
8995
sum = 0
90-
for _ in xrange(n):
96+
for _ in range(n):
9197
sum += r_fib_py(20)
9298
return sum
9399

94100

95101
def py_runner_l(n):
96102
sum = 0
97-
for _ in xrange(n):
103+
for _ in range(n):
98104
sum += l_fib_py(20)
99105
return sum
100106

@@ -123,14 +129,14 @@ def c_runner_lc(n):
123129

124130
def py_runner_rc(n):
125131
sum = 0
126-
for _ in xrange(n):
132+
for _ in range(n):
127133
sum += r_fib_c(20)
128134
return sum
129135

130136

131137
def py_runner_lc(n):
132138
sum = 0
133-
for _ in xrange(n):
139+
for _ in range(n):
134140
sum += l_fib_c(20)
135141
return sum
136142

@@ -155,14 +161,14 @@ def py_runner_lc(n):
155161
from timeit import timeit
156162
py = timeit('r_fib_py(20)', setup='from __main__ import r_fib_py', number=100)
157163
c = timeit('r_fib_c(20)', setup='from __main__ import r_fib_c', number=100)
158-
print 'recursive Python/C:', py, c, float(py) / c
164+
print('recursive Python/C:', py, c, float(py) / c)
159165

160166
py = timeit('l_fib_py(20)', setup='from __main__ import l_fib_py', number=100)
161167
c = timeit('l_fib_c(20)', setup='from __main__ import l_fib_c', number=100)
162-
print 'looped Python/C:', py, c, float(py) / c
168+
print('looped Python/C:', py, c, float(py) / c)
163169

164170
# some call tests Python <--> C
165-
print 'workload - sum(100x fib(20)):'
171+
print('workload - sum(100x fib(20)):')
166172
assert(c_runner_l(100) == py_runner_l(100))
167173
assert(c_runner_l(100) == py_runner_r(100))
168174
assert(c_runner_l(100) == c_runner_rc(100))
@@ -173,19 +179,19 @@ def py_runner_lc(n):
173179
assert(c_runner_r(100) == c_runner_l(100))
174180
r = timeit('c_runner_r(100)', setup='from __main__ import c_runner_r', number=1)
175181
l = timeit('c_runner_l(100)', setup='from __main__ import c_runner_l', number=1)
176-
print 'fib_py from C (rec/loop):', r, l, float(r) / l
182+
print('fib_py from C (rec/loop):', r, l, float(r) / l)
177183

178184
assert(py_runner_r(100) == py_runner_l(100))
179185
r = timeit('py_runner_r(100)', setup='from __main__ import py_runner_r', number=1)
180186
l = timeit('py_runner_l(100)', setup='from __main__ import py_runner_l', number=1)
181-
print 'fib_py from Py (rec/loop):', r, l, float(r) / l
187+
print('fib_py from Py (rec/loop):', r, l, float(r) / l)
182188

183189
assert(c_runner_rc(100) == c_runner_lc(100))
184190
r = timeit('c_runner_rc(100)', setup='from __main__ import c_runner_rc', number=1)
185191
l = timeit('c_runner_lc(100)', setup='from __main__ import c_runner_lc', number=1)
186-
print 'fib_c from C (rec/loop):', r, l, float(r) / l
192+
print('fib_c from C (rec/loop):', r, l, float(r) / l)
187193

188194
assert(py_runner_rc(100) == py_runner_lc(100))
189195
r = timeit('py_runner_rc(100)', setup='from __main__ import py_runner_rc', number=1)
190196
l = timeit('py_runner_lc(100)', setup='from __main__ import py_runner_lc', number=1)
191-
print 'fib_c from Py (rec/loop):', r, l, float(r) / l
197+
print('fib_c from Py (rec/loop):', r, l, float(r) / l)

example_sdl.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# and a callback invocation.
33
# NOTE: For Windows the package contains a binary version
44
# of the SDL2 library.
5+
from __future__ import print_function
56

67
from tinycc import TinyCC
78
from ctypes import CFUNCTYPE, c_int, c_void_p, CDLL
@@ -61,7 +62,7 @@
6162
# callback called from C
6263
@CFUNCTYPE(None, c_int, c_int, c_int)
6364
def get_color(r, g, b):
64-
print 'color is rgb(%d, %d, %d)' % (r, g, b)
65+
print('color is rgb(%d, %d, %d)' % (r, g, b))
6566

6667

6768
if __name__ == '__main__':

example_struct.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Example to show inlining with a struct.
2+
from __future__ import print_function
23

34
from tinycc import TinyCC, InlineGenerator
45
from ctypes import c_int, addressof, c_long
@@ -34,8 +35,7 @@ def adder_c(self):
3435

3536
@gen.callable_method(c_int)
3637
def adder_py(self):
37-
print 'Test.adder_py: %d + %d = %d' % (
38-
self.a, self.b, self.a+self.b)
38+
print('Test.adder_py: %d + %d = %d' % (self.a, self.b, self.a+self.b))
3939
return self.a + self.b
4040

4141
@gen.c_method(c_int)
@@ -57,7 +57,7 @@ def struct_as_return(a, b):
5757
if __name__ == '__main__':
5858
state = TinyCC().create_state()
5959

60-
print gen.code
60+
print(gen.code)
6161

6262
state.compile(gen.code)
6363
state.relocate()
@@ -66,12 +66,12 @@ def struct_as_return(a, b):
6666

6767
# usage
6868
test = Test(1, 2)
69-
print 'addresses equal:', addressof(test) == test.get_address()
70-
print test.add_a(10)
71-
print test.adder_c()
72-
print test.adder_py()
73-
print test.call_methods_c()
69+
print('addresses equal:', addressof(test) == test.get_address())
70+
print(test.add_a(10))
71+
print(test.adder_c())
72+
print(test.adder_py())
73+
print(test.call_methods_c())
7474

7575
# return struct: bug in TCC 0.9.26 zip release
7676
test2 = struct_as_return(23, 24)
77-
print test2, test2.a, test2.b
77+
print(test2, test2.a, test2.b)

0 commit comments

Comments
 (0)