diff --git a/test/bytecode_2.6/05_with.pyc b/test/bytecode_2.6/05_with.pyc index 7f3c025f..3fd9ff60 100644 Binary files a/test/bytecode_2.6/05_with.pyc and b/test/bytecode_2.6/05_with.pyc differ diff --git a/test/simple_source/stmts/05_with.py b/test/simple_source/stmts/05_with.py index 74706b8f..6f52fbdb 100644 --- a/test/simple_source/stmts/05_with.py +++ b/test/simple_source/stmts/05_with.py @@ -1,3 +1,5 @@ from __future__ import with_statement -with (sys) as f: +with open(__file__, 'r') as f: print(f) + with f: + pass diff --git a/uncompyle6/parsers/parse25.py b/uncompyle6/parsers/parse25.py index 93dd342b..ef3343db 100644 --- a/uncompyle6/parsers/parse25.py +++ b/uncompyle6/parsers/parse25.py @@ -21,8 +21,13 @@ class Python25Parser(Python26Parser): # Python 2.6 uses ROT_TWO instead of the STORE_xxx # withas is allowed as a "from future" in 2.5 + # 2.6 and 2.7 do something slightly different setupwithas ::= DUP_TOP LOAD_ATTR store LOAD_ATTR CALL_FUNCTION_0 setup_finally + # opcode SETUP_WITH + setupwith ::= DUP_TOP LOAD_ATTR STORE_NAME LOAD_ATTR CALL_FUNCTION_0 POP_TOP + withstmt ::= expr setupwith SETUP_FINALLY suite_stmts_opt + POP_BLOCK LOAD_CONST COME_FROM with_cleanup store ::= STORE_FAST store ::= STORE_NAME @@ -41,6 +46,14 @@ class Python25Parser(Python26Parser): """ def add_custom_rules(self, tokens, customize): + # grammar rules inherited from Python 2.6 + self.remove_rules(""" + setupwith ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 POP_TOP + withstmt ::= expr setupwith SETUP_FINALLY suite_stmts_opt + POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY + withasstmt ::= expr setupwithas designator suite_stmts_opt + POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY + """) super(Python25Parser, self).add_custom_rules(tokens, customize) if self.version == 2.5: self.check_reduce['tryelsestmt'] = 'tokens' diff --git a/uncompyle6/parsers/parse26.py b/uncompyle6/parsers/parse26.py index d071c5af..0283a6a8 100644 --- a/uncompyle6/parsers/parse26.py +++ b/uncompyle6/parsers/parse26.py @@ -251,6 +251,11 @@ class Python26Parser(Python2Parser): """ def add_custom_rules(self, tokens, customize): + self.remove_rules(""" + withasstmt ::= expr SETUP_WITH designator suite_stmts_opt + POP_BLOCK LOAD_CONST COME_FROM_WITH + WITH_CLEANUP END_FINALLY + """) super(Python26Parser, self).add_custom_rules(tokens, customize) self.check_reduce['and'] = 'AST'