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

Suport platforms where __file__ is not defined #2198

Merged
merged 1 commit into from Nov 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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