Commit ca90416c authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #550 from tjhance:builtin_format

Some minor merge conflicts due to duplication with #544.
parents 96c7b4f2 f49316ac
...@@ -1995,7 +1995,8 @@ float_getzero(PyObject *v, void *closure) ...@@ -1995,7 +1995,8 @@ float_getzero(PyObject *v, void *closure)
return PyFloat_FromDouble(0.0); return PyFloat_FromDouble(0.0);
} }
static PyObject * // pyston change: make not static
PyObject *
float__format__(PyObject *self, PyObject *args) float__format__(PyObject *self, PyObject *args)
{ {
PyObject *format_spec; PyObject *format_spec;
......
...@@ -1096,6 +1096,14 @@ Box* builtinApply(Box* func, Box* args, Box* keywords) { ...@@ -1096,6 +1096,14 @@ Box* builtinApply(Box* func, Box* args, Box* keywords) {
return runtimeCall(func, ArgPassSpec(0, 0, true, keywords != NULL), args, keywords, NULL, NULL, NULL); return runtimeCall(func, ArgPassSpec(0, 0, true, keywords != NULL), args, keywords, NULL, NULL, NULL);
} }
Box* builtinFormat(Box* value, Box* format_spec) {
Box* res = PyObject_Format(value, format_spec);
if (!res) {
throwCAPIException();
}
return res;
}
void setupBuiltins() { void setupBuiltins() {
builtins_module builtins_module
= createModule("__builtin__", NULL, "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is " = createModule("__builtin__", NULL, "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is "
...@@ -1331,5 +1339,7 @@ void setupBuiltins() { ...@@ -1331,5 +1339,7 @@ void setupBuiltins() {
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)input, UNKNOWN, 1, 1, false, false), "input", { NULL })); new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)input, UNKNOWN, 1, 1, false, false), "input", { NULL }));
builtins_module->giveAttr("cmp", builtins_module->giveAttr("cmp",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)builtinCmp, UNKNOWN, 2), "cmp")); new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)builtinCmp, UNKNOWN, 2), "cmp"));
builtins_module->giveAttr(
"format", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)builtinFormat, UNKNOWN, 2), "format"));
} }
} }
...@@ -29,6 +29,7 @@ extern "C" PyObject* float_hex(PyObject* v) noexcept; ...@@ -29,6 +29,7 @@ extern "C" PyObject* float_hex(PyObject* v) noexcept;
extern "C" PyObject* float_fromhex(PyObject* cls, PyObject* arg) noexcept; extern "C" PyObject* float_fromhex(PyObject* cls, PyObject* arg) noexcept;
extern "C" PyObject* float_as_integer_ratio(PyObject* v, PyObject* unused) noexcept; extern "C" PyObject* float_as_integer_ratio(PyObject* v, PyObject* unused) noexcept;
extern "C" PyObject* float_is_integer(PyObject* v) noexcept; extern "C" PyObject* float_is_integer(PyObject* v) noexcept;
extern "C" PyObject* float__format__(PyObject* v) noexcept;
namespace pyston { namespace pyston {
...@@ -1442,7 +1443,8 @@ static PyMethodDef float_methods[] = { { "hex", (PyCFunction)float_hex, METH_NOA ...@@ -1442,7 +1443,8 @@ static PyMethodDef float_methods[] = { { "hex", (PyCFunction)float_hex, METH_NOA
{ "fromhex", (PyCFunction)float_fromhex, METH_O | METH_CLASS, NULL }, { "fromhex", (PyCFunction)float_fromhex, METH_O | METH_CLASS, NULL },
{ "as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, NULL }, { "as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, NULL },
{ "is_integer", (PyCFunction)float_is_integer, METH_NOARGS, NULL } }; { "is_integer", (PyCFunction)float_is_integer, METH_NOARGS, NULL },
{ "__format__", (PyCFunction)float__format__, METH_VARARGS, NULL } };
void setupFloat() { void setupFloat() {
_addFunc("__add__", BOXED_FLOAT, (void*)floatAddFloat, (void*)floatAddInt, (void*)floatAdd); _addFunc("__add__", BOXED_FLOAT, (void*)floatAddFloat, (void*)floatAddInt, (void*)floatAdd);
......
...@@ -117,3 +117,7 @@ print bytearray(xrange(256)) ...@@ -117,3 +117,7 @@ print bytearray(xrange(256))
l = [2, 1, 3] l = [2, 1, 3]
print apply(sorted, [l]) print apply(sorted, [l])
print apply(sorted, [l], { "reverse" : True }) print apply(sorted, [l], { "reverse" : True })
print format(5.0, '+')
print format(5.011111111111, '+.6')
print format("abc", '')
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