Commit f49316ac authored by Travis Hance's avatar Travis Hance

builtin format

parent fb499bc7
...@@ -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;
......
...@@ -364,3 +364,33 @@ PyObject *string_join(PyStringObject *self, PyObject *orig) ...@@ -364,3 +364,33 @@ PyObject *string_join(PyStringObject *self, PyObject *orig)
Py_DECREF(seq); Py_DECREF(seq);
return res; return res;
} }
PyObject *
string__format__(PyObject* self, PyObject* args)
{
PyObject *format_spec;
PyObject *result = NULL;
PyObject *tmp = NULL;
/* If 2.x, convert format_spec to the same type as value */
/* This is to allow things like u''.format('') */
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
goto done;
if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) {
PyErr_Format(PyExc_TypeError, "__format__ arg must be str "
"or unicode, not %s", Py_TYPE(format_spec)->tp_name);
goto done;
}
tmp = PyObject_Str(format_spec);
if (tmp == NULL)
goto done;
format_spec = tmp;
result = _PyBytes_FormatAdvanced(self,
PyString_AS_STRING(format_spec),
PyString_GET_SIZE(format_spec));
done:
Py_XDECREF(tmp);
return result;
}
...@@ -1095,6 +1095,14 @@ Box* builtinApply(Box* func, Box* args, Box* keywords) { ...@@ -1095,6 +1095,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 "
...@@ -1330,5 +1338,7 @@ void setupBuiltins() { ...@@ -1330,5 +1338,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 {
...@@ -1434,7 +1435,8 @@ static PyMethodDef float_methods[] = { { "hex", (PyCFunction)float_hex, METH_NOA ...@@ -1434,7 +1435,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);
......
...@@ -44,6 +44,7 @@ extern "C" PyObject* string_index(PyStringObject* self, PyObject* args) noexcept ...@@ -44,6 +44,7 @@ extern "C" PyObject* string_index(PyStringObject* self, PyObject* args) noexcept
extern "C" PyObject* string_rindex(PyStringObject* self, PyObject* args) noexcept; extern "C" PyObject* string_rindex(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_rfind(PyStringObject* self, PyObject* args) noexcept; extern "C" PyObject* string_rfind(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_splitlines(PyStringObject* self, PyObject* args) noexcept; extern "C" PyObject* string_splitlines(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string__format__(PyStringObject* self, PyObject* args) noexcept;
// from cpython's stringobject.c: // from cpython's stringobject.c:
#define LEFTSTRIP 0 #define LEFTSTRIP 0
...@@ -2652,6 +2653,7 @@ static PyMethodDef string_methods[] = { ...@@ -2652,6 +2653,7 @@ static PyMethodDef string_methods[] = {
{ "expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, NULL }, { "expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, NULL },
{ "splitlines", (PyCFunction)string_splitlines, METH_VARARGS, NULL }, { "splitlines", (PyCFunction)string_splitlines, METH_VARARGS, NULL },
{ "zfill", (PyCFunction)string_zfill, METH_VARARGS, NULL }, { "zfill", (PyCFunction)string_zfill, METH_VARARGS, NULL },
{ "__format__", (PyCFunction)string__format__, METH_VARARGS, NULL },
}; };
void setupStr() { void setupStr() {
......
...@@ -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