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
This commit is contained in:
parent
962a4cce70
commit
6cd5fec0e2
|
@ -51,11 +51,11 @@ psycopg2.extensions.register_type(psycopg2.extensions.new_type((700, 701, 1700,)
|
||||||
|
|
||||||
|
|
||||||
import tools
|
import tools
|
||||||
from tools.func import wraps
|
from tools.func import wraps, frame_codeinfo
|
||||||
from datetime import datetime as mdt
|
from datetime import datetime as mdt
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import threading
|
import threading
|
||||||
from inspect import stack
|
from inspect import currentframe
|
||||||
|
|
||||||
import re
|
import re
|
||||||
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
|
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
|
||||||
|
@ -93,7 +93,7 @@ class Cursor(object):
|
||||||
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
|
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
|
||||||
self.__closed = False # real initialisation value
|
self.__closed = False # real initialisation value
|
||||||
self.autocommit(False)
|
self.autocommit(False)
|
||||||
self.__caller = tuple(stack()[2][1:3])
|
self.__caller = frame_codeinfo(currentframe(),2)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if not self.__closed:
|
if not self.__closed:
|
||||||
|
|
|
@ -93,3 +93,24 @@ def synchronized(lock_attr='_lock'):
|
||||||
return decorator
|
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>", '')
|
||||||
|
|
Loading…
Reference in New Issue