Kaydet (Commit) 0c9886d5 authored tarafından Tim Peters's avatar Tim Peters

Whitespace normalization.

üst 2344fae6
This diff is collapsed.
...@@ -61,7 +61,7 @@ XXX To be done... ...@@ -61,7 +61,7 @@ XXX To be done...
import regex import regex
from regex_syntax import * # RE_* from regex_syntax import * # RE_*
# Default translation table # Default translation table
mastertable = { mastertable = {
......
...@@ -33,9 +33,9 @@ RE_NEWLINE_OR = 16 ...@@ -33,9 +33,9 @@ RE_NEWLINE_OR = 16
# their special meaning regardless of the surrounding context. # their special meaning regardless of the surrounding context.
# 1 means that special characters may act as normal characters in some # 1 means that special characters may act as normal characters in some
# contexts. Specifically, this applies to: # contexts. Specifically, this applies to:
# ^ - only special at the beginning, or after ( or | # ^ - only special at the beginning, or after ( or |
# $ - only special at the end, or before ) or | # $ - only special at the end, or before ) or |
# *, +, ? - only special when not after the beginning, (, or | # *, +, ? - only special when not after the beginning, (, or |
RE_CONTEXT_INDEP_OPS = 32 RE_CONTEXT_INDEP_OPS = 32
# ANSI sequences (\n etc) and \xhh # ANSI sequences (\n etc) and \xhh
......
...@@ -12,7 +12,7 @@ splitx(str, pat, maxsplit): split string using pattern as delimiter plus ...@@ -12,7 +12,7 @@ splitx(str, pat, maxsplit): split string using pattern as delimiter plus
import warnings import warnings
warnings.warn("the regsub module is deprecated; please use re.sub()", warnings.warn("the regsub module is deprecated; please use re.sub()",
DeprecationWarning) DeprecationWarning)
# Ignore further deprecation warnings about this module # Ignore further deprecation warnings about this module
warnings.filterwarnings("ignore", "", DeprecationWarning, __name__) warnings.filterwarnings("ignore", "", DeprecationWarning, __name__)
...@@ -27,12 +27,12 @@ import regex ...@@ -27,12 +27,12 @@ import regex
# compiled pattern. # compiled pattern.
def sub(pat, repl, str): def sub(pat, repl, str):
prog = compile(pat) prog = compile(pat)
if prog.search(str) >= 0: if prog.search(str) >= 0:
regs = prog.regs regs = prog.regs
a, b = regs[0] a, b = regs[0]
str = str[:a] + expand(repl, regs, str) + str[b:] str = str[:a] + expand(repl, regs, str) + str[b:]
return str return str
# Replace all (non-overlapping) occurrences of pattern pat in string # Replace all (non-overlapping) occurrences of pattern pat in string
...@@ -41,23 +41,23 @@ def sub(pat, repl, str): ...@@ -41,23 +41,23 @@ def sub(pat, repl, str):
# a previous match, so e.g. gsub('', '-', 'abc') returns '-a-b-c-'. # a previous match, so e.g. gsub('', '-', 'abc') returns '-a-b-c-'.
def gsub(pat, repl, str): def gsub(pat, repl, str):
prog = compile(pat) prog = compile(pat)
new = '' new = ''
start = 0 start = 0
first = 1 first = 1
while prog.search(str, start) >= 0: while prog.search(str, start) >= 0:
regs = prog.regs regs = prog.regs
a, b = regs[0] a, b = regs[0]
if a == b == start and not first: if a == b == start and not first:
if start >= len(str) or prog.search(str, start+1) < 0: if start >= len(str) or prog.search(str, start+1) < 0:
break break
regs = prog.regs regs = prog.regs
a, b = regs[0] a, b = regs[0]
new = new + str[start:a] + expand(repl, regs, str) new = new + str[start:a] + expand(repl, regs, str)
start = b start = b
first = 0 first = 0
new = new + str[start:] new = new + str[start:]
return new return new
# Split string str in fields separated by delimiters matching pattern # Split string str in fields separated by delimiters matching pattern
...@@ -66,7 +66,7 @@ def gsub(pat, repl, str): ...@@ -66,7 +66,7 @@ def gsub(pat, repl, str):
# The optional 3rd argument sets the number of splits that are performed. # The optional 3rd argument sets the number of splits that are performed.
def split(str, pat, maxsplit = 0): def split(str, pat, maxsplit = 0):
return intsplit(str, pat, maxsplit, 0) return intsplit(str, pat, maxsplit, 0)
# Split string str in fields separated by delimiters matching pattern # Split string str in fields separated by delimiters matching pattern
# pat. Only non-empty matches for the pattern are considered, so e.g. # pat. Only non-empty matches for the pattern are considered, so e.g.
...@@ -76,42 +76,42 @@ def split(str, pat, maxsplit = 0): ...@@ -76,42 +76,42 @@ def split(str, pat, maxsplit = 0):
def splitx(str, pat, maxsplit = 0): def splitx(str, pat, maxsplit = 0):
return intsplit(str, pat, maxsplit, 1) return intsplit(str, pat, maxsplit, 1)
# Internal function used to implement split() and splitx(). # Internal function used to implement split() and splitx().
def intsplit(str, pat, maxsplit, retain): def intsplit(str, pat, maxsplit, retain):
prog = compile(pat) prog = compile(pat)
res = [] res = []
start = next = 0 start = next = 0
splitcount = 0 splitcount = 0
while prog.search(str, next) >= 0: while prog.search(str, next) >= 0:
regs = prog.regs regs = prog.regs
a, b = regs[0] a, b = regs[0]
if a == b: if a == b:
next = next + 1 next = next + 1
if next >= len(str): if next >= len(str):
break break
else: else:
res.append(str[start:a]) res.append(str[start:a])
if retain: if retain:
res.append(str[a:b]) res.append(str[a:b])
start = next = b start = next = b
splitcount = splitcount + 1 splitcount = splitcount + 1
if (maxsplit and (splitcount >= maxsplit)): if (maxsplit and (splitcount >= maxsplit)):
break break
res.append(str[start:]) res.append(str[start:])
return res return res
# Capitalize words split using a pattern # Capitalize words split using a pattern
def capwords(str, pat='[^a-zA-Z0-9_]+'): def capwords(str, pat='[^a-zA-Z0-9_]+'):
import string import string
words = splitx(str, pat) words = splitx(str, pat)
for i in range(0, len(words), 2): for i in range(0, len(words), 2):
words[i] = string.capitalize(words[i]) words[i] = string.capitalize(words[i])
return string.joinfields(words, "") return string.joinfields(words, "")
# Internal subroutines: # Internal subroutines:
...@@ -131,19 +131,19 @@ def capwords(str, pat='[^a-zA-Z0-9_]+'): ...@@ -131,19 +131,19 @@ def capwords(str, pat='[^a-zA-Z0-9_]+'):
cache = {} cache = {}
def compile(pat): def compile(pat):
if type(pat) != type(''): if type(pat) != type(''):
return pat # Assume it is a compiled regex return pat # Assume it is a compiled regex
key = (pat, regex.get_syntax()) key = (pat, regex.get_syntax())
if cache.has_key(key): if cache.has_key(key):
prog = cache[key] # Get it from the cache prog = cache[key] # Get it from the cache
else: else:
prog = cache[key] = regex.compile(pat) prog = cache[key] = regex.compile(pat)
return prog return prog
def clear_cache(): def clear_cache():
global cache global cache
cache = {} cache = {}
# Expand \digit in the replacement. # Expand \digit in the replacement.
...@@ -153,46 +153,46 @@ def clear_cache(): ...@@ -153,46 +153,46 @@ def clear_cache():
# the \ and the following character are both copied). # the \ and the following character are both copied).
def expand(repl, regs, str): def expand(repl, regs, str):
if '\\' not in repl: if '\\' not in repl:
return repl return repl
new = '' new = ''
i = 0 i = 0
ord0 = ord('0') ord0 = ord('0')
while i < len(repl): while i < len(repl):
c = repl[i]; i = i+1 c = repl[i]; i = i+1
if c != '\\' or i >= len(repl): if c != '\\' or i >= len(repl):
new = new + c new = new + c
else: else:
c = repl[i]; i = i+1 c = repl[i]; i = i+1
if '0' <= c <= '9': if '0' <= c <= '9':
a, b = regs[ord(c)-ord0] a, b = regs[ord(c)-ord0]
new = new + str[a:b] new = new + str[a:b]
elif c == '\\': elif c == '\\':
new = new + c new = new + c
else: else:
new = new + '\\' + c new = new + '\\' + c
return new return new
# Test program, reads sequences "pat repl str" from stdin. # Test program, reads sequences "pat repl str" from stdin.
# Optional argument specifies pattern used to split lines. # Optional argument specifies pattern used to split lines.
def test(): def test():
import sys import sys
if sys.argv[1:]: if sys.argv[1:]:
delpat = sys.argv[1] delpat = sys.argv[1]
else: else:
delpat = '[ \t\n]+' delpat = '[ \t\n]+'
while 1: while 1:
if sys.stdin.isatty(): sys.stderr.write('--> ') if sys.stdin.isatty(): sys.stderr.write('--> ')
line = sys.stdin.readline() line = sys.stdin.readline()
if not line: break if not line: break
if line[-1] == '\n': line = line[:-1] if line[-1] == '\n': line = line[:-1]
fields = split(line, delpat) fields = split(line, delpat)
if len(fields) != 3: if len(fields) != 3:
print 'Sorry, not three fields' print 'Sorry, not three fields'
print 'split:', `fields` print 'split:', `fields`
continue continue
[pat, repl, str] = split(line, delpat) [pat, repl, str] = split(line, delpat)
print 'sub :', `sub(pat, repl, str)` print 'sub :', `sub(pat, repl, str)`
print 'gsub:', `gsub(pat, repl, str)` print 'gsub:', `gsub(pat, repl, str)`
...@@ -3,93 +3,93 @@ ...@@ -3,93 +3,93 @@
import string import string
class Repr: class Repr:
def __init__(self): def __init__(self):
self.maxlevel = 6 self.maxlevel = 6
self.maxtuple = 6 self.maxtuple = 6
self.maxlist = 6 self.maxlist = 6
self.maxdict = 4 self.maxdict = 4
self.maxstring = 30 self.maxstring = 30
self.maxlong = 40 self.maxlong = 40
self.maxother = 20 self.maxother = 20
def repr(self, x): def repr(self, x):
return self.repr1(x, self.maxlevel) return self.repr1(x, self.maxlevel)
def repr1(self, x, level): def repr1(self, x, level):
typename = `type(x)`[7:-2] # "<type '......'>" typename = `type(x)`[7:-2] # "<type '......'>"
if ' ' in typename: if ' ' in typename:
parts = string.split(typename) parts = string.split(typename)
typename = string.joinfields(parts, '_') typename = string.joinfields(parts, '_')
if hasattr(self, 'repr_' + typename): if hasattr(self, 'repr_' + typename):
return getattr(self, 'repr_' + typename)(x, level) return getattr(self, 'repr_' + typename)(x, level)
else: else:
s = `x` s = `x`
if len(s) > self.maxother: if len(s) > self.maxother:
i = max(0, (self.maxother-3)/2) i = max(0, (self.maxother-3)/2)
j = max(0, self.maxother-3-i) j = max(0, self.maxother-3-i)
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
return s return s
def repr_tuple(self, x, level): def repr_tuple(self, x, level):
n = len(x) n = len(x)
if n == 0: return '()' if n == 0: return '()'
if level <= 0: return '(...)' if level <= 0: return '(...)'
s = '' s = ''
for i in range(min(n, self.maxtuple)): for i in range(min(n, self.maxtuple)):
if s: s = s + ', ' if s: s = s + ', '
s = s + self.repr1(x[i], level-1) s = s + self.repr1(x[i], level-1)
if n > self.maxtuple: s = s + ', ...' if n > self.maxtuple: s = s + ', ...'
elif n == 1: s = s + ',' elif n == 1: s = s + ','
return '(' + s + ')' return '(' + s + ')'
def repr_list(self, x, level): def repr_list(self, x, level):
n = len(x) n = len(x)
if n == 0: return '[]' if n == 0: return '[]'
if level <= 0: return '[...]' if level <= 0: return '[...]'
s = '' s = ''
for i in range(min(n, self.maxlist)): for i in range(min(n, self.maxlist)):
if s: s = s + ', ' if s: s = s + ', '
s = s + self.repr1(x[i], level-1) s = s + self.repr1(x[i], level-1)
if n > self.maxlist: s = s + ', ...' if n > self.maxlist: s = s + ', ...'
return '[' + s + ']' return '[' + s + ']'
def repr_dictionary(self, x, level): def repr_dictionary(self, x, level):
n = len(x) n = len(x)
if n == 0: return '{}' if n == 0: return '{}'
if level <= 0: return '{...}' if level <= 0: return '{...}'
s = '' s = ''
keys = x.keys() keys = x.keys()
keys.sort() keys.sort()
for i in range(min(n, self.maxdict)): for i in range(min(n, self.maxdict)):
if s: s = s + ', ' if s: s = s + ', '
key = keys[i] key = keys[i]
s = s + self.repr1(key, level-1) s = s + self.repr1(key, level-1)
s = s + ': ' + self.repr1(x[key], level-1) s = s + ': ' + self.repr1(x[key], level-1)
if n > self.maxdict: s = s + ', ...' if n > self.maxdict: s = s + ', ...'
return '{' + s + '}' return '{' + s + '}'
def repr_string(self, x, level): def repr_string(self, x, level):
s = `x[:self.maxstring]` s = `x[:self.maxstring]`
if len(s) > self.maxstring: if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)/2) i = max(0, (self.maxstring-3)/2)
j = max(0, self.maxstring-3-i) j = max(0, self.maxstring-3-i)
s = `x[:i] + x[len(x)-j:]` s = `x[:i] + x[len(x)-j:]`
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
return s return s
def repr_long_int(self, x, level): def repr_long_int(self, x, level):
s = `x` # XXX Hope this isn't too slow... s = `x` # XXX Hope this isn't too slow...
if len(s) > self.maxlong: if len(s) > self.maxlong:
i = max(0, (self.maxlong-3)/2) i = max(0, (self.maxlong-3)/2)
j = max(0, self.maxlong-3-i) j = max(0, self.maxlong-3-i)
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
return s return s
def repr_instance(self, x, level): def repr_instance(self, x, level):
try: try:
s = `x` s = `x`
# Bugs in x.__repr__() can cause arbitrary # Bugs in x.__repr__() can cause arbitrary
# exceptions -- then make up something # exceptions -- then make up something
except: except:
return '<' + x.__class__.__name__ + ' instance at ' + \ return '<' + x.__class__.__name__ + ' instance at ' + \
hex(id(x))[2:] + '>' hex(id(x))[2:] + '>'
if len(s) > self.maxstring: if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)/2) i = max(0, (self.maxstring-3)/2)
j = max(0, self.maxstring-3-i) j = max(0, self.maxstring-3-i)
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
return s return s
aRepr = Repr() aRepr = Repr()
repr = aRepr.repr repr = aRepr.repr
...@@ -278,7 +278,7 @@ class RExec(ihooks._Verbose): ...@@ -278,7 +278,7 @@ class RExec(ihooks._Verbose):
def r_unload(self, m): def r_unload(self, m):
return self.importer.unload(m) return self.importer.unload(m)
# The s_* methods are similar but also swap std{in,out,err} # The s_* methods are similar but also swap std{in,out,err}
def make_delegate_files(self): def make_delegate_files(self):
...@@ -309,7 +309,7 @@ class RExec(ihooks._Verbose): ...@@ -309,7 +309,7 @@ class RExec(ihooks._Verbose):
self.restricted_stdin = s.stdin self.restricted_stdin = s.stdin
self.restricted_stdout = s.stdout self.restricted_stdout = s.stdout
self.restricted_stderr = s.stderr self.restricted_stderr = s.stderr
def save_files(self): def save_files(self):
self.save_stdin = sys.stdin self.save_stdin = sys.stdin
...@@ -320,7 +320,7 @@ class RExec(ihooks._Verbose): ...@@ -320,7 +320,7 @@ class RExec(ihooks._Verbose):
sys.stdin = self.save_stdin sys.stdin = self.save_stdin
sys.stdout = self.save_stdout sys.stdout = self.save_stdout
sys.stderr = self.save_stderr sys.stderr = self.save_stderr
def s_apply(self, func, args=(), kw=None): def s_apply(self, func, args=(), kw=None):
self.save_files() self.save_files()
try: try:
...@@ -331,27 +331,27 @@ class RExec(ihooks._Verbose): ...@@ -331,27 +331,27 @@ class RExec(ihooks._Verbose):
r = apply(func, args) r = apply(func, args)
finally: finally:
self.restore_files() self.restore_files()
def s_exec(self, *args): def s_exec(self, *args):
self.s_apply(self.r_exec, args) self.s_apply(self.r_exec, args)
def s_eval(self, *args): def s_eval(self, *args):
self.s_apply(self.r_eval, args) self.s_apply(self.r_eval, args)
def s_execfile(self, *args): def s_execfile(self, *args):
self.s_apply(self.r_execfile, args) self.s_apply(self.r_execfile, args)
def s_import(self, *args): def s_import(self, *args):
self.s_apply(self.r_import, args) self.s_apply(self.r_import, args)
def s_reload(self, *args): def s_reload(self, *args):
self.s_apply(self.r_reload, args) self.s_apply(self.r_reload, args)
def s_unload(self, *args): def s_unload(self, *args):
self.s_apply(self.r_unload, args) self.s_apply(self.r_unload, args)
# Restricted open(...) # Restricted open(...)
def r_open(self, file, mode='r', buf=-1): def r_open(self, file, mode='r', buf=-1):
if mode not in ('r', 'rb'): if mode not in ('r', 'rb'):
raise IOError, "can't open files for writing in restricted mode" raise IOError, "can't open files for writing in restricted mode"
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment