Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Boxiang Sun
Pyston
Commits
4d15c2f2
Commit
4d15c2f2
authored
Jul 29, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement function decorators
parent
2d55194c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
10 deletions
+53
-10
src/codegen/irgen/irgenerator.cpp
src/codegen/irgen/irgenerator.cpp
+9
-1
src/core/ast.cpp
src/core/ast.cpp
+7
-1
src/runtime/int.cpp
src/runtime/int.cpp
+2
-2
test/tests/decorators.py
test/tests/decorators.py
+35
-6
No files found.
src/codegen/irgen/irgenerator.cpp
View file @
4d15c2f2
...
...
@@ -1494,9 +1494,17 @@ private:
if
(
state
==
PARTIAL
)
return
;
assert
(
!
node
->
decorator_list
.
size
());
std
::
vector
<
CompilerVariable
*>
decorators
;
for
(
auto
d
:
node
->
decorator_list
)
{
decorators
.
push_back
(
evalExpr
(
d
,
exc_info
));
}
CompilerVariable
*
func
=
_createFunction
(
node
,
exc_info
,
node
->
args
,
node
->
body
);
for
(
int
i
=
decorators
.
size
()
-
1
;
i
>=
0
;
i
--
)
{
func
=
decorators
[
i
]
->
call
(
emitter
,
getOpInfoForNode
(
node
,
exc_info
),
ArgPassSpec
(
1
),
{
func
},
NULL
);
}
_doSet
(
node
->
name
,
func
,
exc_info
);
func
->
decvref
(
emitter
);
}
...
...
src/core/ast.cpp
View file @
4d15c2f2
...
...
@@ -1195,7 +1195,13 @@ bool PrintVisitor::visit_for(AST_For* node) {
}
bool
PrintVisitor
::
visit_functiondef
(
AST_FunctionDef
*
node
)
{
assert
(
node
->
decorator_list
.
size
()
==
0
);
for
(
auto
d
:
node
->
decorator_list
)
{
printf
(
"@"
);
d
->
accept
(
this
);
printf
(
"
\n
"
);
printIndent
();
}
printf
(
"def %s("
,
node
->
name
.
c_str
());
node
->
args
->
accept
(
this
);
printf
(
")"
);
...
...
src/runtime/int.cpp
View file @
4d15c2f2
...
...
@@ -92,7 +92,7 @@ extern "C" Box* div_i64_i64(i64 lhs, i64 rhs) {
raiseExcHelper
(
ZeroDivisionError
,
"integer division or modulo by zero"
);
}
// It's possible for division to overflow:
// It's possible for division to overflow:
#if PYSTON_INT_MIN < -PYSTON_INT_MAX
static_assert
(
PYSTON_INT_MIN
==
-
PYSTON_INT_MAX
-
1
,
""
);
...
...
@@ -548,7 +548,7 @@ extern "C" Box* intPos(BoxedInt* v) {
extern
"C"
Box
*
intNeg
(
BoxedInt
*
v
)
{
assert
(
v
->
cls
==
int_cls
);
// It's possible for this to overflow:
// It's possible for this to overflow:
#if PYSTON_INT_MIN < -PYSTON_INT_MAX
static_assert
(
PYSTON_INT_MIN
==
-
PYSTON_INT_MAX
-
1
,
""
);
...
...
test/tests/decorators.py
View file @
4d15c2f2
# expected: fail
# - decorators
def
print_when_eval
(
x
,
msg
):
print
msg
return
x
# Test the order that decorators and function defaults get evaluated
@
print_when_eval
(
lambda
f
:
print_when_eval
(
f
,
"calling decorator"
),
"evaluating decorator"
)
@
print_when_eval
(
lambda
f
:
print_when_eval
(
lambda
*
args
,
**
kw
:
print_when_eval
(
f
,
"calling function (outer)"
)(
*
args
,
**
kw
),
"calling outer decorator"
),
"evaluating outer decorator"
)
@
print_when_eval
(
lambda
f
:
print_when_eval
(
lambda
*
args
,
**
kw
:
print_when_eval
(
f
,
"calling function (inner)"
)(
*
args
,
**
kw
),
"calling inner decorator"
),
"evaluating inner decorator"
)
def
f
(
x
=
print_when_eval
(
1
,
"evaluating default"
)):
pass
# Result: looks like it's decorator first, then defaults, then the decorator is called.
return
x
# Result: looks like it's decorators get evaluated first (outer in), then defaults, then the decorators are called (inside out).
print
print
f
()
print
f
(
2
)
print
def
print_args
(
f
):
print
"calling print_args decorator"
def
inner
(
*
args
,
**
kw
):
print
args
,
kw
return
f
(
*
args
,
**
kw
)
return
inner
def
f1
(
a
,
b
):
print
a
+
b
pf1
=
print_args
(
f1
)
pf1
(
1
,
2
)
pf1
(
2
,
b
=
2
)
@
print_args
def
f2
(
a
,
b
):
print
a
-
b
print
f2
.
__name__
f2
(
1
,
3
)
f2
(
2
,
b
=
3
)
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