From 007aa56a1922eea9f364bf3b73e72077046e2c69 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Sun, 28 Jun 2015 15:58:48 -0400 Subject: 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] --- bs4/tests/test_htmlparser.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'bs4/tests/test_htmlparser.py') diff --git a/bs4/tests/test_htmlparser.py b/bs4/tests/test_htmlparser.py index bcb5ed2..b45e35f 100644 --- a/bs4/tests/test_htmlparser.py +++ b/bs4/tests/test_htmlparser.py @@ -1,6 +1,8 @@ """Tests to ensure that the html.parser tree builder generates good trees.""" +from pdb import set_trace +import pickle from bs4.testing import SoupTest, HTMLTreeBuilderSmokeTest from bs4.builder import HTMLParserTreeBuilder @@ -17,3 +19,14 @@ class HTMLParserTreeBuilderSmokeTest(SoupTest, HTMLTreeBuilderSmokeTest): def test_namespaced_public_doctype(self): # html.parser can't handle namespaced doctypes, so skip this one. pass + + def test_builder_is_pickled(self): + """Unlike most tree builders, HTMLParserTreeBuilder and will + be restored after pickling. + """ + tree = self.soup("foo") + dumped = pickle.dumps(tree, 2) + loaded = pickle.loads(dumped) + self.assertTrue(isinstance(loaded.builder, type(tree.builder))) + + -- cgit v1.2.3