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

Finish pull #1700 #1

Merged
merged 8 commits into from
Nov 19, 2023
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
17 changes: 0 additions & 17 deletions .git-blame-ignore-revs

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

- Fix: the PYTHONSAFEPATH environment variable new in Python 3.11 is properly
supported, closing `issue 1696`_. Thanks, `Philipp A. <pull 1700_>`_.

- Added new :ref:`debug options <cmd_run_debug>`:

- ``pytest`` writes the pytest test name into the debug output.

- ``dataop2`` writes the full data being added to CoverageData objects.

.. _issue 1696: https://github.com/nedbat/coveragepy/issues/1696
.. _pull 1700: https://github.com/nedbat/coveragepy/pull/1700

.. scriv-start-here

Expand Down
2 changes: 1 addition & 1 deletion coverage/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ def main(argv: Optional[List[str]] = None) -> Optional[int]:
# pip install git+https://github.com/emin63/ox_profile.git
#
# $set_env.py: COVERAGE_PROFILE - Set to use ox_profile.
_profile = os.environ.get("COVERAGE_PROFILE", "")
_profile = os.getenv("COVERAGE_PROFILE")
if _profile: # pragma: debugging
from ox_profile.core.launchers import SimpleLauncher # pylint: disable=import-error
original_main = main
Expand Down
6 changes: 3 additions & 3 deletions coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def config_files_to_try(config_file: Union[bool, str]) -> List[Tuple[str, bool,
specified_file = (config_file is not True)
if not specified_file:
# No file was specified. Check COVERAGE_RCFILE.
rcfile = os.environ.get("COVERAGE_RCFILE")
rcfile = os.getenv("COVERAGE_RCFILE")
if rcfile:
config_file = rcfile
specified_file = True
Expand Down Expand Up @@ -602,10 +602,10 @@ def read_coverage_config(

# $set_env.py: COVERAGE_DEBUG - Options for --debug.
# 3) from environment variables:
env_data_file = os.environ.get("COVERAGE_FILE")
env_data_file = os.getenv("COVERAGE_FILE")
if env_data_file:
config.data_file = env_data_file
debugs = os.environ.get("COVERAGE_DEBUG")
debugs = os.getenv("COVERAGE_DEBUG")
if debugs:
config.debug.extend(d.strip() for d in debugs.split(","))

Expand Down
4 changes: 2 additions & 2 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ def plugin_info(plugins: List[Any]) -> List[str]:

# Mega debugging...
# $set_env.py: COVERAGE_DEBUG_CALLS - Lots and lots of output about calls to Coverage.
if int(os.environ.get("COVERAGE_DEBUG_CALLS", 0)): # pragma: debugging
if int(os.getenv("COVERAGE_DEBUG_CALLS", 0)): # pragma: debugging
from coverage.debug import decorate_methods, show_calls

Coverage = decorate_methods( # type: ignore[misc]
Expand Down Expand Up @@ -1369,7 +1369,7 @@ def process_startup() -> Optional[Coverage]:
not started by this call.

"""
cps = os.environ.get("COVERAGE_PROCESS_START")
cps = os.getenv("COVERAGE_PROCESS_START")
if not cps:
# No request for coverage, nothing to do.
return None
Expand Down
4 changes: 2 additions & 2 deletions coverage/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def __init__(self) -> None:

def filter(self, text: str) -> str:
"""Add a message when the pytest test changes."""
test_name = os.environ.get("PYTEST_CURRENT_TEST")
test_name = os.getenv("PYTEST_CURRENT_TEST")
if test_name != self.test_name:
text = f"Pytest context: {test_name}\n" + text
self.test_name = test_name
Expand Down Expand Up @@ -435,7 +435,7 @@ def get_one(
if file_name is not None:
fileobj = open(file_name, "a", encoding="utf-8")
else:
file_name = os.environ.get("COVERAGE_DEBUG_FILE", FORCED_DEBUG_FILE)
file_name = os.getenv("COVERAGE_DEBUG_FILE", FORCED_DEBUG_FILE)
if file_name in ("stdout", "stderr"):
fileobj = getattr(sys, file_name)
elif file_name:
Expand Down
4 changes: 2 additions & 2 deletions coverage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ class PYBEHAVIOR:
C_TRACER = os.getenv("COVERAGE_TEST_TRACER", "c") == "c"

# Are we coverage-measuring ourselves?
METACOV = os.getenv("COVERAGE_COVERAGE", "") != ""
METACOV = os.getenv("COVERAGE_COVERAGE") is not None

# Are we running our test suite?
# Even when running tests, you can use COVERAGE_TESTING=0 to disable the
# test-specific behavior like AST checking.
TESTING = os.getenv("COVERAGE_TESTING", "") == "True"
TESTING = os.getenv("COVERAGE_TESTING") == "True"


def debug_info() -> Iterable[Tuple[str, Any]]:
Expand Down
5 changes: 4 additions & 1 deletion coverage/execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def prepare(self) -> None:
This needs to happen before any importing, and without importing anything.
"""
path0: Optional[str]
if self.as_module:
if env.PYVERSION >= (3, 11) and os.getenv("PYTHONSAFEPATH"):
# See https://docs.python.org/3/using/cmdline.html#cmdoption-P
path0 = None
elif self.as_module:
path0 = os.getcwd()
elif os.path.isdir(self.arg0):
# Running a directory means running the __main__.py file in that
Expand Down
4 changes: 2 additions & 2 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ def __init__(

# Turn on AST dumps with an environment variable.
# $set_env.py: COVERAGE_AST_DUMP - Dump the AST nodes when parsing code.
dump_ast = bool(int(os.environ.get("COVERAGE_AST_DUMP", 0)))
dump_ast = bool(int(os.getenv("COVERAGE_AST_DUMP", "0")))

if dump_ast: # pragma: debugging
# Dump the AST so that failing tests have helpful output.
Expand All @@ -695,7 +695,7 @@ def __init__(
self.block_stack: List[Block] = []

# $set_env.py: COVERAGE_TRACK_ARCS - Trace possible arcs added while parsing code.
self.debug = bool(int(os.environ.get("COVERAGE_TRACK_ARCS", 0)))
self.debug = bool(int(os.getenv("COVERAGE_TRACK_ARCS", "0")))

def analyze(self) -> None:
"""Examine the AST tree from `root_node` to determine possible arcs.
Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#sys.path.append(os.path.abspath('.'))

# on_rtd is whether we are on readthedocs.org
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
on_rtd = os.getenv('READTHEDOCS') == 'True'

# -- General configuration -----------------------------------------------------

Expand Down
14 changes: 7 additions & 7 deletions igor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def ignore_warnings():
yield


VERBOSITY = int(os.environ.get("COVERAGE_IGOR_VERBOSE", "0"))
VERBOSITY = int(os.getenv("COVERAGE_IGOR_VERBOSE", "0"))

# Functions named do_* are executable from the command line: do_blah is run
# by "python igor.py blah".
Expand Down Expand Up @@ -118,7 +118,7 @@ def should_skip(tracer):
skipper = ""

# $set_env.py: COVERAGE_ONE_TRACER - Only run tests for one tracer.
only_one = os.environ.get("COVERAGE_ONE_TRACER")
only_one = os.getenv("COVERAGE_ONE_TRACER")
if only_one:
if CPYTHON:
if tracer == "py":
Expand All @@ -128,10 +128,10 @@ def should_skip(tracer):
skipper = f"No C tracer for {platform.python_implementation()}"
elif tracer == "py":
# $set_env.py: COVERAGE_NO_PYTRACER - Don't run the tests under the Python tracer.
skipper = os.environ.get("COVERAGE_NO_PYTRACER")
skipper = os.getenv("COVERAGE_NO_PYTRACER")
else:
# $set_env.py: COVERAGE_NO_CTRACER - Don't run the tests under the C tracer.
skipper = os.environ.get("COVERAGE_NO_CTRACER")
skipper = os.getenv("COVERAGE_NO_CTRACER")

if skipper:
msg = "Skipping tests " + label_for_tracer(tracer)
Expand Down Expand Up @@ -168,7 +168,7 @@ def run_tests_with_coverage(tracer, *runner_args):
os.environ["COVERAGE_TESTING"] = "True"
os.environ["COVERAGE_PROCESS_START"] = os.path.abspath("metacov.ini")
os.environ["COVERAGE_HOME"] = os.getcwd()
context = os.environ.get("COVERAGE_CONTEXT")
context = os.getenv("COVERAGE_CONTEXT")
if context:
if context[0] == "$":
context = os.environ[context[1:]]
Expand Down Expand Up @@ -235,7 +235,7 @@ def do_combine_html():
cov = coverage.Coverage(config_file="metacov.ini", messages=True)
cov.load()
show_contexts = bool(
os.environ.get("COVERAGE_DYNCTX") or os.environ.get("COVERAGE_CONTEXT")
os.getenv("COVERAGE_DYNCTX") or os.getenv("COVERAGE_CONTEXT")
)
cov.html_report(show_contexts=show_contexts)

Expand All @@ -249,7 +249,7 @@ def do_test_with_tracer(tracer, *runner_args):
return None

os.environ["COVERAGE_TEST_TRACER"] = tracer
if os.environ.get("COVERAGE_COVERAGE", "no") == "yes":
if os.getenv("COVERAGE_COVERAGE", "no") == "yes":
return run_tests_with_coverage(tracer, *runner_args)
else:
return run_tests(tracer, *runner_args)
Expand Down
2 changes: 1 addition & 1 deletion tests/balance_xdist_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, config):
self.config = config
self.running_all = (self.config.getoption("-k") == "")
self.times = collections.defaultdict(float)
self.worker = os.environ.get("PYTEST_XDIST_WORKER", "none")
self.worker = os.getenv("PYTEST_XDIST_WORKER", "none")
self.tests_csv = None

def pytest_sessionstart(self, session):
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def reset_filesdotpy_globals() -> Iterator[None]:
set_relative_directory()
yield

WORKER = os.environ.get("PYTEST_XDIST_WORKER", "none")
WORKER = os.getenv("PYTEST_XDIST_WORKER", "none")

def pytest_sessionstart() -> None:
"""Run once at the start of the test session."""
Expand Down
20 changes: 8 additions & 12 deletions tests/coveragetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,22 +438,18 @@ def run_command_status(self, cmd: str) -> Tuple[int, str]:

cmd = " ".join([shlex.quote(w) for w in command_words] + command_args)

# Add our test modules directory to PYTHONPATH. I'm sure there's too
# much path munging here, but...
pythonpath_name = "PYTHONPATH"

testmods = nice_file(self.working_root(), "tests/modules")
zipfile = nice_file(self.working_root(), "tests/zipmods.zip")
pypath = os.getenv(pythonpath_name, '')
if pypath:
pypath += os.pathsep
pypath += testmods + os.pathsep + zipfile
self.set_environ(pythonpath_name, pypath)

self.last_command_status, self.last_command_output = run_command(cmd)
print(self.last_command_output)
return self.last_command_status, self.last_command_output

def add_test_modules_to_pythonpath(self) -> None:
"""Add our test modules directory to PYTHONPATH."""
# Check that there isn't already a PYTHONPATH.
assert os.getenv("PYTHONPATH") is None
testmods = nice_file(self.working_root(), "tests/modules")
zipfile = nice_file(self.working_root(), "tests/zipmods.zip")
self.set_environ("PYTHONPATH", testmods + os.pathsep + zipfile)

def working_root(self) -> str:
"""Where is the root of the coverage.py working tree?"""
return os.path.dirname(nice_file(__file__, ".."))
Expand Down
6 changes: 3 additions & 3 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run_command(cmd: str) -> Tuple[int, str]:
# the test suite. Use these lines to get a list of the tests using them:
if 0: # pragma: debugging
with open("/tmp/processes.txt", "a") as proctxt: # type: ignore[unreachable]
print(os.environ.get("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True)
print(os.getenv("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True)

# In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of
# the subprocess is set incorrectly to ascii. Use an environment variable
Expand All @@ -65,7 +65,7 @@ def run_command(cmd: str) -> Tuple[int, str]:


# $set_env.py: COVERAGE_DIS - Disassemble test code to /tmp/dis
SHOW_DIS = bool(int(os.environ.get("COVERAGE_DIS", 0)))
SHOW_DIS = bool(int(os.getenv("COVERAGE_DIS", "0")))

def make_file(
filename: str,
Expand Down Expand Up @@ -110,7 +110,7 @@ def make_file(
os.makedirs("/tmp/dis", exist_ok=True)
with open(f"/tmp/dis/{basename}.dis", "w") as fdis:
print(f"# {os.path.abspath(filename)}", file=fdis)
cur_test = os.environ.get("PYTEST_CURRENT_TEST", "unknown")
cur_test = os.getenv("PYTEST_CURRENT_TEST", "unknown")
print(f"# PYTEST_CURRENT_TEST = {cur_test}", file=fdis)
try:
dis.dis(text, file=fdis)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import py_compile
import re
import sys

from pathlib import Path
from typing import Any, Iterator

import pytest
Expand All @@ -23,6 +23,7 @@
from coverage.files import python_reported_file

from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
from tests.helpers import change_dir

TRY_EXECFILE = os.path.join(TESTS_DIR, "modules/process_test/try_execfile.py")

Expand Down
Loading