Skip to content

builtin,vm: add implementation for builtin hex function #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
py.MustNewMethod("globals", py.InternalMethodGlobals, 0, globals_doc),
py.MustNewMethod("hasattr", builtin_hasattr, 0, hasattr_doc),
// py.MustNewMethod("hash", builtin_hash, 0, hash_doc),
// py.MustNewMethod("hex", builtin_hex, 0, hex_doc),
py.MustNewMethod("hex", builtin_hex, 0, hex_doc),
// py.MustNewMethod("id", builtin_id, 0, id_doc),
// py.MustNewMethod("input", builtin_input, 0, input_doc),
py.MustNewMethod("isinstance", builtin_isinstance, 0, isinstance_doc),
Expand Down Expand Up @@ -826,6 +826,52 @@ object.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it.`

const hex_doc = `hex(number) -> string

Return the hexadecimal representation of an integer.

>>> hex(12648430)
'0xc0ffee'
`

func builtin_hex(self, v py.Object) (py.Object, error) {
var (
i int64
err error
)
switch v := v.(type) {
case *py.BigInt:
// test bigint first to make sure we correctly handle the case
// where int64 isn't large enough.
vv := (*big.Int)(v)
format := "%#x"
if vv.Cmp(big.NewInt(0)) == -1 {
format = "%+#x"
}
str := fmt.Sprintf(format, vv)
return py.String(str), nil
case py.IGoInt64:
i, err = v.GoInt64()
case py.IGoInt:
var vv int
vv, err = v.GoInt()
i = int64(vv)
default:
return nil, py.ExceptionNewf(py.TypeError, "'%s' object cannot be interpreted as an integer", v.Type().Name)
}

if err != nil {
return nil, err
}

format := "%#x"
if i < 0 {
format = "%+#x"
}
str := fmt.Sprintf(format, i)
return py.String(str), nil
}

const isinstance_doc = `isinstance(obj, class_or_tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
Expand Down
26 changes: 25 additions & 1 deletion builtin/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from libtest import assertRaises

doc="abs"
assert abs(0) == 0
assert abs(10) == 10
Expand Down Expand Up @@ -151,6 +153,29 @@ def func(p):
ok = True
assert ok, "ValueError not raised"

doc="hex"
assert hex( 0)=="0x0", "hex(0)"
assert hex( 1)=="0x1", "hex(1)"
assert hex(42)=="0x2a", "hex(42)"
assert hex( -0)=="0x0", "hex(-0)"
assert hex( -1)=="-0x1", "hex(-1)"
assert hex(-42)=="-0x2a", "hex(-42)"
assert hex( 1<<64) == "0x10000000000000000", "hex(1<<64)"
assert hex(-1<<64) == "-0x10000000000000000", "hex(-1<<64)"
assert hex( 1<<128) == "0x100000000000000000000000000000000", "hex(1<<128)"
assert hex(-1<<128) == "-0x100000000000000000000000000000000", "hex(-1<<128)"
assertRaises(TypeError, hex, 10.0) ## TypeError: 'float' object cannot be interpreted as an integer
assertRaises(TypeError, hex, float(0)) ## TypeError: 'float' object cannot be interpreted as an integer

doc="isinstance"
class A:
pass
a = A()
assert isinstance(1, (str, tuple, int))
assert isinstance(a, (str, (tuple, (A, ))))
assertRaises(TypeError, isinstance, 1, (A, ), "foo")
assertRaises(TypeError, isinstance, 1, [A, "foo"])

doc="iter"
cnt = 0
def f():
Expand Down Expand Up @@ -413,7 +438,6 @@ class C: pass
try:
zip(1,2,3)
except TypeError as e:
print(e.args[0])
if e.args[0] != "zip argument #1 must support iteration":
raise
ok = True
Expand Down
File renamed without changes.
10 changes: 0 additions & 10 deletions vm/tests/builtin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2018 The go-python Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from libtest import assertRaises

doc="eval"
assert eval("1+2") == 3
Expand Down Expand Up @@ -66,13 +65,4 @@
else:
assert False, "SyntaxError not raised"

doc="isinstance"
class A:
pass
a = A()
assert True, isinstance(1, (str, tuple, int))
assert True, isinstance(a, (str, (tuple, (A, ))))
assertRaises(TypeError, isinstance, 1, (A, ), "foo")
assertRaises(TypeError, isinstance, 1, [A, "foo"])

doc="finished"