Skip to content

Commit 942a3b9

Browse files
author
MarcoGorelli
committed
revert caching stacklevel
1 parent f2a91a0 commit 942a3b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+336
-470
lines changed

doc/source/development/contributing_codebase.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Otherwise, you need to do it manually:
139139
warnings.warn(
140140
'Use new_func instead.',
141141
FutureWarning,
142-
stacklevel=find_stack_level(inspect.currentframe()),
142+
stacklevel=find_stack_level(),
143143
)
144144
new_func()
145145

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Fixed regressions
8686
- Fixed regression in :meth:`DataFrame.apply` when passing non-zero ``axis`` via keyword argument (:issue:`48656`)
8787
- Fixed regression in :meth:`Series.groupby` and :meth:`DataFrame.groupby` when the grouper is a nullable data type (e.g. :class:`Int64`) or a PyArrow-backed string array, contains null values, and ``dropna=False`` (:issue:`48794`)
8888
- Fixed regression in :class:`ExcelWriter` where the ``book`` attribute could no longer be set; however setting this attribute is now deprecated and this ability will be removed in a future version of pandas (:issue:`48780`)
89+
- Fixed regression causing memory leak when finding stacklevel when emitting warnings (:issue:`49052`)
8990

9091
.. ---------------------------------------------------------------------------
9192

pandas/_config/config.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
ContextDecorator,
5555
contextmanager,
5656
)
57-
import inspect
5857
import re
5958
from typing import (
6059
Any,
@@ -662,7 +661,7 @@ def _warn_if_deprecated(key: str) -> bool:
662661
warnings.warn(
663662
d.msg,
664663
FutureWarning,
665-
stacklevel=find_stack_level(inspect.currentframe()),
664+
stacklevel=find_stack_level(),
666665
)
667666
else:
668667
msg = f"'{key}' is deprecated"
@@ -673,9 +672,7 @@ def _warn_if_deprecated(key: str) -> bool:
673672
else:
674673
msg += ", please refrain from using it."
675674

676-
warnings.warn(
677-
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
678-
)
675+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
679676
return True
680677
return False
681678

pandas/_libs/parsers.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ cdef class TextReader:
972972
"Defining usecols with out of bounds indices is deprecated "
973973
"and will raise a ParserError in a future version.",
974974
FutureWarning,
975-
stacklevel=find_stack_level(inspect.currentframe()),
975+
stacklevel=find_stack_level(),
976976
)
977977

978978
results = {}
@@ -1023,7 +1023,7 @@ cdef class TextReader:
10231023
warnings.warn((f"Both a converter and dtype were specified "
10241024
f"for column {name} - only the converter will "
10251025
f"be used."), ParserWarning,
1026-
stacklevel=find_stack_level(inspect.currentframe()))
1026+
stacklevel=find_stack_level())
10271027
results[i] = _apply_converter(conv, self.parser, i, start, end)
10281028
continue
10291029

pandas/_libs/tslib.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
cimport cython
@@ -845,7 +844,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
845844
"deprecated. In a future version, this will match Timestamp('now') "
846845
"and Timestamp.now()",
847846
FutureWarning,
848-
stacklevel=find_stack_level(inspect.currentframe()),
847+
stacklevel=find_stack_level(),
849848
)
850849

851850
return True

pandas/_libs/tslibs/conversion.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
import numpy as np
@@ -275,7 +274,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
275274
"Conversion of non-round float with unit={unit} is ambiguous "
276275
"and will raise in a future version.",
277276
FutureWarning,
278-
stacklevel=find_stack_level(inspect.currentframe()),
277+
stacklevel=find_stack_level(),
279278
)
280279

281280
ts = cast_from_unit(ts, unit)

pandas/_libs/tslibs/nattype.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
from pandas.util._exceptions import find_stack_level
@@ -134,7 +133,7 @@ cdef class _NaT(datetime):
134133
"order to match the standard library behavior. "
135134
"In a future version these will be considered non-comparable.",
136135
FutureWarning,
137-
stacklevel=find_stack_level(inspect.currentframe()),
136+
stacklevel=find_stack_level(),
138137
)
139138
return False
140139

