From 22c1d79eaeb655584c48b6cdb452a1015ca656a3 Mon Sep 17 00:00:00 2001 From: David Straub Date: Sat, 7 Nov 2020 11:00:52 +0100 Subject: [PATCH 1/2] Wrap inspect.stack in if __debug__ --- gramps/gen/db/dummydb.py | 19 ++++++++++--------- gramps/gen/db/utils.py | 18 +++++++++--------- gramps/gen/dbstate.py | 17 +++++++++-------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/gramps/gen/db/dummydb.py b/gramps/gen/db/dummydb.py index 79b702c58..1a556d2cc 100644 --- a/gramps/gen/db/dummydb.py +++ b/gramps/gen/db/dummydb.py @@ -157,15 +157,16 @@ def wrapper(method): This 'wrapped' method logs the original function that was called, and where it was called from. """ - class_name = args[0].__class__.__name__ - func_name = method.__name__ - 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, c_code.co_filename, c_frame.f_lineno, - c_code.co_name) - return method(*args, **keywargs) + if __debug__: + class_name = args[0].__class__.__name__ + func_name = method.__name__ + 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, c_code.co_filename, + c_frame.f_lineno, c_code.co_name) + return method(*args, **keywargs) return wrapped diff --git a/gramps/gen/db/utils.py b/gramps/gen/db/utils.py index ad9c87dc1..042425b68 100644 --- a/gramps/gen/db/utils.py +++ b/gramps/gen/db/utils.py @@ -71,15 +71,15 @@ def make_database(plugin_id): if mod: database = getattr(mod, pdata.databaseclass) db = database() - import inspect - 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)), c_code.co_filename, - c_frame.f_lineno, c_code.co_name) - + if __debug__: + import inspect + 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)), 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 6e748cafe..ee46778d3 100644 --- a/gramps/gen/dbstate.py +++ b/gramps/gen/dbstate.py @@ -85,14 +85,15 @@ class DbState(Callback): This replaces tests on DbState.open, DbState.db, DbState.db.is_open() and DbState.db.db_is_open all of which are deprecated. """ - class_name = self.__class__.__name__ - func_name = "is_open" - 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, c_code.co_filename, c_frame.f_lineno, - c_code.co_name) + if __debug__: + class_name = self.__class__.__name__ + func_name = "is_open" + 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, 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): From eb6ee86d09530b41469b0adfd50b5344c3d977d5 Mon Sep 17 00:00:00 2001 From: David Straub Date: Sat, 7 Nov 2020 11:03:50 +0100 Subject: [PATCH 2/2] Skip inspect.stack also when debug logging is disabled --- gramps/gen/db/dummydb.py | 2 +- gramps/gen/db/txn.py | 4 ++-- gramps/gen/db/utils.py | 2 +- gramps/gen/dbstate.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gramps/gen/db/dummydb.py b/gramps/gen/db/dummydb.py index 1a556d2cc..34b1fb02b 100644 --- a/gramps/gen/db/dummydb.py +++ b/gramps/gen/db/dummydb.py @@ -157,7 +157,7 @@ def wrapper(method): This 'wrapped' method logs the original function that was called, and where it was called from. """ - if __debug__: + if __debug__ and LOG.isEnabledFor(logging.DEBUG): class_name = args[0].__class__.__name__ func_name = method.__name__ frame = inspect.currentframe() diff --git a/gramps/gen/db/txn.py b/gramps/gen/db/txn.py index fe07e5390..e467a2100 100644 --- a/gramps/gen/db/txn.py +++ b/gramps/gen/db/txn.py @@ -77,7 +77,7 @@ class DbTxn(defaultdict): self.db.transaction_abort(self) elapsed_time = time.time() - self.start_time - if __debug__: + if __debug__ and _LOG.isEnabledFor(logging.DEBUG): frame = inspect.currentframe() c_frame = frame.f_back c_code = c_frame.f_code @@ -122,7 +122,7 @@ class DbTxn(defaultdict): """ # Conditional on __debug__ because all that frame stuff may be slow - if __debug__: + if __debug__ and _LOG.isEnabledFor(logging.DEBUG): caller_frame = inspect.stack()[1] # If the call comes from gramps.gen.db.generic.DbGenericTxn.__init__ # then it is just a dummy redirect, so we need to go back another diff --git a/gramps/gen/db/utils.py b/gramps/gen/db/utils.py index 042425b68..7df6c6ed8 100644 --- a/gramps/gen/db/utils.py +++ b/gramps/gen/db/utils.py @@ -71,7 +71,7 @@ def make_database(plugin_id): if mod: database = getattr(mod, pdata.databaseclass) db = database() - if __debug__: + if __debug__ and _LOG.isEnabledFor(logging.DEBUG): import inspect frame = inspect.currentframe() c_frame = frame.f_back diff --git a/gramps/gen/dbstate.py b/gramps/gen/dbstate.py index ee46778d3..ffe831e0c 100644 --- a/gramps/gen/dbstate.py +++ b/gramps/gen/dbstate.py @@ -85,7 +85,7 @@ class DbState(Callback): This replaces tests on DbState.open, DbState.db, DbState.db.is_open() and DbState.db.db_is_open all of which are deprecated. """ - if __debug__: + if __debug__ and _LOG.isEnabledFor(logging.DEBUG): class_name = self.__class__.__name__ func_name = "is_open" frame = inspect.currentframe()