Skip to content

Commit

Permalink
Merge pull request #58 from Callek/frame_traversal
Browse files Browse the repository at this point in the history
Better support for missing __name__ in frame traversal
  • Loading branch information
hynek committed Sep 24, 2015
2 parents 8d13674 + ff3bfa7 commit b2ef5fe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following folks helped forming ``structlog`` into what it is now:
- `Itamar Turner-Trauring <https://github.com/itamarst>`_
- `Jack Pearkes <https://github.com/pearkes>`_
- `Jean-Paul Calderone <http://as.ynchrono.us>`_
- `Justin Wood (Callek) <https://github.com/Callek>`_
- `Lakshmi Kannan <https://github.com/lakshmi-kannan>`_
- `Lynn Root <https://github.com/econchick>`_
- `Mathieu Leplatre <https://github.com/leplatrem>`_
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Changelog
=========

- :bug:`- major` Tolerate frames without a ``__name__``, better.
- :feature:`-` Officially support Python 3.5.
- :feature:`-` Add :func:`structlog.ReturnLogger.failure` and :func:`structlog.PrintLogger.failure` as preparation for the new Twisted logging system.
- :release:`15.2.0 <2015-06-10>`
Expand Down
4 changes: 2 additions & 2 deletions structlog/_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def _find_first_app_frame_and_name(additional_ignores=None):
"""
ignores = ["structlog"] + (additional_ignores or [])
f = sys._getframe()
name = f.f_globals.get("__name__", "?")
name = f.f_globals.get("__name__") or "?"
while any(name.startswith(i) for i in ignores):
f = f.f_back
name = f.f_globals.get("__name__", "?")
name = f.f_globals.get("__name__") or "?"
return f, name


Expand Down
28 changes: 24 additions & 4 deletions tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_ignores_structlog_by_default(self, monkeypatch):
f2 = stub(f_globals={'__name__': 'structlog.blubb'}, f_back=f1)
monkeypatch.setattr(structlog._frames.sys, '_getframe', lambda: f2)
f, n = _find_first_app_frame_and_name()
assert ((f1, 'test') == f, n)
assert ((f1, 'test') == (f, n))

def test_ignoring_of_additional_frame_names_works(self, monkeypatch):
"""
Expand All @@ -38,17 +38,37 @@ def test_ignoring_of_additional_frame_names_works(self, monkeypatch):
f2 = stub(f_globals={'__name__': 'ignored.bar'}, f_back=f1)
f3 = stub(f_globals={'__name__': 'structlog.blubb'}, f_back=f2)
monkeypatch.setattr(structlog._frames.sys, '_getframe', lambda: f3)
f, n = _find_first_app_frame_and_name()
assert ((f1, 'test') == f, n)
f, n = _find_first_app_frame_and_name(additional_ignores=['ignored'])
assert ((f1, 'test') == (f, n))

def test_tolerates_missing_name(self, monkeypatch):
"""
Use ``?`` if `f_globals` lacks a `__name__` key
"""
f1 = stub(f_globals={}, f_back=None)
monkeypatch.setattr(structlog._frames.sys, "_getframe", lambda: f1)
f, n = _find_first_app_frame_and_name()
assert ((f1, "?") == (f, n))

def test_tolerates_name_explicitly_None_oneframe(self, monkeypatch):
"""
Use ``?`` if `f_globals` has a `None` valued `__name__` key
"""
f1 = stub(f_globals={'__name__': None}, f_back=None)
monkeypatch.setattr(structlog._frames.sys, "_getframe", lambda: f1)
assert ((f1, "?") == f, n)
f, n = _find_first_app_frame_and_name()
assert ((f1, "?") == (f, n))

def test_tolerates_name_explicitly_None_manyframe(self, monkeypatch):
"""
Use ``?`` if `f_globals` has a `None` valued `__name__` key,
multiple frames up.
"""
f1 = stub(f_globals={'__name__': None}, f_back=None)
f2 = stub(f_globals={'__name__': 'structlog.blubb'}, f_back=f1)
monkeypatch.setattr(structlog._frames.sys, "_getframe", lambda: f2)
f, n = _find_first_app_frame_and_name()
assert ((f1, "?") == (f, n))


@pytest.fixture
Expand Down

0 comments on commit b2ef5fe

Please sign in to comment.