annotate genshi/compat.py @ 931:ade3abe742e9

Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
author hodgestar
date Fri, 18 Mar 2011 09:05:58 +0000
parents
children
rev   line source
931
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
2 #
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
4 # All rights reserved.
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
5 #
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: 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.
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
9 #
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: 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/.
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
13
ade3abe742e9 Merge r1139 from py3k: 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."""
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
15
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
16 import sys
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
18
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
19
ade3abe742e9 Merge r1139 from py3k: 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)
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
21
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
22
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
24
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
25 if IS_PYTHON2:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
26 def stringrepr(string):
ade3abe742e9 Merge r1139 from py3k: 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')
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
28 quoted = "'" + ascii.replace("'", "\\'") + "'"
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
30 return 'u' + quoted
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
31 return quoted
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
32 else:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
33 def stringrepr(string):
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
34 raise RuntimeError(
ade3abe742e9 Merge r1139 from py3k: 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.')
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
36
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
37
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
39
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
40 if IS_PYTHON2:
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
42 try:
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
44 except ImportError:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
45 BytesIO = StringIO
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
46 else:
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
48
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
49
ade3abe742e9 Merge r1139 from py3k: 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.
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
51
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
52 if IS_PYTHON2:
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: 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')
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
55 return bstr[1:]
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
56 else:
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: 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')
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
59 return bstr
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
60
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
61
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
63
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
64 if IS_PYTHON2:
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
69 code.co_lnotab, (), ())
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
70
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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, (), ())
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
76 else:
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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, (), ())
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
82
ade3abe742e9 Merge r1139 from py3k: 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):
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: 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,
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
88 code.co_lnotab, (), ())
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
89
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
91
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
92 try:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
93 next = next
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
94 except NameError:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
95 def next(iterator):
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
96 return iterator.next()
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
97
ade3abe742e9 Merge r1139 from py3k: 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
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
99
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
100 try:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
101 all = all
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
102 any = any
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
103 except NameError:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
104 def any(S):
ade3abe742e9 Merge r1139 from py3k: 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:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
106 if x:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
107 return True
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
108 return False
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
109
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
110 def all(S):
ade3abe742e9 Merge r1139 from py3k: 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:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
112 if not x:
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
113 return False
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
114 return True
ade3abe742e9 Merge r1139 from py3k: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
hodgestar
parents:
diff changeset
115
Copyright (C) 2012-2017 Edgewall Software