aboutsummaryrefslogtreecommitdiff
blob: d594f56d01c1c588076e747e8234b2f517e54645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
http://bugs.python.org/issue13032

--- Tools/scripts/h2py.py
+++ Tools/scripts/h2py.py
@@ -23,36 +23,36 @@
 
 import sys, re, getopt, os
 
-p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
+p_define = re.compile(b'^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
 
 p_macro = re.compile(
-  '^[\t ]*#[\t ]*define[\t ]+'
-  '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
+  b'^[\t ]*#[\t ]*define[\t ]+'
+  b'([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
 
-p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
+p_include = re.compile(b'^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
 
-p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
-p_cpp_comment = re.compile('//.*')
+p_comment = re.compile(br'/\*([^*]+|\*+[^/])*(\*+/)?')
+p_cpp_comment = re.compile(b'//.*')
 
 ignores = [p_comment, p_cpp_comment]
 
-p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
+p_char = re.compile(br"'(\\.[^\\]*|[^\\])'")
 
-p_hex = re.compile(r"0x([0-9a-fA-F]+)L?")
+p_hex = re.compile(br"0x([0-9a-fA-F]+)L?")
 
 filedict = {}
 importable = {}
 
 try:
-    searchdirs=os.environ['include'].split(';')
+    searchdirs=os.environb[b'include'].split(b';')
 except KeyError:
     try:
-        searchdirs=os.environ['INCLUDE'].split(';')
+        searchdirs=os.environb[b'INCLUDE'].split(b';')
     except KeyError:
-        searchdirs=['/usr/include']
+        searchdirs=[b'/usr/include']
         try:
-            searchdirs.insert(0, os.path.join('/usr/include',
-                                              os.environ['MULTIARCH']))
+            searchdirs.insert(0, os.path.join(b'/usr/include',
+                                              os.environb[b'MULTIARCH']))
         except KeyError:
             pass
 
@@ -61,22 +61,23 @@
     opts, args = getopt.getopt(sys.argv[1:], 'i:')
     for o, a in opts:
         if o == '-i':
-            ignores.append(re.compile(a))
+            ignores.append(re.compile(a.encode()))
     if not args:
         args = ['-']
     for filename in args:
         if filename == '-':
             sys.stdout.write('# Generated by h2py from stdin\n')
-            process(sys.stdin, sys.stdout)
+            process(sys.stdin.buffer, sys.stdout.buffer)
         else:
-            fp = open(filename, 'r')
+            filename = filename.encode()
+            fp = open(filename, 'rb')
             outfile = os.path.basename(filename)
-            i = outfile.rfind('.')
+            i = outfile.rfind(b'.')
             if i > 0: outfile = outfile[:i]
             modname = outfile.upper()
-            outfile = modname + '.py'
-            outfp = open(outfile, 'w')
-            outfp.write('# Generated by h2py from %s\n' % filename)
+            outfile = modname + b'.py'
+            outfp = open(outfile, 'wb')
+            outfp.write(b'# Generated by h2py from ' + filename + b'\n')
             filedict = {}
             for dir in searchdirs:
                 if filename[:len(dir)] == dir:
@@ -90,9 +91,9 @@
 def pytify(body):
     # replace ignored patterns by spaces
     for p in ignores:
-        body = p.sub(' ', body)
+        body = p.sub(b' ', body)
     # replace char literals by ord(...)
-    body = p_char.sub("ord('\\1')", body)
+    body = p_char.sub(b"ord('\\1')", body)
     # Compute negative hexadecimal constants
     start = 0
     UMAX = 2*(sys.maxsize+1)
@@ -103,7 +104,7 @@
         val = int(body[slice(*m.span(1))], 16)
         if val > sys.maxsize:
             val -= UMAX
-            body = body[:s] + "(" + str(val) + ")" + body[e:]
+            body = body[:s] + b"(" + str(val).encode() + b")" + body[e:]
         start = s + 1
     return body
 
@@ -116,7 +117,7 @@
         match = p_define.match(line)
         if match:
             # gobble up continuation lines
-            while line[-2:] == '\\\n':
+            while line[-2:] == b'\\\n':
                 nextline = fp.readline()
                 if not nextline: break
                 lineno = lineno + 1
@@ -125,11 +126,11 @@
             body = line[match.end():]
             body = pytify(body)
             ok = 0
-            stmt = '%s = %s\n' % (name, body.strip())
+            stmt = name + b' = ' + body.strip() + b'\n'
             try:
                 exec(stmt, env)
             except:
-                sys.stderr.write('Skipping: %s' % stmt)
+                sys.stderr.buffer.write(b'Skipping: ' + stmt)
             else:
                 outfp.write(stmt)
         match = p_macro.match(line)
@@ -137,11 +138,11 @@
             macro, arg = match.group(1, 2)
             body = line[match.end():]
             body = pytify(body)
-            stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
+            stmt = b'def ' + macro + b'(' + arg + b'): return ' + body + b'\n'
             try:
                 exec(stmt, env)
             except:
-                sys.stderr.write('Skipping: %s' % stmt)
+                sys.stderr.buffer.write(b'Skipping: ' + stmt)
             else:
                 outfp.write(stmt)
         match = p_include.match(line)
@@ -150,23 +151,24 @@
             a, b = regs[1]
             filename = line[a:b]
             if filename in importable:
-                outfp.write('from %s import *\n' % importable[filename])
+                outfp.write(b'from ' + importable[filename] + b' import *\n')
             elif filename not in filedict:
                 filedict[filename] = None
                 inclfp = None
                 for dir in searchdirs:
                     try:
-                        inclfp = open(dir + '/' + filename)
+                        inclfp = open(dir + b'/' + filename, 'rb')
                         break
                     except IOError:
                         pass
                 if inclfp:
                     outfp.write(
-                            '\n# Included from %s\n' % filename)
+                            b'\n# Included from ' + filename + b'\n')
                     process(inclfp, outfp, env)
+                    inclfp.close()
                 else:
-                    sys.stderr.write('Warning - could not find file %s\n' %
-                                     filename)
+                    sys.stderr.buffer.write(b'Warning - could not find file ' +
+                                            filename + b'\n')
 
 if __name__ == '__main__':
     main()