home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''Safely evaluate Python string literals without using eval().'''
- import re
- simple_escapes = {
- 'a': '\x07',
- 'b': '\x08',
- 'f': '\x0c',
- 'n': '\n',
- 'r': '\r',
- 't': '\t',
- 'v': '\x0b',
- "'": "'",
- '"': '"',
- '\\': '\\' }
-
- def escape(m):
- (all, tail) = m.group(0, 1)
- if not all.startswith('\\'):
- raise AssertionError
- esc = simple_escapes.get(tail)
- if esc is not None:
- return esc
- if tail.startswith('x'):
- hexes = tail[1:]
- if len(hexes) < 2:
- raise ValueError("invalid hex string escape ('\\%s')" % tail)
- len(hexes) < 2
-
- try:
- i = int(hexes, 16)
- except ValueError:
- esc is not None
- esc is not None
- all.startswith('\\')
- raise ValueError("invalid hex string escape ('\\%s')" % tail)
- except:
- esc is not None<EXCEPTION MATCH>ValueError
-
-
- esc is not None
-
- try:
- i = int(tail, 8)
- except ValueError:
- esc is not None
- esc is not None
- all.startswith('\\')
- raise ValueError("invalid octal string escape ('\\%s')" % tail)
- except:
- esc is not None
-
- return chr(i)
-
-
- def evalString(s):
- if not s.startswith("'") and s.startswith('"'):
- raise AssertionError, repr(s[:1])
- q = s[0]
- if s[:3] == q * 3:
- q = q * 3
-
- if not s.endswith(q):
- raise AssertionError, repr(s[-len(q):])
- if not len(s) >= 2 * len(q):
- raise AssertionError
- s = s[len(q):-len(q)]
- return re.sub('\\\\(\\\'|\\"|\\\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})', escape, s)
-
-
- def test():
- for i in range(256):
- c = chr(i)
- s = repr(c)
- e = evalString(s)
- if e != c:
- print i, c, s, e
- continue
-
-
- if __name__ == '__main__':
- test()
-
-