PKäˆ6G"á%ØØ pretext.pyfrom __future__ import print_function try: import reprlib except ImportError: import repr as reprlib import sys __all__ = [ 'PrefixRepr', ] class PrefixRepr(reprlib.Repr): """A Repr variant that always prefixes bytes/str/unicode literals """ def __init__(self): self._old_repr = repr self.maxlevel = sys.maxsize self.maxdict = sys.maxsize self.maxlist = sys.maxsize self.maxset = sys.maxsize self.maxfrozenset = sys.maxsize self.maxdeque = sys.maxsize self.maxarray = sys.maxsize self.maxlong = sys.maxsize self.maxstring = sys.maxsize self.maxother = sys.maxsize def repr_str(self, obj, level): """Return the repr of a string, prefixed with b or u """ if str is bytes: # Python 2.x return b'b%s' % (self._old_repr(obj),) else: # Python 3.x return u'u%s' % (self._old_repr(obj),) PK–6GÑçâe e 'pretext-0.0.1.dist-info/DESCRIPTION.rstpretext ======= .. image:: https://travis-ci.org/moreati/b-prefix-all-the-doctests.svg :target: https://travis-ci.org/moreati/b-prefix-all-the-doctests This package is an experiment in writing doctests that involve strings, and that are cross-compatible with Python 2.6, 2.7, and 3.3+. The problem ----------- Suppose you have the following doctest .. code:: python >>> textfunc() u'I return a textual (unicode) string' >>> bytesfunc() b'I return a byte (binary) string' On Python 3.x the first test case will fail, because doctest will compare ``repr(textfunc())`` which will not include a ``u''`` prefix. On Python 2.x the second test will fail, because ``repr(bytesfunc())`` will not include the ``b''`` prefix. If the tests cases are editted to remove the prefixes, i.e. .. code:: python >>> textfunc() 'I return a textual (unicode) string' >>> bytesfunc() 'I return a byte (binary) string' then the failures will be reversed. ``textfunc()`` will now fail on Python 2.x, ``bytesfunc()`` will fail on Python 3.x. The hack -------- Replace ``repr()`` and ``sys.displayhook`` with versions that always prefix string literals, regardless of the Python version. Now the doctests can - directly show the string values returned by functions/methods, without resorting to ``print()``, or ``.encode()`` etc - successfully test the examples on all Python versions Proof of concept: .. code:: python r""" >>> import sys >>> import pretext >>> myrepr = bar.PrefixRepr() >>> repr = myrepr.repr >>> def _displayhook(value): ... if value is not None: ... sys.stdout.write(myrepr.repr(value)) >>> sys.displayhook = _displayhook >>> u'' u'' >>> b'' b'' >>> bytes() b'' >>> b'\0' b'\x00' >>> b"'" b"'" """ Alternatives ------------ If you're ready to run screaming at the above, there are alternatives e.g. - Wrap byte-string returns in ``bytearray()``. ``repr(bytearray(b'abc')) == "bytearray(b'abc'))"`` on all versions of python that have ``bytearray()`` (2.6 onward) e.g. .. code:: python >>> bytearray(bytesfunc()) bytearray(b'I return a byte (binary) string') - Support Python 3.x exclusively - Use ``print(bytesfunc().decode('ascii'))`` and choose your input values carefully - Use ``#doctest: +SKIP`` - Use ``#doctest: +ELLIPSIS`` PK–6Gsúàÿÿ%pretext-0.0.1.dist-info/metadata.json{"classifiers": ["Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Topic :: Documentation", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Testing"], "extensions": {"python.details": {"contacts": [{"email": "alex@moreati.org.uk", "name": "Alex Willmer", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "https://github.com/moreati/b-prefix-all-the-doctests"}}}, "generator": "bdist_wheel (0.24.0)", "keywords": ["doctest", "bytes", "unicode", "bytestring", "prefix", "literal", "string", "str"], "license": "Apache Software License 2.0", "metadata_version": "2.0", "name": "pretext", "summary": "Use doctest with bytes, str & unicode on Python 2.x and 3.x", "version": "0.0.1"}PK–6GúGp// pretext-0.0.1.dist-info/pbr.json{"is_release": false, "git_version": "0d02d03"}PK–6G(>> textfunc() u'I return a textual (unicode) string' >>> bytesfunc() b'I return a byte (binary) string' On Python 3.x the first test case will fail, because doctest will compare ``repr(textfunc())`` which will not include a ``u''`` prefix. On Python 2.x the second test will fail, because ``repr(bytesfunc())`` will not include the ``b''`` prefix. If the tests cases are editted to remove the prefixes, i.e. .. code:: python >>> textfunc() 'I return a textual (unicode) string' >>> bytesfunc() 'I return a byte (binary) string' then the failures will be reversed. ``textfunc()`` will now fail on Python 2.x, ``bytesfunc()`` will fail on Python 3.x. The hack -------- Replace ``repr()`` and ``sys.displayhook`` with versions that always prefix string literals, regardless of the Python version. Now the doctests can - directly show the string values returned by functions/methods, without resorting to ``print()``, or ``.encode()`` etc - successfully test the examples on all Python versions Proof of concept: .. code:: python r""" >>> import sys >>> import pretext >>> myrepr = bar.PrefixRepr() >>> repr = myrepr.repr >>> def _displayhook(value): ... if value is not None: ... sys.stdout.write(myrepr.repr(value)) >>> sys.displayhook = _displayhook >>> u'' u'' >>> b'' b'' >>> bytes() b'' >>> b'\0' b'\x00' >>> b"'" b"'" """ Alternatives ------------ If you're ready to run screaming at the above, there are alternatives e.g. - Wrap byte-string returns in ``bytearray()``. ``repr(bytearray(b'abc')) == "bytearray(b'abc'))"`` on all versions of python that have ``bytearray()`` (2.6 onward) e.g. .. code:: python >>> bytearray(bytesfunc()) bytearray(b'I return a byte (binary) string') - Support Python 3.x exclusively - Use ``print(bytesfunc().decode('ascii'))`` and choose your input values carefully - Use ``#doctest: +SKIP`` - Use ``#doctest: +ELLIPSIS`` PK–6G]yº‰‰pretext-0.0.1.dist-info/RECORDpretext.py,sha256=_MhgKcJIu3R9Gx5x47c28wDvE4ScmXD57QKM6pGHU_o,984 pretext-0.0.1.dist-info/WHEEL,sha256=AvR0WeTpDaxT645bl5FQxUK6NPsTls2ttpcGJg3j1Xg,110 pretext-0.0.1.dist-info/RECORD,, pretext-0.0.1.dist-info/METADATA,sha256=fcZzFCd3yg1MhmAI5G5OSi61Dnvc7_QL6_hluJuIve8,3597 pretext-0.0.1.dist-info/top_level.txt,sha256=AdEydxGeSzbdOGBTpuaF1mBJhaZAUr1Cq-dMZGW9p80,8 pretext-0.0.1.dist-info/DESCRIPTION.rst,sha256=3kzow7vSqRd0GMeuhypTLWsyzpq4aBuHnMavA5oQzts,2405 pretext-0.0.1.dist-info/metadata.json,sha256=Jn02CkeKVEXLqtZccmpS7-f4IgB7Zx6eSUHGbHst-oY,1279 pretext-0.0.1.dist-info/pbr.json,sha256=S8mU2I6lkcBIgYiNFt6M1De84N6VPfkRaEJPssowoSs,47 PKäˆ6G"á%ØØ pretext.pyPK–6GÑçâe e 'pretext-0.0.1.dist-info/DESCRIPTION.rstPK–6Gsúàÿÿ%ª pretext-0.0.1.dist-info/metadata.jsonPK–6GúGp// ìpretext-0.0.1.dist-info/pbr.jsonPK–6G(