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

Skip to content

Commit ed0af8f

Browse files
committed
Support __complex__ method on instances, for complex() conversion.
Keep gcc -Wall happy.
1 parent 150b2df commit ed0af8f

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

Python/bltinmodule.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,28 @@ builtin_complex(self, args)
308308
"complex() argument can't be converted to complex");
309309
return NULL;
310310
}
311+
/* XXX Hack to support classes with __complex__ method */
312+
if (is_instanceobject(r)) {
313+
static object *complexstr;
314+
object *f;
315+
if (complexstr == NULL) {
316+
complexstr = newstringobject("__complex__");
317+
if (complexstr == NULL)
318+
return NULL;
319+
}
320+
f = getattro(r, complexstr);
321+
if (f == NULL)
322+
err_clear();
323+
else {
324+
object *args = mkvalue("()");
325+
if (args == NULL)
326+
return NULL;
327+
r = call_object(f, args);
328+
DECREF(args);
329+
if (r == NULL)
330+
return NULL;
331+
}
332+
}
311333
if (is_complexobject(r))
312334
cr = ((complexobject*)r)->cval;
313335
else {
@@ -632,7 +654,7 @@ builtin_map(self, args)
632654

633655
/* XXX Special case map(None, single_list) could be more efficient */
634656
for (i = 0; ; ++i) {
635-
object *alist, *item, *value;
657+
object *alist, *item=NULL, *value;
636658
int any = 0;
637659

638660
if (func == None && n == 1)

0 commit comments

Comments
 (0)