Skip to content

Commit e5900ef

Browse files
authored
Merge branch 'main' into 132413-take2
2 parents 7225ec2 + 80295a8 commit e5900ef

File tree

14 files changed

+514
-135
lines changed

14 files changed

+514
-135
lines changed

Doc/using/cmdline.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ source.
7373

7474
.. audit-event:: cpython.run_command command cmdoption-c
7575

76+
.. versionchanged:: next
77+
*command* is automatically dedented before execution.
78+
7679
.. option:: -m <module-name>
7780

7881
Search :data:`sys.path` for the named module and execute its contents as

Doc/whatsnew/3.14.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ Other language changes
474474
explicitly overridden in the subclass.
475475
(Contributed by Tomasz Pytel in :gh:`132329`.)
476476

477+
* The command line option :option:`-c` now automatically dedents its code
478+
argument before execution. The auto-dedentation behavior mirrors
479+
:func:`textwrap.dedent`.
480+
(Contributed by Jon Crall and Steven Sun in :gh:`103998`.)
481+
482+
477483
.. _whatsnew314-pep765:
478484

479485
PEP 765: Disallow return/break/continue that exit a finally block

Include/internal/pycore_unicodeobject.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ extern Py_ssize_t _PyUnicode_InsertThousandsGrouping(
247247
Py_UCS4 *maxchar,
248248
int forward);
249249

250+
/* Dedent a string.
251+
Behaviour is expected to be an exact match of `textwrap.dedent`.
252+
Return a new reference on success, NULL with exception set on error.
253+
*/
254+
extern PyObject* _PyUnicode_Dedent(PyObject *unicode);
255+
250256
/* --- Misc functions ----------------------------------------------------- */
251257

252258
extern PyObject* _PyUnicode_FormatLong(PyObject *, int, int, int);

Lib/test/test_cmd_line.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
spawn_python, kill_python, assert_python_ok, assert_python_failure,
1818
interpreter_requires_environment
1919
)
20+
from textwrap import dedent
21+
2022

2123
if not support.has_subprocess_support:
2224
raise unittest.SkipTest("test module requires subprocess")
@@ -1051,6 +1053,88 @@ def test_int_max_str_digits(self):
10511053
)
10521054
self.assertEqual(res2int(res), (6000, 6000))
10531055

1056+
def test_cmd_dedent(self):
1057+
# test that -c auto-dedents its arguments
1058+
test_cases = [
1059+
(
1060+
"""
1061+
print('space-auto-dedent')
1062+
""",
1063+
"space-auto-dedent",
1064+
),
1065+
(
1066+
dedent(
1067+
"""
1068+
^^^print('tab-auto-dedent')
1069+
"""
1070+
).replace("^", "\t"),
1071+
"tab-auto-dedent",
1072+
),
1073+
(
1074+
dedent(
1075+
"""
1076+
^^if 1:
1077+
^^^^print('mixed-auto-dedent-1')
1078+
^^print('mixed-auto-dedent-2')
1079+
"""
1080+
).replace("^", "\t \t"),
1081+
"mixed-auto-dedent-1\nmixed-auto-dedent-2",
1082+
),
1083+
(
1084+
'''
1085+
data = """$
1086+
1087+
this data has an empty newline above and a newline with spaces below $
1088+
$
1089+
"""$
1090+
if 1: $
1091+
print(repr(data))$
1092+
'''.replace(
1093+
"$", ""
1094+
),
1095+
# Note: entirely blank lines are normalized to \n, even if they
1096+
# are part of a data string. This is consistent with
1097+
# textwrap.dedent behavior, but might not be intuitive.
1098+
"'\\n\\nthis data has an empty newline above and a newline with spaces below \\n\\n'",
1099+
),
1100+
(
1101+
'',
1102+
'',
1103+
),
1104+
(
1105+
' \t\n\t\n \t\t\t \t\t \t\n\t\t \n\n\n\t\t\t ',
1106+
'',
1107+
),
1108+
]
1109+
for code, expected in test_cases:
1110+
# Run the auto-dedent case
1111+
args1 = sys.executable, '-c', code
1112+
proc1 = subprocess.run(args1, stdout=subprocess.PIPE)
1113+
self.assertEqual(proc1.returncode, 0, proc1)
1114+
output1 = proc1.stdout.strip().decode(encoding='utf-8')
1115+
1116+
# Manually dedent beforehand, check the result is the same.
1117+
args2 = sys.executable, '-c', dedent(code)
1118+
proc2 = subprocess.run(args2, stdout=subprocess.PIPE)
1119+
self.assertEqual(proc2.returncode, 0, proc2)
1120+
output2 = proc2.stdout.strip().decode(encoding='utf-8')
1121+
1122+
self.assertEqual(output1, output2)
1123+
self.assertEqual(output1.replace('\r\n', '\n'), expected)
1124+
1125+
def test_cmd_dedent_failcase(self):
1126+
# Mixing tabs and spaces is not allowed
1127+
from textwrap import dedent
1128+
template = dedent(
1129+
'''
1130+
-+if 1:
1131+
+-++ print('will fail')
1132+
''')
1133+
code = template.replace('-', ' ').replace('+', '\t')
1134+
assert_python_failure('-c', code)
1135+
code = template.replace('-', '\t').replace('+', ' ')
1136+
assert_python_failure('-c', code)
1137+
10541138
def test_cpu_count(self):
10551139
code = "import os; print(os.cpu_count(), os.process_cpu_count())"
10561140
res = assert_python_ok('-X', 'cpu_count=4321', '-c', code)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
String arguments passed to "-c" are now automatically dedented as if by
2+
:func:`textwrap.dedent`. This allows "python -c" invocations to be indented
3+
in shell scripts without causing indentation errors. (Patch by Jon Crall and
4+
Steven Sun)

0 commit comments

Comments
 (0)