Opened 5 years ago

Closed 5 years ago

#12256 closed (invalid)

TypeError when calling unicode(type(Form()))

Reported by: sethrh Owned by: nobody
Component: Forms Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Hello all,

I've discovered a bug (I think) relating to the Form metaclass. I noticed it while running Eric4's debugger through a view which handled a form.

My code is doing this:

def view_function(request):
    if request.method == "POST": 
        form = MyForm(request.POST)
        if form.is_valid():
            ...

As soon as I create the form instance, I get a TypeError returned on the web page. This is happens when the debugger calls unicode() on all of the local variables to display their types and values. After the server gets the TypeError, the debug session hangs.

>>> type(Form)
<class 'django.forms.forms.DeclarativeFieldsMetaclass'>
>>> f = Form()
>>> type(f)
<class 'django.forms.forms.Form'>
>>> unicode(type(f))
Traceback (most recent call last):
   File "<console>", line 1, in <module>
TypeError: unbound method __unicode__() must be called with Form instance as first argument (got nothing instead)
>>> import inspect
>>> inspect.getsource(type(f).__unicode__)
'    def __unicode__(self):\n        return self.as_table()\n'

It appears that the metaclass is somehow sticking Form.__unicode__ into type(Form).__unicode__.

I'm running Django 1.1 SVN-11366 under Python 2.5.1 on Windows XP pro sp2.

Change History (7)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I'm unable to reproduce:

>>> f = FeedbackForm ()

>>> type(f)
[10] <class 'feedback.forms.FeedbackForm'>

>>> unicode(type(f))
[11] u"<class 'feedback.forms.FeedbackForm'>"

comment:2 Changed 5 years ago by kmtracey

Ha, I am able to reproduce, but I also find there's no need to do any fancy metaclass stuff to see similar behavior. A simple class based on object, that provides its own implementation of __unicode__ behaves the same way:

>>> class X(object):
...     def __init__(self, x):
...         self.x = x
...     def __unicode__(self):
...         return unicode(self.x)
...
>>> x = X(5)
>>> unicode(x)
u'5'
>>> unicode(type(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method __unicode__() must be called with X instance as first argument (got nothing instead)
>>>

Given that, I don't see how this is a bug in Django instead of a bug in the debugger. (And I'd expect it to have trouble with other Django classes -- plenty of which define __unicode__ -- are forms really its only trouble spot?)

comment:3 follow-up: Changed 5 years ago by Alex

This must be a bug in certain versions of python because your code snippet runs fine for me:

>>> class X(object):
...     def __init__(self, x):
...         self.x = x
...     def __unicode__(self):
...         return unicode(self.x)
...     
...     

>>> x = X(5)

>>> unicode(x)
[3] u'5'

>>> unicode(type(x))
[4] u"<class '__main__.X'>"

What version of CPython are you guys on?

comment:4 in reply to: ↑ 3 ; follow-up: Changed 5 years ago by sethrh

Replying to Alex:

This must be a bug in certain versions of python because your code snippet runs fine for me:

>>> class X(object):
...     def __init__(self, x):
...         self.x = x
...     def __unicode__(self):
...         return unicode(self.x)
...     
...     

>>> x = X(5)

>>> unicode(x)
[3] u'5'

>>> unicode(type(x))
[4] u"<class '__main__.X'>"

What version of CPython are you guys on?

I tried reproducing on Python 2.6.2 (r262:71600 Apr 16 2009 / Mac OS X 10.4). Neither mine nor kmtracey's example cause issues there. Python 2.5.1 (r251:54863 Apr 18 2007 / XP Pro) gets exceptions with both cases. (Wow! Looks like python 2.5.1 is getting pretty old!)

comment:5 in reply to: ↑ 4 ; follow-up: Changed 5 years ago by kmtracey

Replying to Alex:

Mine was with Python 2.5.2. Trying with 2.6.4 I see the behavior you show. Unless someone knows of a really easy workaround I think this should be closed as invalid. I still think the debugger should be handling it better, given the way older Pythons behave, unless the debugger only claims to work properly on Python 2.6.

comment:6 in reply to: ↑ 5 Changed 5 years ago by sethrh

Replying to kmtracey:

Replying to Alex:

Mine was with Python 2.5.2. Trying with 2.6.4 I see the behavior you show. Unless someone knows of a really easy workaround I think this should be closed as invalid. I still think the debugger should be handling it better, given the way older Pythons behave, unless the debugger only claims to work properly on Python 2.6.

Works for me. I originally thought this was a bug with Eric4's debugger, and have already filed a bug with detlev. Thanks.

comment:7 Changed 5 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top