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

Skip to content

Commit 5524a59

Browse files
committed
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
1 parent 879c581 commit 5524a59

2 files changed

Lines changed: 40 additions & 50 deletions

File tree

Objects/object.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,39 @@ testbool(v)
319319
return res;
320320
}
321321

322+
/* Coerce two numeric types to the "larger" one.
323+
Increment the reference count on each argument.
324+
Return -1 and raise an exception if no coercion is possible
325+
(and then no reference count is incremented).
326+
*/
327+
328+
int
329+
coerce(pv, pw)
330+
object **pv, **pw;
331+
{
332+
register object *v = *pv;
333+
register object *w = *pw;
334+
int res;
335+
336+
if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
337+
INCREF(v);
338+
INCREF(w);
339+
return 0;
340+
}
341+
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
342+
res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
343+
if (res <= 0)
344+
return res;
345+
}
346+
if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
347+
res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
348+
if (res <= 0)
349+
return res;
350+
}
351+
err_setstr(TypeError, "number coercion failed");
352+
return -1;
353+
}
354+
322355

323356
/*
324357
NoObject is usable as a non-NULL undefined value, used by the macro None.

Python/bltinmodule.c

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -255,32 +255,22 @@ builtin_cmp(self, args)
255255
return newintobject((long)cmpobject(a, b));
256256
}
257257

258-
static object *
259-
do_coerce(v, w)
260-
object *v, *w;
261-
{
262-
object *res;
263-
if (is_instanceobject(v) || is_instanceobject(w))
264-
return instancebinop(v, w, "__coerce__", "__rcoerce__",
265-
do_coerce);
266-
if (coerce(&v, &w) < 0)
267-
return NULL;
268-
res = mkvalue("(OO)", v, w);
269-
DECREF(v);
270-
DECREF(w);
271-
return res;
272-
}
273-
274258
static object *
275259
builtin_coerce(self, args)
276260
object *self;
277261
object *args;
278262
{
279263
object *v, *w;
264+
object *res;
280265

281266
if (!newgetargs(args, "OO:coerce", &v, &w))
282267
return NULL;
283-
return do_coerce(v, w);
268+
if (coerce(&v, &w) < 0)
269+
return NULL;
270+
res = mkvalue("(OO)", v, w);
271+
DECREF(v);
272+
DECREF(w);
273+
return res;
284274
}
285275

286276
static object *
@@ -1464,39 +1454,6 @@ initbuiltin()
14641454
(void) dictinsert(builtin_dict, "None", None);
14651455
}
14661456

1467-
/* Coerce two numeric types to the "larger" one.
1468-
Increment the reference count on each argument.
1469-
Return -1 and raise an exception if no coercion is possible
1470-
(and then no reference count is incremented).
1471-
*/
1472-
1473-
int
1474-
coerce(pv, pw)
1475-
object **pv, **pw;
1476-
{
1477-
register object *v = *pv;
1478-
register object *w = *pw;
1479-
int res;
1480-
1481-
if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
1482-
INCREF(v);
1483-
INCREF(w);
1484-
return 0;
1485-
}
1486-
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1487-
res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1488-
if (res <= 0)
1489-
return res;
1490-
}
1491-
if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1492-
res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1493-
if (res <= 0)
1494-
return res;
1495-
}
1496-
err_setstr(TypeError, "number coercion failed");
1497-
return -1;
1498-
}
1499-
15001457

15011458
/* Helper for filter(): filter a tuple through a function */
15021459

0 commit comments

Comments
 (0)