Skip to content

Commit 651b013

Browse files
SinaChavoshiTensorflow Cloud maintainers
authored andcommitted
Covers a few changes for handling special characters:
1. Throw an exception for % instead of silently removing lines that start with %. 2. Run lines starting with ! as subprocess. 3. Do not remove comments PiperOrigin-RevId: 384721952
1 parent d91eea9 commit 651b013

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/python/tensorflow_cloud/core/preprocess.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,19 @@ def get_preprocessed_entry_point(
213213
# Remove any iPython special commands and add the python code
214214
# to script_lines.
215215
for line in py_content:
216+
if line.strip().startswith("%"):
217+
raise RuntimeError("Magic commands '%' are not supported.")
218+
219+
elif line.strip().startswith("!"):
220+
commands_list = line.strip()[1:].split(" ")
221+
script_lines.extend([
222+
"import sys\n",
223+
"import subprocess\n",
224+
f"print(subprocess.run({commands_list}",
225+
",capture_output=True, text=True).stdout)\n"
226+
])
227+
216228
if not (
217-
line.strip().startswith("!") or
218-
line.strip().startswith("%") or
219-
line.strip().startswith("#") or
220229
line.strip().startswith("get_ipython().system(")
221230
):
222231
script_lines.append(line)

0 commit comments

Comments
 (0)