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
b61ec8f0
Commit
b61ec8f0
authored
Jan 07, 2017
by
Cholerae Hu
Committed by
Dylan Trotter
Jan 07, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[+] implement builtin function callable (#38)
parent
f8a9ed35
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
0 deletions
+51
-0
runtime/builtin_types.go
runtime/builtin_types.go
+12
-0
runtime/builtin_types_test.go
runtime/builtin_types_test.go
+15
-0
testing/builtin_test.py
testing/builtin_test.py
+24
-0
No files found.
runtime/builtin_types.go
View file @
b61ec8f0
...
...
@@ -230,6 +230,17 @@ func builtinBin(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
return
NewStr
(
numberToBase
(
"0b"
,
2
,
index
))
.
ToObject
(),
nil
}
func
builtinCallable
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
if
raised
:=
checkFunctionArgs
(
f
,
"callable"
,
args
,
ObjectType
);
raised
!=
nil
{
return
nil
,
raised
}
o
:=
args
[
0
]
if
call
:=
o
.
Type
()
.
slots
.
Call
;
call
==
nil
{
return
False
.
ToObject
(),
nil
}
return
True
.
ToObject
(),
nil
}
func
builtinChr
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
if
raised
:=
checkFunctionArgs
(
f
,
"chr"
,
args
,
IntType
);
raised
!=
nil
{
return
nil
,
raised
...
...
@@ -499,6 +510,7 @@ func init() {
"abs"
:
newBuiltinFunction
(
"abs"
,
builtinAbs
)
.
ToObject
(),
"all"
:
newBuiltinFunction
(
"all"
,
builtinAll
)
.
ToObject
(),
"bin"
:
newBuiltinFunction
(
"bin"
,
builtinBin
)
.
ToObject
(),
"callable"
:
newBuiltinFunction
(
"callable"
,
builtinCallable
)
.
ToObject
(),
"chr"
:
newBuiltinFunction
(
"chr"
,
builtinChr
)
.
ToObject
(),
"dir"
:
newBuiltinFunction
(
"dir"
,
builtinDir
)
.
ToObject
(),
"False"
:
False
.
ToObject
(),
...
...
runtime/builtin_types_test.go
View file @
b61ec8f0
...
...
@@ -64,6 +64,12 @@ func TestBuiltinFuncs(t *testing.T) {
return
newObject
(
badNextType
),
nil
})
.
ToObject
(),
}))
fooBuiltinFunc
:=
newBuiltinFunction
(
"foo"
,
func
(
f
*
Frame
,
args
Args
,
kwargs
KWArgs
)
(
*
Object
,
*
BaseException
)
{
return
newTestTuple
(
NewTuple
(
args
.
makeCopy
()
...
),
kwargs
.
makeDict
())
.
ToObject
(),
nil
})
.
ToObject
()
fooFunc
:=
NewFunction
(
NewCode
(
"foo"
,
"foo.py"
,
nil
,
CodeFlagVarArg
,
func
(
f
*
Frame
,
args
[]
*
Object
)
(
*
Object
,
*
BaseException
)
{
return
args
[
0
],
nil
}),
nil
)
cases
:=
[]
struct
{
f
string
args
Args
...
...
@@ -96,6 +102,15 @@ func TestBuiltinFuncs(t *testing.T) {
{
f
:
"bin"
,
args
:
wrapArgs
(
0.1
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"float object cannot be interpreted as an index"
)},
{
f
:
"bin"
,
args
:
wrapArgs
(
1
,
2
,
3
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"'bin' requires 1 arguments"
)},
{
f
:
"bin"
,
args
:
wrapArgs
(
newObject
(
indexType
)),
want
:
NewStr
(
"0b1111011"
)
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
fooBuiltinFunc
),
want
:
True
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
fooFunc
),
want
:
True
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
0
),
want
:
False
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
0.1
),
want
:
False
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
"foo"
),
want
:
False
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
newTestDict
(
"foo"
,
1
,
"bar"
,
2
)),
want
:
False
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
newTestList
(
1
,
2
,
3
)),
want
:
False
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
iter
),
want
:
False
.
ToObject
()},
{
f
:
"callable"
,
args
:
wrapArgs
(
1
,
2
),
wantExc
:
mustCreateException
(
TypeErrorType
,
"'callable' requires 1 arguments"
)},
{
f
:
"chr"
,
args
:
wrapArgs
(
0
),
want
:
NewStr
(
"
\x00
"
)
.
ToObject
()},
{
f
:
"chr"
,
args
:
wrapArgs
(
65
),
want
:
NewStr
(
"A"
)
.
ToObject
()},
{
f
:
"chr"
,
args
:
wrapArgs
(
300
),
wantExc
:
mustCreateException
(
ValueErrorType
,
"chr() arg not in range(256)"
)},
...
...
testing/builtin_test.py
View file @
b61ec8f0
...
...
@@ -54,3 +54,27 @@ except TypeError as e:
assert
str
(
e
)
==
"'int' object is not iterable"
else
:
raise
AssertionError
(
'this was supposed to raise an exception'
)
# callable(x)
assert
not
callable
(
1
)
assert
not
callable
(
0.1
)
assert
not
callable
([
1
,
2
,
3
])
assert
not
callable
((
1
,
2
,
3
))
assert
not
callable
({
'foo'
:
1
,
'bar'
:
2
})
assert
callable
(
lambda
x
:
x
+
1
)
def
foo
(
x
):
pass
assert
callable
(
foo
)
class
bar
(
object
):
def
__call__
(
self
,
*
args
,
**
kwargs
):
pass
assert
callable
(
bar
)
assert
callable
(
bar
())
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