Skip to content

Commit b32aade

Browse files
Merge python#6
6: Add warnings for the print statement r=ltratt a=nanjekyejoannah A Py3k syntax warning has been added for the print statement. This PR should replace python#2 Co-authored-by: Joannah Nanjekye <[email protected]>
2 parents 739b5db + 6202a85 commit b32aade

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Lib/test/test_grammar.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,23 @@ def tellme(file=None):
458458
check_syntax_error(self, 'print ,')
459459
check_syntax_error(self, 'print >> x,')
460460

461+
def test_print_py3k_warnings(self):
462+
if sys.py3kwarning:
463+
with warnings.catch_warnings(record=True) as w:
464+
warnings.filterwarnings('always', category=Py3xWarning)
465+
print 1, 2, 3
466+
print 1, 2, 3,
467+
print
468+
469+
# 'print' '>>' test ','
470+
print >> sys.stdout, 1, 2, 3
471+
print >> sys.stdout, 1, 2, 3,
472+
print >> sys.stdout
473+
for warning in w:
474+
self.assertTrue(Py3xWarning is w.category)
475+
self.assertEqual(str(w.message), "print must be called as a function, not a statement in 3.x",
476+
"You can fix this now by using parentheses for arguments to 'print'")
477+
461478
def test_del_stmt(self):
462479
# 'del' exprlist
463480
abc = [1,2,3]

Python/ast.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,12 @@ ast_for_print_stmt(struct compiling *c, const node *n)
22942294
int i, j, values_count, start = 1;
22952295

22962296
REQ(n, print_stmt);
2297+
if (Py_Py3kWarningFlag && TYPE(CHILD(n, 1)) != LPAR) {
2298+
if (!ast_3x_warn(c, n,
2299+
"print must be called as a function, not a statement in 3.x",
2300+
"You can fix this now by using parentheses for arguments to 'print'"))
2301+
return NULL;
2302+
}
22972303
if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
22982304
dest = ast_for_expr(c, CHILD(n, 2));
22992305
if (!dest)

0 commit comments

Comments
 (0)