object of type 'generator' has no len()

Issue #1 resolved
Cristian Ciupitu created an issue

I'm getting the following error when I visit http://192.168.123.238:8000/product/PY-Rocks/ :
{{{
TypeError at /product/PY-Rocks/
object of type 'generator' has no len()

Exception Value: object of type 'generator' has no len()
Exception Location: /usr/lib/python2.6/site-packages/product/views/__init__.py in get_product, line 148

}}}

As far as I can see the problem is caused by the fact that in the {{{get_product}}} function the errors are "stored" in a generator:
{{{
errors = (m for m in get_messages(request) if m.level == constants.ERROR)
}}}
This means you can't use it like this:
{{{
'error_message' : errors[0] if len(errors) else None,
}}}
I would propose to store the errors in a list using list comprehension, but I'm not familiar enough with the code.

This code is from change <<changeset 1952:e486b4733374>>.

Here's the full traceback in case it's needed:
{{{
Environment:

Request Method: GET
Request URL: http://192.168.123.238:8000/product/PY-Rocks/
Django Version: 1.2.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.sites',
'satchmo_store.shop',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.comments',
'django.contrib.sessions',
'django.contrib.sitemaps',
'registration',
'sorl.thumbnail',
'keyedcache',
'livesettings',
'l10n',
'satchmo_utils.thumbnail',
'satchmo_store.contact',
'tax',
'tax.modules.no',
'tax.modules.area',
'tax.modules.percent',
'shipping',
'product',
'product.modules.configurable',
'product.modules.custom',
'payment',
'payment.modules.dummy',
'satchmo_ext.satchmo_toolbar',
'satchmo_utils',
'app_plugins',
'store.localsite']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'threaded_multihost.middleware.ThreadLocalMiddleware',
'satchmo_store.shop.SSLMiddleware.SSLRedirect')

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, callback_args, *callback_kwargs)
File "/usr/lib/python2.6/site-packages/product/views/init.py" in get_product
148. 'error_message' : errors[0] if len(errors) else None,

Exception Type: TypeError at /product/PY-Rocks/
Exception Value: object of type 'generator' has no len()

}}}

Comments (2)

  1. Cristian Ciupitu reporter

    P.S. If you choose to use a list then if len(errors) can be replaced with if errors.

  2. Log in to comment