Skip to content

Commit 0673c42

Browse files
chore: replace inspect.stack with sys._getframe (#1433)
1 parent f9f937a commit 0673c42

4 files changed

Lines changed: 34 additions & 45 deletions

File tree

packages/uipath-platform/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-platform"
3-
version = "0.0.19"
3+
version = "0.0.20"
44
description = "HTTP client library for programmatic access to UiPath Platform"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.11"

packages/uipath-platform/src/uipath/platform/common/_base_service.py

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import inspect
1+
import sys
2+
import types
23
from logging import getLogger
34
from typing import Any, Literal, Union
45

@@ -32,6 +33,33 @@
3233
platform_wait_strategy,
3334
)
3435

36+
_THIS_FILE = __file__
37+
_MAX_CALLER_FRAMES = 5
38+
39+
40+
def _get_caller_component() -> str:
41+
try:
42+
current: types.FrameType | None = sys._getframe(1)
43+
for _ in range(_MAX_CALLER_FRAMES):
44+
if current is None:
45+
break
46+
code = current.f_code
47+
if code.co_filename == _THIS_FILE:
48+
current = current.f_back
49+
continue
50+
# Skip frames from third-party libraries (e.g. tenacity)
51+
if "site-packages" in code.co_filename:
52+
current = current.f_back
53+
continue
54+
qualname = code.co_qualname
55+
if "." in qualname:
56+
parts = qualname.rsplit(".", 2)
57+
return f"{parts[-2]}.{parts[-1]}"
58+
current = current.f_back
59+
except Exception:
60+
pass
61+
return ""
62+
3563

3664
class BaseService:
3765
def __init__(
@@ -78,26 +106,7 @@ def request(
78106
self._logger.debug(f"Request: {method} {url}")
79107
self._logger.debug(f"HEADERS: {kwargs.get('headers', self._client.headers)}")
80108

81-
try:
82-
stack = inspect.stack()
83-
84-
# use the third frame because of the retry decorator
85-
caller_frame = stack[3].frame
86-
function_name = caller_frame.f_code.co_name
87-
88-
if "self" in caller_frame.f_locals:
89-
module_name = type(caller_frame.f_locals["self"]).__name__
90-
elif "cls" in caller_frame.f_locals:
91-
module_name = caller_frame.f_locals["cls"].__name__
92-
else:
93-
module_name = ""
94-
except Exception:
95-
function_name = ""
96-
module_name = ""
97-
98-
specific_component = (
99-
f"{module_name}.{function_name}" if module_name and function_name else ""
100-
)
109+
specific_component = _get_caller_component()
101110

102111
kwargs.setdefault("headers", {})
103112
kwargs["headers"][HEADER_USER_AGENT] = user_agent_value(specific_component)
@@ -181,24 +190,4 @@ def custom_headers(self) -> dict[str, str]:
181190

182191
@property
183192
def _specific_component(self) -> str:
184-
try:
185-
stack = inspect.stack()
186-
187-
caller_frame = stack[4].frame
188-
function_name = caller_frame.f_code.co_name
189-
190-
if "self" in caller_frame.f_locals:
191-
module_name = type(caller_frame.f_locals["self"]).__name__
192-
elif "cls" in caller_frame.f_locals:
193-
module_name = caller_frame.f_locals["cls"].__name__
194-
else:
195-
module_name = ""
196-
except Exception:
197-
function_name = ""
198-
module_name = ""
199-
200-
specific_component = (
201-
f"{module_name}.{function_name}" if module_name and function_name else ""
202-
)
203-
204-
return specific_component
193+
return _get_caller_component()

packages/uipath-platform/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/uipath/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)