##// END OF EJS Templates
Manually document py3compat module....
Thomas Kluyver -
Show More
@@ -31,6 +31,8 b" if __name__ == '__main__':"
31 r'\.testing\.plugin',
31 r'\.testing\.plugin',
32 # This just prints a deprecation msg:
32 # This just prints a deprecation msg:
33 r'\.frontend$',
33 r'\.frontend$',
34 # We document this manually.
35 r'\.utils\.py3compat',
34 ]
36 ]
35
37
36 # We're rarely working on machines with the Azure SDK installed, so we
38 # We're rarely working on machines with the Azure SDK installed, so we
@@ -1,12 +1,19 b''
1 Writing code for Python 2 and 3
1 Writing code for Python 2 and 3
2 ===============================
2 ===============================
3
3
4 .. module:: IPython.utils.py3compat
5 :synopsis: Python 2 & 3 compatibility helpers
6
7 .. data:: PY3
8
9 Boolean indicating whether we're currently in Python 3.
10
4 Iterators
11 Iterators
5 ---------
12 ---------
6
13
7 Many built in functions and methods in Python 2 come in pairs, one
14 Many built in functions and methods in Python 2 come in pairs, one
8 returning a list, and one returning an iterator (e.g. :func:`range` and
15 returning a list, and one returning an iterator (e.g. :func:`range` and
9 :func:`xrange`). In Python 3, there is usually only the iterator form,
16 :func:`python:xrange`). In Python 3, there is usually only the iterator form,
10 but it has the name which gives a list in Python 2 (e.g. :func:`range`).
17 but it has the name which gives a list in Python 2 (e.g. :func:`range`).
11
18
12 The way to write compatible code depends on what you need:
19 The way to write compatible code depends on what you need:
@@ -32,6 +39,16 b' to use :meth:`dict.keys` or :meth:`dict.iterkeys`.'
32 Avoid using :func:`map` to cause function side effects. This is more
39 Avoid using :func:`map` to cause function side effects. This is more
33 clearly written with a simple for loop.
40 clearly written with a simple for loop.
34
41
42 .. data:: xrange
43
44 A reference to ``range`` on Python 3, and :func:`python:xrange` on Python 2.
45
46 .. function:: iteritems(d)
47 itervalues(d)
48
49 Iterate over (key, value) pairs of a dictionary, or just over values.
50 ``iterkeys`` is not defined: iterating over the dictionary yields its keys.
51
35 Changed standard library locations
52 Changed standard library locations
36 ----------------------------------
53 ----------------------------------
37
54
@@ -62,6 +79,15 b' but it behaves differently from :class:`StringIO.StringIO`, and much of'
62 our code assumes the use of the latter on Python 2. So a try/except on
79 our code assumes the use of the latter on Python 2. So a try/except on
63 the import may cause problems.
80 the import may cause problems.
64
81
82 .. function:: input
83
84 Behaves like :func:`python:raw_input` on Python 2.
85
86 .. data:: builtin_mod
87 builtin_mod_name
88
89 A reference to the module containing builtins, and its name as a string.
90
65 Unicode
91 Unicode
66 -------
92 -------
67
93
@@ -74,6 +100,42 b' the Python 3 builtin ``open`` function, available on Python 2 as well.'
74 We almost always need to specify the encoding parameter, because the
100 We almost always need to specify the encoding parameter, because the
75 default is platform dependent.
101 default is platform dependent.
76
102
103 We have several helper functions for converting between string types. They all
104 use the encoding from :func:`IPython.utils.encoding.getdefaultencoding` by default,
105 and the ``errors='replace'`` option to do best-effort conversions for the user's
106 system.
107
108 .. function:: unicode_to_str(u, encoding=None)
109 str_to_unicode(s, encoding=None)
110
111 Convert between unicode and the native str type. No-ops on Python 3.
112
113 .. function:: str_to_bytes(s, encoding=None)
114 bytes_to_str(u, encoding=None)
115
116 Convert between bytes and the native str type. No-ops on Python 2.
117
118 .. function:: cast_unicode(s, encoding=None)
119 cast_bytes(s, encoding=None)
120
121 Convert strings to unicode/bytes when they may be of either type.
122
123 .. function:: cast_unicode_py2(s, encoding=None)
124 cast_bytes_py2(s, encoding=None)
125
126 Convert strings to unicode/bytes when they may be of either type on Python 2,
127 but return them unaltered on Python 3 (where string types are more
128 predictable).
129
130 .. data:: unicode_type
131
132 A reference to ``str`` on Python 3, and to ``unicode`` on Python 2.
133
134 .. data:: string_types
135
136 A tuple for isinstance checks: ``(str,)`` on Python 3, ``(str, unicode)`` on
137 Python 2.
138
77 Relative imports
139 Relative imports
78 ----------------
140 ----------------
79
141
@@ -104,12 +166,16 b' Metaclasses'
104 -----------
166 -----------
105
167
106 The syntax for declaring a class with a metaclass is different in
168 The syntax for declaring a class with a metaclass is different in
107 Python 2 and 3. In most cases, the helper function
169 Python 2 and 3. A helper function works for most cases:
108 :func:`~IPython.utils.py3compat.with_metaclass` (copied from the six
109 library) can be used like this::
110
170
111 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
171 .. function:: with_metaclass
112 ...
172
173 Create a base class with a metaclass. Copied from the six library.
174
175 Used like this::
176
177 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
178 ...
113
179
114 Combining inheritance between Qt and the traitlets system, however, does
180 Combining inheritance between Qt and the traitlets system, however, does
115 not work with this. Instead, we do this::
181 not work with this. Instead, we do this::
@@ -120,3 +186,48 b' not work with this. Instead, we do this::'
120 This gives the new class a metaclass of :class:`~IPython.qt.util.MetaQObjectHasTraits`,
186 This gives the new class a metaclass of :class:`~IPython.qt.util.MetaQObjectHasTraits`,
121 and the parent classes :class:`~IPython.utils.traitlets.HasTraits` and
187 and the parent classes :class:`~IPython.utils.traitlets.HasTraits` and
122 :class:`~IPython.qt.util.SuperQObject`.
188 :class:`~IPython.qt.util.SuperQObject`.
189
190
191 Doctests
192 --------
193
194 .. function:: doctest_refactor_print(func_or_str)
195
196 Refactors print statements in doctests in Python 3 only. Accepts a string
197 or a function, so it can be used as a decorator.
198
199 .. function:: u_format(func_or_str)
200
201 Handle doctests written with ``{u}'abcþ'``, replacing the ``{u}`` with ``u``
202 for Python 2, and removing it for Python 3.
203
204 Accepts a string or a function, so it can be used as a decorator.
205
206 Execfile
207 --------
208
209 .. function:: execfile(fname, glob, loc=None)
210
211 Equivalent to the Python 2 :func:`python:execfile` builtin. We redefine it in
212 Python 2 to better handle non-ascii filenames.
213
214 Miscellaneous
215 -------------
216
217 .. autofunction:: safe_unicode
218
219 .. function:: isidentifier(s, dotted=False)
220
221 Checks whether the string s is a valid identifier in this version of Python.
222 In Python 3, non-ascii characters are allowed. If ``dotted`` is True, it
223 allows dots (i.e. attribute access) in the string.
224
225 .. function:: getcwd()
226
227 Return the current working directory as unicode, like :func:`os.getcwdu` on
228 Python 2.
229
230 .. function:: MethodType
231
232 Constructor for :class:`types.MethodType` that takes two arguments, like
233 the real constructor on Python 3.
General Comments 0
You need to be logged in to leave comments. Login now