Cannot create new `Tag()` element with non-empty attrs and no builder

Bug #1307471 reported by Martijn Pieters
18
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Released
Undecided
Unassigned

Bug Description

The `Tag.__init__` method assumes that `builder` is always set when you pass in attributes:

>>> from bs4 import Tag
>>> Tag(name='div', attrs={'id': 'foo'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/bs4/element.py", line 762, in __init__
    elif attrs and builder.cdata_list_attributes:
AttributeError: 'NoneType' object has no attribute 'cdata_list_attributes'

This is caused by:

        if attrs is None:
            attrs = {}
        elif attrs and builder.cdata_list_attributes:
            attrs = builder._replace_cdata_list_attribute_values(
                self.name, attrs)
        else:
            attrs = dict(attrs)

while later in the method `builder` is not assumed to be always set:

        if builder is not None:
            builder.set_up_substitutions(self)
            self.can_be_empty_element = builder.can_be_empty_element(name)
        else:
            self.can_be_empty_element = False

I've attached a simple patch.

Revision history for this message
Martijn Pieters (mjpieters) wrote :
Neftas (srvandenakker)
Changed in beautifulsoup:
status: New → Confirmed
status: Confirmed → New
Revision history for this message
Leonard Richardson (leonardr) wrote :

OK. I can't guarantee there won't be subtle bugs if you introduce a Tag created with no builder to a normal BeautifulSoup object, but I agree it shouldn't crash immediately.

Changed in beautifulsoup:
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.