@@ -378,7 +377,7 @@ class NaTType(_NaT):
378377
warnings.warn(
379378
"NaT.freq is deprecated and will be removed in a future version.",
380379
FutureWarning,
381-
stacklevel=find_stack_level(inspect.currentframe()),
380+
stacklevel=find_stack_level(),
382381
)
383382
return None
384383

pandas/_libs/tslibs/offsets.pyx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import re
32
import time
43
import warnings
@@ -501,7 +500,7 @@ cdef class BaseOffset:
501500
"DateOffset.__call__ is deprecated and will be removed in a future "
502501
"version. Use `offset + other` instead.",
503502
FutureWarning,
504-
stacklevel=find_stack_level(inspect.currentframe()),
503+
stacklevel=find_stack_level(),
505504
)
506505
return self._apply(other)
507506

@@ -511,7 +510,7 @@ cdef class BaseOffset:
511510
f"{type(self).__name__}.apply is deprecated and will be removed "
512511
"in a future version. Use `offset + other` instead",
513512
FutureWarning,
514-
stacklevel=find_stack_level(inspect.currentframe()),
513+
stacklevel=find_stack_level(),
515514
)
516515
return self._apply(other)
517516

@@ -825,15 +824,15 @@ cdef class BaseOffset:
825824
warnings.warn(
826825
"onOffset is a deprecated, use is_on_offset instead.",
827826
FutureWarning,
828-
stacklevel=find_stack_level(inspect.currentframe()),
827+
stacklevel=find_stack_level(),
829828
)
830829
return self.is_on_offset(dt)
831830

832831
def isAnchored(self) -> bool:
833832
warnings.warn(
834833
"isAnchored is a deprecated, use is_anchored instead.",
835834
FutureWarning,
836-
stacklevel=find_stack_level(inspect.currentframe()),
835+
stacklevel=find_stack_level(),
837836
)
838837
return self.is_anchored()
839838

pandas/_libs/tslibs/parsing.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Parsing functions for datetime and datetime-like strings.
33
"""
4-
import inspect
54
import re
65
import time
76
import warnings
@@ -218,15 +217,15 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
218217
format='MM/DD/YYYY',
219218
dayfirst='True',
220219
),
221-
stacklevel=find_stack_level(inspect.currentframe()),
220+
stacklevel=find_stack_level(),
222221
)
223222
elif not dayfirst and swapped_day_and_month:
224223
warnings.warn(
225224
PARSING_WARNING_MSG.format(
226225
format='DD/MM/YYYY',
227226
dayfirst='False (the default)',
228227
),
229-
stacklevel=find_stack_level(inspect.currentframe()),
228+
stacklevel=find_stack_level(),
230229
)
231230
# In Python <= 3.6.0 there is no range checking for invalid dates
232231
# in C api, thus we call faster C version for 3.6.1 or newer

pandas/_libs/tslibs/period.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import warnings
32

43
from pandas.util._exceptions import find_stack_level
@@ -1830,7 +1829,7 @@ cdef class _Period(PeriodMixin):
18301829
"be removed in a future version. Use "
18311830
"`per.to_timestamp(...).tz_localize(tz)` instead.",
18321831
FutureWarning,
1833-
stacklevel=find_stack_level(inspect.currentframe()),
1832+
stacklevel=find_stack_level(),
18341833
)
18351834

18361835
how = validate_end_alias(how)

pandas/_libs/tslibs/timedeltas.pyx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import collections
2-
import inspect
32
import warnings
43

54
from pandas.util._exceptions import find_stack_level
@@ -687,7 +686,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
687686
"Units 'M', 'Y' and 'y' do not represent unambiguous "
688687
"timedelta values and will be removed in a future version.",
689688
FutureWarning,
690-
stacklevel=find_stack_level(inspect.currentframe()),
689+
stacklevel=find_stack_level(),
691690
)
692691

693692
if unit == 'M':
@@ -1066,7 +1065,7 @@ cdef class _Timedelta(timedelta):
10661065
warnings.warn(
10671066
"Timedelta.freq is deprecated and will be removed in a future version",
10681067
FutureWarning,
1069-
stacklevel=find_stack_level(inspect.currentframe()),
1068+
stacklevel=find_stack_level(),
10701069
)
10711070
return None
10721071

@@ -1082,7 +1081,7 @@ cdef class _Timedelta(timedelta):
10821081
warnings.warn(
10831082
"Timedelta.is_populated is deprecated and will be removed in a future version",
10841083
FutureWarning,
1085-
stacklevel=find_stack_level(inspect.currentframe()),
1084+
stacklevel=find_stack_level(),
10861085
)
10871086
return self._is_populated
10881087

@@ -1289,7 +1288,7 @@ cdef class _Timedelta(timedelta):
12891288
warnings.warn(
12901289
"Timedelta.delta is deprecated and will be removed in a future version.",
12911290
FutureWarning,
1292-
stacklevel=find_stack_level(inspect.currentframe()),
1291+
stacklevel=find_stack_level(),
12931292
)
12941293
return self.value
12951294

pandas/_libs/tslibs/timestamps.pyx

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ construction requirements, we need to do object instantiation in python
66
(see Timestamp class below). This will serve as a C extension type that
77
shadows the python class, where we do any heavy lifting.
88
"""
9-
import inspect
109
import warnings
1110

