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
641bb659
Commit
641bb659
authored
Feb 17, 2016
by
asaka
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
check duplicate function params
parent
75bd1059
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
3 deletions
+75
-3
src/codegen/cpython_ast.cpp
src/codegen/cpython_ast.cpp
+44
-3
test/tests/compile_test.py
test/tests/compile_test.py
+31
-0
No files found.
src/codegen/cpython_ast.cpp
View file @
641bb659
...
...
@@ -14,6 +14,8 @@
#include "codegen/cpython_ast.h"
#include <set>
#include "llvm/ADT/STLExtras.h"
#include "core/types.h"
...
...
@@ -93,16 +95,51 @@ public:
return
pool
->
get
(
static_cast
<
BoxedString
*>
(
ident
)
->
s
());
}
AST_arguments
*
convert
(
arguments_ty
ident
)
{
AST_arguments
*
convert
(
arguments_ty
ident
,
AST
*
parent
)
{
auto
r
=
new
AST_arguments
();
convertAll
<
expr_ty
>
(
ident
->
args
,
r
->
args
);
convertAll
<
expr_ty
>
(
ident
->
defaults
,
r
->
defaults
);
r
->
vararg
=
convert
(
ident
->
vararg
);
r
->
kwarg
=
convert
(
ident
->
kwarg
);
if
((
!
r
->
vararg
.
s
().
empty
())
&&
(
!
r
->
kwarg
.
s
().
empty
())
&&
(
r
->
vararg
==
r
->
kwarg
))
{
char
buf
[
1024
];
snprintf
(
buf
,
sizeof
(
buf
),
"duplicate argument '%s' in function definition"
,
r
->
vararg
.
c_str
());
raiseSyntaxError
(
buf
,
parent
->
lineno
,
parent
->
col_offset
,
fn
,
""
,
true
);
}
std
::
set
<
InternedString
>
seen
;
if
(
!
r
->
vararg
.
s
().
empty
())
{
seen
.
insert
(
r
->
vararg
);
}
if
(
!
r
->
kwarg
.
s
().
empty
())
{
seen
.
insert
(
r
->
kwarg
);
}
checkDuplicateArgs
(
parent
,
r
->
args
,
&
seen
);
return
r
;
}
void
checkDuplicateArgs
(
AST
*
parent
,
std
::
vector
<
AST_expr
*>
args
,
std
::
set
<
InternedString
>*
seen
)
{
for
(
auto
arg
:
args
)
{
if
(
arg
->
type
==
AST_TYPE
::
Name
)
{
auto
name_node
=
static_cast
<
AST_Name
*>
(
arg
);
if
(
seen
->
find
(
name_node
->
id
)
!=
seen
->
end
())
{
char
buf
[
1024
];
snprintf
(
buf
,
sizeof
(
buf
),
"duplicate argument '%s' in function definition"
,
name_node
->
id
.
c_str
());
raiseSyntaxError
(
buf
,
parent
->
lineno
,
parent
->
col_offset
,
fn
,
""
,
true
);
}
seen
->
insert
(
name_node
->
id
);
}
else
if
(
arg
->
type
==
AST_TYPE
::
Tuple
)
{
auto
slice_node
=
static_cast
<
AST_Tuple
*>
(
arg
);
checkDuplicateArgs
(
parent
,
slice_node
->
elts
,
seen
);
}
else
{
RELEASE_ASSERT
(
0
,
""
);
}
}
}
#define CASE(N) \
case N: \
return AST_TYPE::N
...
...
@@ -244,8 +281,10 @@ public:
}
case
Lambda_kind
:
{
auto
r
=
new
AST_Lambda
();
r
->
lineno
=
expr
->
lineno
;
r
->
col_offset
=
expr
->
col_offset
;
auto
v
=
expr
->
v
.
Lambda
;
r
->
args
=
convert
(
v
.
args
);
r
->
args
=
convert
(
v
.
args
,
r
);
r
->
body
=
convert
(
v
.
body
);
return
r
;
}
...
...
@@ -465,9 +504,11 @@ public:
switch
(
stmt
->
kind
)
{
case
FunctionDef_kind
:
{
auto
r
=
new
AST_FunctionDef
();
r
->
lineno
=
stmt
->
lineno
;
r
->
col_offset
=
stmt
->
col_offset
;
auto
v
=
stmt
->
v
.
FunctionDef
;
r
->
name
=
convert
(
v
.
name
);
r
->
args
=
convert
(
v
.
args
);
r
->
args
=
convert
(
v
.
args
,
r
);
r
->
body
=
convert
<
stmt_ty
,
AST_stmt
*>
(
v
.
body
);
r
->
decorator_list
=
convert
<
expr_ty
,
AST_expr
*>
(
v
.
decorator_list
);
return
r
;
...
...
test/tests/compile_test.py
View file @
641bb659
...
...
@@ -36,3 +36,34 @@ try:
assert
False
except
SyntaxError
:
pass
# test duplicate args
try
:
exec
compile
(
'lambda x, x: none'
,
'foo'
,
'exec'
)
except
SyntaxError
as
e
:
print
(
e
)
else
:
raise
Exception
(
'SyntaxError not raised'
)
# test nests duplicate args
try
:
exec
compile
(
'lambda a, (b, (c, a)): none'
,
'foo'
,
'exec'
)
except
SyntaxError
as
e
:
print
(
e
)
else
:
raise
Exception
(
'SyntaxError not raised'
)
# test duplicate vararg and kwarg
try
:
eval
(
'lambda *a, **a: 0'
)
except
SyntaxError
as
e
:
print
(
e
)
else
:
raise
Exception
(
'SyntaxError not raised'
)
try
:
eval
(
'lambda a, *a: 0'
)
except
SyntaxError
as
e
:
print
(
e
)
else
:
raise
Exception
(
'SyntaxError not raised'
)
Boxiang Sun
@Daetalus
mentioned in commit
4fccd71f
·
Sep 08, 2016
mentioned in commit
4fccd71f
mentioned in commit 4fccd71fc00e5e18703baf60be9655d09c122e74
Toggle commit list
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