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:
John Ralls 2020-09-14 09:08:58 -07:00 committed by GitHub
parent d91fc9e2fb
commit b38f77f2aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 36 deletions

View File

@ -52,7 +52,6 @@ methods should be changed to generate exceptions. Possibly by globally changing
#
#-------------------------------------------------------------------------
import logging
import os
import inspect
from abc import ABCMeta
from types import FunctionType
@ -160,10 +159,12 @@ def wrapper(method):
"""
class_name = args[0].__class__.__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',
class_name, func_name, os.path.split(caller_frame[1])[1],
caller_frame[2], caller_frame[3])
class_name, func_name, c_code.co_filename, c_frame.f_lineno,
c_code.co_name)
return method(*args, **keywargs)
return wrapped

View File

@ -78,15 +78,13 @@ class DbTxn(defaultdict):
elapsed_time = time.time() - self.start_time
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, "
"line %s, in %s **** %.2f seconds" %
((hex(id(self)),)+
(os.path.split(caller_frame[1])[1],)+
tuple(caller_frame[i] for i in range(2, 4))+
(elapsed_time,)
)
)
"line %s, in %s **** %.2f seconds",
hex(id(self)), c_code.co_filename, c_frame.f_lineno,
c_code.co_name, elapsed_time)
return False

View File

@ -70,12 +70,14 @@ def make_database(plugin_id):
database = getattr(mod, pdata.databaseclass)
db = database()
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. "
"Called from File %s, line %s, in %s"
% ((db.__class__.__name__, hex(id(db)))
+ (os.path.split(caller_frame[1])[1],)
+ tuple(caller_frame[i] for i in range(2, 4))))
"Called from File %s, line %s, in %s",
db.__class__.__name__, hex(id(db)), c_code.co_filename,
c_frame.f_lineno, c_code.co_name)
return db
else:
raise Exception("can't load database backend: '%s'" % plugin_id)

View File

@ -29,7 +29,6 @@ Provide the database state class
#
#------------------------------------------------------------------------
import sys
import os
import logging
import inspect
@ -88,10 +87,12 @@ class DbState(Callback):
"""
class_name = self.__class__.__name__
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',
class_name, func_name, os.path.split(caller_frame[1])[1],
caller_frame[2], caller_frame[3])
class_name, func_name, c_code.co_filename, c_frame.f_lineno,
c_code.co_name)
return (self.db is not None) and self.db.is_open()
def change_database(self, database):

View File

@ -99,7 +99,8 @@ class BaseTest(unittest.TestCase):
stime = perf_counter()
results = filter_.apply(self.db)
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))
return set(results)

View File

@ -324,12 +324,16 @@ class Callback:
return
# 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:
self._warn("Attempt to emit to unknown signal: %s\n"
" from: file: %s\n"
" line: %d\n"
" func: %s\n"
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
% ((str(signal_name), ) + frame_info))
return
# check that the signal is not already being emitted. This prevents
@ -340,7 +344,7 @@ class Callback:
" from: file: %s\n"
" line: %d\n"
" func: %s\n"
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
% ((str(signal_name), ) + frame_info))
return
try:
@ -358,7 +362,7 @@ class Callback:
" from: file: %s\n"
" line: %d\n"
" func: %s\n"
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
% ((str(signal_name), ) + frame_info))
return
# type check arguments
@ -369,7 +373,7 @@ class Callback:
" from: file: %s\n"
" line: %d\n"
" func: %s\n"
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
% ((str(signal_name), ) + frame_info))
return
if len(args) > 0:
@ -379,7 +383,7 @@ class Callback:
" from: file: %s\n"
" line: %d\n"
" func: %s\n"
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
% ((str(signal_name), ) + frame_info))
return
if arg_types is not None:
@ -391,7 +395,7 @@ class Callback:
" line: %d\n"
" func: %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]))))
return
if signal_name in self.__callback_map:

View File

@ -29,7 +29,6 @@ BSDDBTxn class: Wrapper for BSDDB transaction-oriented methods
#-------------------------------------------------------------------------
import logging
import inspect
import os
#-------------------------------------------------------------------------
#
@ -73,14 +72,13 @@ class BSDDBTxn:
"""
# Conditional on __debug__ because all that frame stuff may be slow
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,"
" line %s, in %s" %
((hex(id(self)),)+
(os.path.split(caller_frame[1])[1],)+
(tuple(caller_frame[i] for i in range(2, 4)))
)
)
" line %s, in %s", hex(id(self)), c_code.co_filename,
c_frame.f_lineno, c_code.co_name)
self.env = env
self.db = db
self.txn = None