Mercurial > genshi > genshi-test
annotate genshi/compat.py @ 914:c5faa881d87f experimental-py3k
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
author | hodgestar |
---|---|
date | Sun, 24 Oct 2010 21:38:19 +0000 |
parents | |
children |
rev | line source |
---|---|
914
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
2 # |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
4 # All rights reserved. |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
5 # |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
9 # |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
13 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
14 """Various Python version compatibility classes and functions.""" |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
15 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
16 import sys |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
17 from types import CodeType |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
18 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
19 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
20 IS_PYTHON2 = (sys.version_info[0] == 2) |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
21 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
22 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
23 # This function should only be called in Python 2, and will fail in Python 3 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
24 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
25 if IS_PYTHON2: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
26 def stringrepr(string): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
27 ascii = string.encode('ascii', 'backslashreplace') |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
28 quoted = "'" + ascii.replace("'", "\\'") + "'" |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
29 if len(ascii) > len(string): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
30 return 'u' + quoted |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
31 return quoted |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
32 else: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
33 def stringrepr(string): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
34 raise RuntimeError( |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
35 'Python 2 compatibility function. Not usable in Python 3.') |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
36 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
37 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
38 # We need to differentiate between StringIO and BytesIO in places |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
39 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
40 if IS_PYTHON2: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
41 from StringIO import StringIO |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
42 try: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
43 from cStringIO import StringIO as BytesIO |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
44 except ImportError: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
45 BytesIO = StringIO |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
46 else: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
47 from io import StringIO, BytesIO |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
48 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
49 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
50 # We want to test bytestring input to some stuff. |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
51 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
52 if IS_PYTHON2: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
53 def wrapped_bytes(bstr): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
54 assert bstr.startswith('b') |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
55 return bstr[1:] |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
56 else: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
57 def wrapped_bytes(bstr): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
58 assert bstr.startswith('b') |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
59 return bstr |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
60 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
61 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
62 # We do some scary stuff with CodeType() in template/eval.py |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
63 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
64 if IS_PYTHON2: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
65 def get_code_params(code): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
66 return (code.co_nlocals, code.co_stacksize, code.co_flags, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
67 code.co_code, code.co_consts, code.co_names, code.co_varnames, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
68 code.co_filename, code.co_name, code.co_firstlineno, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
69 code.co_lnotab, (), ()) |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
70 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
71 def build_code_chunk(code, filename, name, lineno): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
72 return CodeType(0, code.co_nlocals, code.co_stacksize, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
73 code.co_flags | 0x0040, code.co_code, code.co_consts, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
74 code.co_names, code.co_varnames, filename, name, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
75 lineno, code.co_lnotab, (), ()) |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
76 else: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
77 def get_code_params(code): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
78 return (code.co_nlocals, code.co_kwonlyargcount, code.co_stacksize, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
79 code.co_flags, code.co_code, code.co_consts, code.co_names, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
80 code.co_varnames, code.co_filename, code.co_name, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
81 code.co_firstlineno, code.co_lnotab, (), ()) |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
82 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
83 def build_code_chunk(code, filename, name, lineno): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
84 return CodeType(0, code.co_nlocals, code.co_kwonlyargcount, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
85 code.co_stacksize, code.co_flags | 0x0040, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
86 code.co_code, code.co_consts, code.co_names, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
87 code.co_varnames, filename, name, lineno, |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
88 code.co_lnotab, (), ()) |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
89 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
90 # Compatibility fallback implementations for Python < 2.6 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
91 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
92 try: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
93 next = next |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
94 except NameError: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
95 def next(iterator): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
96 return iterator.next() |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
97 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
98 # Compatibility fallback implementations for Python < 2.5 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
99 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
100 try: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
101 all = all |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
102 any = any |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
103 except NameError: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
104 def any(S): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
105 for x in S: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
106 if x: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
107 return True |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
108 return False |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
109 |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
110 def all(S): |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
111 for x in S: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
112 if not x: |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
113 return False |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
114 return True |
c5faa881d87f
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff
changeset
|
115 |