1211
cimport cython
@@ -253,7 +252,7 @@ cdef class _Timestamp(ABCTimestamp):
253252
warnings.warn(
254253
"Timestamp.freq is deprecated and will be removed in a future version.",
255254
FutureWarning,
256-
stacklevel=find_stack_level(inspect.currentframe()),
255+
stacklevel=find_stack_level(),
257256
)
258257
return self._freq
259258

@@ -372,7 +371,7 @@ cdef class _Timestamp(ABCTimestamp):
372371
"In a future version these will be considered non-comparable. "
373372
"Use 'ts == pd.Timestamp(date)' or 'ts.date() == date' instead.",
374373
FutureWarning,
375-
stacklevel=find_stack_level(inspect.currentframe()),
374+
stacklevel=find_stack_level(),
376375
)
377376
return NotImplemented
378377
else:
@@ -663,7 +662,7 @@ cdef class _Timestamp(ABCTimestamp):
663662
"version. When you have a freq, use "
664663
f"freq.{field}(timestamp) instead.",
665664
FutureWarning,
666-
stacklevel=find_stack_level(inspect.currentframe()),
665+
stacklevel=find_stack_level(),
667666
)
668667

669668
@property
@@ -1169,7 +1168,7 @@ cdef class _Timestamp(ABCTimestamp):
11691168
"""
11701169
if self.nanosecond != 0 and warn:
11711170
warnings.warn("Discarding nonzero nanoseconds in conversion.",
1172-
UserWarning, stacklevel=find_stack_level(inspect.currentframe()))
1171+
UserWarning, stacklevel=find_stack_level())
11731172

11741173
return datetime(self.year, self.month, self.day,
11751174
self.hour, self.minute, self.second,
@@ -1248,7 +1247,7 @@ cdef class _Timestamp(ABCTimestamp):
12481247
warnings.warn(
12491248
"Converting to Period representation will drop timezone information.",
12501249
UserWarning,
1251-
stacklevel=find_stack_level(inspect.currentframe()),
1250+
stacklevel=find_stack_level(),
12521251
)
12531252

12541253
if freq is None:
@@ -1257,7 +1256,7 @@ cdef class _Timestamp(ABCTimestamp):
12571256
"In a future version, calling 'Timestamp.to_period()' without "
12581257
"passing a 'freq' will raise an exception.",
12591258
FutureWarning,
1260-
stacklevel=find_stack_level(inspect.currentframe()),
1259+
stacklevel=find_stack_level(),
12611260
)
12621261

12631262
return Period(self, freq=freq)
@@ -1449,7 +1448,7 @@ class Timestamp(_Timestamp):
14491448
"Timestamp.utcfromtimestamp(ts).tz_localize(None). "
14501449
"To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')",
14511450
FutureWarning,
1452-
stacklevel=find_stack_level(inspect.currentframe()),
1451+
stacklevel=find_stack_level(),
14531452
)
14541453
return cls(datetime.utcfromtimestamp(ts))
14551454

@@ -1685,7 +1684,7 @@ class Timestamp(_Timestamp):
16851684
"as a wall time, not a UTC time. To interpret as a UTC time, "
16861685
"use `Timestamp(dt64).tz_localize('UTC').tz_convert(tz)`",
16871686
FutureWarning,
1688-
stacklevel=find_stack_level(inspect.currentframe()),
1687+
stacklevel=find_stack_level(),
16891688
)
16901689
# Once this deprecation is enforced, we can do
16911690
# return Timestamp(ts_input).tz_localize(tzobj)
@@ -1708,7 +1707,7 @@ class Timestamp(_Timestamp):
17081707
"The 'freq' argument in Timestamp is deprecated and will be "
17091708
"removed in a future version.",
17101709
FutureWarning,
1711-
stacklevel=find_stack_level(inspect.currentframe()),
1710+
stacklevel=find_stack_level(),
17121711
)
17131712
if not is_offset_object(freq):
17141713
freq = to_offset(freq)
@@ -2044,7 +2043,7 @@ timedelta}, default 'raise'
20442043
warnings.warn(
20452044
"Timestamp.freqstr is deprecated and will be removed in a future version.",
20462045
FutureWarning,
2047-
stacklevel=find_stack_level(inspect.currentframe()),
2046+
stacklevel=find_stack_level(),
20482047
)
20492048
return self._freqstr
20502049

pandas/_testing/asserters.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import inspect
43
from typing import (
54
Literal,
65
cast,
@@ -113,7 +112,7 @@ def assert_almost_equal(
113112
"is deprecated and will be removed in a future version. "
114113
"You can stop passing 'check_less_precise' to silence this warning.",
115114
FutureWarning,
116-
stacklevel=find_stack_level(inspect.currentframe()),
115+
stacklevel=find_stack_level(),
117116
)
118117
rtol = atol = _get_tol_from_less_precise(check_less_precise)
119118

@@ -340,7 +339,7 @@ def _get_ilevel_values(index, level):
340339
"is deprecated and will be removed in a future version. "
341340
"You can stop passing 'check_less_precise' to silence this warning.",
342341
FutureWarning,
343-
stacklevel=find_stack_level(inspect.currentframe()),
342+
stacklevel=find_stack_level(),
344343
)
345344
rtol = atol = _get_tol_from_less_precise(check_less_precise)
346345

@@ -830,7 +829,7 @@ def assert_extension_array_equal(
830829
"is deprecated and will be removed in a future version. "
831830
"You can stop passing 'check_less_precise' to silence this warning.",
832831
FutureWarning,
833-
stacklevel=find_stack_level(inspect.currentframe()),
832+
stacklevel=find_stack_level(),
834833
)
835834
rtol = atol = _get_tol_from_less_precise(check_less_precise)
836835

@@ -985,7 +984,7 @@ def assert_series_equal(
985984
"is deprecated and will be removed in a future version. "
986985
"You can stop passing 'check_less_precise' to silence this warning.",
987986
FutureWarning,
988-
stacklevel=find_stack_level(inspect.currentframe()),
987+
stacklevel=find_stack_level(),
989988
)
990989
rtol = atol = _get_tol_from_less_precise(check_less_precise)
991990

@@ -1278,7 +1277,7 @@ def assert_frame_equal(
12781277
"is deprecated and will be removed in a future version. "
12791278
"You can stop passing 'check_less_precise' to silence this warning.",
12801279
FutureWarning,
1281-
stacklevel=find_stack_level(inspect.currentframe()),
1280+
stacklevel=find_stack_level(),
12821281
)
12831282
rtol = atol = _get_tol_from_less_precise(check_less_precise)
12841283

0 commit comments

Comments
 (0)