[Django] #19928: python_2_unicode_compatible breaks Python 2

16 views
Skip to first unread message

Django

unread,
Feb 27, 2013, 6:04:43 AM2/27/13
to django-...@googlegroups.com
#19928: python_2_unicode_compatible breaks Python 2
-------------------------------+---------------------------------
Reporter: vernondcole | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords: porting to python 3
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------
Following the example in
[https://docs.djangoproject.com/en/1.5/topics/python3/]
I made my models.py look like this:

{{{
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
#@python_2_unicode_compatible
class Ward_Name(models.Model):
state = models.CharField("State", max_length=55,
help_text="Nigearin State (large political subdivision)")
lga = models.CharField("LGA", max_length=255, blank=True,
help_text="Nigerain Local Government Area (medium political
subdivision)")
ward = models.CharField(max_length=255, blank=True,
help_text="Ward (small political subdivision)")
db_ward = models.CharField("Db Ward",max_length=255, blank=True,
help_text="database altered version of Ward name")
def __str__(self):
return str('State={}, LGA={}, Ward={}, called "{}"'.format(
self.state, self.lga, self.ward, self.db_ward))
class Meta:
unique_together = (("lga","ward"))
index_together = [["lga","ward"]]
}}}
Note that the decorator is commented out...
I test using the following code, which works in both Python2.7 and
Python3.3
$python manage.py x_test
{{{
from __future__ import print_function
from django.core.management.base import BaseCommand
from nomads.models import Ward_Name

class Command(BaseCommand):
args = "<(none)>"
help = 'test reading Ward Name data table'

def handle(self, *args, **options):
self.test_read(*args)

def test_read(self, *args):
qs = Ward_Name.objects.all()
for w in qs:
print('Ward=',w)
}}}
When I un-comment the decorator the program fails under python 2

$ python manage.py x_test
RuntimeError: maximum recursion depth exceeded while calling a Python
object

Is the error in the django 1.5 code, the documentation, or my
understanding?

(the work-around is easy: just do not use the feature and everything
works in both versions)

(My environment is: Ubuntu 12.10 64 bit
django 1.5stable pulled from github this morning

--
Ticket URL: <https://code.djangoproject.com/ticket/19928>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 27, 2013, 6:55:18 AM2/27/13
to django-...@googlegroups.com
#19928: python_2_unicode_compatible breaks Python 2
-------------------------------------+-------------------------------------
Reporter: vernondcole | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: master
Severity: Normal | Resolution: invalid

Keywords: porting to python 3 | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

The [https://docs.djangoproject.com/en/dev/topics/python3/#str-and-
unicode-methods docs] say "you must define a `__str__()` method returning
text".

Your `__str__()` method returns a bytestring under Python 2.

--
Ticket URL: <https://code.djangoproject.com/ticket/19928#comment:1>

Django

unread,
Jan 1, 2014, 12:18:22 PM1/1/14
to django-...@googlegroups.com
#19928: python_2_unicode_compatible breaks Python 2
-------------------------------------+-------------------------------------
Reporter: vernondcole | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: master
Severity: Normal | Resolution: invalid

Keywords: porting to python 3 | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by msopacua):

* cc: m.r.sopacua@… (added)


Comment:

I'd like to reopen this issue or create a new one for expanding the
documentation. It's very slim on the whole __str__ method in python 2 and
specifically doesn't deal with abstract classes. I just spent quite a bit
of time debugging this issue, where I had the __str__() method defined in
the abstract class and repeated the decorator on derived classes. I'm
guessing this is the error and somehow things get screwed when doing it
like that. No matter what I returned in the abstract class I got the
recursion error. When I finally reimplemented the method in the derived
class and gave back a simple string literal, things went ok.
I'm still figuring out how to deal with it, cause what I really want to
return is a property of a foreign key.
Anyway, reopen this, or create a new one?

--
Ticket URL: <https://code.djangoproject.com/ticket/19928#comment:2>

Django

unread,
Jan 1, 2014, 12:38:25 PM1/1/14
to django-...@googlegroups.com
#19928: python_2_unicode_compatible breaks Python 2
-------------------------------------+-------------------------------------
Reporter: vernondcole | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: master
Severity: Normal | Resolution: invalid

Keywords: porting to python 3 | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aaugustin):

Don't repeat the decorator on derived classes. It's only appropriate on
the class that defines `__str__`.

--
Ticket URL: <https://code.djangoproject.com/ticket/19928#comment:3>

Reply all
Reply to author
Forward
0 new messages