Skip to content

Commit 5a54002

Browse files
authored
Merge pull request #22 from Code-Partners/feat-enter-leave-method
Feat enter leave method
2 parents 67641f1 + 0138cff commit 5a54002

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

session/session.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,16 @@ def reset_call_stack(self, **kwargs) -> None:
315315
if self.is_on_level(level):
316316
self.__send_log_entry(level, None, LogEntryType.RESET_CALLSTACK, ViewerId.NO_VIEWER)
317317

318-
def enter_method(self, method_name: str, *args, **kwargs) -> None:
318+
def enter_method(self, method_name: str = "", *args, **kwargs) -> None:
319319
"""
320320
This method used to enter a method using default level or custom log level (if provided via kwargs).
321-
The resulting method name consists of the method_name string formatted using
322-
optional args and kwargs. The EnterMethod method notifies the Console
323-
that a new method has been entered. The Console includes the method in the
321+
If a method name string is provided via method_name argument, the resulting method name consists
322+
of the method_name string formatted using optional args and kwargs.
323+
If the default value is used (empty string) for the method name, SmartInspect will try
324+
to get the calling method name out of the stack together with the module name and line of code where
325+
this method is located.
326+
The enter_method() method notifies the Console that a new method has been entered.
327+
The Console includes the method in the
324328
method hierarchy. If this method is used consequently, a full call stack
325329
is visible in the console which helps in locating bugs in the source code.
326330
Please see the leave_method() method as the counter piece to enter_method().
@@ -343,18 +347,45 @@ def enter_method(self, method_name: str, *args, **kwargs) -> None:
343347
try:
344348
if not isinstance(method_name, str):
345349
raise TypeError('Method name must be a string')
346-
method_name = method_name.format(*args, **kwargs)
347-
348-
instance = kwargs.get("instance")
349-
if instance is not None:
350-
class_name = instance.__class__.__name__
351-
method_name = f"{class_name}.{method_name}"
350+
if method_name:
351+
method_name = method_name.format(*args, **kwargs)
352+
353+
instance = kwargs.get("instance")
354+
if instance is not None:
355+
class_name = instance.__class__.__name__
356+
method_name = f"{class_name}.{method_name}"
357+
else:
358+
method_name = self._get_method_name()
352359
except Exception as e:
353360
return self.__process_internal_error(e)
354361

355362
self.__send_log_entry(level, method_name, LogEntryType.ENTER_METHOD, ViewerId.TITLE)
356363
self.__send_process_flow(level, method_name, ProcessFlowType.ENTER_METHOD)
357364

365+
# noinspection PyBroadException
366+
@staticmethod
367+
def _get_method_name() -> str:
368+
method_name = "<Unknown>"
369+
370+
try:
371+
stack_frame = inspect.stack()[2]
372+
if stack_frame is None:
373+
return method_name
374+
375+
# extract the parts of the stack frame.
376+
filepath, line, func_name = stack_frame[1:4]
377+
method_name = func_name.strip()
378+
module_name = os.path.basename(filepath)
379+
380+
# add source position to method name.
381+
if module_name is not None:
382+
method_name += " ({0}, line {1})".format(module_name, line)
383+
384+
return method_name
385+
386+
except Exception:
387+
return method_name
388+
358389
def __process_internal_error(self, e: Exception) -> None:
359390
tb = e.__traceback__
360391
calling_method_name = traceback.extract_tb(tb)[-1].name
@@ -372,11 +403,15 @@ def __get_level(self, **kwargs):
372403

373404
return level
374405

375-
def leave_method(self, method_name: str, *args, **kwargs) -> None:
406+
def leave_method(self, method_name: str = "", *args, **kwargs) -> None:
376407
"""
377408
Leaves a method by using default level or custom log level (if provided via kwargs).
378-
The resulting method name consists of the method_name string formatted using
379-
optional args and kwargs. The leave_method() method notifies the Console that a method
409+
If a method name string is provided via method_name argument, the resulting method name consists
410+
of the method_name string formatted using optional args and kwargs.
411+
If the default value is used (empty string) for the method name, SmartInspect will try
412+
to get the calling method name out of the stack together with the module name and line of code where
413+
this method is located.
414+
The leave_method() method notifies the Console that a method
380415
has been left. The Console closes the current method in the method hierarchy. If this method is used
381416
consequently, a full call stack is visible in the Console which helps locate bugs in the source code.
382417
Please see the enter_method() method as the counter piece to leave_method().
@@ -397,11 +432,14 @@ def leave_method(self, method_name: str, *args, **kwargs) -> None:
397432
try:
398433
if not isinstance(method_name, str):
399434
raise TypeError('Method name must be a string')
400-
method_name = method_name.format(*args, **kwargs)
401-
instance = kwargs.get("instance")
402-
if instance is not None:
403-
class_name = instance.__class__.__name__
404-
method_name = f"{class_name}.{method_name}"
435+
if method_name:
436+
method_name = method_name.format(*args, **kwargs)
437+
instance = kwargs.get("instance")
438+
if instance is not None:
439+
class_name = instance.__class__.__name__
440+
method_name = f"{class_name}.{method_name}"
441+
else:
442+
method_name = self._get_method_name()
405443
except Exception as e:
406444
return self.__process_internal_error(e)
407445

0 commit comments

Comments
 (0)