summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-02-12 16:29:05 (GMT)
committerGuido van Rossum <guido@python.org>1993-02-12 16:29:05 (GMT)
commit9e51f9bec85ac8bc167c2fb8d3956968ef8311af (patch)
tree502d6a37c21a409246c9a29a40fe0149fe7d3715 /Python
parente8a3c28f8d962ca0dff6f6ad82f335706c90fa58 (diff)
downloadcpython-9e51f9bec85ac8bc167c2fb8d3956968ef8311af.zip
cpython-9e51f9bec85ac8bc167c2fb8d3956968ef8311af.tar.gz
cpython-9e51f9bec85ac8bc167c2fb8d3956968ef8311af.tar.bz2
bltinmodule.c: added round(x, [n]); coerce() of two class instances
will try to coerce anyway. classobject.c: instance 'nonzero' should first try __nonzero__ only then __len__.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 85fd58e..97c1303 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -616,6 +616,34 @@ builtin_repr(self, v)
}
static object *
+builtin_round(self, args)
+ object *self;
+ object *args;
+{
+ extern double floor PROTO((double));
+ extern double ceil PROTO((double));
+ double x;
+ double f;
+ int ndigits = 0;
+ int sign = 1;
+ int i;
+ if (!getargs(args, "d", &x)) {
+ err_clear();
+ if (!getargs(args, "(di)", &x, &ndigits))
+ return NULL;
+ }
+ f = 1.0;
+ for (i = ndigits; --i >= 0; )
+ f = f*10.0;
+ for (i = ndigits; ++i <= 0; )
+ f = f*0.1;
+ if (x >= 0.0)
+ return newfloatobject(floor(x*f + 0.5) / f);
+ else
+ return newfloatobject(ceil(x*f - 0.5) / f);
+}
+
+static object *
builtin_str(self, v)
object *self;
object *v;
@@ -674,6 +702,7 @@ static struct methodlist builtin_methods[] = {
{"raw_input", builtin_raw_input},
{"reload", builtin_reload},
{"repr", builtin_repr},
+ {"round", builtin_round},
{"setattr", builtin_setattr},
{"str", builtin_str},
{"type", builtin_type},
@@ -766,7 +795,7 @@ coerce(pv, pw)
register object *w = *pw;
int res;
- if (v->ob_type == w->ob_type) {
+ if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
INCREF(v);
INCREF(w);
return 0;