Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Python 2 and Python 3 with single source #1

Closed
wants to merge 19 commits into from

Conversation

msabramo
Copy link
Owner

@msabramo msabramo commented May 7, 2014

Travis CI: Build Status
Drone.io: Build Status

Right now you have to install unittest2 for Python 2 and unittest2py3k for Python 3.

This is a little clunky when using pip requirements files and tox because you have to have two separate requirements files and two separate tox targets. E.g.:

# test-requirements-py2.txt
unittest2
mock
lxml
# test-requirements-py3.txt
unittest2py3k
mock
lxml
# tox.ini
[testenv]
deps = -r{toxinidir}/test-requirements-py2.txt
commands = python -m discover

[testenv:py33]
deps = -r{toxinidir}/test-requirements-py3.txt

[testenv:py34]
deps = -r{toxinidir}/test-requirements-py3.txt

It would be nice if there were one package that supported both.

Google Code issue: https://code.google.com/p/unittest-ext/issues/detail?id=81
Bitbucket (hg) branch: https://bitbucket.org/msabramo/unittest2/branch/py2_and_py3_single_source
GitHub branch: https://github.com/msabramo/unittest2/tree/bm/py2_and_py3_single_source

$ /Library/Frameworks/Python.framework/Versions/2.5/bin/tox -e py24,py25
...
[TOX] py24: commands succeeded
[TOX] py25: commands succeeded
[TOX] congratulations :)
$ /Library/Frameworks/Python.framework/Versions/3.1/bin/pip freeze
argparse==1.2.1
backports.ssl-match-hostname==3.4.0.1
py==1.4.20
tox==1.3
virtualenv==1.6.4
wsgiref==0.1.2

$ /Library/Frameworks/Python.framework/Versions/3.1/bin/tox \
       -e py24,py25,py26,py27,py30,py31,py32,pypy
...
[TOX] py24: commands succeeded
[TOX] py25: commands succeeded
[TOX] py26: commands succeeded
[TOX] py27: commands succeeded
[TOX] py30: commands succeeded
[TOX] py31: commands succeeded
[TOX] py32: commands succeeded
[TOX] pypy: commands succeeded
[TOX] congratulations :)
$ tox -e py26,py27,py33,py34,pypy
...
  py26: commands succeeded
  py27: commands succeeded
  py33: commands succeeded
  py34: commands succeeded
  pypy: commands succeeded
  congratulations :)

Successful Drone.io build: https://drone.io/bitbucket.org/msabramo/unittest2/5

Passing Travis CI build: https://travis-ci.org/msabramo/unittest2/builds/24631644

😄

Cc: @voidspace

msabramo added 18 commits May 6, 2014 10:41
This idiom works in Python 3 and in Python 2, including < 2.6 unlike the `as`
syntax that only works in Python >= 2.6

--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
…quotes

around module name in exception message for Python >= 3.3

--HG--
branch : py2_and_py3_single_source
that causes a test failure in Python 3

--HG--
branch : py2_and_py3_single_source
AttributeError: 'str' object has no attribute 'decode'

--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
by copying code from unittest2py3k

This fixes the final test failure
(Test_TestLoader.test_loadTestsFromNames__callable__call_staticmethod)

--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
--HG--
branch : py2_and_py3_single_source
@domenkozar
Copy link

🍻

@jezdez
Copy link

jezdez commented May 7, 2014

❤️

--HG--
branch : py2_and_py3_single_source
@msabramo
Copy link
Owner Author

Folks who are interested in this PR might also be interested in this package which I uploaded to PyPI yesterday:

https://pypi.python.org/pypi/unittest2six

It's a simple meta package to easily get unittest2 functionality in both Python 2 and Python 3. On Python 2, it install_requires unittest2. On Python 3, it install_requires unittest2py3k.

@msabramo
Copy link
Owner Author

msabramo commented Jul 7, 2014

@voidspace: Any thoughts on this one?

@msabramo msabramo closed this Jan 13, 2015
@domenkozar
Copy link

is this fixed or forgotten?

@voidspace
Copy link

This is fixed in unittest2 source.

@domenkozar
Copy link

🍻

mika pushed a commit to sipwise/jenkins-job-builder that referenced this pull request Apr 29, 2024
Patch Set 10:

Darragh wrote:

> just spotted that the introduction of unittest2 causes problems with python3, probably because the python3 compatible version is called unittest2py3k on pypi.

Good catch! I've run into this a number of times with various projects, so a few months ago I created a "meta-package" called "unittest2six" on PyPI:

https://pypi.python.org/pypi/unittest2six

It's one package that's installable on both Python 2 and Python 3. I also have a PR open to make unittest2 work natively on Python 3, thus eliminating the need for unittest2py3k -- if you like the idea, you might want to +1 the PR:

msabramo/unittest2#1

Patch-set: 10
Label: Verified=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants