Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/h2py.py @ 1590:ac2e5cc29949 legacy-trunk
Added tests for missing host to open_http and open_gopher
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 18 Apr 1994 09:39:56 +0000 |
| parents | 28948181fb2f |
| children | 907c5dd89c6c |
| rev | line source |
|---|---|
|
1269
28948181fb2f
*** empty log message ***
Guido van Rossum <guido@python.org>
parents:
898
diff
changeset
|
1 #! /usr/local/bin/python |
| 687 | 2 |
| 3 # Read #define's from stdin and translate to Python code on stdout. | |
| 4 # Very primitive: non-#define's are ignored, no check for valid Python | |
| 5 # syntax is made -- you will have to edit the output in most cases. | |
| 6 | |
| 7 # XXX To do: | |
| 8 # - accept filename arguments | |
| 9 # - turn trailing C comments into Python comments | |
| 10 # - turn C string quotes into Python comments | |
| 11 # - turn C Boolean operators "&& || !" into Python "and or not" | |
| 12 # - what to do about #if(def)? | |
| 13 # - what to do about macros with parameters? | |
| 14 # - reject definitions with semicolons in them | |
| 15 | |
| 16 import sys, regex, string | |
| 17 | |
| 18 p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+') | |
| 19 | |
|
898
ef86502508e2
Fix regexp recognizing comments to cope with unterminated comments.
Guido van Rossum <guido@python.org>
parents:
687
diff
changeset
|
20 p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?') |
| 687 | 21 |
| 22 def main(): | |
| 23 process(sys.stdin) | |
| 24 | |
| 25 def process(fp): | |
| 26 lineno = 0 | |
| 27 while 1: | |
| 28 line = fp.readline() | |
| 29 if not line: break | |
| 30 lineno = lineno + 1 | |
| 31 if p_define.match(line) >= 0: | |
| 32 # gobble up continuation lines | |
| 33 while line[-2:] == '\\\n': | |
| 34 nextline = fp.readline() | |
| 35 if not nextline: break | |
| 36 lineno = lineno + 1 | |
| 37 line = line + nextline | |
| 38 regs = p_define.regs | |
| 39 a, b = regs[1] # where the macro name is | |
| 40 name = line[a:b] | |
| 41 a, b = regs[0] # the whole match | |
| 42 body = line[b:] | |
| 43 # replace comments by spaces | |
| 44 while p_comment.search(body) >= 0: | |
| 45 a, b = p_comment.regs[0] | |
| 46 body = body[:a] + ' ' + body[b:] | |
| 47 print name, '=', string.strip(body) | |
| 48 | |
| 49 main() |
