Skip to content

Commit e6c2bae

Browse files
committed
Fix line mix-up in python<=3.7
1 parent 6395eb5 commit e6c2bae

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

cloudpathlib/cloudpath.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import inspect
77
import os
88
from pathlib import Path, PosixPath, PurePosixPath, WindowsPath
9+
import sys
910
from textwrap import dedent
1011
from typing import Any, IO, Iterable, Optional, TYPE_CHECKING, Union
1112
from urllib.parse import urlparse
@@ -221,36 +222,41 @@ def __fspath__(self):
221222
frame = inspect.currentframe().f_back
222223

223224
# line number of the call for this frame
224-
lineno = inspect.getframeinfo(frame).lineno
225+
lineno = frame.f_lineno
225226

226227
# get source lines and start of the entire function
227228
lines, start_lineno = inspect.getsourcelines(frame)
228229

229-
# in some contexts, start_lineno is 0, but should be 1-indexed
230+
# in some contexts like jupyter, start_lineno is 0, but should be 1-indexed
230231
if start_lineno == 0:
231232
start_lineno = 1
232233

233-
# walk forward from this call until we find the line
234-
# that actually has "open" call on it
235-
if "open" not in lines[lineno - start_lineno]:
236-
lineno += 1
234+
all_lines = "".join(lines)
237235

238-
# 1-indexed line within this scope
239-
line_to_check = (lineno - start_lineno) + 1
236+
if "open" in all_lines:
237+
# walk from this call until we find the line
238+
# that actually has "open" call on it
239+
# only needed on Python <= 3.7
240+
if (sys.version_info.major, sys.version_info.minor) <= (3, 7):
241+
while "open" not in lines[lineno - start_lineno]:
242+
lineno -= 1
240243

241-
# Walk the AST of the previous frame source and see if we
242-
# ended up here from a call to the builtin open with and a writeable mode
243-
if any(
244-
_is_open_call_write_with_var(n, line_to_check)
245-
for n in ast.walk(ast.parse(dedent("".join(lines))))
246-
):
247-
raise BuiltInOpenWriteError(
248-
"Cannot use built-in open function with a CloudPath in a writeable mode. "
249-
"Changes would not be uploaded to the cloud; instead, "
250-
"please use the .open() method instead. "
251-
"NOTE: If you are sure and want to skip this check with "
252-
"set the env var CLOUDPATHLIB_CHECK_UNSAFE_OPEN=False"
253-
)
244+
# 1-indexed line within this scope
245+
line_to_check = (lineno - start_lineno) + 1
246+
247+
# Walk the AST of the previous frame source and see if we
248+
# ended up here from a call to the builtin open with and a writeable mode
249+
if any(
250+
_is_open_call_write_with_var(n, line_to_check)
251+
for n in ast.walk(ast.parse(dedent(all_lines)))
252+
):
253+
raise BuiltInOpenWriteError(
254+
"Cannot use built-in open function with a CloudPath in a writeable mode. "
255+
"Changes would not be uploaded to the cloud; instead, "
256+
"please use the .open() method instead. "
257+
"NOTE: If you are sure and want to skip this check with "
258+
"set the env var CLOUDPATHLIB_CHECK_UNSAFE_OPEN=False"
259+
)
254260

255261
if self.is_file():
256262
self._refresh_cache(force_overwrite_from_cloud=False)

0 commit comments

Comments
 (0)