Skip to content

Commit

Permalink
Add support for Python 3.
Browse files Browse the repository at this point in the history
This requires to drop `.browser.ExtendedTestBrowser.pretty_print`
as its requirements are deprecated (`formatter`) or even removed from
Python's StdLib (htmllib).
  • Loading branch information
Michael Howitz committed Feb 27, 2019
1 parent 0885d4c commit c37f15f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 54 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
@@ -1,15 +1,17 @@
# Configuration script for Travis-CI
language: python
dist: xenial
python:
- 2.7
- 3.6
- 3.7
- 3.8
matrix:
include:
- python: 2.7
name: "2.7"
- python: 2.7
name: "Flake8"
install: pip install -U flake8
script: flake8 --doctests src setup.py
script: flake8 src setup.py
after_success:
install:
- pip install -U pip setuptools zope.testrunner
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.txt
Expand Up @@ -17,10 +17,18 @@ Backwards incompatible changes
- Drop the ``zope.app.testing`` extra introduced in version 2.0.0 as
it dropped its special ``zope.testbrowser`` support.

- Drop ``.browser.ExtendedTestBrowser.pretty_print`` as its requirements are
deprecated or even removed from Python's StdLib.

- Adapt the code to newer ``lxml`` versions which no longer raise an exception
if the string to be parsed by ``lxml.etree`` is empty. We now raise a
``ValueError`` in this case.

Features
--------

- Add support for Python 3.6 up to 3.8.


2.0.1 (2015-11-09)
==================
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Expand Up @@ -48,8 +48,12 @@ def read(*rnames):
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: Zope Public License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Natural Language :: English',
'Operating System :: OS Independent',
'Topic :: Internet :: WWW/HTTP',
Expand All @@ -68,6 +72,7 @@ def read(*rnames):
install_requires=[
'lxml >= 2.2',
'setuptools',
'six',
'zope.deferredimport',
'zope.testbrowser >= 5.0',
],
Expand Down
30 changes: 5 additions & 25 deletions src/z3c/etestbrowser/README.txt
Expand Up @@ -26,7 +26,7 @@ Example:
>>> from z3c.etestbrowser.testing import ExtendedTestBrowser
>>> browser = ExtendedTestBrowser()
>>> browser.open("http://localhost/")
>>> print browser.contents
>>> print(browser.contents)
<!DOCTYPE ...>
...
</html>
Expand Down Expand Up @@ -60,8 +60,8 @@ contains a German umlaut:

>>> browser.xml_strict = False
>>> browser.open('http://localhost/lxml.html')
>>> browser.etree.xpath("//span")[0].text
u'K\xfcgelblitz.'
>>> browser.etree.xpath("//span")[0].text == u'K\xfcgelblitz.'
True

Invalid XML/HTML responses
++++++++++++++++++++++++++
Expand All @@ -81,26 +81,6 @@ general TestBrowser use:
ValueError: ...


Pretty printing
~~~~~~~~~~~~~~~

Sometimes a normal `print` of the browsers contents is hard to read for
debugging:

>>> browser.open('http://localhost/')
>>> print browser.contents
<!DOCTYPE html ...
...Name...Title...Created...Modified...

The extended test browser provides a method to provide a formatted version of
the HTML (using htmllib internally):

>>> browser.pretty_print()
@import url(http://localhost/@@/zope3_tablelayout.css); User: Fallback
unauthenticated principal [Login][1] (image)[2] Location:...[top][3] /
Navigation
Loading... ... Name Title Created Modified ...

HTML/XML normalization
~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -109,7 +89,7 @@ testing examples with HTML or XML a bit easier when unimportant details like
whitespace are changing:

>>> browser.open('http://localhost/funny.html')
>>> print browser.contents
>>> print(browser.contents)
<html>
<head>
<title>Foo</title>
Expand All @@ -124,7 +104,7 @@ whitespace are changing:

versus

>>> print browser.normalized_contents
>>> print(browser.normalized_contents)
<html>
<head>
<title>Foo</title>
Expand Down
26 changes: 5 additions & 21 deletions src/z3c/etestbrowser/browser.py
Expand Up @@ -11,17 +11,13 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Extensions for z3c.etestbrowser
$Id$
"""
"""Extensions for z3c.etestbrowser."""

import re
import htmllib
import formatter

import lxml.etree
import lxml.html
import six

import zope.testbrowser.browser

Expand Down Expand Up @@ -79,7 +75,8 @@ def etree(self):
match = RE_CHARSET.match(content_type)
if match is not None:
charset = match.groups()[0]
content = content.decode(charset)
if six.PY2: # pragma: no cover
content = content.decode(charset) # pragma: no cover
self._etree = lxml.etree.HTML(content)

if self._etree is None:
Expand All @@ -91,24 +88,11 @@ def etree(self):
def normalized_contents(self):
if self._normalized_contents is None:
indent(self.etree)
self._normalized_contents = lxml.etree.tostring(
self._normalized_contents = lxml.etree.tounicode(
self.etree, pretty_print=True)
return self._normalized_contents

def _changed(self):
super(ExtendedTestBrowser, self)._changed()
self._etree = None
self._normalized_contents = None

def pretty_print(self):
"""Print a pretty (formatted) version of the HTML content.
If the content is not text/html then it is just printed.
"""
if not self.headers['content-type'].lower().startswith('text/html'):
print self.contents
else:
parser = htmllib.HTMLParser(
formatter.AbstractFormatter(formatter.DumbWriter()))
parser.feed(self.contents)
parser.close()
5 changes: 4 additions & 1 deletion src/z3c/etestbrowser/tests.py
Expand Up @@ -47,7 +47,10 @@ def test_suite():
"over_the_wire.txt",
"wsgi.txt",
setUp=setUpWSGI,
optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS)
optionflags=(
doctest.NORMALIZE_WHITESPACE
| doctest.ELLIPSIS
| doctest.IGNORE_EXCEPTION_DETAIL))
wsgi_test.layer = wsgi_layer
suite.addTest(wsgi_test)
return suite
2 changes: 1 addition & 1 deletion src/z3c/etestbrowser/wsgi.txt
Expand Up @@ -10,7 +10,7 @@ Example:
>>> import z3c.etestbrowser.wsgi
>>> browser = z3c.etestbrowser.wsgi.Browser(wsgi_app=wsgi_app)
>>> browser.open("http://localhost/")
>>> print browser.contents
>>> print(browser.contents)
<!DOCTYPE ...>
...
</html>
Expand Down
7 changes: 5 additions & 2 deletions tox.ini
Expand Up @@ -2,6 +2,9 @@
envlist =
flake8,
py27,
py36,
py37,
py38,
coverage,

[testenv]
Expand All @@ -10,10 +13,10 @@ commands =
zope-testrunner --test-path=src

[testenv:coverage]
basepython = python2.7
basepython = python3.7
commands =
coverage run -m zope.testrunner --test-path=src {posargs:--auto-color -vv}
coverage report --fail-under=92
coverage report --show-missing --fail-under=93
deps = coverage

[testenv:flake8]
Expand Down

0 comments on commit c37f15f

Please sign in to comment.