Skip to content

Commit

Permalink
Made caller_file_line public
Browse files Browse the repository at this point in the history
  • Loading branch information
lhupfeldt committed Sep 18, 2017
1 parent 004cf72 commit d56e442
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/multiconf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ multiconf package
=================

.. automodule:: multiconf
:members: mc_config, McInvalidValue, McTodoHandling
:members: mc_config, McInvalidValue, McTodoHandling, caller_file_line
:show-inheritance:

.. autoclass:: ConfigItem
Expand Down
1 change: 1 addition & 0 deletions multiconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .config_errors import ConfigException, ConfigDefinitionException, ConfigApiException, InvalidUsageException
from .config_errors import ConfigAttributeError, ConfigExcludedAttributeError
from .config_errors import caller_file_line
from .values import MC_REQUIRED, MC_TODO, McInvalidValue, McTodoHandling


Expand Down
1 change: 1 addition & 0 deletions multiconf/config_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def message(self):


def caller_file_line(up_level=2):
"""Return the file and line of the caller of the function calliing this function (depending on up_level)"""
frame = sys._getframe(up_level)
return frame.f_globals['__file__'], frame.f_lineno

Expand Down
36 changes: 36 additions & 0 deletions test/utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2012 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.

from __future__ import print_function

from multiconf import caller_file_line

from .utils.utils import next_line_num

from .utils_test_helpers import bb, fn_aa_exp, ln_aa_exp


ln_bb_exp = None


def test_caller_file_line():
def cc():
global ln_bb_exp

fnc, lnc = caller_file_line(2)
print("fnc, lnc:", fnc, lnc)

ln_bb_exp = next_line_num()
fnb, lnb, fna, lna = bb()

return fnc, lnc, fnb, lnb, fna, lna

fn_exp = __file__
ln_cc_exp = next_line_num()
fnc, lnc, fnb, lnb, fna, lna = cc()

assert fn_exp == fnc
assert ln_cc_exp == lnc

assert fn_exp == fnb
assert ln_bb_exp == lnb
31 changes: 31 additions & 0 deletions test/utils_test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2012 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.

from __future__ import print_function

from multiconf import caller_file_line

from .utils.utils import next_line_num


fn_aa_exp = __file__
ln_aa_exp = None


def aa():
fna, lna = caller_file_line()
print("fna, lna:", fna, lna)

return fna, lna


def bb():
global ln_aa_exp

fnb, lnb = caller_file_line()
print("fnb, lnb:", fnb, lnb)

ln_aa_exp = next_line_num()
fna, lna = aa()

return fnb, lnb, fna, lna

0 comments on commit d56e442

Please sign in to comment.