When a Tag is copied, multi-valued attribute value lists are not deeply copied

Bug #2067412 reported by Chris Papademetrious
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Committed
Undecided
Unassigned

Bug Description

When a tag is copied with copy.copy() or the private _clone() method, multi-valued attribute value lists are not deeply copied. As a result, changes to a copied tag's value list also affect the original tag.

In this example, p1 is copied to p2, then a change to p1's "class" attribute affects p2:

====
import bs4
import copy

html_doc = '<p class="foo"/>'
soup = bs4.BeautifulSoup(html_doc, "lxml")
p1 = soup.find("p")
p2 = p1._clone()
p1.attrs["class"].append("BAR") # <-- also affects p2

print(f"p1: {p1}")
print(f"p2: {p2}")
====

Perhaps the value lists need to be copied using [:] syntax (or something similar).

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

A good catch. This is fixed in revision 4aaacc0.

Changed in beautifulsoup:
status: New → Fix Committed
Revision history for this message
Chris Papademetrious (chrispitude) wrote :

Awesome - thanks Leonard!

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.