Rinohtype: UnboundLocalError: local variable 'width' referenced before assignment

Created on 28 Feb 2016  路  6Comments  路  Source: brechtm/rinohtype

demo.txt:

+------------------------+------------+----------+----------+
| Header row, column 1   | Header 2   | Header 3 | Header 4 |
| (header rows optional) |            |          |          |
+========================+============+==========+==========+
| body row 1, column 1   | column 2   | column 3 | column 4 |
+------------------------+------------+----------+----------+
$ rinoh demo.txt
RinohType 0.1.3-154-g264f (Feb 28 2016, 09:57:09)  Copyright (c) Brecht Machiels
This program comes with ABSOLUTELY NO WARRANTY. Its use is subject
to the terms of the GNU Affero General Public License version 3.
No style 'table of contents title' found in stylesheet
No style 'table of contents title' found in stylesheet
No style 'table of contents title' found in stylesheet
No style 'table head cell right border' found in stylesheet
No style 'table cell right border' found in stylesheet
No style 'table head cell right border' found in stylesheet
No style 'table cell right border' found in stylesheet
No style 'table head left border' found in stylesheet
No style 'table left border' found in stylesheet
No style 'table head cell left border' found in stylesheet
No style 'table cell left border' found in stylesheet
No style 'table head left border' found in stylesheet
No style 'table left border' found in stylesheet
No style 'table head cell left border' found in stylesheet
No style 'table cell left border' found in stylesheet
Traceback (most recent call last):
  File "env3/lib/python3.5/site-packages/rinoh/util.py", line 101, in function_wrapper
    return cache[args_kwargs]
KeyError: ('vertical_align', <rinohlib.templates.article.Article object at 0x7f0cad6bc4e0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "env3/lib/python3.5/site-packages/rinoh/util.py", line 101, in function_wrapper
    return cache[args_kwargs]
KeyError: (<rinohlib.templates.article.Article object at 0x7f0cad6bc4e0>,)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "env3/lib/python3.5/site-packages/rinoh/flowable.py", line 412, in flow
    state)
  File "env3/lib/python3.5/site-packages/rinoh/flowable.py", line 125, in flow
    state=state, **kwargs)
  File "env3/lib/python3.5/site-packages/rinoh/table.py", line 101, in render
    if render_rows(self.head) != len(self.head):
  File "env3/lib/python3.5/site-packages/rinoh/table.py", line 95, in render_rows
    rendered_rows)
  File "env3/lib/python3.5/site-packages/rinoh/table.py", line 242, in _place_rows_and_render_borders
    document)
  File "env3/lib/python3.5/site-packages/rinoh/util.py", line 103, in function_wrapper
    cache_value = function(obj, *args, **kwargs)
  File "env3/lib/python3.5/site-packages/rinoh/style.py", line 338, in get_style
    return self.get_style_recursive(attribute, flowable_target)
  File "env3/lib/python3.5/site-packages/rinoh/style.py", line 358, in get_style_recursive
    style = self._style(flowable_target)
  File "env3/lib/python3.5/site-packages/rinoh/util.py", line 103, in function_wrapper
    cache_value = function(obj, *args, **kwargs)
  File "env3/lib/python3.5/site-packages/rinoh/style.py", line 374, in _style
    return container.document.stylesheet.find_style(self, container)
AttributeError: 'Article' object has no attribute 'document'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "env3/bin/rinoh", line 5, in <module>
    main()
  File "env3/lib/python3.5/site-packages/rinoh/tool.py", line 44, in main
    document.render(input_root)
  File "env3/lib/python3.5/site-packages/rinoh/document.py", line 360, in render
    section_num_pages = self.render_pages(_sections)
  File "env3/lib/python3.5/site-packages/rinoh/document.py", line 386, in render_pages
    section_page_count = section.render(sum(section_page_counts))
  File "env3/lib/python3.5/site-packages/rinoh/document.py", line 235, in render
    part_page_count = part.render(doc_page_count + section_page_count)
  File "env3/lib/python3.5/site-packages/rinoh/document.py", line 171, in render
    chains_requiring_new_page = set(chain for chain in page.render())
  File "env3/lib/python3.5/site-packages/rinoh/document.py", line 171, in <genexpr>
    chains_requiring_new_page = set(chain for chain in page.render())
  File "env3/lib/python3.5/site-packages/rinoh/document.py", line 117, in render
    for chain in super().render(rerender=index > 0):
  File "env3/lib/python3.5/site-packages/rinoh/layout.py", line 178, in render
    for chain in child.render(rerender):
  File "env3/lib/python3.5/site-packages/rinoh/layout.py", line 178, in render
    for chain in child.render(rerender):
  File "env3/lib/python3.5/site-packages/rinoh/layout.py", line 278, in render
    last_descender=last_descender):
  File "env3/lib/python3.5/site-packages/rinoh/layout.py", line 557, in render
    self._state.flowable_state)
  File "env3/lib/python3.5/site-packages/rinoh/flowable.py", line 417, in flow
    self._align(align_container, width)
UnboundLocalError: local variable 'width' referenced before assignment
bug

All 6 comments

This bug is no longer present in the current master branch.

@brechtm I beg to differ.

With rinohtype==0.3.1.dev0 (installed from f1cca5b261de8b03b4301912702235d766e5821d)

