Add documentation about python3 compatibility #89

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

@claudep
Django member

No description provided.

@alex alex commented on the diff May 23, 2012
django/utils/py3.py
+ long_type = long
+
+ from io import BytesIO as OutputIO
+
+ # Glue code for syntax differences
+ def exec_(code, globs=None, locs=None):
+ """Execute code in a namespace."""
+ if globs is None:
+ frame = sys._getframe(1)
+ globs = frame.f_globals
+ if locs is None:
+ locs = frame.f_locals
+ del frame
+ elif locs is None:
+ locs = globs
+ exec("""exec code in globs, locs""")
@alex
alex May 23, 2012

Is exec actually used anywhere besides in this file?

@claudep
claudep May 24, 2012

AFAIK, it's only used for the reraise definition just below. You can find the exact same code in six: https://bitbucket.org/gutworth/six/src/175a03e21623/six.py#cl-294

@alex
alex May 24, 2012

If we don't actually need exec for django why don't we simplify this implementation and give it some name so it won't be public (e.g. _exec).

@claudep
claudep May 24, 2012

Feel free to suggest any improvement/simplification. I see now that Vinay used it once in his branch in _doctest.py. I have chosen another route for updating _doctest.py and propose to copy python 3.2 doctest.py in _doctest3.py, reapply Python #1521051 on it and import conditionally _doctest.py/_doctest3.py. It's difficult for me to evaluate if this function might be useful or not in Django apps ecosystem.

@alex alex commented on the diff May 23, 2012
docs/topics/python3.txt
+Python 2 and Python 3, you can import it from ``django.utils.py3`` where it
+will be automatically converted depending on your current Python version.
+
+PY3
+---
+
+If you need to know anywhere in your code if you are running Python 3 or a
+previous Python 2 version, you can check the ``PY3`` boolean variable::
+
+ from django.utils.py3 import PY3
+
+ if PY3:
+ # Do stuff Python 3-wise
+ else:
+ # Do stuff Python 2-wise
+
@alex
alex May 23, 2012

This section should probably note that using the various variables in the py3 module is preferrable to branching on if PY3

@claudep
claudep May 24, 2012

Good suggestion. Something like: "This should be considered as a last resort solution when it is not possible to import a compatible name from django.utils.py3."

@kezabelle kezabelle commented on the diff May 24, 2012
docs/topics/python3.txt
+
+Should be replaced by::
+
+ from django.utils.py3 import string_types, text_type
+
+ if isinstance(foo, string_types):
+ print("foo is a string")
+
+ # I want to convert a number to a Unicode string
+ bar = 45
+ bar_string = text_type(bar)
+
+No more long type
+-----------------
+``long`` and ``int`` types have been unified in Python 3, meaning that ``long``
+is no more available. ``django.utils.py3`` provides both ``long_type`` and
@kezabelle
kezabelle May 24, 2012

"no more available" doesn't read particularly well, it'd be better to say something like "no longer available", but I'm not enthused with that either because it seems accidentally punny.

@YorikSar YorikSar commented on the diff May 29, 2012
django/utils/py3.py
+ text_type = str
+ integer_types = int,
+ long_type = int
+
+ from io import StringIO as OutputIO
+
+ # Glue code for syntax differences
+ def reraise(tp, value, tb=None):
+ if value.__traceback__ is not tb:
+ raise value.with_traceback(tb)
+ raise value
+
+ exec_ = getattr(builtins, 'exec')
+ def with_metaclass(meta, base=object):
+ ns = dict(base=base, meta=meta)
+ exec_("""class _DjangoBase(base, metaclass=meta):
@YorikSar
YorikSar May 29, 2012

Why use exec? You can just write contents of the string in the function.

@YorikSar YorikSar commented on the diff May 29, 2012
django/utils/py3.py
+
+ # Glue code for syntax differences
+ def exec_(code, globs=None, locs=None):
+ """Execute code in a namespace."""
+ if globs is None:
+ frame = sys._getframe(1)
+ globs = frame.f_globals
+ if locs is None:
+ locs = frame.f_locals
+ del frame
+ elif locs is None:
+ locs = globs
+ exec("""exec code in globs, locs""")
+ exec_("""def reraise(tp, value, tb=None):
+ raise tp, value, tb
+""")
@YorikSar
YorikSar May 29, 2012

Once again, exec is not needed here.

@claudep
Django member

Pushed in 5e6ded2.
Thanks for the review. I'm not sure if I included all what you meant in the review comments, feel free to open tickets to fix unsolved issues or to add further improvements.

@claudep claudep closed this Jun 7, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment