#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)
Change History (8)
by , 13 years ago
Attachment: | py3_layer.diff added |
---|
comment:2 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
follow-up: 4 comment:3 by , 12 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
After some discussion, we will try to replace django.utils.py3 by the six module as django.utils.six.
comment:4 by , 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.
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 >>>
comment:6 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:7 by , 12 years ago
It's actually possible to use six.with_metaclass()
for ModelBase
, see [ae4125ffce6c0a82e9016f67d60d31bc7595887c].
Add Python 3 compatibility layer