Skip to content

Commit

Permalink
Suport non-existence of __file__
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Nov 9, 2019
1 parent b0c28d6 commit 21da5c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch allows Hypothesis to run in environments that do not specify
a ``__file__``, such as a :mod:`python:zipapp` (:issue:`2196`).
6 changes: 0 additions & 6 deletions hypothesis-python/src/hypothesis/core.py
Expand Up @@ -24,7 +24,6 @@
import contextlib
import datetime
import inspect
import os
import random as rnd_module
import traceback
import warnings
Expand Down Expand Up @@ -485,11 +484,6 @@ def new_given_argspec(original_argspec, generator_kwargs):
)


ROOT = os.path.dirname(__file__)

STDLIB = os.path.dirname(os.__file__)


class StateForActualGivenExecution(object):
def __init__(self, test_runner, search_strategy, test, settings, random, had_seed):
self.test_runner = test_runner
Expand Down
3 changes: 3 additions & 0 deletions hypothesis-python/src/hypothesis/internal/escalation.py
Expand Up @@ -37,6 +37,9 @@


def belongs_to(package):
if not hasattr(package, "__file__"): # pragma: no cover
return lambda filepath: False

root = os.path.dirname(package.__file__)
cache = {text_type: {}, binary_type: {}}

Expand Down
19 changes: 12 additions & 7 deletions hypothesis-python/src/hypothesis/provisional.py
Expand Up @@ -45,13 +45,18 @@

# This file is sourced from http://data.iana.org/TLD/tlds-alpha-by-domain.txt
# The file contains additional information about the date that it was last updated.
with open(
os.path.join(os.path.dirname(__file__), "vendor", "tlds-alpha-by-domain.txt")
) as tld_file:
__header = next(tld_file)
assert __header.startswith("#")
TOP_LEVEL_DOMAINS = sorted((line.rstrip() for line in tld_file), key=len)
TOP_LEVEL_DOMAINS.insert(0, "COM")
try:
from importlib.resources import read_text # type: ignore
except ImportError:
# If we don't have importlib.resources (Python 3.7+) or the importlib_resources
# backport available, fall back to __file__ and hope we're on a filesystem.
f = os.path.join(os.path.dirname(__file__), "vendor", "tlds-alpha-by-domain.txt")
with open(f) as tld_file:
_tlds = tld_file.read().splitlines()
else: # pragma: no cover # new in Python 3.7
_tlds = read_text("hypothesis.vendor", "tlds-alpha-by-domain.txt").splitlines()
assert _tlds[0].startswith("#")
TOP_LEVEL_DOMAINS = ["COM"] + sorted(_tlds[1:], key=len)


class DomainNameStrategy(SearchStrategy):
Expand Down

0 comments on commit 21da5c4

Please sign in to comment.