Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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__.
  • Loading branch information
gvanrossum committed Feb 12, 1993
1 parent e8a3c28 commit 9e51f9b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Objects/classobject.c
Expand Up @@ -619,9 +619,9 @@ instance_nonzero(self)
object *func, *res;
long outcome;

if ((func = instance_getattr(self, "__len__")) == NULL) {
if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
err_clear();
if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
if ((func = instance_getattr(self, "__len__")) == NULL) {
err_clear();
/* Fall back to the default behavior:
all instances are nonzero */
Expand Down
31 changes: 30 additions & 1 deletion Python/bltinmodule.c
Expand Up @@ -615,6 +615,34 @@ builtin_repr(self, v)
return reprobject(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;
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9e51f9b

Please sign in to comment.