Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
7ad94f01
Commit
7ad94f01
authored
Feb 28, 2006
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update the compiler package to compile the with-statement.
Jeremy, please review!
parent
40d8459d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
1 deletion
+63
-1
Lib/compiler/pycodegen.py
Lib/compiler/pycodegen.py
+41
-0
Lib/compiler/transformer.py
Lib/compiler/transformer.py
+21
-0
Lib/test/test_compiler.py
Lib/test/test_compiler.py
+1
-1
No files found.
Lib/compiler/pycodegen.py
View file @
7ad94f01
...
...
@@ -807,6 +807,47 @@ class CodeGenerator:
self
.
emit
(
'END_FINALLY'
)
self
.
setups
.
pop
()
__with_count
=
0
def
visitWith
(
self
,
node
):
body
=
self
.
newBlock
()
final
=
self
.
newBlock
()
exitvar
=
"$exit%d"
%
self
.
__with_count
valuevar
=
"$value%d"
%
self
.
__with_count
self
.
__with_count
+=
1
self
.
set_lineno
(
node
)
self
.
visit
(
node
.
expr
)
self
.
emit
(
'LOAD_ATTR'
,
'__context__'
)
self
.
emit
(
'CALL_FUNCTION'
,
0
)
self
.
emit
(
'DUP_TOP'
)
self
.
emit
(
'LOAD_ATTR'
,
'__exit__'
)
self
.
_implicitNameOp
(
'STORE'
,
exitvar
)
self
.
emit
(
'LOAD_ATTR'
,
'__enter__'
)
self
.
emit
(
'CALL_FUNCTION'
,
0
)
if
node
.
vars
is
None
:
self
.
emit
(
'POP_TOP'
)
else
:
self
.
_implicitNameOp
(
'STORE'
,
valuevar
)
self
.
emit
(
'SETUP_FINALLY'
,
final
)
self
.
nextBlock
(
body
)
self
.
setups
.
push
((
TRY_FINALLY
,
body
))
if
node
.
vars
is
not
None
:
self
.
_implicitNameOp
(
'LOAD'
,
valuevar
)
self
.
_implicitNameOp
(
'DELETE'
,
valuevar
)
self
.
visit
(
node
.
vars
)
self
.
visit
(
node
.
body
)
self
.
emit
(
'POP_BLOCK'
)
self
.
setups
.
pop
()
self
.
emit
(
'LOAD_CONST'
,
None
)
self
.
nextBlock
(
final
)
self
.
setups
.
push
((
END_FINALLY
,
final
))
self
.
emit
(
'WITH_CLEANUP'
)
self
.
emit
(
'CALL_FUNCTION'
,
3
)
self
.
emit
(
'POP_TOP'
)
self
.
emit
(
'END_FINALLY'
)
self
.
setups
.
pop
()
self
.
__with_count
-=
1
# misc
def
visitDiscard
(
self
,
node
):
...
...
Lib/compiler/transformer.py
View file @
7ad94f01
...
...
@@ -536,6 +536,12 @@ class Transformer:
return
self
.
com_try_except
(
nodelist
)
def
with_stmt
(
self
,
nodelist
):
return
self
.
com_with
(
nodelist
)
def
with_var
(
self
,
nodelist
):
return
self
.
com_with_var
(
nodelist
)
def
suite
(
self
,
nodelist
):
# simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
if
len
(
nodelist
)
==
1
:
...
...
@@ -926,6 +932,20 @@ class Transformer:
return
TryExcept
(
self
.
com_node
(
nodelist
[
2
]),
clauses
,
elseNode
,
lineno
=
nodelist
[
0
][
2
])
def
com_with
(
self
,
nodelist
):
# with_stmt: 'with' expr [with_var] ':' suite
expr
=
self
.
com_node
(
nodelist
[
1
])
body
=
self
.
com_node
(
nodelist
[
-
1
])
if
nodelist
[
2
][
0
]
==
token
.
COLON
:
var
=
None
else
:
var
=
self
.
com_node
(
nodelist
[
2
])
return
With
(
expr
,
var
,
body
,
lineno
=
nodelist
[
0
][
2
])
def
com_with_var
(
self
,
nodelist
):
# with_var: 'as' expr
return
self
.
com_node
(
nodelist
[
1
])
def
com_augassign_op
(
self
,
node
):
assert
node
[
0
]
==
symbol
.
augassign
return
node
[
1
]
...
...
@@ -1390,6 +1410,7 @@ _legal_node_types = [
symbol
.
while_stmt
,
symbol
.
for_stmt
,
symbol
.
try_stmt
,
symbol
.
with_stmt
,
symbol
.
suite
,
symbol
.
testlist
,
symbol
.
testlist_safe
,
...
...
Lib/test/test_compiler.py
View file @
7ad94f01
...
...
@@ -20,7 +20,7 @@ class CompilerTest(unittest.TestCase):
for
basename
in
os
.
listdir
(
dir
):
if
not
basename
.
endswith
(
".py"
):
continue
if
not
TEST_ALL
and
random
()
<
0.98
:
if
not
TEST_ALL
and
random
()
<
0.98
and
basename
!=
"test_with.py"
:
continue
path
=
os
.
path
.
join
(
dir
,
basename
)
if
test
.
test_support
.
verbose
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment