Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/h2py.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 981fff7363b9 |
| children | 835c176239d0 c94f37054f19 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
4112
diff
changeset
|
1 #! /usr/bin/env python |
| 687 | 2 |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
3 # Read #define's and translate to Python code. |
|
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
4 # Handle #include statements. |
|
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
5 # Handle #define macros with one argument. |
|
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
6 # Anything that isn't recognized or doesn't translate into valid |
|
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
7 # Python is ignored. |
|
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
8 |
|
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
9 # Without filename arguments, acts as a filter. |
|
1602
907c5dd89c6c
Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
10 # If one or more filenames are given, output is written to corresponding |
|
907c5dd89c6c
Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
11 # filenames in the local directory, translated to all uppercase, with |
|
907c5dd89c6c
Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
12 # the extension replaced by ".py". |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
13 |
|
1639
37cf87f561ec
Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents:
1602
diff
changeset
|
14 # By passing one or more options of the form "-i regular_expression" |
|
37cf87f561ec
Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents:
1602
diff
changeset
|
15 # you can specify additional strings to be ignored. This is useful |
|
37cf87f561ec
Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents:
1602
diff
changeset
|
16 # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'". |
| 687 | 17 |
| 18 # XXX To do: | |
| 19 # - turn trailing C comments into Python comments | |
| 20 # - turn C Boolean operators "&& || !" into Python "and or not" | |
| 21 # - what to do about #if(def)? | |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
22 # - what to do about macros with multiple parameters? |
| 687 | 23 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
24 import sys, re, getopt, os |
| 687 | 25 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
26 p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+') |
| 687 | 27 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
28 p_macro = re.compile( |
|
4112
5726be8ee59f
Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents:
2169
diff
changeset
|
29 '^[\t ]*#[\t ]*define[\t ]+' |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
30 '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+') |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
31 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
32 p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)') |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
33 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
34 p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?') |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
35 p_cpp_comment = re.compile('//.*') |
| 687 | 36 |
| 6032 | 37 ignores = [p_comment, p_cpp_comment] |
|
1639
37cf87f561ec
Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents:
1602
diff
changeset
|
38 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
39 p_char = re.compile(r"'(\\.[^\\]*|[^\\])'") |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
40 |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
41 p_hex = re.compile(r"0x([0-9a-fA-F]+)L?") |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
42 |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
43 filedict = {} |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
44 importable = {} |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
45 |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
46 try: |
|
21247
ba16856bfbab
SF patch #489680 (David Abrahams): h2py uses nonexistent method splitfields()
Guido van Rossum <guido@python.org>
parents:
19093
diff
changeset
|
47 searchdirs=os.environ['include'].split(';') |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
48 except KeyError: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
49 try: |
|
21247
ba16856bfbab
SF patch #489680 (David Abrahams): h2py uses nonexistent method splitfields()
Guido van Rossum <guido@python.org>
parents:
19093
diff
changeset
|
50 searchdirs=os.environ['INCLUDE'].split(';') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
51 except KeyError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
52 try: |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
53 if sys.platform.find("beos") == 0: |
|
21247
ba16856bfbab
SF patch #489680 (David Abrahams): h2py uses nonexistent method splitfields()
Guido van Rossum <guido@python.org>
parents:
19093
diff
changeset
|
54 searchdirs=os.environ['BEINCLUDES'].split(';') |
|
23777
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
21248
diff
changeset
|
55 elif sys.platform.startswith("atheos"): |
|
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
21248
diff
changeset
|
56 searchdirs=os.environ['C_INCLUDE_PATH'].split(':') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
57 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
58 raise KeyError |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
59 except KeyError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
60 searchdirs=['/usr/include'] |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
61 |
| 687 | 62 def main(): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
63 global filedict |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
64 opts, args = getopt.getopt(sys.argv[1:], 'i:') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
65 for o, a in opts: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
66 if o == '-i': |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
67 ignores.append(re.compile(a)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
68 if not args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
69 args = ['-'] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
70 for filename in args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
71 if filename == '-': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
72 sys.stdout.write('# Generated by h2py from stdin\n') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
73 process(sys.stdin, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
74 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
75 fp = open(filename, 'r') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
76 outfile = os.path.basename(filename) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
77 i = outfile.rfind('.') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
78 if i > 0: outfile = outfile[:i] |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
79 modname = outfile.upper() |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
80 outfile = modname + '.py' |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
81 outfp = open(outfile, 'w') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
82 outfp.write('# Generated by h2py from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
83 filedict = {} |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
84 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
85 if filename[:len(dir)] == dir: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
86 filedict[filename[len(dir)+1:]] = None # no '/' trailing |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
87 importable[filename[len(dir)+1:]] = modname |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
88 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
89 process(fp, outfp) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
90 outfp.close() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
91 fp.close() |
| 687 | 92 |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
93 def pytify(body): |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
94 # replace ignored patterns by spaces |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
95 for p in ignores: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
96 body = p.sub(' ', body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
97 # replace char literals by ord(...) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
98 body = p_char.sub('ord(\\0)', body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
99 # Compute negative hexadecimal constants |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
100 start = 0 |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
101 UMAX = 2*(sys.maxint+1) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
102 while 1: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
103 m = p_hex.search(body, start) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
104 if not m: break |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
105 s,e = m.span() |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
106 val = long(body[slice(*m.span(1))], 16) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
107 if val > sys.maxint: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
108 val -= UMAX |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
109 body = body[:s] + "(" + str(val) + ")" + body[e:] |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
110 start = s + 1 |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
111 return body |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
112 |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
113 def process(fp, outfp, env = {}): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
114 lineno = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
115 while 1: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
116 line = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
117 if not line: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
118 lineno = lineno + 1 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
119 match = p_define.match(line) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
120 if match: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
121 # gobble up continuation lines |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
122 while line[-2:] == '\\\n': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
123 nextline = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
124 if not nextline: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
125 lineno = lineno + 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
126 line = line + nextline |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
127 name = match.group(1) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
128 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
129 body = pytify(body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
130 ok = 0 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
131 stmt = '%s = %s\n' % (name, body.strip()) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
132 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
133 exec stmt in env |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
134 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
135 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
136 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
137 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
138 match = p_macro.match(line) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
139 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
140 macro, arg = match.group(1, 2) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
141 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
142 body = pytify(body) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
143 stmt = 'def %s(%s): return %s\n' % (macro, arg, body) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
144 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
145 exec stmt in env |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
146 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
147 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
148 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
149 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
150 match = p_include.match(line) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
151 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
152 regs = match.regs |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
153 a, b = regs[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
154 filename = line[a:b] |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
155 if importable.has_key(filename): |
|
19093
97de868ad0f4
When representing #includes as imports, do from MOD import *.
Martin v. Löwis <martin@v.loewis.de>
parents:
19092
diff
changeset
|
156 outfp.write('from %s import *\n' % importable[filename]) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
157 elif not filedict.has_key(filename): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
158 filedict[filename] = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
159 inclfp = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
160 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
161 try: |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
162 inclfp = open(dir + '/' + filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
163 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
164 except IOError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
165 pass |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
166 if inclfp: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
167 outfp.write( |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
168 '\n# Included from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
169 process(inclfp, outfp, env) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
170 else: |
|
21248
062c6f47106f
Warning message about unfound file was missing trailing \n.
Guido van Rossum <guido@python.org>
parents:
21247
diff
changeset
|
171 sys.stderr.write('Warning - could not find file %s\n' % |
|
062c6f47106f
Warning message about unfound file was missing trailing \n.
Guido van Rossum <guido@python.org>
parents:
21247
diff
changeset
|
172 filename) |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
173 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
174 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
175 main() |
