[IronPython] why?

Curt Hagenlocher curt at hagenlocher.org
Wed Sep 15 10:02:52 CEST 2004

> the simple code
> returne True but nothing print
> why? is that right?
> from System.Console import *
> WriteLine("Hello world!")

Actually, the code is printing True but returning nothing.  If
you assign the result of the WriteLine to a variable, you'll
see that its value is None.

The bug appears to be this:
In ReflectedMethodBase.Call (file Objects/ReflectedMembers.cs),
there is code to pick one of the overloads when a particular
name has been overloaded.  This code simply goes through the
list of overloads and sees if the number of parameters match and
if each parameter can be converted from the Python type into the
desired CLR type.  The list of overloads appears to be returned
by the reflection API in the order that the functions are stored
in the assembly.  The first overload for WriteLine with one 
parameter is System.Console.WriteLine(boolean).  A call is made
to the function Ops.ConvertTo, which happily converts a string
into a boolean, resulting in that particular overload being called.

This behavior is probably a result of implementing the Python idiom
s = 'abc'
if s:

It would be nice if ReflectedMethodBase.Call would try to find
the best match instead of the first available match, but it would
probably be cleaner to eliminate the automatic cast of string to
boolean.  Is there anyplace other than a conditional expression
where this is legal in Python?

Curt Hagenlocher
curt at hagenlocher.org

More information about the Ironpython-users mailing list