Quantcast

[Django] #18463: Using len() in Paginator object can raise an error

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

[Django] #18463: Using len() in Paginator object can raise an error

Django
#18463: Using len() in Paginator object can raise an error
------------------------------+--------------------
     Reporter:  renato@…      |      Owner:  nobody
         Type:  Bug           |     Status:  new
    Component:  Core (Other)  |    Version:  master
     Severity:  Normal        |   Keywords:
 Triage Stage:  Unreviewed    |  Has patch:  0
Easy pickings:  0             |      UI/UX:  0
------------------------------+--------------------
 I'm getting the following error if I try to run a piece of code through my
 views.py:

 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/mp/webapps/django/rep/app/views.py", line 331, in
 search_species
     print len(recs)
   File "/home/mp/webapps/django/lib/python2.7/django/core/paginator.py",
 line 88, in __len__
     return len(self.object_list)
   File "/home/mp/webapps/django/lib/python2.7/django/db/models/query.py",
 line 87, in __len__
     self._result_cache = list(self.iterator())
   File "/home/mp/webapps/django/lib/python2.7/django/db/models/query.py",
 line 284, in iterator
     model_cls = deferred_class_factory(self.model, skip)
   File
 "/home/mp/webapps/django/lib/python2.7/django/db/models/query_utils.py",
 line 180, in deferred_class_factory
     return type(name, (model,), overrides)
 TypeError: type() argument 1 must be string, not unicode

 Strangely, if I run a similar code directly in the Django shell,
 everything goes well. The code looks like this:

 {{{
 from app.models import MyClass
 qs = MyClass.objects.all()
 from django.core.paginator import Paginator
 paginator = Paginator(qs, 25)
 recs = paginator.page(1)
 print len(recs)
 }}}

 So you have to run this code through a method in views.py to get a crash
 in the last line.

 I'm afraid I have no idea of what's going on and how to properly fix this,
 but if I change the offending line from the traceback by forcing str() in
 the name parameter it works:

 {{{
 return type(str(name), (model,), overrides)
 }}}

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

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [Django] #18463: Using len() in Paginator object can raise an error

Django
#18463: Using len() in Paginator object can raise an error
------------------------------+------------------------------------
     Reporter:  renato@…      |                    Owner:  claudep
         Type:  Bug           |                   Status:  assigned
    Component:  Core (Other)  |                  Version:  master
     Severity:  Normal        |               Resolution:
     Keywords:                |             Triage Stage:  Accepted
    Has patch:  0             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+------------------------------------
Changes (by claudep):

 * status:  new => assigned
 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * owner:  nobody => claudep
 * needs_docs:   => 0
 * stage:  Unreviewed => Accepted


Old description:

> I'm getting the following error if I try to run a piece of code through
> my views.py:
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File "/home/mp/webapps/django/rep/app/views.py", line 331, in
> search_species
>     print len(recs)
>   File "/home/mp/webapps/django/lib/python2.7/django/core/paginator.py",
> line 88, in __len__
>     return len(self.object_list)
>   File "/home/mp/webapps/django/lib/python2.7/django/db/models/query.py",
> line 87, in __len__
>     self._result_cache = list(self.iterator())
>   File "/home/mp/webapps/django/lib/python2.7/django/db/models/query.py",
> line 284, in iterator
>     model_cls = deferred_class_factory(self.model, skip)
>   File
> "/home/mp/webapps/django/lib/python2.7/django/db/models/query_utils.py",
> line 180, in deferred_class_factory
>     return type(name, (model,), overrides)
> TypeError: type() argument 1 must be string, not unicode
>
> Strangely, if I run a similar code directly in the Django shell,
> everything goes well. The code looks like this:
>
> {{{
> from app.models import MyClass
> qs = MyClass.objects.all()
> from django.core.paginator import Paginator
> paginator = Paginator(qs, 25)
> recs = paginator.page(1)
> print len(recs)
> }}}
>
> So you have to run this code through a method in views.py to get a crash
> in the last line.
>
> I'm afraid I have no idea of what's going on and how to properly fix
> this, but if I change the offending line from the traceback by forcing
> str() in the name parameter it works:
>
> {{{
> return type(str(name), (model,), overrides)
> }}}

New description:

 I'm getting the following error if I try to run a piece of code through my
 views.py:

 {{{
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/mp/webapps/django/rep/app/views.py", line 331, in
 search_species
     print len(recs)
   File "/home/mp/webapps/django/lib/python2.7/django/core/paginator.py",
 line 88, in __len__
     return len(self.object_list)
   File "/home/mp/webapps/django/lib/python2.7/django/db/models/query.py",
 line 87, in __len__
     self._result_cache = list(self.iterator())
   File "/home/mp/webapps/django/lib/python2.7/django/db/models/query.py",
 line 284, in iterator
     model_cls = deferred_class_factory(self.model, skip)
   File
 "/home/mp/webapps/django/lib/python2.7/django/db/models/query_utils.py",
 line 180, in deferred_class_factory
     return type(name, (model,), overrides)
 TypeError: type() argument 1 must be string, not unicode
 }}}

 Strangely, if I run a similar code directly in the Django shell,
 everything goes well. The code looks like this:

 {{{
 from app.models import MyClass
 qs = MyClass.objects.all()
 from django.core.paginator import Paginator
 paginator = Paginator(qs, 25)
 recs = paginator.page(1)
 print len(recs)
 }}}

 So you have to run this code through a method in views.py to get a crash
 in the last line.

 I'm afraid I have no idea of what's going on and how to properly fix this,
 but if I change the offending line from the traceback by forcing str() in
 the name parameter it works:

 {{{
 return type(str(name), (model,), overrides)
 }}}

--

Comment:

 This bug is only triggered when the model name in deferred_class_factory
 is truncated (> 80 chars) and then a Unicode string is returned. Calling
 str() seems a good solution.

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

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [Django] #18463: Using len() in Paginator object can raise an error

Django
In reply to this post by Django
#18463: Using len() in Paginator object can raise an error
------------------------------+------------------------------------
     Reporter:  renato@…      |                    Owner:  claudep
         Type:  Bug           |                   Status:  closed
    Component:  Core (Other)  |                  Version:  master
     Severity:  Normal        |               Resolution:  fixed
     Keywords:                |             Triage Stage:  Accepted
    Has patch:  0             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+------------------------------------
Changes (by Claude Paroz <claude@…>):

 * status:  assigned => closed
 * resolution:   => fixed


Comment:

 In [3dd5d726d1c3bf8f5901c992d7586e5ec146bc2d]:
 {{{
 #!CommitTicketReference repository=""
 revision="3dd5d726d1c3bf8f5901c992d7586e5ec146bc2d"
 Fixed #18463 -- Forced type() argument to be a byte string
 }}}

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

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.

Loading...