Skip to content

Commit

Permalink
rosgraph: update code from Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
jspricke committed Nov 13, 2022
1 parent dd78ac8 commit 685a96e
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions tools/rosgraph/src/rosgraph/roslogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,58 @@
class LoggingException(Exception): pass

class RospyLogger(logging.getLoggerClass()):
def findCaller(self, *args, **kwargs):
# copied from python3.11/logging/__init__.py
# _srcfile is only used in conjunction with sys._getframe().
# Setting _srcfile to None will prevent findCaller() from being called. This
# way, you can avoid the overhead of fetching caller information.

# The following is based on warnings._is_internal_frame. It makes sure that
# frames of the import mechanism are skipped when logging at module level and
# using a stacklevel value greater than one.
@staticmethod
def _is_internal_frame(frame):
"""Signal whether the frame is a CPython or logging module internal."""
filename = os.path.normcase(frame.f_code.co_filename)
return filename == logging._srcfile or (
"importlib" in filename and "_bootstrap" in filename
)


def findCaller(self, stack_info=False, stacklevel=1):
"""
Find the stack frame of the caller so that we can note the source
file name, line number, and function name with class name if possible.
"""
file_name, lineno, func_name = super(RospyLogger, self).findCaller(*args, **kwargs)[:3]
file_name = os.path.normcase(file_name)

f = inspect.currentframe()
if f is not None:
f = f.f_back
while hasattr(f, "f_code"):
# Search for the right frame using the data already found by parent class.
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename == file_name and f.f_lineno == lineno and co.co_name == func_name:


f = logging.currentframe()
#On some versions of IronPython, currentframe() returns None if
#IronPython isn't run with -X:Frames.
if f is None:
return "(unknown file)", 0, "(unknown function)", None
while stacklevel > 0:
next_f = f.f_back
if next_f is None:
## We've got options here.
## If we want to use the last (deepest) frame:
break
if f.f_back:
f = f.f_back

# Jump up two more frames, as the logger methods have been double wrapped.
if f is not None and f.f_back and f.f_code and f.f_code.co_name == '_base_logger':
f = f.f_back
if f.f_back:
f = f.f_back
## If we want to mimic the warnings module:
#return ("sys", 1, "(unknown function)", None)
## If we want to be pedantic:
#raise ValueError("call stack is not deep enough")
f = next_f
if not RospyLogger._is_internal_frame(f):
stacklevel -= 1
co = f.f_code
sinfo = None
if stack_info:
with io.StringIO() as sio:
sio.write("Stack (most recent call last):\n")
traceback.print_stack(f, file=sio)
sinfo = sio.getvalue()
if sinfo[-1] == '\n':
sinfo = sinfo[:-1]

func_name = co.co_name

# Now extend the function name with class name, if available.
Expand All @@ -85,11 +111,7 @@ def findCaller(self, *args, **kwargs):
except KeyError: # if the function is unbound, there is no self.
pass

if sys.version_info > (3, 2):
# Dummy last argument to match Python3 return type
return co.co_filename, f.f_lineno, func_name, None
else:
return co.co_filename, f.f_lineno, func_name
return co.co_filename, f.f_lineno, func_name, sinfo

logging.setLoggerClass(RospyLogger)

Expand Down

0 comments on commit 685a96e

Please sign in to comment.