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 1 commit
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
Next Next commit
builtin,vm: add implementation for builtin hex function
  • Loading branch information
sbinet committed Nov 5, 2019
commit 3835a01350b385d7c2537e2744980bd75b159a88
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
14 changes: 14 additions & 0 deletions vm/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@
else:
assert False, "SyntaxError not raised"

doc="hex"
assert True, hex( 0)=="0x0"
assert True, hex( 1)=="0x1"
assert True, hex(42)=="0x2a"
assert True, hex( -0)=="0x0"
assert True, hex( -1)=="-0x1"
assert True, hex(-42)=="-0x2a"
assert True, hex( 1<<64) == "0x10000000000000000"
assert True, hex(-1<<64) == "-0x10000000000000000"
assert True, hex( 1<<128) == "0x100000000000000000000000000000000"
assert True, hex(-1<<128) == "-0x100000000000000000000000000000000"
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
Expand Down