Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#18363 closed New feature (fixed)

Add a compatibility layer for Python 3

Reported by: Claude Paroz Owned by: nobody
Component: Python 3 Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For Django to run both on Python 2 and Python 3, we need to add some code which conditionally executes code depending on the Python version at runtime.

This patch is based on Vinay Sajip's branch (https://bitbucket.org/vinay.sajip/django), minus some code that I'm not sure we will really need (as now we dropped Python 2.5). We can always add some more later.

I've added some docs, but as I'm not the best English writer, this will need careful review.

Attachments (1)

py3_layer.diff (14.3 KB ) - added by Claude Paroz 13 years ago.
Add Python 3 compatibility layer

Download all attachments as: .zip

Change History (8)

by Claude Paroz, 13 years ago

Attachment: py3_layer.diff added

Add Python 3 compatibility layer

comment:2 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [5e6ded2e58597fa324c550bad35a30ee630ce223]:

Fixed #18363 -- Added Python 3 compatibility layer.

Thanks Vinay Sajip for the support of his django3 branch
and Alex Gaynor, kezabelle, YorikSar for the review.

comment:3 by Claude Paroz, 12 years ago

Resolution: fixed
Status: closedreopened

After some discussion, we will try to replace django.utils.py3 by the six module as django.utils.six.

http://pypi.python.org/pypi/six

in reply to:  3 comment:4 by Vinay Sajip, 12 years ago

Replying to claudep:

After some discussion, we will try to replace django.utils.py3 by the six module as django.utils.six.

http://pypi.python.org/pypi/six

Did this discussion happen on the django-core mailing list? I didn't see anything about it on django-developers.

I've no very strong views on using six, it's a nice library and I used a few of the ideas, but IMO there are points which go against using it directly; for example, because of how Django uses metaclasses, and how with_metaclass works, we need to distinguish Django's usage from usage in third-party modules. Just grep my sources for "DjangoBase" to see what I mean. I actually use a pretty small subsection of six functionality, and did consider using it in django.utils.six style, but decided against it.

You could of course keep django.utils.py3 and in it just do from .six import * followed by any additions or redefinitions which are needed, like n().

I also think that using io.StringIO on 2.x, as I believe six does, will lead to problems; 2.x io.StringIO expects only Unicode to be written to it, and that's not what e.g. parts of the stdlib (like json) do when writing to streams. Example (using Python 2.6):

>>> from io import StringIO
>>> fp = StringIO()
>>> import json; json.dump({}, fp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python26\lib\json\__init__.py", line 181, in dump
    fp.write(chunk)
  File "C:\python26\lib\io.py", line 1515, in write
    s.__class__.__name__)
TypeError: can't write str to text stream
>>>

By the way, in my repository on GitHub, I've removed the 2.5 compatibility cruft.

Last edited 12 years ago by Vinay Sajip (previous) (diff)

comment:5 by Aymeric Augustin <aymeric.augustin@…>, 12 years ago

In [8b0190984116873158ee627c7a033a7bd4c3a199]:

[py3] Bundled six for Python 3 compatibility.

Refs #18363.

comment:6 by Aymeric Augustin, 12 years ago

Resolution: fixed
Status: reopenedclosed

comment:7 by Aymeric Augustin, 12 years ago

It's actually possible to use six.with_metaclass() for ModelBase, see [ae4125ffce6c0a82e9016f67d60d31bc7595887c].

Note: See TracTickets for help on using tickets.
Back to Top