Skip to content

Commit 41df3ab

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #342 from bmermet/pyramid-relativeimport-bug
Fix relative imports in pyramid
2 parents b2e9349 + 5e67413 commit 41df3ab

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

ddtrace/contrib/pyramid/patch.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .trace import trace_pyramid
44

55
import pyramid.config
6+
from pyramid.path import caller_package
67

78
import wrapt
89

@@ -20,17 +21,23 @@ def patch():
2021

2122

2223
def traced_init(wrapped, instance, args, kwargs):
23-
settings = kwargs.pop("settings", {})
24-
service = os.environ.get("DATADOG_SERVICE_NAME") or "pyramid"
24+
settings = kwargs.pop('settings', {})
25+
service = os.environ.get('DATADOG_SERVICE_NAME') or 'pyramid'
2526
trace_settings = {
2627
'datadog_trace_service' : service,
2728
}
2829
settings.update(trace_settings)
29-
kwargs["settings"] = settings
30+
kwargs['settings'] = settings
3031

3132
# Commit actions immediately after they are configured so as to
3233
# skip conflict resolution when adding our tween
33-
kwargs["autocommit"] = True
34+
kwargs['autocommit'] = True
35+
36+
# `caller_package` works by walking a fixed amount of frames up the stack
37+
# to find the calling package. So if we let the original `__init__`
38+
# function call it, our wrapper will mess things up.
39+
if not kwargs.get('package', None):
40+
kwargs['package'] = caller_package()
3441

3542
wrapped(*args, **kwargs)
3643
trace_pyramid(instance)

tests/contrib/pyramid/test_pyramid_autopatch.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
import ddtrace
1616
from ddtrace import compat
1717

18+
def _include_me(config):
19+
pass
20+
21+
def test_config_include():
22+
""" This test makes sure that relative imports still work when the
23+
application is run with ddtrace-run """
24+
config = Configurator()
25+
config.include('._include_me')
26+
1827

1928
def test_200():
2029
app, tracer = _get_test_app(service='foobar')

0 commit comments

Comments
 (0)