Skip to content

bpo-34084: Fix setting an error message for the "Barry as BDFL" easter egg. #8262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions Lib/test/test_flufl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
class FLUFLTests(unittest.TestCase):

def test_barry_as_bdfl(self):
code = "from __future__ import barry_as_FLUFL; 2 {0} 3"
code = "from __future__ import barry_as_FLUFL\n2 {0} 3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with compile. My question is when flags is passed, what's the meaning of the import here? Is it a must?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is redundant if flags is passed.

The bug is that this tests is failed if not pass flags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know flags have to be passed. I just don't understand why not simply '2 {0} 3'?.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention of the test is that from __future__ import barry_as_FLUFL should change grammar. But this doesn't work, and we need to pass flags as a temporary workaround. After fixing this bug we could remove flags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha. I wrongly thought the from __future__ import ... doesn't work in compile and flags must be passed.

compile(code.format('<>'), '<BDFL test>', 'exec',
__future__.CO_FUTURE_BARRY_AS_BDFL)
self.assertRaises(SyntaxError, compile, code.format('!='),
'<FLUFL test>', 'exec',
__future__.CO_FUTURE_BARRY_AS_BDFL)
with self.assertRaises(SyntaxError) as cm:
compile(code.format('!='), '<FLUFL test>', 'exec',
__future__.CO_FUTURE_BARRY_AS_BDFL)
self.assertRegex(str(cm.exception),
"with Barry as BDFL, use '<>' instead of '!='")
self.assertEqual(cm.exception.text, '2 != 3\n')
self.assertEqual(cm.exception.filename, '<FLUFL test>')
self.assertEqual(cm.exception.lineno, 2)
self.assertEqual(cm.exception.offset, 4)

def test_guido_as_bdfl(self):
code = '2 {0} 3'
compile(code.format('!='), '<BDFL test>', 'exec')
self.assertRaises(SyntaxError, compile, code.format('<>'),
'<FLUFL test>', 'exec')
with self.assertRaises(SyntaxError) as cm:
compile(code.format('<>'), '<FLUFL test>', 'exec')
self.assertRegex(str(cm.exception), "invalid syntax")
self.assertEqual(cm.exception.text, '2 <> 3\n')
self.assertEqual(cm.exception.filename, '<FLUFL test>')
self.assertEqual(cm.exception.lineno, 1)
self.assertEqual(cm.exception.offset, 4)


if __name__ == '__main__':
Expand Down
3 changes: 1 addition & 2 deletions Parser/parsetok.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
strcmp(str, "<>")) {
PyObject_FREE(str);
err_ret->text = "with Barry as BDFL, use '<>' "
"instead of '!='";
err_ret->expected = NOTEQUAL;
err_ret->error = E_SYNTAX;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,10 @@ err_input(perrdetail *err)
msg = "unexpected indent";
else if (err->token == DEDENT)
msg = "unexpected unindent";
else if (err->expected == NOTEQUAL) {
errtype = PyExc_SyntaxError;
msg = "with Barry as BDFL, use '<>' instead of '!='";
}
else {
errtype = PyExc_SyntaxError;
msg = "invalid syntax";
Expand Down