Skip to content

Commit d5202b7

Browse files
SiegeLordExSiegeLord
authored andcommitted
Convert our Python files to Python3.
This mostly involved switching 'prints' to the function syntax, but in make_converters.py this process revealed a bug where the generated code epended on the iteration order of a dictionary.
1 parent 878420f commit d5202b7

File tree

9 files changed

+184
-155
lines changed

9 files changed

+184
-155
lines changed

contrib/luajit/generate_luajit_ffi.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
"""
44
This script will use the prototypes from "checkdocs.py -s" to concoct
@@ -80,10 +80,17 @@ def parse_protos(self, filename):
8080
if "=" in field:
8181
fname, val = field.split("=", 1)
8282
fname = fname.strip()
83+
# replace any 'X' (an integer value in C) with
84+
# ord('X') to match up in Python
85+
val = re.sub("('.')", "ord(\\1)", val)
8386
try:
8487
i = int(eval(val, globals(), self.constants))
8588
except NameError:
8689
i = val
90+
except Exception:
91+
raise ValueError(
92+
"Exception while parsing '{}'".format(
93+
val))
8794
else:
8895
fname = field.strip()
8996
if not fname:

misc/make_converters.py

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/usr/bin/env python
2-
import optparse, re, sys
1+
#!/usr/bin/env python3
2+
import collections, optparse, re, sys
33

44
formats_by_name = {}
55
formats_list = []
@@ -32,46 +32,58 @@ def parse_format(format):
3232
separator = format.find("_")
3333
class Info: pass
3434
class Component: pass
35+
36+
Info = collections.namedtuple("Info", "components, name, little_endian,"
37+
"float, single_channel, size")
38+
Component = collections.namedtuple("Component", "color, size,"
39+
"position_from_left, position")
40+
3541
pos = 0
36-
info = Info()
37-
info.components = {}
38-
info.name = format
39-
info.little_endian = "_LE" in format
40-
info.float = False
41-
info.single_channel = False
42+
info = Info(
43+
components={},
44+
name=format,
45+
little_endian="_LE" in format,
46+
float=False,
47+
single_channel=False,
48+
size=None,
49+
)
4250

4351
if "F32" in format:
44-
info.float = True
45-
info.size = 128
46-
return info
47-
52+
return info._replace(
53+
float=True,
54+
size=128,
55+
)
56+
4857
if "SINGLE_CHANNEL" in format:
49-
info.single_channel = True
50-
info.size = 8
51-
return info
52-
58+
return info._replace(
59+
single_channel=True,
60+
size=8,
61+
)
62+
5363
for i in range(separator):
54-
c = Component()
55-
c.color = format[i]
56-
c.size = int(format[separator + 1 + i])
57-
c.position_from_left = pos
64+
c = Component(
65+
color=format[i],
66+
size=int(format[separator + 1 + i]),
67+
position_from_left=pos,
68+
position=None,
69+
)
5870
info.components[c.color] = c
5971
pos += c.size
6072
size = pos
61-
for c in info.components.values():
62-
c.position = size - c.position_from_left - c.size
63-
info.size = size
64-
65-
info.float = False
66-
return info
73+
return info._replace(
74+
components={k: c._replace(position=size - c.position_from_left - c.size)
75+
for k, c in info.components.items()},
76+
size=size,
77+
float=False,
78+
)
6779

6880
def macro_lines(info_a, info_b):
6981
"""
7082
Write out the lines of a conversion macro.
7183
"""
7284
r = ""
7385

74-
names = info_b.components.keys()
86+
names = list(info_b.components.keys())
7587
names.sort()
7688

7789
if info_a.float:
@@ -174,20 +186,24 @@ def macro_lines(info_a, info_b):
174186
# Collapse multiple components if possible.
175187
common_shifts = {}
176188
for name, (mask, shift, add, size_a, size_b, mask_pos) in ops.items():
177-
if not add:
189+
if not add and not (size_a != 8 and size_b == 8):
178190
if shift in common_shifts: common_shifts[shift].append(name)
179191
else: common_shifts[shift] = [name]
180192
for newshift, colors in common_shifts.items():
181193
if len(colors) == 1: continue
182194
newname = ""
183195
newmask = 0
184196
colors.sort()
197+
masks_pos = []
185198
for name in colors:
199+
mask, shift, add, size_a, size_b, mask_pos = ops[name]
200+
186201
names.remove(name)
187202
newname += name
188-
newmask |= ops[name][0]
203+
newmask |= mask
204+
masks_pos.append(mask_pos)
189205
names.append(newname)
190-
ops[newname] = (newmask, shift, 0, size_a, size_b, mask_pos)
206+
ops[newname] = (newmask, newshift, 0, size_a, size_b, min(masks_pos))
191207

192208
# Write out a line for each remaining operation.
193209
lines = []
@@ -441,7 +457,7 @@ def main(argv):
441457

442458
# Read in color.h to get the available formats.
443459
formats = read_color_h("include/allegro5/color.h")
444-
460+
445461
print(formats)
446462

447463
# Parse the component info for each format.

misc/make_mixer_helpers.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
22
#
33
# Run:
4-
# python2 misc/make_resamplers.py | indent -kr -i3 -l0
4+
# misc/make_resamplers.py | indent -kr -i3 -l0
55

66
import sys, re
77

@@ -88,7 +88,7 @@ def index_s16(self, buf, index):
8888
]
8989

9090
def make_point_interpolator(name, fmt):
91-
print interp("""\
91+
print(interp("""\
9292
static INLINE const void *
9393
#{name}
9494
(SAMP_BUF *samp_buf,
@@ -99,27 +99,27 @@ def make_point_interpolator(name, fmt):
9999
unsigned int i;
100100
101101
switch (spl->spl_data.depth) {
102-
""")
102+
"""))
103103

104104
for depth in depths:
105105
buf_index = depth.index(fmt)("spl->spl_data.buffer", "i0 + i")
106-
print interp("""\
106+
print(interp("""\
107107
case #{depth.constant()}:
108108
for (i = 0; i < maxc; i++) {
109109
samp_buf-> #{fmt} [i] = #{buf_index};
110110
}
111111
break;
112-
""")
112+
"""))
113113

114-
print interp("""\
114+
print(interp("""\
115115
}
116116
return samp_buf-> #{fmt} ;
117-
}""")
117+
}"""))
118118

119119
def make_linear_interpolator(name, fmt):
120120
assert fmt == "f32" or fmt == "s16"
121121

122-
print interp("""\
122+
print(interp("""\
123123
static INLINE const void *
124124
#{name}
125125
(SAMP_BUF *samp_buf,
@@ -164,50 +164,50 @@ def make_linear_interpolator(name, fmt):
164164
p1 *= maxc;
165165
166166
switch (spl->spl_data.depth) {
167-
""")
167+
"""))
168168

169169
for depth in depths:
170170
x0 = depth.index(fmt)("spl->spl_data.buffer", "p0 + i")
171171
x1 = depth.index(fmt)("spl->spl_data.buffer", "p1 + i")
172-
print interp("""\
172+
print(interp("""\
173173
case #{depth.constant()}:
174-
{""")
174+
{"""))
175175

176176
if fmt == "f32":
177-
print interp("""\
177+
print(interp("""\
178178
const float t = (float)spl->pos_bresenham_error / spl->step_denom;
179179
int i;
180180
for (i = 0; i < (int)maxc; i++) {
181181
const float x0 = #{x0};
182182
const float x1 = #{x1};
183183
const float s = (x0 * (1.0f - t)) + (x1 * t);
184184
samp_buf->f32[i] = s;
185-
}""")
185+
}"""))
186186
elif fmt == "s16":
187-
print interp("""\
187+
print(interp("""\
188188
const int32_t t = 256 * spl->pos_bresenham_error / spl->step_denom;
189189
int i;
190190
for (i = 0; i < (int)maxc; i++) {
191191
const int32_t x0 = #{x0};
192192
const int32_t x1 = #{x1};
193193
const int32_t s = ((x0 * (256 - t))>>8) + ((x1 * t)>>8);
194194
samp_buf->s16[i] = (int16_t)s;
195-
}""")
195+
}"""))
196196

197-
print interp("""\
197+
print(interp("""\
198198
}
199199
break;
200-
""")
200+
"""))
201201

202-
print interp("""\
202+
print(interp("""\
203203
}
204204
return samp_buf-> #{fmt};
205-
}""")
205+
}"""))
206206

207207
def make_cubic_interpolator(name, fmt):
208208
assert fmt == "f32"
209209

210-
print interp("""\
210+
print(interp("""\
211211
static INLINE const void *
212212
#{name}
213213
(SAMP_BUF *samp_buf,
@@ -256,7 +256,7 @@ def make_cubic_interpolator(name, fmt):
256256
p3 *= maxc;
257257
258258
switch (spl->spl_data.depth) {
259-
""")
259+
"""))
260260

261261
for depth in depths:
262262
value0 = depth.index(fmt)("spl->spl_data.buffer", "p0 + i")
@@ -267,7 +267,7 @@ def make_cubic_interpolator(name, fmt):
267267
# Code transcribed from "Polynomial Interpolators for High-Quality
268268
# Resampling of Oversampled Audio" by Olli Niemitalo
269269
# http://yehar.com/blog/?p=197
270-
print interp("""\
270+
print(interp("""\
271271
case #{depth.constant()}:
272272
{
273273
const float t = (float)spl->pos_bresenham_error / spl->step_denom;
@@ -286,16 +286,16 @@ def make_cubic_interpolator(name, fmt):
286286
}
287287
}
288288
break;
289-
""")
289+
"""))
290290

291-
print interp("""\
291+
print(interp("""\
292292
}
293293
return samp_buf-> #{fmt} ;
294-
}""")
294+
}"""))
295295

296296
if __name__ == "__main__":
297-
print "// Warning: This file was created by make_resamplers.py - do not edit."
298-
print "// vim: set ft=c:"
297+
print("// Warning: This file was created by make_resamplers.py - do not edit.")
298+
print("// vim: set ft=c:")
299299

300300
make_point_interpolator("point_spl32", "f32")
301301
make_point_interpolator("point_spl16", "s16")

0 commit comments

Comments
 (0)