Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/h2py.py @ 58689:514b56efd89a 3.1
Merged revisions 77962 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r77962 | r.david.murray | 2010-02-04 11:41:57 -0500 (Thu, 04 Feb 2010) | 17 lines
Merged revisions 77943,77959-77960 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77943 | r.david.murray | 2010-02-03 08:36:23 -0500 (Wed, 03 Feb 2010) | 2 lines
Explicitly mention the default value for formatdate's usegmt parameter.
........
r77959 | nick.coghlan | 2010-02-04 07:43:58 -0500 (Thu, 04 Feb 2010) | 1 line
Issue 6760: Clarify args handling for subprocess.Popen. Patch by Chris Rebert
........
r77960 | r.david.murray | 2010-02-04 11:33:31 -0500 (Thu, 04 Feb 2010) | 2 lines
Add Chris Rebert to ACKS for issue 6760 Popen doc improvements.
........
................
| author | R. David Murray <rdmurray@bitdance.com> |
|---|---|
| date | Thu, 04 Feb 2010 16:44:31 +0000 |
| parents | 893a4dc17a91 |
| children | 05ee6f8c0260 14b55a4e8725 |
| 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: |
| 42645 | 53 if sys.platform.startswith("atheos"): |
|
23777
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
21248
diff
changeset
|
54 searchdirs=os.environ['C_INCLUDE_PATH'].split(':') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
55 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
56 raise KeyError |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
57 except KeyError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
58 searchdirs=['/usr/include'] |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
59 |
| 687 | 60 def main(): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
61 global filedict |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
62 opts, args = getopt.getopt(sys.argv[1:], 'i:') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
63 for o, a in opts: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
64 if o == '-i': |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
65 ignores.append(re.compile(a)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
66 if not args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
67 args = ['-'] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
68 for filename in args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
69 if filename == '-': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
70 sys.stdout.write('# Generated by h2py from stdin\n') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
71 process(sys.stdin, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
72 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
73 fp = open(filename, 'r') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
74 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
|
75 i = outfile.rfind('.') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
76 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
|
77 modname = outfile.upper() |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
78 outfile = modname + '.py' |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
79 outfp = open(outfile, 'w') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
80 outfp.write('# Generated by h2py from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
81 filedict = {} |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
82 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
83 if filename[:len(dir)] == dir: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
84 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
|
85 importable[filename[len(dir)+1:]] = modname |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
86 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
87 process(fp, outfp) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
88 outfp.close() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
89 fp.close() |
| 687 | 90 |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
91 def pytify(body): |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
92 # replace ignored patterns by spaces |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
93 for p in ignores: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
94 body = p.sub(' ', body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
95 # replace char literals by ord(...) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
96 body = p_char.sub('ord(\\0)', body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
97 # Compute negative hexadecimal constants |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
98 start = 0 |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
42645
diff
changeset
|
99 UMAX = 2*(sys.maxsize+1) |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
100 while 1: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
101 m = p_hex.search(body, start) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
102 if not m: break |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
103 s,e = m.span() |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
44160
diff
changeset
|
104 val = int(body[slice(*m.span(1))], 16) |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
42645
diff
changeset
|
105 if val > sys.maxsize: |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
106 val -= UMAX |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
107 body = body[:s] + "(" + str(val) + ")" + body[e:] |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
108 start = s + 1 |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
109 return body |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
110 |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
111 def process(fp, outfp, env = {}): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
112 lineno = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
113 while 1: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
114 line = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
115 if not line: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
116 lineno = lineno + 1 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
117 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
|
118 if match: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
119 # gobble up continuation lines |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
120 while line[-2:] == '\\\n': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
121 nextline = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
122 if not nextline: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
123 lineno = lineno + 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
124 line = line + nextline |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
125 name = match.group(1) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
126 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
127 body = pytify(body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
128 ok = 0 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
129 stmt = '%s = %s\n' % (name, body.strip()) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
130 try: |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
33074
diff
changeset
|
131 exec(stmt, env) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
132 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
133 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
134 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
135 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
136 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
|
137 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
138 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
|
139 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
140 body = pytify(body) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
141 stmt = 'def %s(%s): return %s\n' % (macro, arg, body) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
142 try: |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
33074
diff
changeset
|
143 exec(stmt, env) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
144 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
145 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
146 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
147 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
148 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
|
149 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
150 regs = match.regs |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
151 a, b = regs[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
152 filename = line[a:b] |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
44160
diff
changeset
|
153 if filename in importable: |
|
19093
97de868ad0f4
When representing #includes as imports, do from MOD import *.
Martin v. Löwis <martin@v.loewis.de>
parents:
19092
diff
changeset
|
154 outfp.write('from %s import *\n' % importable[filename]) |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
44160
diff
changeset
|
155 elif filename not in filedict: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
156 filedict[filename] = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
157 inclfp = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
158 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
159 try: |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
160 inclfp = open(dir + '/' + filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
161 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
162 except IOError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
163 pass |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
164 if inclfp: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
165 outfp.write( |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
166 '\n# Included from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
167 process(inclfp, outfp, env) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
168 else: |
|
21248
062c6f47106f
Warning message about unfound file was missing trailing \n.
Guido van Rossum <guido@python.org>
parents:
21247
diff
changeset
|
169 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
|
170 filename) |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
171 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
172 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
173 main() |
