Skip to content
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
8 changes: 8 additions & 0 deletions tests/test_trace_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ def get(self, *args, **kwargs):
self.assertFalse(client.enabled)
self.assertEqual("environment", client.disabled_reason)

def test_a_value_that_is_not_a_string_opts_out_even_when_it_is_empty(self):
for variable in ("TRACE_USAGE_REPORTING", "DO_NOT_TRACK"):
for value in (0, False, [], 0.0, b"", 1, True):
self.assertTrue(environment_opts_out({variable: value}),
"%s=%r is not a string, so it must opt out, not read as unset" % (variable, value))
self.assertFalse(environment_opts_out({"TRACE_USAGE_REPORTING": None, "DO_NOT_TRACK": None}),
"a None value is a missing variable and leaves the program setting in charge")

def test_blank_or_clearly_on_environment_values_leave_the_program_setting_in_charge(self):
for variable, value in (("TRACE_USAGE_REPORTING", "on"), ("TRACE_USAGE_REPORTING", ""),
("TRACE_USAGE_REPORTING", " "), ("TRACE_USAGE_REPORTING", "1"),
Expand Down
21 changes: 17 additions & 4 deletions trace_client/trace_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,32 @@ def environment_opts_out(environ: Optional[Mapping[str, str]] = None) -> bool:
"on" (``true``, ``on``, ``yes``, ``1``), or ``DO_NOT_TRACK`` set to
anything but blank or a clear "no" (``0``, ``false``, ``off``, ``no``),
opts out; case and surrounding space do not matter. An environment that
cannot be read -- or a value that is not a string -- opts out too: it may
hold ``DO_NOT_TRACK=1``. Never raises."""
cannot be read -- or a value that is not a string, even ``0`` or
``False`` -- opts out too: it may hold ``DO_NOT_TRACK=1``. Never raises."""
try:
env = os.environ if environ is None else environ
usage_reporting = (env.get(ENV_TRACE_USAGE_REPORTING) or "").strip().lower()
usage_reporting = _variable(env, ENV_TRACE_USAGE_REPORTING)
if usage_reporting and usage_reporting not in _ON_VALUES:
return True
do_not_track = (env.get(ENV_DO_NOT_TRACK) or "").strip().lower()
do_not_track = _variable(env, ENV_DO_NOT_TRACK)
return bool(do_not_track) and do_not_track not in _NEGATIVE_VALUES
except Exception: # noqa: BLE001 - an environment that cannot be read counts as opting out
return True


def _variable(env: Mapping[str, str], name: str) -> str:
"""The variable ``name`` in ``env``, trimmed and lower-cased; blank when
unset. A value that is not a string raises, so the caller opts out: only
a missing variable may leave reporting on, never a ``0`` or ``False``
that a blanket ``or ""`` would have read as unset."""
value = env.get(name)
if value is None:
return ""
if not isinstance(value, str):
raise TypeError("%s is %s, not a string" % (name, type(value).__name__))
return value.strip().lower()


def allows_reporting(setting: object) -> bool:
"""Whether a program's own reporting setting, as read from its
configuration, allows reporting -- for ``enabled=``. Fails closed: only
Expand Down
Loading