Skip to content

Commit

Permalink
Fix is_hypothesis_file internal function
Browse files Browse the repository at this point in the history
 Errors in third-party extensions such as `hypothesis-trio` or
 `hypothesis-jsonschema` used to be incorrectly considered to be
 Hypothesis internal errors, which could result in confusing error.
  • Loading branch information
vxgmichel committed Jan 10, 2020
1 parent 11c7c08 commit ed895e8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ their individual contributions.
* `Tyler Gibbons <https://www.github.com/kavec>`_ ([email protected])
* `Tyler Nickerson <https://www.github.com/nmbrgts>`_
* `Vidya Rani <https://www.github.com/vidyarani-dg>`_ ([email protected])
* `Vincent Michel <https://www.github.com/vxgmichel>`_ ([email protected])
* `Will Hall <https://www.github.com/wrhall>`_ ([email protected])
* `Will Thompson <https://www.github.com/wjt>`_ ([email protected])
* `Wilfred Hughes <https://www.github.com/wilfred>`_
Expand Down
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This patch fixes a bug where errors in third-party extensions such as :pypi:hypothesis-trio or :pypi:hypothesis-jsonschema were incorrectly considered to be Hypothesis internal errors, which could result in confusing error messages.

Thanks to Vincent Michel for reporting and fixing the bug!
11 changes: 9 additions & 2 deletions hypothesis-python/src/hypothesis/internal/escalation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import sys
import traceback
from inspect import getframeinfo
from pathlib import Path

import hypothesis
from hypothesis.errors import (
Expand All @@ -35,7 +36,7 @@ def belongs_to(package):
if not hasattr(package, "__file__"): # pragma: no cover
return lambda filepath: False

root = os.path.dirname(package.__file__)
root = Path(package.__file__).resolve().parent
cache = {str: {}, bytes: {}}

def accept(filepath):
Expand All @@ -44,7 +45,13 @@ def accept(filepath):
return cache[ftype][filepath]
except KeyError:
pass
result = os.path.abspath(filepath).startswith(root)
abspath = Path(filepath).resolve()
try:
abspath.relative_to(root)
except ValueError:
result = False
else:
result = True
cache[ftype][filepath] = result
return result

Expand Down
26 changes: 26 additions & 0 deletions hypothesis-python/tests/cover/test_escalation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
#
# END HEADER

import os
import sys

import pytest

import hypothesis
import hypothesis.internal.escalation as esc
import hypothesis.strategies as st
from hypothesis import given
Expand Down Expand Up @@ -62,3 +66,25 @@ def test(i):
test()

assert count == [1]


def test_is_hypothesis_file_not_confused_by_prefix(monkeypatch):
# Errors in third-party extensions such as `hypothesis-trio` or
# `hypothesis-jsonschema` used to be incorrectly considered to be
# Hypothesis internal errors, which could result in confusing error
# messages. This test makes sure that files like:
# `[...]/python3.7/site-packages/hypothesis_something/[...]`
# are not considered as hypothesis files.
root = os.path.dirname(hypothesis.__file__)
assert esc.is_hypothesis_file(hypothesis.__file__)
assert esc.is_hypothesis_file(esc.__file__)

# Ignore `Path.resolve` for python 3.5 because it is strict by default
# and some paths in this test are made up (this behavior has been changed
# since python 3.6)
if sys.version_info < (3, 6):
monkeypatch.setattr("pathlib.Path.resolve", lambda x: x, raising=False)

assert not esc.is_hypothesis_file(pytest.__file__)
assert not esc.is_hypothesis_file(root + "-suffix")
assert not esc.is_hypothesis_file(root + "-suffix/something.py")

0 comments on commit ed895e8

Please sign in to comment.