Edgewall Software

source: trunk/genshi/compat.py

Last change on this file was 1248, checked in by hodgestar, 10 years ago

Add isstring helper.

  • Property svn:eol-style set to native
File size: 3.6 KB
RevLine 
[1139]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
16import sys
17from types import CodeType
18
19
20IS_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
25if 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
32else:
33    def stringrepr(string):
34        raise RuntimeError(
35                'Python 2 compatibility function. Not usable in Python 3.')
36
37
[1248]38# We need to test if an object is an instance of a string type in places
39
40if IS_PYTHON2:
41    def isstring(obj):
42        return isinstance(obj, basestring)
43else:
44    def isstring(obj):
45        return isinstance(obj, str)
46
[1139]47# We need to differentiate between StringIO and BytesIO in places
48
49if IS_PYTHON2:
50    from StringIO import StringIO
51    try:
52        from cStringIO import StringIO as BytesIO
53    except ImportError:
54        BytesIO = StringIO
55else:
56    from io import StringIO, BytesIO
57
58
59# We want to test bytestring input to some stuff.
60
61if IS_PYTHON2:
62    def wrapped_bytes(bstr):
63        assert bstr.startswith('b')
64        return bstr[1:]
65else:
66    def wrapped_bytes(bstr):
67        assert bstr.startswith('b')
68        return bstr
69
70
71# We do some scary stuff with CodeType() in template/eval.py
72
73if IS_PYTHON2:
74    def get_code_params(code):
75        return (code.co_nlocals, code.co_stacksize, code.co_flags,
76                code.co_code, code.co_consts, code.co_names, code.co_varnames,
77                code.co_filename, code.co_name, code.co_firstlineno,
78                code.co_lnotab, (), ())
79
80    def build_code_chunk(code, filename, name, lineno):
81        return CodeType(0, code.co_nlocals, code.co_stacksize,
82                        code.co_flags | 0x0040, code.co_code, code.co_consts,
83                        code.co_names, code.co_varnames, filename, name,
84                        lineno, code.co_lnotab, (), ())
85else:
86    def get_code_params(code):
87        return (code.co_nlocals, code.co_kwonlyargcount, code.co_stacksize,
88                code.co_flags, code.co_code, code.co_consts, code.co_names,
89                code.co_varnames, code.co_filename, code.co_name,
90                code.co_firstlineno, code.co_lnotab, (), ())
91
92    def build_code_chunk(code, filename, name, lineno):
93        return CodeType(0, code.co_nlocals, code.co_kwonlyargcount,
94                        code.co_stacksize, code.co_flags | 0x0040,
95                        code.co_code, code.co_consts, code.co_names,
96                        code.co_varnames, filename, name, lineno,
97                        code.co_lnotab, (), ())
98
99# Compatibility fallback implementations for Python < 2.6
100
101try:
102    next = next
103except NameError:
104    def next(iterator):
105        return iterator.next()
106
107# Compatibility fallback implementations for Python < 2.5
108
109try:
110    all = all
111    any = any
112except NameError:
113    def any(S):
114        for x in S:
115            if x:
116                return True
117        return False
118
119    def all(S):
120        for x in S:
121            if not x:
122                return False
123        return True
Note: See TracBrowser for help on using the repository browser.