summaryrefslogtreecommitdiff
path: root/bs4/testing.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2015-06-28 15:58:48 -0400
committerLeonard Richardson <leonardr@segfault.org>2015-06-28 15:58:48 -0400
commit007aa56a1922eea9f364bf3b73e72077046e2c69 (patch)
tree709684c6357fc69013c5d39b026b1d303da9a40b /bs4/testing.py
parent9428b9d6ed0d279a72414a986290821ca4f0caaf (diff)
It's now possible to pickle a BeautifulSoup object no matter which
tree builder was used to create it. However, the only tree builder that survives the pickling process is the HTMLParserTreeBuilder ('html.parser'). If you unpickle a BeautifulSoup object created with some other tree builder, soup.builder will be None. [bug=1231545]
Diffstat (limited to 'bs4/testing.py')
-rw-r--r--bs4/testing.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/bs4/testing.py b/bs4/testing.py
index 7232513..9e5e295 100644
--- a/bs4/testing.py
+++ b/bs4/testing.py
@@ -1,5 +1,6 @@
"""Helper classes for tests."""
+import pickle
import copy
import functools
import unittest
@@ -64,6 +65,15 @@ class HTMLTreeBuilderSmokeTest(object):
markup in these tests, there's not much room for interpretation.
"""
+ def test_pickle_and_unpickle_identity(self):
+ # Pickling a tree, then unpickling it, yields a tree identical
+ # to the original.
+ tree = self.soup("<a><b>foo</a>")
+ dumped = pickle.dumps(tree, 2)
+ loaded = pickle.loads(dumped)
+ self.assertEqual(loaded.__class__, BeautifulSoup)
+ self.assertEqual(loaded.decode(), tree.decode())
+
def assertDoctypeHandled(self, doctype_fragment):
"""Assert that a given doctype string is handled correctly."""
doctype_str, soup = self._document_with_doctype(doctype_fragment)
@@ -532,6 +542,15 @@ Hello, world!
class XMLTreeBuilderSmokeTest(object):
+ def test_pickle_and_unpickle_identity(self):
+ # Pickling a tree, then unpickling it, yields a tree identical
+ # to the original.
+ tree = self.soup("<a><b>foo</a>")
+ dumped = pickle.dumps(tree, 2)
+ loaded = pickle.loads(dumped)
+ self.assertEqual(loaded.__class__, BeautifulSoup)
+ self.assertEqual(loaded.decode(), tree.decode())
+
def test_docstring_generated(self):
soup = self.soup("<root/>")
self.assertEqual(