From b38f77f2aa5ca896d1008450330769f7386e2a2f Mon Sep 17 00:00:00 2001 From: John Ralls Date: Mon, 14 Sep 2020 09:08:58 -0700 Subject: [PATCH] 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. --- gramps/gen/db/dummydb.py | 9 +++++---- gramps/gen/db/txn.py | 14 ++++++-------- gramps/gen/db/utils.py | 12 +++++++----- gramps/gen/dbstate.py | 9 +++++---- .../gen/filters/rules/test/person_rules_test.py | 3 ++- gramps/gen/utils/callback.py | 16 ++++++++++------ gramps/plugins/db/bsddb/bsddbtxn.py | 14 ++++++-------- 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/gramps/gen/db/dummydb.py b/gramps/gen/db/dummydb.py index 89c93ab69..63f904296 100644 --- a/gramps/gen/db/dummydb.py +++ b/gramps/gen/db/dummydb.py @@ -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 diff --git a/gramps/gen/db/txn.py b/gramps/gen/db/txn.py index 52620c6dd..fe07e5390 100644 --- a/gramps/gen/db/txn.py +++ b/gramps/gen/db/txn.py @@ -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 diff --git a/gramps/gen/db/utils.py b/gramps/gen/db/utils.py index b5a1336f7..2222a3d1f 100644 --- a/gramps/gen/db/utils.py +++ b/gramps/gen/db/utils.py @@ -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) diff --git a/gramps/gen/dbstate.py b/gramps/gen/dbstate.py index 21e7e4a5b..6e748cafe 100644 --- a/gramps/gen/dbstate.py +++ b/gramps/gen/dbstate.py @@ -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): diff --git a/gramps/gen/filters/rules/test/person_rules_test.py b/gramps/gen/filters/rules/test/person_rules_test.py index e7997b025..737dda05a 100644 --- a/gramps/gen/filters/rules/test/person_rules_test.py +++ b/gramps/gen/filters/rules/test/person_rules_test.py @@ -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) diff --git a/gramps/gen/utils/callback.py b/gramps/gen/utils/callback.py index b2f801cd7..a3f94e444 100644 --- a/gramps/gen/utils/callback.py +++ b/gramps/gen/utils/callback.py @@ -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: diff --git a/gramps/plugins/db/bsddb/bsddbtxn.py b/gramps/plugins/db/bsddb/bsddbtxn.py index 360c8b7dd..7af99494e 100644 --- a/gramps/plugins/db/bsddb/bsddbtxn.py +++ b/gramps/plugins/db/bsddb/bsddbtxn.py @@ -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