Commit 308c5d36 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add hex() and oct()

parent 7ae78bdc
......@@ -120,6 +120,32 @@ extern "C" Box* abs_(Box* x) {
}
}
extern "C" Box* hexFunc(Box* x) {
static const std::string hex_str("__hex__");
Box* r = callattr(x, &hex_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = true }), ArgPassSpec(0),
NULL, NULL, NULL, NULL, NULL);
if (!r)
raiseExcHelper(TypeError, "hex() argument can't be converted to hex");
if (!isSubclass(r->cls, str_cls))
raiseExcHelper(TypeError, "__hex__() returned non-string (type %.200s)", r->cls->tp_name);
return r;
}
extern "C" Box* octFunc(Box* x) {
static const std::string oct_str("__oct__");
Box* r = callattr(x, &oct_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = true }), ArgPassSpec(0),
NULL, NULL, NULL, NULL, NULL);
if (!r)
raiseExcHelper(TypeError, "oct() argument can't be converted to oct");
if (!isSubclass(r->cls, str_cls))
raiseExcHelper(TypeError, "__oct__() returned non-string (type %.200s)", r->cls->tp_name);
return r;
}
extern "C" Box* all(Box* container) {
for (Box* e : container->pyElements()) {
if (!nonzero(e)) {
......@@ -945,6 +971,8 @@ void setupBuiltins() {
builtins_module->giveAttr("hash", hash_obj);
abs_obj = new BoxedFunction(boxRTFunction((void*)abs_, UNKNOWN, 1));
builtins_module->giveAttr("abs", abs_obj);
builtins_module->giveAttr("hex", new BoxedFunction(boxRTFunction((void*)hexFunc, UNKNOWN, 1)));
builtins_module->giveAttr("oct", new BoxedFunction(boxRTFunction((void*)octFunc, UNKNOWN, 1)));
min_obj = new BoxedFunction(boxRTFunction((void*)min, UNKNOWN, 1, 0, true, false));
builtins_module->giveAttr("min", min_obj);
......
......@@ -733,6 +733,26 @@ extern "C" Box* intHash(BoxedInt* self) {
return boxInt(self->n);
}
extern "C" Box* intHex(BoxedInt* self) {
if (!isSubclass(self->cls, int_cls))
raiseExcHelper(TypeError, "descriptor '__hex__' requires a 'int' object but received a '%s'",
getTypeName(self)->c_str());
char buf[80];
int len = snprintf(buf, sizeof(buf), "0x%lx", self->n);
return new BoxedString(std::string(buf, len));
}
extern "C" Box* intOct(BoxedInt* self) {
if (!isSubclass(self->cls, int_cls))
raiseExcHelper(TypeError, "descriptor '__oct__' requires a 'int' object but received a '%s'",
getTypeName(self)->c_str());
char buf[80];
int len = snprintf(buf, sizeof(buf), "%#lo", self->n);
return new BoxedString(std::string(buf, len));
}
extern "C" Box* intNew(Box* _cls, Box* val) {
if (!isSubclass(_cls->cls, type_cls))
raiseExcHelper(TypeError, "int.__new__(X): X is not a type object (%s)", getTypeName(_cls)->c_str());
......@@ -855,6 +875,9 @@ void setupInt() {
int_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)intHash, BOXED_INT, 1)));
int_cls->giveAttr("__divmod__", new BoxedFunction(boxRTFunction((void*)intDivmod, BOXED_TUPLE, 2)));
int_cls->giveAttr("__hex__", new BoxedFunction(boxRTFunction((void*)intHex, STR, 1)));
int_cls->giveAttr("__oct__", new BoxedFunction(boxRTFunction((void*)intOct, STR, 1)));
int_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)intNew, BOXED_INT, 2, 1, false, false), { boxInt(0) }));
......
......@@ -58,3 +58,13 @@ print vars(C()).items()
print globals().get("not a real variable")
print globals().get("not a real variable", 1)
print hex(12345)
print oct(234)
print hex(0)
print oct(0)
try:
print hex([])
except TypeError, e:
print e
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment