I’m appending an object to list images (initialized as empty) at every iteration of a for loop. I’m not explicitly accessing the list by index, I’m just appending an object to it, which is why to me inexplicable that I get an IndexError: list index out of range back.
Here’s the stack trace:
Traceback (most recent call last):
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/newrelic-1.4.0.137-py2.7-linux-x86_64.egg/newrelic/api/object_wrapper.py", line 166, in __call__
self._nr_instance, args, kwargs)
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/newrelic-1.4.0.137-py2.7-linux-x86_64.egg/newrelic/api/function_trace.py", line 81, in literal_wrapper
return wrapped(*args, **kwargs)
File "/mnt/www-data/run/backend/lib/python2.7/site-packages/newrelic-1.4.0.137-py2.7-linux-x86_64.egg/newrelic/api/name_transaction.py", line 58, in __call__
return self._nr_next_object(*args, **kwargs)
File "/mnt/www-data/run/backend/webservice/decorators/controller_wrapper.py", line 75, in decorated_function
return _return(f(*args, body=body, **kwargs))
File "/mnt/www-data/run/backend/webservice/decorators/controller_wrapper.py", line 90, in decorated_function
r = f(*args, **kwargs)
File "/mnt/www-data/run/backend/webservice/decorators/login_required.py", line 16, in decorated_function
return f(*args, **kwargs)
File "/mnt/www-data/run/backend/webservice/controllers/me/photos.py", line 28, in create_photo
phs = photos.store_photos(g.user, data)
File "/mnt/www-data/run/backend/evertale/tasks/photos.py", line 106, in store_photos
images.append(entity)
IndexError: list index out of range
And an excerpt of …/tasks/photos.py, the failing line is the last of the for loop:
def store_photos(user, photos, update=False):
if type(photos) is dict:
photos = [photos]
images = []
for pic in photos:
if 'gps' in pic and pic['gps']:
loc = Location(ts=long(pic['ts']), user=user, _latlon=pic['gps'])
loc.save()
image = None
if 'data' in pic:
image = _read_image_from_base64(pic['data'])
image = _normalize_rotation(image)
entity = _persist_entity(user, image, update, **pic)
if image:
file_name = _file_name(entity.id)
_persist_file(image, file_name)
thumb_photo(entity.id)
images.append(entity)
if len(images) > 1:
return images
elif len(images) == 1:
return images[0]
else:
return []
Anyone having any idea why this happens? I can’t debug this because the code is on the production server, and I’ve never experienced anything like it when testing on local machine.
I suspect the sourcecode on your server has changed since you started it. In fact, I am almost certain of it.
Python byte-code includes a line number, and when a traceback occurs, this line number is used to look up and show you the non-compiled source line.
Once the bytecode is loaded into memory however, it is not reloaded if you were to change the source file. If you add or remove lines from the source file after your server has started, lines in a traceback will be wrong.
In other words, the line shown in your traceback is almost certainly not the line where the error occurs. I certainly would not expect a simple
.append()to ever throw that exception. The specific exception (list index out of range) would only occur when trying to look up a specific entry in the list through__getattr__(alst[index]operation).