Skip to content

Commit d8593b3

Browse files
committed
chore: replace inspect.stack with sys._getframe
1 parent 03b641c commit d8593b3

4 files changed

Lines changed: 111 additions & 60 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.18"
3+
version = "0.0.19"
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: 37 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,39 @@
3233
platform_wait_strategy,
3334
)
3435

36+
_THIS_FILE = __file__
37+
38+
39+
def _get_caller_component() -> str:
40+
try:
41+
current: types.FrameType | None = sys._getframe(1)
42+
while current is not None:
43+
# Skip frames from this module (_base_service.py)
44+
if current.f_code.co_filename == _THIS_FILE:
45+
current = current.f_back
46+
continue
47+
# Skip frames without self/cls or from third-party libraries
48+
# (e.g. tenacity retry decorator internals)
49+
locals_ = current.f_locals
50+
if "self" not in locals_ and "cls" not in locals_:
51+
current = current.f_back
52+
continue
53+
if "site-packages" in current.f_code.co_filename:
54+
current = current.f_back
55+
continue
56+
# Found a meaningful caller
57+
if "self" in locals_:
58+
module_name = type(locals_["self"]).__name__
59+
else:
60+
module_name = locals_["cls"].__name__
61+
function_name = current.f_code.co_name
62+
if module_name and function_name:
63+
return f"{module_name}.{function_name}"
64+
break
65+
except Exception:
66+
pass
67+
return ""
68+
3569

3670
class BaseService:
3771
def __init__(
@@ -78,26 +112,7 @@ def request(
78112
self._logger.debug(f"Request: {method} {url}")
79113
self._logger.debug(f"HEADERS: {kwargs.get('headers', self._client.headers)}")
80114

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-
)
115+
specific_component = _get_caller_component()
101116

102117
kwargs.setdefault("headers", {})
103118
kwargs["headers"][HEADER_USER_AGENT] = user_agent_value(specific_component)
@@ -181,24 +196,4 @@ def custom_headers(self) -> dict[str, str]:
181196

182197
@property
183198
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
199+
return _get_caller_component()

packages/uipath-platform/uv.lock

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/uipath/uv.lock

Lines changed: 49 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)