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