@@ -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
277267static 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
328311static 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
383351static 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
437395static 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
514463static object *
0 commit comments