-
Notifications
You must be signed in to change notification settings - Fork 9
/
gen_exc.py
executable file
·71 lines (54 loc) · 1.75 KB
/
gen_exc.py
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
#!/usr/bin/env python
# Copyright 2011 Julian Phillips. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import re
import subprocess
import threading
header = """// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is automatically generated. To regenerate:
// ./gen_exc.py | gofmt > exc.go
package py
// #include "utils.h"
import "C"
"""
exc_re = re.compile('^extern .* PyExc_(?P<name>\w+);$')
def get_ffi_flags():
cmd = ['pkg-config', '--cflags', 'libffi']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
return out.strip().split(' ')
def get_python3_flags():
cmd = ['python3.3-config', '--cflags']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
return out.strip().split(' ')
def process(inp):
exceptions = []
maxlen = 0
for rawline in inp:
line = rawline.strip()
excm = exc_re.match(line)
if excm is None:
continue
exc = excm.group('name')
exceptions.append(exc)
if len(exc) > maxlen:
maxlen = len(exc)
print header
print 'var ('
for exception in exceptions:
print '\t%s%s = newException(C.PyExc_%s)' % \
(exception, ' ' * (maxlen - len(exception)), exception)
print ')\n'
def main():
cmd = ["cc", "-E", "-o", "-", "utils.c"] + get_ffi_flags() + get_python3_flags()
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
t = threading.Thread(target=process, args=(p.stdout,))
t.start()
p.wait()
t.join()
if __name__ == "__main__":
main()