Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/h2py.py @ 82251:bb5a8564e186 2.7
Issue #13153: Tkinter functions now raise TclError instead of ValueError when
a unicode argument contains non-BMP character.
| author | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| date | Mon, 18 Feb 2013 13:00:08 +0200 |
| parents | 5981425cc48e |
| children |
| 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'] |
|
81707
5981425cc48e
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
66462
diff
changeset
|
61 try: |
|
5981425cc48e
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
66462
diff
changeset
|
62 searchdirs.insert(0, os.path.join('/usr/include', |
|
5981425cc48e
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
66462
diff
changeset
|
63 os.environ['MULTIARCH'])) |
|
5981425cc48e
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
66462
diff
changeset
|
64 except KeyError: |
|
5981425cc48e
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
66462
diff
changeset
|
65 pass |
|
5981425cc48e
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
66462
diff
changeset
|
66 |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
67 |
| 687 | 68 def main(): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
69 global filedict |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
70 opts, args = getopt.getopt(sys.argv[1:], 'i:') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
71 for o, a in opts: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
72 if o == '-i': |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
73 ignores.append(re.compile(a)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
74 if not args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
75 args = ['-'] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
76 for filename in args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
77 if filename == '-': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
78 sys.stdout.write('# Generated by h2py from stdin\n') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
79 process(sys.stdin, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
80 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
81 fp = open(filename, 'r') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
82 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
|
83 i = outfile.rfind('.') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
84 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
|
85 modname = outfile.upper() |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
86 outfile = modname + '.py' |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
87 outfp = open(outfile, 'w') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
88 outfp.write('# Generated by h2py from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
89 filedict = {} |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
90 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
91 if filename[:len(dir)] == dir: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
92 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
|
93 importable[filename[len(dir)+1:]] = modname |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
94 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
95 process(fp, outfp) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
96 outfp.close() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
97 fp.close() |
| 687 | 98 |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
99 def pytify(body): |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
100 # replace ignored patterns by spaces |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
101 for p in ignores: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
102 body = p.sub(' ', body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
103 # replace char literals by ord(...) |
|
66462
835c176239d0
Merged revisions 85768-85771,85773,85777,85823,85825 via svnmerge from
Georg Brandl <georg@python.org>
parents:
33074
diff
changeset
|
104 body = p_char.sub("ord('\\1')", body) |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
105 # Compute negative hexadecimal constants |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
106 start = 0 |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
107 UMAX = 2*(sys.maxint+1) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
108 while 1: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
109 m = p_hex.search(body, start) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
110 if not m: break |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
111 s,e = m.span() |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
112 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
|
113 if val > sys.maxint: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
114 val -= UMAX |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
115 body = body[:s] + "(" + str(val) + ")" + body[e:] |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
116 start = s + 1 |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
117 return body |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
118 |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
119 def process(fp, outfp, env = {}): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
120 lineno = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
121 while 1: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
122 line = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
123 if not line: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
124 lineno = lineno + 1 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
125 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
|
126 if match: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
127 # gobble up continuation lines |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
128 while line[-2:] == '\\\n': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
129 nextline = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
130 if not nextline: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
131 lineno = lineno + 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
132 line = line + nextline |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
133 name = match.group(1) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
134 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
135 body = pytify(body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
136 ok = 0 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
137 stmt = '%s = %s\n' % (name, body.strip()) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
138 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
139 exec stmt in env |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
140 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
141 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
142 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
143 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
144 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
|
145 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
146 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
|
147 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
148 body = pytify(body) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
149 stmt = 'def %s(%s): return %s\n' % (macro, arg, body) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
150 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
151 exec stmt in env |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
152 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
153 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
154 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
155 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
156 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
|
157 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
158 regs = match.regs |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
159 a, b = regs[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 elif not filedict.has_key(filename): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
164 filedict[filename] = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
165 inclfp = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
166 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
167 try: |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
168 inclfp = open(dir + '/' + filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
169 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
170 except IOError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
171 pass |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
172 if inclfp: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
173 outfp.write( |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
174 '\n# Included from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
175 process(inclfp, outfp, env) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
176 else: |
|
21248
062c6f47106f
Warning message about unfound file was missing trailing \n.
Guido van Rossum <guido@python.org>
parents:
21247
diff
changeset
|
177 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
|
178 filename) |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
179 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
180 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
181 main() |
