[Python.NET] FromManagedObject and Booleans

Michael Foord fuzzyman at voidspace.org.uk
Fri Nov 2 17:14:10 CET 2007

Hello all,

I'm using the Python.Runtime assembly to provide access to CPython 
modules from IronPython.

I'm converting objects that pass from CPython to IronPython (and 
vice-versa) by converting datatypes or proxying access.

To convert to CPython types I use "PyObject.FromManagedObject(obj)" 
where possible and do type specific conversion for other types.

Unfortunately "PyObject.FromManagedObject(obj)" doesn't work for 
booleans and passing in booleans to CPython doesn't result in 'True' or 
'False', but a 'System.Boolean' instead.

As a workaround I'm using:

def _getBools():
    "Hack because 'FromManagedObject' doesn't work for bools"
    builtin = engine.ImportModule('__builtin__')
    return {False: builtin.GetAttr('False'), True: builtin.GetAttr('True')}

_bools = _getBools()

Is the fact that "PyObject.FromManagedObject(obj)" doesn't work for 
booleans a bug? (Or is there an alternative method you can suggest?)

All the best,

Michael Foord

More information about the PythonDotNet mailing list