Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#18363 closed New feature (fixed)

Add a compatibility layer for Python 3

Reported by: claudep Owned by: nobody
Component: Python 3 Version: master
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 claudep 2 years ago.
Add Python 3 compatibility layer

Download all attachments as: .zip

Change History (8)

Changed 2 years ago by claudep

Add Python 3 compatibility layer

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

  • Resolution set to fixed
  • Status changed from new to closed

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 follow-up: Changed 2 years ago by claudep

  • Resolution fixed deleted
  • Status changed from closed to reopened

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

comment:4 in reply to: ↑ 3 Changed 2 years ago by vsajip

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
>>>
Version 0, edited 2 years ago by vsajip (next)

comment:5 Changed 2 years ago by Aymeric Augustin <aymeric.augustin@…>

In [8b0190984116873158ee627c7a033a7bd4c3a199]:

[py3] Bundled six for Python 3 compatibility.

Refs #18363.

comment:6 Changed 2 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from reopened to closed

comment:7 Changed 2 years ago by aaugustin

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.