Page MenuHomeSoftware Heritage

writer/__init__.py: Fix UnboundLocalError
ClosedPublic

Authored by anlambert on Jan 6 2020, 3:39 PM.

Details

Summary

I noticed docker tests were failing since a couple of days
(see https://jenkins.softwareheritage.org/view/all/job/swh-docker-dev/286/ for instance).

This is due to the following error being raised since a9fb7c733a4f:

;1mswh-storage_1                   | Traceback (most recent call last):
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
;1mswh-storage_1                   |     rv = self.dispatch_request()
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
;1mswh-storage_1                   |     return self.view_functions[rule.endpoint](**req.view_args)
;1mswh-storage_1                   |   File "</srv/softwareheritage/venv/lib/python3.7/site-packages/decorator.py:decorator-gen-100>", line 2, in origin_add_one
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/core/api/negotiation.py", line 144, in _negotiate
;1mswh-storage_1                   |     return f.negotiator(*args, **kwargs)
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/core/api/negotiation.py", line 79, in __call__
;1mswh-storage_1                   |     result = self.func(*args, **kwargs)
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/core/api/__init__.py", line 374, in _f
;1mswh-storage_1                   |     obj_meth = getattr(backend_factory(), meth_name)
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/storage/api/server.py", line 22, in get_storage
;1mswh-storage_1                   |     storage = get_swhstorage(**app.config['storage'])
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/storage/__init__.py", line 62, in get_storage
;1mswh-storage_1                   |     return Storage(**kwargs)
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/storage/storage.py", line 76, in __init__
;1mswh-storage_1                   |     self.journal_writer = get_journal_writer(**journal_writer)
;1mswh-storage_1                   |   File "/srv/softwareheritage/venv/lib/python3.7/site-packages/swh/journal/writer/__init__.py", line 11, in get_journal_writer
;1mswh-storage_1                   |     warnings.warn(
;1mswh-storage_1                   | UnboundLocalError: local variable 'warnings' referenced before assignment

This simple diff fixes the issue but I do not really understand why we get such an error though.

Diff Detail

Repository
rDJNL Journal infrastructure
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

anlambert created this revision.Jan 6 2020, 3:39 PM
olasd requested changes to this revision.Jan 6 2020, 3:46 PM
olasd added a subscriber: olasd.

This is plain Python AST stupid^Wbehavior:

  • when the code is parsed by the compiler, the compiler notices that the warnings variable is created by the import statement on line 17; warnings is therefore set as a local variable to the get_journal_writer function
  • when the code is run, line 10 accesses the warnings variable, which hasn't been bound yet within the function, even though it exists globally. This raises an UnboundLocalError.

Removing the extra import should fix the issue.

swh/journal/writer/__init__.py
17

This line should be removed instead.

This revision now requires changes to proceed.Jan 6 2020, 3:46 PM
anlambert updated this revision to Diff 8873.Jan 6 2020, 3:51 PM

Update: Properly fix the observed issue

Oh, that was pretty subtle indeed. I updated the diff accordingly.

olasd accepted this revision.Jan 6 2020, 4:12 PM

Thanks!

This revision is now accepted and ready to land.Jan 6 2020, 4:12 PM

nitpick: it's about scopes, not ASTs

I will tag a new version (0.0.24) to fix the docker tests issue.

This revision was automatically updated to reflect the committed changes.