MCPcopy
hub / github.com/pydata/xarray / find_stack_level

Function find_stack_level

xarray/core/utils.py:1215–1262  ·  view source on GitHub ↗

Find the first place in the stack that is not inside xarray or the Python standard library. This is unless the code emanates from a test, in which case we would prefer to see the xarray source. This function is taken from pandas and modified to exclude standard library paths. Para

(test_mode=False)

Source from the content-addressed store, hash-verified

1213
1214
1215def find_stack_level(test_mode=False) -> int:
1216 """Find the first place in the stack that is not inside xarray or the Python standard library.
1217
1218 This is unless the code emanates from a test, in which case we would prefer
1219 to see the xarray source.
1220
1221 This function is taken from pandas and modified to exclude standard library paths.
1222
1223 Parameters
1224 ----------
1225 test_mode : bool
1226 Flag used for testing purposes to switch off the detection of test
1227 directories in the stack trace.
1228
1229 Returns
1230 -------
1231 stacklevel : int
1232 First level in the stack that is not part of xarray or the Python standard library.
1233 """
1234 import xarray as xr
1235
1236 pkg_dir = Path(xr.__file__).parent
1237 test_dir = pkg_dir / "tests"
1238
1239 std_lib_init = sys.modules["os"].__file__
1240 # Mostly to appease mypy; I don't think this can happen...
1241 if std_lib_init is None:
1242 return 0
1243
1244 std_lib_dir = Path(std_lib_init).parent
1245
1246 frame = inspect.currentframe()
1247 n = 0
1248 while frame:
1249 fname = inspect.getfile(frame)
1250 if (
1251 fname.startswith(str(pkg_dir))
1252 and (not fname.startswith(str(test_dir)) or test_mode)
1253 ) or (
1254 fname.startswith(str(std_lib_dir))
1255 and "site-packages" not in fname
1256 and "dist-packages" not in fname
1257 ):
1258 frame = frame.f_back
1259 n += 1
1260 else:
1261 break
1262 return n
1263
1264
1265def emit_user_level_warning(message, category=None) -> None:

Callers 1

emit_user_level_warningFunction · 0.85

Calls 1

startswithMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…