Passing a function as 'formatter' breaks on empty elements

Bug #1784408 reported by Matt Westcott
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Released
High
Unassigned

Bug Description

In 4.6.1, passing a function as the 'formatter' argument to 'decode' (https://www.crummy.com/software/BeautifulSoup/bs4/doc/#output-formatters) fails with "AttributeError: 'function' object has no attribute 'void_element_close_prefix'" when the document contains empty elements. This is a regression from 4.6.0.

Test case:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup('<br>', 'html5lib')
    soup.decode(formatter=lambda x: x)

Expected result (seen on 4.6.0):

    '<html><head></head><body><br/></body></html>'

Actual result:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/__init__.py", line 556, in decode
        indent_level, eventual_encoding, formatter)
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1242, in decode
        indent_contents, eventual_encoding, formatter)
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1311, in decode_contents
        formatter))
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1242, in decode
        indent_contents, eventual_encoding, formatter)
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1311, in decode_contents
        formatter))
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1242, in decode
        indent_contents, eventual_encoding, formatter)
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1311, in decode_contents
        formatter))
      File "/home/vagrant/.virtualenvs/bakerydemo/lib/python3.4/site-packages/bs4/element.py", line 1227, in decode
        close = formatter.void_element_close_prefix or ''
    AttributeError: 'function' object has no attribute 'void_element_close_prefix'

I am using html5lib (1.0.1) and Python 3.4.3.

Revision history for this message
Leonard Richardson (leonardr) wrote :

Thanks for the detailed bug report. This is fixed in revision 474.

Changed in beautifulsoup:
importance: Undecided → High
status: New → Fix Committed
Changed in beautifulsoup:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.