From 6ef9b0c70abf4237ed07cb8314a7ef5b67d35869 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 30 Jul 2018 20:19:51 -0400 Subject: Fix an exception when a custom formatter was asked to format a void element. [bug=1784408] --- LICENSE | 2 +- NEWS.txt | 5 +++++ bs4/__init__.py | 2 +- bs4/element.py | 4 +++- bs4/tests/test_tree.py | 4 ++-- prepare-release.sh | 34 ++++++++++++++++++---------------- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/LICENSE b/LICENSE index 19fac0e..c500452 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ Beautiful Soup is made available under the MIT license: - Copyright (c) 2004-2016 Leonard Richardson + Copyright (c) 2004-2018 Leonard Richardson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/NEWS.txt b/NEWS.txt index ff1fd52..a18b623 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,3 +1,8 @@ += 4.6.2 (unreleased) + +* Fix an exception when a custom formatter was asked to format a void + element. [bug=1784408] + = 4.6.1 (20180728) * Stop data loss when encountering an empty numeric entity, and diff --git a/bs4/__init__.py b/bs4/__init__.py index ac3c172..8c662f8 100644 --- a/bs4/__init__.py +++ b/bs4/__init__.py @@ -21,7 +21,7 @@ http://www.crummy.com/software/BeautifulSoup/bs4/doc/ # found in the LICENSE file. __author__ = "Leonard Richardson (leonardr@segfault.org)" -__version__ = "4.6.1" +__version__ = "4.6.2" __copyright__ = "Copyright (c) 2004-2018 Leonard Richardson" __license__ = "MIT" diff --git a/bs4/element.py b/bs4/element.py index 8383c3f..886eb91 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -1223,7 +1223,9 @@ class Tag(PageElement): prefix = self.prefix + ":" if self.is_empty_element: - close = formatter.void_element_close_prefix or '' + close = '' + if isinstance(formatter, Formatter): + close = formatter.void_element_close_prefix or close else: closeTag = '' % (prefix, self.name) diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 68887b4..883cd8a 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1474,14 +1474,14 @@ class TestSubstitutions(SoupTest): self.document_for(u"<>")) def test_formatter_custom(self): - markup = u"<foo>bar" + markup = u"<foo>bar
" soup = self.soup(markup) decoded = soup.decode(formatter = lambda x: x.upper()) # Instead of normal entity conversion code, the custom # callable is called on every string. self.assertEqual( decoded, - self.document_for(u"BAR")) + self.document_for(u"BAR
")) def test_formatter_is_run_on_attribute_values(self): markup = u'e' diff --git a/prepare-release.sh b/prepare-release.sh index c278b67..ff3f6c0 100644 --- a/prepare-release.sh +++ b/prepare-release.sh @@ -17,7 +17,7 @@ rm -rf build dist python setup.py sdist bdist_wheel # Upload the 2.x source distro and wheel to pypi test -python setup.py register -r test +# python setup.py register -r test python setup.py sdist bdist_wheel upload -r test # Try 2.x install from pypi test @@ -35,7 +35,7 @@ rm -rf ../py2-install-test-virtualenv rm -rf ../py3-source-install virtualenv -p /usr/bin/python3 ../py3-source-install source ../py3-source-install/bin/activate -pip install -i https://testpypi.python.org/pypi beautifulsoup4 +pip3 install -i https://testpypi.python.org/pypi beautifulsoup4 echo "EXPECT HTML ON LINE BELOW" (cd .. && python -c "from bs4 import _s; print(_s('foo', 'html.parser'))") # That should print 'foo' @@ -90,8 +90,8 @@ echo rm -rf ../py3-install-test-virtualenv virtualenv -p /usr/bin/python3 ../py3-install-test-virtualenv source ../py3-install-test-virtualenv/bin/activate -pip install --upgrade setuptools -pip install dist/beautifulsoup4-4.*-py3-none-any.whl -e .[html5lib] +pip3 install --upgrade setuptools +pip3 install dist/beautifulsoup4-4.*-py3-none-any.whl -e .[html5lib] echo "EXPECT HTML ON LINE BELOW" (cd .. && python -c "from bs4 import _s; print(_s('foo', 'html5lib'))") # That should print 'foo' @@ -102,28 +102,30 @@ rm -rf ../py3-install-test-virtualenv Do the release for real. +twine upload dist/* + # Register the project and upload the source distribution and Python 2 wheel. -python setup.py register -python setup.py sdist bdist_wheel upload +# python setup.py register +# python setup.py sdist bdist_wheel upload # Create a Python 3 environment and install Beautiful Soup # from the source distribution that was just uploaded -rm -rf ../py3-source-install -virtualenv -p /usr/bin/python3 ../py3-source-install -source ../py3-source-install/bin/activate -pip install -i https://pypi.python.org/pypi beautifulsoup4 -echo "EXPECT HTML ON LINE BELOW" -(cd .. && python -c "from bs4 import _s; print(_s('foo', 'html.parser'))") +#rm -rf ../py3-source-install +#virtualenv -p /usr/bin/python3 ../py3-source-install +#source ../py3-source-install/bin/activate +#pip install -i https://pypi.python.org/pypi beautifulsoup4 +#echo "EXPECT HTML ON LINE BELOW" +#(cd .. && python -c "from bs4 import _s; print(_s('foo', 'html.parser'))") # That should print 'foo' # Create and upload a Python 3 wheel from within a virtual environment # that has the Python 3 version of the code. -pip install wheel -python3 setup.py bdist_wheel upload +#pip install wheel +#python3 setup.py bdist_wheel upload # Remove the Python 3 virtual environment. -deactivate -rm -rf ../py3-source-install +#deactivate +#rm -rf ../py3-source-install ################ -- cgit v1.2.1