Replace inspect.stack() with inspect.currentframe() (#1104)
* Replace inspect.stack() with inspect.currentframe() Fixes #11874 Works around https://bugs.python.org/issue12920 which causes every call to inspect.trace() to fail because __main__ is always the starting point. * Fix a few Codecov complaints from files touched by previous commit. Ignoring the "duplicate code" issue caused by the empty comment line at the beginning of every file.
This commit is contained in:
parent
d91fc9e2fb
commit
b38f77f2aa
@ -52,7 +52,6 @@ methods should be changed to generate exceptions. Possibly by globally changing
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import inspect
|
import inspect
|
||||||
from abc import ABCMeta
|
from abc import ABCMeta
|
||||||
from types import FunctionType
|
from types import FunctionType
|
||||||
@ -160,10 +159,12 @@ def wrapper(method):
|
|||||||
"""
|
"""
|
||||||
class_name = args[0].__class__.__name__
|
class_name = args[0].__class__.__name__
|
||||||
func_name = method.__name__
|
func_name = method.__name__
|
||||||
caller_frame = inspect.stack()[1]
|
frame = inspect.currentframe()
|
||||||
|
c_frame = frame.f_back
|
||||||
|
c_code = c_frame.f_code
|
||||||
LOG.debug('calling %s.%s()... from file %s, line %s in %s',
|
LOG.debug('calling %s.%s()... from file %s, line %s in %s',
|
||||||
class_name, func_name, os.path.split(caller_frame[1])[1],
|
class_name, func_name, c_code.co_filename, c_frame.f_lineno,
|
||||||
caller_frame[2], caller_frame[3])
|
c_code.co_name)
|
||||||
return method(*args, **keywargs)
|
return method(*args, **keywargs)
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
|
@ -78,15 +78,13 @@ class DbTxn(defaultdict):
|
|||||||
|
|
||||||
elapsed_time = time.time() - self.start_time
|
elapsed_time = time.time() - self.start_time
|
||||||
if __debug__:
|
if __debug__:
|
||||||
caller_frame = inspect.stack()[1]
|
frame = inspect.currentframe()
|
||||||
|
c_frame = frame.f_back
|
||||||
|
c_code = c_frame.f_code
|
||||||
_LOG.debug(" **** DbTxn %s exited. Called from file %s, "
|
_LOG.debug(" **** DbTxn %s exited. Called from file %s, "
|
||||||
"line %s, in %s **** %.2f seconds" %
|
"line %s, in %s **** %.2f seconds",
|
||||||
((hex(id(self)),)+
|
hex(id(self)), c_code.co_filename, c_frame.f_lineno,
|
||||||
(os.path.split(caller_frame[1])[1],)+
|
c_code.co_name, elapsed_time)
|
||||||
tuple(caller_frame[i] for i in range(2, 4))+
|
|
||||||
(elapsed_time,)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -70,12 +70,14 @@ def make_database(plugin_id):
|
|||||||
database = getattr(mod, pdata.databaseclass)
|
database = getattr(mod, pdata.databaseclass)
|
||||||
db = database()
|
db = database()
|
||||||
import inspect
|
import inspect
|
||||||
caller_frame = inspect.stack()[1]
|
frame = inspect.currentframe()
|
||||||
|
c_frame = frame.f_back
|
||||||
|
c_code = c_frame.f_code
|
||||||
_LOG.debug("Database class instance created Class:%s instance:%s. "
|
_LOG.debug("Database class instance created Class:%s instance:%s. "
|
||||||
"Called from File %s, line %s, in %s"
|
"Called from File %s, line %s, in %s",
|
||||||
% ((db.__class__.__name__, hex(id(db)))
|
db.__class__.__name__, hex(id(db)), c_code.co_filename,
|
||||||
+ (os.path.split(caller_frame[1])[1],)
|
c_frame.f_lineno, c_code.co_name)
|
||||||
+ tuple(caller_frame[i] for i in range(2, 4))))
|
|
||||||
return db
|
return db
|
||||||
else:
|
else:
|
||||||
raise Exception("can't load database backend: '%s'" % plugin_id)
|
raise Exception("can't load database backend: '%s'" % plugin_id)
|
||||||
|
@ -29,7 +29,6 @@ Provide the database state class
|
|||||||
#
|
#
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
import sys
|
import sys
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
@ -88,10 +87,12 @@ class DbState(Callback):
|
|||||||
"""
|
"""
|
||||||
class_name = self.__class__.__name__
|
class_name = self.__class__.__name__
|
||||||
func_name = "is_open"
|
func_name = "is_open"
|
||||||
caller_frame = inspect.stack()[1]
|
frame = inspect.currentframe()
|
||||||
|
c_frame = frame.f_back
|
||||||
|
c_code = c_frame.f_code
|
||||||
_LOG.debug('calling %s.%s()... from file %s, line %s in %s',
|
_LOG.debug('calling %s.%s()... from file %s, line %s in %s',
|
||||||
class_name, func_name, os.path.split(caller_frame[1])[1],
|
class_name, func_name, c_code.co_filename, c_frame.f_lineno,
|
||||||
caller_frame[2], caller_frame[3])
|
c_code.co_name)
|
||||||
return (self.db is not None) and self.db.is_open()
|
return (self.db is not None) and self.db.is_open()
|
||||||
|
|
||||||
def change_database(self, database):
|
def change_database(self, database):
|
||||||
|
@ -99,7 +99,8 @@ class BaseTest(unittest.TestCase):
|
|||||||
stime = perf_counter()
|
stime = perf_counter()
|
||||||
results = filter_.apply(self.db)
|
results = filter_.apply(self.db)
|
||||||
if __debug__:
|
if __debug__:
|
||||||
rulename = inspect.stack()[1][3]
|
frame = inspect.currentframe()
|
||||||
|
rulename = frame.f_back.f_code.co_name
|
||||||
print("%s: %.2f\n" % (rulename, perf_counter() - stime))
|
print("%s: %.2f\n" % (rulename, perf_counter() - stime))
|
||||||
return set(results)
|
return set(results)
|
||||||
|
|
||||||
|
@ -324,12 +324,16 @@ class Callback:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Check signal exists
|
# Check signal exists
|
||||||
|
frame = inspect.currentframe()
|
||||||
|
c_frame = frame.f_back
|
||||||
|
c_code = c_frame.f_code
|
||||||
|
frame_info = (c_code.co_filename, c_frame.f_lineno, c_code.co_name)
|
||||||
if signal_name not in self.__signal_map:
|
if signal_name not in self.__signal_map:
|
||||||
self._warn("Attempt to emit to unknown signal: %s\n"
|
self._warn("Attempt to emit to unknown signal: %s\n"
|
||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + frame_info))
|
||||||
return
|
return
|
||||||
|
|
||||||
# check that the signal is not already being emitted. This prevents
|
# check that the signal is not already being emitted. This prevents
|
||||||
@ -340,7 +344,7 @@ class Callback:
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + frame_info))
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -358,7 +362,7 @@ class Callback:
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + frame_info))
|
||||||
return
|
return
|
||||||
|
|
||||||
# type check arguments
|
# type check arguments
|
||||||
@ -369,7 +373,7 @@ class Callback:
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + frame_info))
|
||||||
return
|
return
|
||||||
|
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
@ -379,7 +383,7 @@ class Callback:
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + frame_info))
|
||||||
return
|
return
|
||||||
|
|
||||||
if arg_types is not None:
|
if arg_types is not None:
|
||||||
@ -391,7 +395,7 @@ class Callback:
|
|||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
" arg passed was: %s, type of arg passed %s, type should be: %s\n"
|
" arg passed was: %s, type of arg passed %s, type should be: %s\n"
|
||||||
% ((str(signal_name), ) + inspect.stack()[1][1:4] +\
|
% ((str(signal_name), ) + frame_info +\
|
||||||
(args[i], repr(type(args[i])), repr(arg_types[i]))))
|
(args[i], repr(type(args[i])), repr(arg_types[i]))))
|
||||||
return
|
return
|
||||||
if signal_name in self.__callback_map:
|
if signal_name in self.__callback_map:
|
||||||
|
@ -29,7 +29,6 @@ BSDDBTxn class: Wrapper for BSDDB transaction-oriented methods
|
|||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import logging
|
import logging
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -73,14 +72,13 @@ class BSDDBTxn:
|
|||||||
"""
|
"""
|
||||||
# Conditional on __debug__ because all that frame stuff may be slow
|
# Conditional on __debug__ because all that frame stuff may be slow
|
||||||
if __debug__:
|
if __debug__:
|
||||||
caller_frame = inspect.stack()[1]
|
frame = inspect.currentframe()
|
||||||
|
c_frame = frame.f_back
|
||||||
|
c_code = c_frame.f_code
|
||||||
_LOG.debug(" BSDDBTxn %s instantiated. Called from file %s,"
|
_LOG.debug(" BSDDBTxn %s instantiated. Called from file %s,"
|
||||||
" line %s, in %s" %
|
" line %s, in %s", hex(id(self)), c_code.co_filename,
|
||||||
((hex(id(self)),)+
|
c_frame.f_lineno, c_code.co_name)
|
||||||
(os.path.split(caller_frame[1])[1],)+
|
|
||||||
(tuple(caller_frame[i] for i in range(2, 4)))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self.env = env
|
self.env = env
|
||||||
self.db = db
|
self.db = db
|
||||||
self.txn = None
|
self.txn = None
|
||||||
|
Loading…
Reference in New Issue
Block a user