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

MRG: fix for doctest errors on Python 3.5 #363

Merged
merged 2 commits into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ python:
- 3.2
- 3.3
- 3.4
- 3.5
matrix:
include:
- python: 2.7
Expand All @@ -44,11 +45,13 @@ matrix:
env:
- DOC_DOC_TEST=1
before_install:
- source tools/travis_tools.sh
- virtualenv --python=python venv
- source venv/bin/activate
- python --version # just to check
- pip install nose # always
- pip install --no-index -f http://travis-wheels.scikit-image.org $DEPENDS
- pip install -U pip # upgrade to latest pip to find 3.5 wheels
- retry pip install nose # always
- wheelhouse_pip_install $DEPENDS
# pydicom <= 0.9.8 doesn't install on python 3
- if [ "${TRAVIS_PYTHON_VERSION:0:1}" == "2" ]; then
if [ "$PYDICOM" == "1" ]; then
Expand Down
6 changes: 6 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ References like "pr/298" refer to github pull request numbers.
are raising a DataError if the track is truncated when ``strict=True``
(the default), rather than a TypeError when trying to create the points
array.
* tripwire.TripWire object now raises subclass of AttributeError when trying
to get an attribute, rather than a direct subclass of Exception. This
prevents Python 3.5 triggering the tripwire when doing inspection prior to
running doctests.
* Minor API change for tripwire.TripWire object; code that checked for
AttributeError will now also catch TripWireError.

* 2.0.1 (Saturday 27 June 2015)

Expand Down
29 changes: 29 additions & 0 deletions nibabel/tests/test_tripwire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""" Testing tripwire module
"""

from ..tripwire import TripWire, is_tripwire, TripWireError

from nose import SkipTest
from nose.tools import (assert_true, assert_false, assert_raises,
assert_equal, assert_not_equal)


def test_is_tripwire():
assert_false(is_tripwire(object()))
assert_true(is_tripwire(TripWire('some message')))


def test_tripwire():
# Test tripwire object
silly_module_name = TripWire('We do not have silly_module_name')
assert_raises(TripWireError,
getattr,
silly_module_name,
'do_silly_thing')
# Check AttributeError can be checked too
try:
silly_module_name.__wrapped__
except TripWireError as err:
assert_true(isinstance(err, AttributeError))
else:
raise RuntimeError("No error raised, but expected")
15 changes: 8 additions & 7 deletions nibabel/tripwire.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"""


class TripWireError(Exception):
class TripWireError(AttributeError):
""" Exception if trying to use TripWire object """
# Has to be subclass of AttributeError, to work round Python 3.5 inspection
# for doctests. Python 3.5 looks for a ``__wrapped__`` attribute during
# initialization of doctests, and only allows AttributeError as signal this
# is not present.


def is_tripwire(obj):
Expand Down Expand Up @@ -32,14 +36,11 @@ class TripWire(object):

Examples
--------
>>> try:
... import silly_module_name
... except ImportError:
... silly_module_name = TripWire('We do not have silly_module_name')
>>> silly_module_name.do_silly_thing('with silly string') #doctest: +IGNORE_EXCEPTION_DETAIL
>>> a_module = TripWire('We do not have a_module')
>>> a_module.do_silly_thing('with silly string') #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TripWireError: We do not have silly_module_name
TripWireError: We do not have a_module
"""
def __init__(self, msg):
self._msg = msg
Expand Down
26 changes: 26 additions & 0 deletions tools/travis_tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Tools for working with travis-ci
export WHEELHOST="travis-wheels.scikit-image.org"
export WHEELHOUSE="http://${WHEELHOST}/"

retry () {
# https://gist.github.com/fungusakafungus/1026804
local retry_max=5
local count=$retry_max
while [ $count -gt 0 ]; do
"$@" && break
count=$(($count - 1))
sleep 1
done

[ $count -eq 0 ] && {
echo "Retry failed [$retry_max]: $@" >&2
return 1
}
return 0
}


wheelhouse_pip_install() {
# Install pip requirements via travis wheelhouse
retry pip install --timeout=60 --no-index --trusted-host $WHEELHOST --find-links $WHEELHOUSE $@
}