Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e51f9b

Browse files
committed
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__.
1 parent e8a3c28 commit 9e51f9b

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

Objects/classobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ instance_nonzero(self)
619619
object *func, *res;
620620
long outcome;
621621

622-
if ((func = instance_getattr(self, "__len__")) == NULL) {
622+
if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
623623
err_clear();
624-
if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
624+
if ((func = instance_getattr(self, "__len__")) == NULL) {
625625
err_clear();
626626
/* Fall back to the default behavior:
627627
all instances are nonzero */

Python/bltinmodule.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,34 @@ builtin_repr(self, v)
615615
return reprobject(v);
616616
}
617617

618+
static object *
619+
builtin_round(self, args)
620+
object *self;
621+
object *args;
622+
{
623+
extern double floor PROTO((double));
624+
extern double ceil PROTO((double));
625+
double x;
626+
double f;
627+
int ndigits = 0;
628+
int sign = 1;
629+
int i;
630+
if (!getargs(args, "d", &x)) {
631+
err_clear();
632+
if (!getargs(args, "(di)", &x, &ndigits))
633+
return NULL;
634+
}
635+
f = 1.0;
636+
for (i = ndigits; --i >= 0; )
637+
f = f*10.0;
638+
for (i = ndigits; ++i <= 0; )
639+
f = f*0.1;
640+
if (x >= 0.0)
641+
return newfloatobject(floor(x*f + 0.5) / f);
642+
else
643+
return newfloatobject(ceil(x*f - 0.5) / f);
644+
}
645+
618646
static object *
619647
builtin_str(self, v)
620648
object *self;
@@ -674,6 +702,7 @@ static struct methodlist builtin_methods[] = {
674702
{"raw_input", builtin_raw_input},
675703
{"reload", builtin_reload},
676704
{"repr", builtin_repr},
705+
{"round", builtin_round},
677706
{"setattr", builtin_setattr},
678707
{"str", builtin_str},
679708
{"type", builtin_type},
@@ -766,7 +795,7 @@ coerce(pv, pw)
766795
register object *w = *pw;
767796
int res;
768797

769-
if (v->ob_type == w->ob_type) {
798+
if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
770799
INCREF(v);
771800
INCREF(w);
772801
return 0;

0 commit comments

Comments
 (0)