Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
8cabc639
Commit
8cabc639
authored
Jan 21, 2017
by
YOU
Committed by
Dylan Trotter
Jan 20, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Decorators (#177)
parent
c33ad966
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
1 deletion
+28
-1
compiler/stmt.py
compiler/stmt.py
+8
-1
compiler/stmt_test.py
compiler/stmt_test.py
+20
-0
No files found.
compiler/stmt.py
View file @
8cabc639
...
...
@@ -309,9 +309,16 @@ class StatementVisitor(ast.NodeVisitor):
self
.
writer
.
write_label
(
loop
.
end_label
)
def
visit_FunctionDef
(
self
,
node
):
self
.
_write_py_context
(
node
.
lineno
)
self
.
_write_py_context
(
node
.
lineno
+
len
(
node
.
decorator_list
)
)
func
=
self
.
expr_visitor
.
visit_function_inline
(
node
)
self
.
block
.
bind_var
(
self
.
writer
,
node
.
name
,
func
.
expr
)
while
node
.
decorator_list
:
decorator
=
node
.
decorator_list
.
pop
()
wrapped
=
ast
.
Name
(
node
.
name
,
ast
.
Load
)
decorated
=
ast
.
Call
(
decorator
,
[
wrapped
],
[],
None
,
None
)
target
=
ast
.
Assign
([
wrapped
],
decorated
)
target
.
lineno
=
node
.
lineno
+
len
(
node
.
decorator_list
)
self
.
visit_Assign
(
target
)
def
visit_Global
(
self
,
node
):
self
.
_write_py_context
(
node
.
lineno
)
...
...
compiler/stmt_test.py
View file @
8cabc639
...
...
@@ -212,6 +212,26 @@ class StatementVisitorTest(unittest.TestCase):
util
.
ParseError
,
"'continue' not in loop"
,
_ParseAndVisit
,
'for i in (1,):
\
n
pass
\
n
else:
\
n
continue'
)
def
testFunctionDecorator
(
self
):
self
.
assertEqual
((
0
,
'<b>foo</b>
\
n
'
),
_GrumpRun
(
textwrap
.
dedent
(
"""
\
def bold(fn):
return lambda: '<b>' + fn() + '</b>'
@bold
def foo():
return 'foo'
print foo()"""
)))
def
testFunctionDecoratorWithArg
(
self
):
self
.
assertEqual
((
0
,
'<b id=red>foo</b>
\
n
'
),
_GrumpRun
(
textwrap
.
dedent
(
"""
\
def tag(name):
def bold(fn):
return lambda: '<b id=' + name + '>' + fn() + '</b>'
return bold
@tag('red')
def foo():
return 'foo'
print foo()"""
)))
def
testFunctionDef
(
self
):
self
.
assertEqual
((
0
,
'bar baz
\
n
'
),
_GrumpRun
(
textwrap
.
dedent
(
"""
\
def foo(a, b):
...
...
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