diff options
| author | P. Christeas <p_christ@hol.gr> | 2010-11-23 15:57:42 +0200 |
|---|---|---|
| committer | P. Christeas <p_christ@hol.gr> | 2010-11-23 15:57:42 +0200 |
| commit | 6cd5fec0e295d7b25f6ee66ae4b7ab8bddf25306 (patch) | |
| tree | 00a1409ae8a18bac9e298fb4a872f9744ad2f385 /bin/tools/func.py | |
| parent | 962a4cce702bc69924cd09702690b8df6bfd6ef0 (diff) | |
sql_db: Avoid using inspect.stack(), use failsafe alternative.
Inspecting the full stack just to learn the file:line info for debugging
purposes is both expensive and error-prone. Write an alternative function
in tools/func.py that will just go N frames back and try to get the info.
bzr revid: p_christ@hol.gr-20101123135742-2jp75f3uxyg3a6b6
Diffstat (limited to 'bin/tools/func.py')
| -rw-r--r-- | bin/tools/func.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/bin/tools/func.py b/bin/tools/func.py index 8cedf6715fd..4150b3e688b 100644 --- a/bin/tools/func.py +++ b/bin/tools/func.py @@ -93,3 +93,24 @@ def synchronized(lock_attr='_lock'): return decorator + +from inspect import getsourcefile + +def frame_codeinfo(fframe, back=0): + """ Return a (filename, line) pair for a previous frame . + @return (filename, lineno) where lineno is either int or string=='' + """ + + try: + if not fframe: + return ("<unknown>", '') + for i in range(back): + fframe = fframe.f_back + try: + fname = getsourcefile(fframe) + except TypeError: + fname = '<builtin>' + lineno = fframe.f_lineno or '' + return (fname, lineno) + except Exception: + return ("<unknown>", '') |
