Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/h2py.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 039e17133391 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
56726
diff
changeset
|
1 #! /usr/bin/env python3 |
| 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: |
|
56726
14b55a4e8725
Remove AtheOS support, as per PEP 11 (which claims that all code was removed in Python 3.0).
Antoine Pitrou <solipsis@pitrou.net>
parents:
47100
diff
changeset
|
52 searchdirs=['/usr/include'] |
|
81708
039e17133391
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
68257
diff
changeset
|
53 try: |
|
039e17133391
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
68257
diff
changeset
|
54 searchdirs.insert(0, os.path.join('/usr/include', |
|
039e17133391
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
68257
diff
changeset
|
55 os.environ['MULTIARCH'])) |
|
039e17133391
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
68257
diff
changeset
|
56 except KeyError: |
|
039e17133391
- Issue #17029: Let h2py search the multiarch system include directory.
doko@python.org
parents:
68257
diff
changeset
|
57 pass |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
58 |
| 687 | 59 def main(): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
60 global filedict |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
61 opts, args = getopt.getopt(sys.argv[1:], 'i:') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
62 for o, a in opts: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
63 if o == '-i': |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
64 ignores.append(re.compile(a)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
65 if not args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
66 args = ['-'] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
67 for filename in args: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
68 if filename == '-': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
69 sys.stdout.write('# Generated by h2py from stdin\n') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
70 process(sys.stdin, sys.stdout) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
71 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
72 fp = open(filename, 'r') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
73 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
|
74 i = outfile.rfind('.') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
75 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
|
76 modname = outfile.upper() |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
77 outfile = modname + '.py' |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
78 outfp = open(outfile, 'w') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
79 outfp.write('# Generated by h2py from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
80 filedict = {} |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
81 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
82 if filename[:len(dir)] == dir: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
83 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
|
84 importable[filename[len(dir)+1:]] = modname |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
85 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
86 process(fp, outfp) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
87 outfp.close() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
88 fp.close() |
| 687 | 89 |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
90 def pytify(body): |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
91 # replace ignored patterns by spaces |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
92 for p in ignores: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
93 body = p.sub(' ', body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
94 # replace char literals by ord(...) |
|
66473
05ee6f8c0260
Merged revisions 85728,85731,85735,85766-85771,85773,85777 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47100
diff
changeset
|
95 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
|
96 # Compute negative hexadecimal constants |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
97 start = 0 |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
42645
diff
changeset
|
98 UMAX = 2*(sys.maxsize+1) |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
99 while 1: |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
100 m = p_hex.search(body, start) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
101 if not m: break |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
102 s,e = m.span() |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
44160
diff
changeset
|
103 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
|
104 if val > sys.maxsize: |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
105 val -= UMAX |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
106 body = body[:s] + "(" + str(val) + ")" + body[e:] |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
107 start = s + 1 |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
108 return body |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
109 |
|
1928
a8b7ee0e572d
Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents:
1639
diff
changeset
|
110 def process(fp, outfp, env = {}): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
111 lineno = 0 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
112 while 1: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
113 line = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
114 if not line: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
115 lineno = lineno + 1 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
116 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
|
117 if match: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
118 # gobble up continuation lines |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
119 while line[-2:] == '\\\n': |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
120 nextline = fp.readline() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
121 if not nextline: break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
122 lineno = lineno + 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
123 line = line + nextline |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
124 name = match.group(1) |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
125 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
126 body = pytify(body) |
|
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
127 ok = 0 |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
128 stmt = '%s = %s\n' % (name, body.strip()) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
129 try: |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
33074
diff
changeset
|
130 exec(stmt, env) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
131 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
132 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
133 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
134 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
135 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
|
136 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
137 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
|
138 body = line[match.end():] |
|
26368
c1caa5bff292
Expand negative hexadecimal constants.
Martin v. Löwis <martin@v.loewis.de>
parents:
23777
diff
changeset
|
139 body = pytify(body) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
140 stmt = 'def %s(%s): return %s\n' % (macro, arg, body) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
141 try: |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
33074
diff
changeset
|
142 exec(stmt, env) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
143 except: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
144 sys.stderr.write('Skipping: %s' % stmt) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
145 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
146 outfp.write(stmt) |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
147 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
|
148 if match: |
|
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
149 regs = match.regs |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
150 a, b = regs[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
151 filename = line[a:b] |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
44160
diff
changeset
|
152 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
|
153 outfp.write('from %s import *\n' % importable[filename]) |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
44160
diff
changeset
|
154 elif filename not in filedict: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
155 filedict[filename] = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
156 inclfp = None |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
157 for dir in searchdirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
158 try: |
|
19092
e79fd3934f61
Patch #437683: Use re instead of regex.
Martin v. Löwis <martin@v.loewis.de>
parents:
15930
diff
changeset
|
159 inclfp = open(dir + '/' + filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
160 break |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
161 except IOError: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
162 pass |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
163 if inclfp: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
164 outfp.write( |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
165 '\n# Included from %s\n' % filename) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
166 process(inclfp, outfp, env) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9644
diff
changeset
|
167 else: |
|
21248
062c6f47106f
Warning message about unfound file was missing trailing \n.
Guido van Rossum <guido@python.org>
parents:
21247
diff
changeset
|
168 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
|
169 filename) |
|
2169
92cd85470a76
use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents:
1928
diff
changeset
|
170 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
171 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
26368
diff
changeset
|
172 main() |
