Skip to content

Commit 68e30cd

Browse files
authored
Use traceback.extract_stack() instead of inspect.stack() to check upper stack method, they are at same functionality, but the traceback(5μs) is 50x faster than inspect.stack(2ms). This method affects many common operation, such as add() and from_timestamp(tz), 2ms delay typically cause performance issues. (#716)
1 parent 32a36f4 commit 68e30cd

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

pendulum/datetime.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import calendar
44
import datetime
55
import sys
6+
import traceback
67

78
from typing import TYPE_CHECKING
89
from typing import Any
@@ -1218,9 +1219,7 @@ def __add__(self, other: datetime.timedelta) -> Self:
12181219
# This is a workaround for Python 3.8+
12191220
# since calling astimezone() will call this method
12201221
# instead of the base datetime class one.
1221-
import inspect
1222-
1223-
caller = inspect.stack()[1][3]
1222+
caller = traceback.extract_stack(limit=2)[0].name
12241223
if caller == "astimezone":
12251224
return super().__add__(other)
12261225

0 commit comments

Comments
 (0)