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

Skip to content

Commit 1899c2e

Browse files
committed
Made builtins int(), long(), float(), oct() and hex() more generic.
1 parent 5c85062 commit 1899c2e

6 files changed

Lines changed: 193 additions & 91 deletions

File tree

Include/object.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ typedef struct {
146146
object *(*nb_xor) FPROTO((object *, object *));
147147
object *(*nb_or) FPROTO((object *, object *));
148148
int (*nb_coerce) FPROTO((object **, object **));
149+
object *(*nb_int) FPROTO((object *));
150+
object *(*nb_long) FPROTO((object *));
151+
object *(*nb_float) FPROTO((object *));
152+
object *(*nb_oct) FPROTO((object *));
153+
object *(*nb_hex) FPROTO((object *));
149154
} number_methods;
150155

151156
typedef struct {

Objects/classobject.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,12 @@ instance_coerce(pv, pw)
714714
return 0;
715715
}
716716

717+
UNARY(instance_int, "__int__")
718+
UNARY(instance_long, "__long__")
719+
UNARY(instance_float, "__float__")
720+
UNARY(instance_oct, "__oct__")
721+
UNARY(instance_hex, "__hex__")
722+
717723
static number_methods instance_as_number = {
718724
instance_add, /*nb_add*/
719725
instance_sub, /*nb_subtract*/
@@ -733,6 +739,11 @@ static number_methods instance_as_number = {
733739
instance_xor, /*nb_xor*/
734740
instance_or, /*nb_or*/
735741
instance_coerce, /*nb_coerce*/
742+
instance_int, /*nb_int*/
743+
instance_long, /*nb_long*/
744+
instance_float, /*nb_float*/
745+
instance_oct, /*nb_oct*/
746+
instance_hex, /*nb_hex*/
736747
};
737748

738749
typeobject Instancetype = {

Objects/floatobject.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,33 @@ float_coerce(pv, pw)
318318
return 1; /* Can't do it */
319319
}
320320

321+
static object *
322+
float_int(v)
323+
object *v;
324+
{
325+
double x = getfloatvalue(v);
326+
/* XXX should check for overflow */
327+
/* XXX should define how we round */
328+
return newintobject((long)x);
329+
}
330+
331+
static object *
332+
float_long(v)
333+
object *v;
334+
{
335+
double x = getfloatvalue(v);
336+
return dnewlongobject(x);
337+
}
338+
339+
static object *
340+
float_float(v)
341+
object *v;
342+
{
343+
INCREF(v);
344+
return v;
345+
}
346+
347+
321348
static number_methods float_as_number = {
322349
float_add, /*nb_add*/
323350
float_sub, /*nb_subtract*/
@@ -337,6 +364,11 @@ static number_methods float_as_number = {
337364
0, /*nb_xor*/
338365
0, /*nb_or*/
339366
float_coerce, /*nb_coerce*/
367+
float_int, /*nb_int*/
368+
float_long, /*nb_long*/
369+
float_float, /*nb_float*/
370+
0, /*nb_oct*/
371+
0, /*nb_hex*/
340372
};
341373

342374
typeobject Floattype = {

Objects/intobject.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,59 @@ int_or(v, w)
421421
return newintobject(a | b);
422422
}
423423

424+
static object *
425+
int_int(v)
426+
object *v;
427+
{
428+
INCREF(v);
429+
return v;
430+
}
431+
432+
static object *
433+
int_long(v)
434+
object *v;
435+
{
436+
long x = getintvalue(v);
437+
return newlongobject(x);
438+
}
439+
440+
static object *
441+
int_float(v)
442+
object *v;
443+
{
444+
long x = getintvalue(v);
445+
return newfloatobject((double)x);
446+
}
447+
448+
static object *
449+
int_oct(v)
450+
object *v;
451+
{
452+
char buf[20];
453+
long x = getintvalue(v);
454+
if (x == 0)
455+
strcpy(buf, "0");
456+
else if (x > 0)
457+
sprintf(buf, "0%lo", x);
458+
else
459+
sprintf(buf, "-0%lo", -x);
460+
return newstringobject(buf);
461+
}
462+
463+
static object *
464+
int_hex(v)
465+
object *v;
466+
{
467+
char buf[20];
468+
long x = getintvalue(v);
469+
if (x >= 0)
470+
sprintf(buf, "0x%lx", x);
471+
else
472+
sprintf(buf, "-0x%lx", -x);
473+
return newstringobject(buf);
474+
}
475+
476+
424477
static number_methods int_as_number = {
425478
int_add, /*nb_add*/
426479
int_sub, /*nb_subtract*/
@@ -439,6 +492,12 @@ static number_methods int_as_number = {
439492
int_and, /*nb_and*/
440493
int_xor, /*nb_xor*/
441494
int_or, /*nb_or*/
495+
0, /*nb_coerce*/
496+
int_int, /*nb_int*/
497+
int_long, /*nb_long*/
498+
int_float, /*nb_float*/
499+
int_oct, /*nb_oct*/
500+
int_hex, /*nb_hex*/
442501
};
443502

444503
typeobject Inttype = {

Objects/longobject.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,47 @@ long_coerce(pv, pw)
12681268
return 1; /* Can't do it */
12691269
}
12701270

1271+
static object *
1272+
long_int(v)
1273+
object *v;
1274+
{
1275+
long x;
1276+
x = getlongvalue(v);
1277+
if (err_occurred())
1278+
return NULL;
1279+
return newintobject(x);
1280+
}
1281+
1282+
static object *
1283+
long_long(v)
1284+
object *v;
1285+
{
1286+
INCREF(v);
1287+
return v;
1288+
}
1289+
1290+
static object *
1291+
long_float(v)
1292+
object *v;
1293+
{
1294+
return newfloatobject(dgetlongvalue(v));
1295+
}
1296+
1297+
static object *
1298+
long_oct(v)
1299+
object *v;
1300+
{
1301+
return long_format(v, 8);
1302+
}
1303+
1304+
static object *
1305+
long_hex(v)
1306+
object *v;
1307+
{
1308+
return long_format(v, 16);
1309+
}
1310+
1311+
12711312
#define UF (object* (*) FPROTO((object *))) /* Unary function */
12721313
#define BF (object* (*) FPROTO((object *, object *))) /* Binary function */
12731314
#define IF (int (*) FPROTO((object *))) /* Int function */
@@ -1292,6 +1333,11 @@ static number_methods long_as_number = {
12921333
BF long_or, /*nb_or*/
12931334
(int (*) FPROTO((object **, object **)))
12941335
long_coerce, /*nb_coerce*/
1336+
UF long_int, /*nb_int*/
1337+
UF long_long, /*nb_long*/
1338+
UF long_float, /*nb_float*/
1339+
UF long_oct, /*nb_oct*/
1340+
UF long_hex, /*nb_hex*/
12951341
};
12961342

12971343
typeobject Longtype = {

Python/bltinmodule.c

Lines changed: 40 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -253,25 +253,15 @@ builtin_float(self, v)
253253
object *self;
254254
object *v;
255255
{
256-
if (v == NULL) {
257-
/* */
258-
}
259-
else if (is_intobject(v)) {
260-
long x = getintvalue(v);
261-
return newfloatobject((double)x);
262-
}
263-
else if (is_longobject(v)) {
264-
return newfloatobject(dgetlongvalue(v));
265-
}
266-
else if (is_floatobject(v)) {
267-
INCREF(v);
268-
return v;
269-
}
270-
else if (is_instanceobject(v)) {
271-
return instance_convert(v, "__float__");
256+
number_methods *nb;
257+
258+
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
259+
nb->nb_float == NULL) {
260+
err_setstr(TypeError,
261+
"float() argument can't be converted to float");
262+
return NULL;
272263
}
273-
err_setstr(TypeError, "float() argument must be int, long or float");
274-
return NULL;
264+
return (*nb->nb_float)(v);
275265
}
276266

277267
static object *
@@ -307,22 +297,15 @@ builtin_hex(self, v)
307297
object *self;
308298
object *v;
309299
{
310-
if (v != NULL) {
311-
if (is_intobject(v)) {
312-
char buf[20];
313-
long x = getintvalue(v);
314-
if (x >= 0)
315-
sprintf(buf, "0x%lx", x);
316-
else
317-
sprintf(buf, "-0x%lx", -x);
318-
return newstringobject(buf);
319-
}
320-
if (is_longobject(v)) {
321-
return long_format(v, 16);
322-
}
300+
number_methods *nb;
301+
302+
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
303+
nb->nb_hex == NULL) {
304+
err_setstr(TypeError,
305+
"hex() argument can't be converted to hex");
306+
return NULL;
323307
}
324-
err_setstr(TypeError, "hex() requires int/long argument");
325-
return NULL;
308+
return (*nb->nb_hex)(v);
326309
}
327310

328311
static object *
@@ -354,30 +337,15 @@ builtin_int(self, v)
354337
object *self;
355338
object *v;
356339
{
357-
if (v == NULL) {
358-
/* */
359-
}
360-
else if (is_intobject(v)) {
361-
INCREF(v);
362-
return v;
363-
}
364-
else if (is_longobject(v)) {
365-
long x;
366-
x = getlongvalue(v);
367-
if (err_occurred())
368-
return NULL;
369-
return newintobject(x);
370-
}
371-
else if (is_floatobject(v)) {
372-
double x = getfloatvalue(v);
373-
/* XXX should check for overflow */
374-
return newintobject((long)x);
375-
}
376-
else if (is_instanceobject(v)) {
377-
return instance_convert(v, "__int__");
340+
number_methods *nb;
341+
342+
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
343+
nb->nb_int == NULL) {
344+
err_setstr(TypeError,
345+
"int() argument can't be converted to int");
346+
return NULL;
378347
}
379-
err_setstr(TypeError, "int() argument must be int, long or float");
380-
return NULL;
348+
return (*nb->nb_int)(v);
381349
}
382350

383351
static object *
@@ -413,25 +381,15 @@ builtin_long(self, v)
413381
object *self;
414382
object *v;
415383
{
416-
if (v == NULL) {
417-
/* */
418-
}
419-
else if (is_intobject(v)) {
420-
return newlongobject(getintvalue(v));
421-
}
422-
else if (is_longobject(v)) {
423-
INCREF(v);
424-
return v;
425-
}
426-
else if (is_floatobject(v)) {
427-
double x = getfloatvalue(v);
428-
return dnewlongobject(x);
429-
}
430-
else if (is_instanceobject(v)) {
431-
return instance_convert(v, "__long__");
384+
number_methods *nb;
385+
386+
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
387+
nb->nb_long == NULL) {
388+
err_setstr(TypeError,
389+
"long() argument can't be converted to long");
390+
return NULL;
432391
}
433-
err_setstr(TypeError, "long() argument must be int, long or float");
434-
return NULL;
392+
return (*nb->nb_long)(v);
435393
}
436394

437395
static object *
@@ -491,24 +449,15 @@ builtin_oct(self, v)
491449
object *self;
492450
object *v;
493451
{
494-
if (v != NULL) {
495-
if (is_intobject(v)) {
496-
char buf[20];
497-
long x = getintvalue(v);
498-
if (x == 0)
499-
strcpy(buf, "0");
500-
else if (x > 0)
501-
sprintf(buf, "0%lo", x);
502-
else
503-
sprintf(buf, "-0%lo", -x);
504-
return newstringobject(buf);
505-
}
506-
if (is_longobject(v)) {
507-
return long_format(v, 8);
508-
}
452+
number_methods *nb;
453+
454+
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
455+
nb->nb_oct == NULL) {
456+
err_setstr(TypeError,
457+
"oct() argument can't be converted to oct");
458+
return NULL;
509459
}
510-
err_setstr(TypeError, "oct() requires int/long argument");
511-
return NULL;
460+
return (*nb->nb_oct)(v);
512461
}
513462

514463
static object *

0 commit comments

Comments
 (0)