@@ -417,13 +417,19 @@ static object *
417417instance_concat (inst , other )
418418 instanceobject * inst , * other ;
419419{
420- object * func , * res ;
420+ object * func , * arg , * res ;
421421
422422 func = instance_getattr (inst , "__add__" );
423423 if (func == NULL )
424424 return NULL ;
425- res = call_object (func , (object * )other );
425+ arg = mkvalue ("(O)" , other );
426+ if (arg == NULL ) {
427+ DECREF (func );
428+ return NULL ;
429+ }
430+ res = call_object (func , arg );
426431 DECREF (func );
432+ DECREF (arg );
427433 return res ;
428434}
429435
@@ -568,12 +574,18 @@ generic_binary_op(self, other, methodname)
568574 object * other ;
569575 char * methodname ;
570576{
571- object * func , * res ;
577+ object * func , * arg , * res ;
572578
573579 if ((func = instance_getattr (self , methodname )) == NULL )
574580 return NULL ;
575- res = call_object (func , other );
581+ arg = mkvalue ("O" , other );
582+ if (arg == NULL ) {
583+ DECREF (func );
584+ return NULL ;
585+ }
586+ res = call_object (func , arg );
576587 DECREF (func );
588+ DECREF (arg );
577589 return res ;
578590}
579591
@@ -653,6 +665,45 @@ BINARY(instance_and, "__and__")
653665BINARY (instance_xor , "__xor__" )
654666BINARY (instance_or , "__or__" )
655667
668+ static int
669+ instance_coerce (pv , pw )
670+ object * * pv , * * pw ;
671+ {
672+ object * v = * pv ;
673+ object * w = * pw ;
674+ object * func ;
675+ object * res ;
676+ int outcome ;
677+
678+ if (!is_instanceobject (v ))
679+ return 1 ; /* XXX shouldn't be possible */
680+ func = instance_getattr ((instanceobject * )v , "__coerce__" );
681+ if (func == NULL ) {
682+ err_clear ();
683+ return 1 ;
684+ }
685+ res = call_object (func , w );
686+ if (res == NULL )
687+ return -1 ;
688+ if (res == None ) {
689+ DECREF (res );
690+ return 1 ;
691+ }
692+ outcome = getargs (res , "(OO)" , & v , & w );
693+ if (!outcome || v -> ob_type != w -> ob_type ||
694+ v -> ob_type -> tp_as_number == NULL ) {
695+ DECREF (res );
696+ err_setstr (TypeError , "bad __coerce__ result" );
697+ return -1 ;
698+ }
699+ INCREF (v );
700+ INCREF (w );
701+ DECREF (res );
702+ * pv = v ;
703+ * pw = w ;
704+ return 0 ;
705+ }
706+
656707static number_methods instance_as_number = {
657708 instance_add , /*nb_add*/
658709 instance_sub , /*nb_subtract*/
@@ -671,6 +722,7 @@ static number_methods instance_as_number = {
671722 instance_and , /*nb_and*/
672723 instance_xor , /*nb_xor*/
673724 instance_or , /*nb_or*/
725+ instance_coerce , /*nb_coerce*/
674726};
675727
676728typeobject Instancetype = {
@@ -690,58 +742,6 @@ typeobject Instancetype = {
690742 & instance_as_mapping , /*tp_as_mapping*/
691743};
692744
693- static int
694- one_coerce (pv , pw )
695- object * * pv , * * pw ;
696- {
697- object * v = * pv ;
698- object * w = * pw ;
699- object * func ;
700-
701- if (!is_instanceobject (v ))
702- return 1 ;
703- func = instance_getattr ((instanceobject * )v , "__coerce__" );
704- if (func == NULL ) {
705- err_clear ();
706- return 1 ;
707- }
708- if (func != NULL ) {
709- object * res = call_object (func , w );
710- int outcome ;
711- if (res == NULL )
712- return -1 ;
713- outcome = getargs (res , "(OO)" , & v , & w );
714- if (!outcome || v -> ob_type != w -> ob_type ||
715- v -> ob_type -> tp_as_number == NULL ) {
716- DECREF (res );
717- err_setstr (TypeError , "bad __coerce__ result" );
718- return -1 ;
719- }
720- INCREF (v );
721- INCREF (w );
722- DECREF (res );
723- * pv = v ;
724- * pw = w ;
725- return 0 ;
726- }
727- }
728-
729- int
730- instance_coerce (pv , pw )
731- object * * pv , * * pw ;
732- {
733- int outcome ;
734- outcome = one_coerce (pv , pw );
735- if (outcome > 0 ) {
736- outcome = one_coerce (pw , pv );
737- if (outcome > 0 ) {
738- err_setstr (TypeError , "uncoerceable instance" );
739- outcome = -1 ;
740- }
741- }
742- return outcome ;
743- }
744-
745745object *
746746instance_convert (inst , methodname )
747747 object * inst ;
0 commit comments