# Sphinx version: 1.6.3
# Python version: 3.6.1 (CPython)
# Docutils version: 0.13.1 release
# Jinja2 version: 2.9.6
# Last messages:
#   quality_requirements
#   
#   risks_and_technical_debt
#   
#   
#   
#   resolving references...
#   
#   rendering...
#   
# Loaded extensions:
#   alabaster (0.7.10) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/alabaster/__init__.py
#   sphinx.ext.todo (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/todo.py
#   sphinx.ext.autodoc (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/autodoc.py
#   sphinx.ext.autosummary (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/autosummary/__init__.py
#   sphinx.ext.intersphinx (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/intersphinx.py
#   sphinx.ext.mathjax (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/mathjax.py
#   sphinx.ext.viewcode (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/viewcode.py
#   sphinx.ext.graphviz (1.6.3) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/ext/graphviz.py
#   rinoh.frontend.sphinx (unknown version) from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/frontend/sphinx/__init__.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/image.py", line 192, in render
    container.advance(h, ignore_overflow)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/layout.py", line 265, in advance
    raise ContainerOverflow(self.page.number)
rinoh.layout.ContainerOverflow: 17

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 154, in flow
    raise eoc
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 149, in flow
    state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 186, in flow_inner
    self.render(pad_cntnr, descender, state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/image.py", line 194, in render
    raise EndOfContainer(state)
rinoh.layout.EndOfContainer: <rinoh.image.ImageState object at 0x10b529048>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/cmdline.py", line 306, in main
    app.build(opts.force_all, filenames)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/application.py", line 333, in build
    self.builder.build_all()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 288, in build_all
    self.build(None, summary='all source files', method='all')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 397, in build
    self.write(docnames, list(updated_docnames), method)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/frontend/sphinx/__init__.py", line 220, in write
    targetname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/frontend/sphinx/__init__.py", line 242, in write_doc
    rinoh_document.render(outfilename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/document.py", line 428, in render
    self.part_page_counts = self._render_pages()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/document.py", line 492, in _render_pages
    part_page_count += part.render(part_page_count.count + 1)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/document.py", line 228, in render
    page.render()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/document.py", line 159, in render
    super().render(CONTENT, rerender=index > 0)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/layout.py", line 185, in render
    child.render(type, rerender)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/layout.py", line 185, in render
    child.render(type, rerender)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/layout.py", line 273, in render
    self._render(type, rerender)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/layout.py", line 327, in _render
    self.chain.render(self, rerender=rerender)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/layout.py", line 616, in render
    self._state.flowable_state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 149, in flow
    state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 186, in flow_inner
    self.render(pad_cntnr, descender, state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 392, in render
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 416, in _flow_with_next
    state=state.first_flowable_state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 149, in flow
    state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 186, in flow_inner
    self.render(pad_cntnr, descender, state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 392, in render
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 416, in _flow_with_next
    state=state.first_flowable_state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 149, in flow
    state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 186, in flow_inner
    self.render(pad_cntnr, descender, state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 392, in render
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 416, in _flow_with_next
    state=state.first_flowable_state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 791, in flow
    return super().flow(container, last_descender, state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 149, in flow
    state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 186, in flow_inner
    self.render(pad_cntnr, descender, state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 392, in render
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 416, in _flow_with_next
    state=state.first_flowable_state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 755, in flow
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 149, in flow
    state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 186, in flow_inner
    self.render(pad_cntnr, descender, state=state, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 318, in render
    flowable.flow(maybe_container, last_descender)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 715, in flow
    super().flow(align_container, last_descender, state)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rinoh/flowable.py", line 159, in flow
    0, 0, width, height)
UnboundLocalError: local variable 'width' referenced before assignment

Indeed, width is not set in the except block, but is referenced in the finally block.

Note: I take this bug to be specifically about the UnboundLocalError -- the question of why ContainerOverflow is being raised on a well-formed sources is another (separate) interesting question.

As a quick fix, I added width = margin_container.width to the finally block -- this seems to squelch the error and cause the image to be rendered. If the image is oversize for the page it simply overflows the right-hand side; I'm not sure whether or not that is the expected behavior.

Note that when the width is not set (in the .rst source) on this oversize image, it causes runaway creation of excess pages. So I suspect that this fix is not appropriate, but it will do for my purposes for now.

I'm not familiar enough with the inner workings of rinohtype to say much more, but hopefully this is helpful.

This is in fact a different but similar bug. But I'm still grateful for the detailed report. Thanks @erickpeirson!

Development is taking place on the stable branch, (I need to clean up) so you might want to check with the latest version. If it still crashes (looks like it will), can you share the Sphinx project with me so I can see exactly what is going on?

Note: I take this bug to be specifically about the UnboundLocalError -- the question of why ContainerOverflow is being raised on a well-formed sources is another (separate) interesting question.

It is normal for ContainerOverflow to be raised. This is just how things are handled every time an item (Flowable) cannot fit on the current container/page and we need to move to the next.

As a quick fix, I added width = margin_container.width to the finally block -- this seems to squelch the error and cause the image to be rendered. If the image is oversize for the page it simply overflows the right-hand side; I'm not sure whether or not that is the expected behavior.

In the stable branch, oversized images are downsized automatically to fit in the available width.

Note that when the width is not set (in the .rst source) on this oversize image, it causes runaway creation of excess pages. So I suspect that this fix is not appropriate, but it will do for my purposes for now.

This will probably still happen when the image is too tall to fit on the page. Or it could be that this is being handled now. I can't remember...

@brechtm Fantastic, thanks for the quick response. I'll switch over to the stable branch. Great project; really enjoying it so far!

It's not clear whether this bug still exists since the _Flowable_ code has changed significantly. Please create a new issue when you run into an _UnboundLocalError_.

Was this page helpful?
0 / 5 - 0 ratings