python3.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. ===================
  2. Porting to Python 3
  3. ===================
  4. Django 1.5 is the first version of Django to support Python 3. The same code
  5. runs both on Python 2 (≥ 2.6.5) and Python 3 (≥ 3.2), thanks to the six_
  6. compatibility layer.
  7. .. _six: http://pythonhosted.org/six/
  8. This document is primarily targeted at authors of pluggable application
  9. who want to support both Python 2 and 3. It also describes guidelines that
  10. apply to Django's code.
  11. Philosophy
  12. ==========
  13. This document assumes that you are familiar with the changes between Python 2
  14. and Python 3. If you aren't, read `Python's official porting guide`_ first.
  15. Refreshing your knowledge of unicode handling on Python 2 and 3 will help; the
  16. `Pragmatic Unicode`_ presentation is a good resource.
  17. Django uses the *Python 2/3 Compatible Source* strategy. Of course, you're
  18. free to chose another strategy for your own code, especially if you don't need
  19. to stay compatible with Python 2. But authors of pluggable applications are
  20. encouraged to use the same porting strategy as Django itself.
  21. Writing compatible code is much easier if you target Python ≥ 2.6. Django 1.5
  22. introduces compatibility tools such as :mod:`django.utils.six`, which is a
  23. customized version of the :mod:`six module <six>`. For convenience,
  24. forwards-compatible aliases were introduced in Django 1.4.2. If your
  25. application takes advantage of these tools, it will require Django ≥ 1.4.2.
  26. Obviously, writing compatible source code adds some overhead, and that can
  27. cause frustration. Django's developers have found that attempting to write
  28. Python 3 code that's compatible with Python 2 is much more rewarding than the
  29. opposite. Not only does that make your code more future-proof, but Python 3's
  30. advantages (like the saner string handling) start shining quickly. Dealing
  31. with Python 2 becomes a backwards compatibility requirement, and we as
  32. developers are used to dealing with such constraints.
  33. Porting tools provided by Django are inspired by this philosophy, and it's
  34. reflected throughout this guide.
  35. .. _Python's official porting guide: http://docs.python.org/3/howto/pyporting.html
  36. .. _Pragmatic Unicode: http://nedbatchelder.com/text/unipain.html
  37. Porting tips
  38. ============
  39. Unicode literals
  40. ----------------
  41. This step consists in:
  42. - Adding ``from __future__ import unicode_literals`` at the top of your Python
  43. modules -- it's best to put it in each and every module, otherwise you'll
  44. keep checking the top of your files to see which mode is in effect;
  45. - Removing the ``u`` prefix before unicode strings;
  46. - Adding a ``b`` prefix before bytestrings.
  47. Performing these changes systematically guarantees backwards compatibility.
  48. However, Django applications generally don't need bytestrings, since Django
  49. only exposes unicode interfaces to the programmer. Python 3 discourages using
  50. bytestrings, except for binary data or byte-oriented interfaces. Python 2
  51. makes bytestrings and unicode strings effectively interchangeable, as long as
  52. they only contain ASCII data. Take advantage of this to use unicode strings
  53. wherever possible and avoid the ``b`` prefixes.
  54. .. note::
  55. Python 2's ``u`` prefix is a syntax error in Python 3.2 but it will be
  56. allowed again in Python 3.3 thanks to :pep:`414`. Thus, this
  57. transformation is optional if you target Python ≥ 3.3. It's still
  58. recommended, per the "write Python 3 code" philosophy.
  59. String handling
  60. ---------------
  61. Python 2's `unicode`_ type was renamed :class:`str` in Python 3,
  62. ``str()`` was renamed :func:`bytes`, and `basestring`_ disappeared.
  63. six_ provides :ref:`tools <string-handling-with-six>` to deal with these
  64. changes.
  65. Django also contains several string related classes and functions in the
  66. :mod:`django.utils.encoding` and :mod:`django.utils.safestring` modules. Their
  67. names used the words ``str``, which doesn't mean the same thing in Python 2
  68. and Python 3, and ``unicode``, which doesn't exist in Python 3. In order to
  69. avoid ambiguity and confusion these concepts were renamed ``bytes`` and
  70. ``text``.
  71. Here are the name changes in :mod:`django.utils.encoding`:
  72. ================== ==================
  73. Old name New name
  74. ================== ==================
  75. ``smart_str`` ``smart_bytes``
  76. ``smart_unicode`` ``smart_text``
  77. ``force_unicode`` ``force_text``
  78. ================== ==================
  79. For backwards compatibility, the old names still work on Python 2. Under
  80. Python 3, ``smart_str`` is an alias for ``smart_text``.
  81. For forwards compatibility, the new names work as of Django 1.4.2.
  82. .. note::
  83. :mod:`django.utils.encoding` was deeply refactored in Django 1.5 to
  84. provide a more consistent API. Check its documentation for more
  85. information.
  86. :mod:`django.utils.safestring` is mostly used via the
  87. :func:`~django.utils.safestring.mark_safe` and
  88. :func:`~django.utils.safestring.mark_for_escaping` functions, which didn't
  89. change. In case you're using the internals, here are the name changes:
  90. ================== ==================
  91. Old name New name
  92. ================== ==================
  93. ``EscapeString`` ``EscapeBytes``
  94. ``EscapeUnicode`` ``EscapeText``
  95. ``SafeString`` ``SafeBytes``
  96. ``SafeUnicode`` ``SafeText``
  97. ================== ==================
  98. For backwards compatibility, the old names still work on Python 2. Under
  99. Python 3, ``EscapeString`` and ``SafeString`` are aliases for ``EscapeText``
  100. and ``SafeText`` respectively.
  101. For forwards compatibility, the new names work as of Django 1.4.2.
  102. :meth:`~object.__str__` and ` __unicode__()`_ methods
  103. -----------------------------------------------------
  104. In Python 2, the object model specifies :meth:`~object.__str__` and
  105. ` __unicode__()`_ methods. If these methods exist, they must return
  106. ``str`` (bytes) and ``unicode`` (text) respectively.
  107. The ``print`` statement and the :class:`str` built-in call
  108. :meth:`~object.__str__` to determine the human-readable representation of an
  109. object. The ``unicode`` built-in calls ` __unicode__()`_ if it
  110. exists, and otherwise falls back to :meth:`~object.__str__` and decodes the
  111. result with the system encoding. Conversely, the
  112. :class:`~django.db.models.Model` base class automatically derives
  113. :meth:`~object.__str__` from ` __unicode__()`_ by encoding to UTF-8.
  114. In Python 3, there's simply :meth:`~object.__str__`, which must return ``str``
  115. (text).
  116. (It is also possible to define :meth:`~object.__bytes__`, but Django application
  117. have little use for that method, because they hardly ever deal with ``bytes``.)
  118. Django provides a simple way to define :meth:`~object.__str__` and
  119. ` __unicode__()`_ methods that work on Python 2 and 3: you must
  120. define a :meth:`~object.__str__` method returning text and to apply the
  121. :func:`~django.utils.encoding.python_2_unicode_compatible` decorator.
  122. On Python 3, the decorator is a no-op. On Python 2, it defines appropriate
  123. ` __unicode__()`_ and :meth:`~object.__str__` methods (replacing the
  124. original :meth:`~object.__str__` method in the process). Here's an example::
  125. from __future__ import unicode_literals
  126. from django.utils.encoding import python_2_unicode_compatible
  127. @python_2_unicode_compatible
  128. class MyClass(object):
  129. def __str__(self):
  130. return "Instance of my class"
  131. This technique is the best match for Django's porting philosophy.
  132. For forwards compatibility, this decorator is available as of Django 1.4.2.
  133. Finally, note that :meth:`~object.__repr__` must return a ``str`` on all
  134. versions of Python.
  135. :class:`dict` and :class:`dict`-like classes
  136. --------------------------------------------
  137. :meth:`dict.keys`, :meth:`dict.items` and :meth:`dict.values` return lists in
  138. Python 2 and iterators in Python 3. :class:`~django.http.QueryDict` and the
  139. :class:`dict`-like classes defined in :mod:`django.utils.datastructures`
  140. behave likewise in Python 3.
  141. six_ provides compatibility functions to work around this change:
  142. :func:`~six.iterkeys`, :func:`~six.iteritems`, and :func:`~six.itervalues`.
  143. It also contains an undocumented ``iterlists`` function that works well for
  144. ``django.utils.datastructures.MultiValueDict`` and its subclasses.
  145. :class:`~django.http.HttpRequest` and :class:`~django.http.HttpResponse` objects
  146. --------------------------------------------------------------------------------
  147. According to :pep:`3333`:
  148. - headers are always ``str`` objects,
  149. - input and output streams are always ``bytes`` objects.
  150. Specifically, :attr:`HttpResponse.content <django.http.HttpResponse.content>`
  151. contains ``bytes``, which may become an issue if you compare it with a
  152. ``str`` in your tests. The preferred solution is to rely on
  153. :meth:`~django.test.SimpleTestCase.assertContains` and
  154. :meth:`~django.test.SimpleTestCase.assertNotContains`. These methods accept a
  155. response and a unicode string as arguments.
  156. Coding guidelines
  157. =================
  158. The following guidelines are enforced in Django's source code. They're also
  159. recommended for third-party application who follow the same porting strategy.
  160. Syntax requirements
  161. -------------------
  162. Unicode
  163. ~~~~~~~
  164. In Python 3, all strings are considered Unicode by default. The ``unicode``
  165. type from Python 2 is called ``str`` in Python 3, and ``str`` becomes
  166. ``bytes``.
  167. You mustn't use the ``u`` prefix before a unicode string literal because it's
  168. a syntax error in Python 3.2. You must prefix byte strings with ``b``.
  169. In order to enable the same behavior in Python 2, every module must import
  170. ``unicode_literals`` from ``__future__``::
  171. from __future__ import unicode_literals
  172. my_string = "This is an unicode literal"
  173. my_bytestring = b"This is a bytestring"
  174. If you need a byte string literal under Python 2 and a unicode string literal
  175. under Python 3, use the :class:`str` builtin::
  176. str('my string')
  177. In Python 3, there aren't any automatic conversions between ``str`` and
  178. ``bytes``, and the :mod:`codecs` module became more strict. :meth:`str.encode`
  179. always returns ``bytes``, and ``bytes.decode`` always returns ``str``. As a
  180. consequence, the following pattern is sometimes necessary::
  181. value = value.encode('ascii', 'ignore').decode('ascii')
  182. Be cautious if you have to `index bytestrings`_.
  183. .. _index bytestrings: https://docs.python.org/3/howto/pyporting.html#indexing-bytes-objects
  184. Exceptions
  185. ~~~~~~~~~~
  186. When you capture exceptions, use the ``as`` keyword::
  187. try:
  188. ...
  189. except MyException as exc:
  190. ...
  191. This older syntax was removed in Python 3::
  192. try:
  193. ...
  194. except MyException, exc: # Don't do that!
  195. ...
  196. The syntax to reraise an exception with a different traceback also changed.
  197. Use :func:`six.reraise`.
  198. Magic methods
  199. -------------
  200. Use the patterns below to handle magic methods renamed in Python 3.
  201. Iterators
  202. ~~~~~~~~~
  203. ::
  204. class MyIterator(six.Iterator):
  205. def __iter__(self):
  206. return self # implement some logic here
  207. def __next__(self):
  208. raise StopIteration # implement some logic here
  209. Boolean evaluation
  210. ~~~~~~~~~~~~~~~~~~
  211. ::
  212. class MyBoolean(object):
  213. def __bool__(self):
  214. return True # implement some logic here
  215. def __nonzero__(self): # Python 2 compatibility
  216. return type(self).__bool__(self)
  217. Division
  218. ~~~~~~~~
  219. ::
  220. class MyDivisible(object):
  221. def __truediv__(self, other):
  222. return self / other # implement some logic here
  223. def __div__(self, other): # Python 2 compatibility
  224. return type(self).__truediv__(self, other)
  225. def __itruediv__(self, other):
  226. return self // other # implement some logic here
  227. def __idiv__(self, other): # Python 2 compatibility
  228. return type(self).__itruediv__(self, other)
  229. Special methods are looked up on the class and not on the instance to reflect
  230. the behavior of the Python interpreter.
  231. .. module: django.utils.six
  232. Writing compatible code with six
  233. --------------------------------
  234. six_ is the canonical compatibility library for supporting Python 2 and 3 in
  235. a single codebase. Read its documentation!
  236. A :mod:`customized version of six <django.utils.six>` is bundled with Django
  237. as of version 1.4.2. You can import it as ``django.utils.six``.
  238. Here are the most common changes required to write compatible code.
  239. .. _string-handling-with-six:
  240. String handling
  241. ~~~~~~~~~~~~~~~
  242. The ``basestring`` and ``unicode`` types were removed in Python 3, and the
  243. meaning of ``str`` changed. To test these types, use the following idioms::
  244. isinstance(myvalue, six.string_types) # replacement for basestring
  245. isinstance(myvalue, six.text_type) # replacement for unicode
  246. isinstance(myvalue, bytes) # replacement for str
  247. Python ≥ 2.6 provides ``bytes`` as an alias for ``str``, so you don't need
  248. :data:`six.binary_type`.
  249. ``long``
  250. ~~~~~~~~
  251. The ``long`` type no longer exists in Python 3. ``1L`` is a syntax error. Use
  252. :data:`six.integer_types` check if a value is an integer or a long::
  253. isinstance(myvalue, six.integer_types) # replacement for (int, long)
  254. ``xrange``
  255. ~~~~~~~~~~
  256. Import ``six.moves.xrange`` wherever you use ``xrange``.
  257. Moved modules
  258. ~~~~~~~~~~~~~
  259. Some modules were renamed in Python 3. The ``django.utils.six.moves``
  260. module (based on the :mod:`six.moves module <six.moves>`) provides a
  261. compatible location to import them.
  262. PY2
  263. ~~~
  264. If you need different code in Python 2 and Python 3, check :data:`six.PY2`::
  265. if six.PY2:
  266. # compatibility code for Python 2
  267. This is a last resort solution when :mod:`six` doesn't provide an appropriate
  268. function.
  269. .. module:: django.utils.six
  270. Django customized version of six
  271. --------------------------------
  272. The version of six bundled with Django (``django.utils.six``) includes a few
  273. extras.
  274. .. function:: assertRaisesRegex(testcase, *args, **kwargs)
  275. This replaces ``testcase.assertRaisesRegexp`` on Python 2, and
  276. ``testcase.assertRaisesRegex`` on Python 3. ``assertRaisesRegexp`` still
  277. exists in current Python 3 versions, but issues a warning.
  278. .. function:: assertRegex(testcase, *args, **kwargs)
  279. This replaces ``testcase.assertRegexpMatches`` on Python 2, and
  280. ``testcase.assertRegex`` on Python 3. ``assertRegexpMatches`` still
  281. exists in current Python 3 versions, but issues a warning.
  282. In addition to six' defaults moves, Django's version provides ``thread`` as
  283. ``_thread`` and ``dummy_thread`` as ``_dummy_thread``.
  284. .. _unicode: http://docs.python.org/2/library/functions.html#unicode
  285. .. _ __unicode__(): https://docs.python.org/2/reference/datamodel.html#object.__unicode__
  286. .. _basestring: http://docs.python.org/2/library/functions.html#basestring