Skip to content

Commit

Permalink
Merge pull request #1268 from newrelic/fix-configfile-pathobj
Browse files Browse the repository at this point in the history
Fix Pathlike objects not accepted by newrelic.initialize()
  • Loading branch information
hmstepanek authored Dec 4, 2024
2 parents 4853f32 + 64df833 commit 956e8bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,9 @@ def _load_configuration(

_configuration_done = True

# Normalize configuration file into a string path
config_file = os.fsdecode(config_file) if config_file is not None else config_file

# Update global variables tracking what configuration file and
# environment was used, plus whether errors are to be ignored.

Expand Down
40 changes: 40 additions & 0 deletions tests/agent_features/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import collections
import copy
import logging
import pathlib
import sys
import tempfile
import urllib.parse as urlparse
Expand Down Expand Up @@ -1059,6 +1060,45 @@ def test_toml_parse_production():
assert value.enabled is False


@pytest.mark.parametrize(
"pathtype", [str, lambda s: s.encode("utf-8"), pathlib.Path], ids=["str", "bytes", "pathlib.Path"]
)
def test_config_file_path_types_ini(pathtype):
settings = global_settings()
_reset_configuration_done()
_reset_config_parser()
_reset_instrumentation_done()

with tempfile.NamedTemporaryFile(suffix=".ini") as f:
f.write(newrelic_ini_contents)
f.seek(0)

config_file = pathtype(f.name)
initialize(config_file=config_file)
value = fetch_config_setting(settings, "app_name")
assert value == "Python Agent Test (agent_features)"


@pytest.mark.parametrize(
"pathtype", [str, lambda s: s.encode("utf-8"), pathlib.Path], ids=["str", "bytes", "pathlib.Path"]
)
@SKIP_IF_NOT_PY311
def test_config_file_path_types_toml(pathtype):
settings = global_settings()
_reset_configuration_done()
_reset_config_parser()
_reset_instrumentation_done()

with tempfile.NamedTemporaryFile(suffix=".toml") as f:
f.write(newrelic_toml_contents)
f.seek(0)

config_file = pathtype(f.name)
initialize(config_file=config_file)
value = fetch_config_setting(settings, "app_name")
assert value == "test11"


@pytest.fixture
def caplog_handler():
class CaplogHandler(logging.StreamHandler):
Expand Down

0 comments on commit 956e8bc

Please sign in to comment.