Comment 3 for bug 2065120

Revision history for this message
Chris Papademetrious (chrispitude) wrote :

Speaking of Python's native shallow/deep copy methods, is a copy.copy() of a BeautifulSoup tag considered "shallow" or "deep"?

It seems to always be deep, but I just did a quick test and I think I found a bug.

In this code, I save a copy.copy() of soup1 as soup2, the modify soup1 in various ways:

====
import bs4
import copy

html_doc = '<p class="foo" style="orig">text</p>'
soup1 = bs4.BeautifulSoup(html_doc, "lxml")
soup2 = copy.copy(soup1)

p = soup1.find("p")
p.attrs["data-added"] = "TRUE" # soup1
p.attrs["style"] = "NEW" # soup1
p.append("-HELLO") # soup1
p.attrs["class"].append("BAR") # soup1 and soup2 <--

print(f"soup1: {soup1}")
print(f"soup2: {soup2}")
====

Notice that the addition of "BAR" to soup1.p's class attribute also affects soup2. Perhaps the lists will need to be deep-copied with [:]. I tried copy.deepcopy() and got the same behavior. Do you want me to file a separate bug for this?