Skip to content

Commit 956e8bc

Browse files
authored
Merge pull request #1268 from newrelic/fix-configfile-pathobj
Fix Pathlike objects not accepted by newrelic.initialize()
2 parents 4853f32 + 64df833 commit 956e8bc

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

newrelic/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,9 @@ def _load_configuration(
977977

978978
_configuration_done = True
979979

980+
# Normalize configuration file into a string path
981+
config_file = os.fsdecode(config_file) if config_file is not None else config_file
982+
980983
# Update global variables tracking what configuration file and
981984
# environment was used, plus whether errors are to be ignored.
982985

tests/agent_features/test_configuration.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import collections
1616
import copy
1717
import logging
18+
import pathlib
1819
import sys
1920
import tempfile
2021
import urllib.parse as urlparse
@@ -1059,6 +1060,45 @@ def test_toml_parse_production():
10591060
assert value.enabled is False
10601061

10611062

1063+
@pytest.mark.parametrize(
1064+
"pathtype", [str, lambda s: s.encode("utf-8"), pathlib.Path], ids=["str", "bytes", "pathlib.Path"]
1065+
)
1066+
def test_config_file_path_types_ini(pathtype):
1067+
settings = global_settings()
1068+
_reset_configuration_done()
1069+
_reset_config_parser()
1070+
_reset_instrumentation_done()
1071+
1072+
with tempfile.NamedTemporaryFile(suffix=".ini") as f:
1073+
f.write(newrelic_ini_contents)
1074+
f.seek(0)
1075+
1076+
config_file = pathtype(f.name)
1077+
initialize(config_file=config_file)
1078+
value = fetch_config_setting(settings, "app_name")
1079+
assert value == "Python Agent Test (agent_features)"
1080+
1081+
1082+
@pytest.mark.parametrize(
1083+
"pathtype", [str, lambda s: s.encode("utf-8"), pathlib.Path], ids=["str", "bytes", "pathlib.Path"]
1084+
)
1085+
@SKIP_IF_NOT_PY311
1086+
def test_config_file_path_types_toml(pathtype):
1087+
settings = global_settings()
1088+
_reset_configuration_done()
1089+
_reset_config_parser()
1090+
_reset_instrumentation_done()
1091+
1092+
with tempfile.NamedTemporaryFile(suffix=".toml") as f:
1093+
f.write(newrelic_toml_contents)
1094+
f.seek(0)
1095+
1096+
config_file = pathtype(f.name)
1097+
initialize(config_file=config_file)
1098+
value = fetch_config_setting(settings, "app_name")
1099+
assert value == "test11"
1100+
1101+
10621102
@pytest.fixture
10631103
def caplog_handler():
10641104
class CaplogHandler(logging.StreamHandler):

0 commit comments

Comments
 (0)