Cannot create new `Tag()` element with non-empty attrs and no builder
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/
elif attrs and builder.
AttributeError: 'NoneType' object has no attribute 'cdata_
This is caused by:
if attrs is None:
attrs = {}
elif attrs and builder.
attrs = builder.
else:
attrs = dict(attrs)
while later in the method `builder` is not assumed to be always set:
if builder is not None:
else:
I've attached a simple patch.
Changed in beautifulsoup: | |
status: | New → Confirmed |
status: | Confirmed → New |
Changed in beautifulsoup: | |
status: | Fix Committed → Fix Released |
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.