[Python.NET] Experimental C Extensions from IronPython with Python.NET - code and article

Sanghyeon Seo sanxiyn at gmail.com
Wed Oct 24 04:54:39 CEST 2007


2007/10/24, Michael Foord <fuzzyman at voidspace.org.uk>:
> I've just posted the experimental code for accessing CPython extensions
> from IronPython.
>
> Article: http://www.voidspace.org.uk/ironpython/cpython_extensions.shtml
>
> Comments, bugfixes and improvements welcomed!

* Strings from .NET come in as unicode on the CPython type

An evil workaround is to define PyStringConvert as:
def PyStringConvert(obj):
    # evil
    return PyString(obj).GetAttr('encode').Invoke()

This could be improved on Python.NET side. Any idea?

* Can't yet pass in keyword args to function calls.

This is related to the previous item, because:
>>> def f(a): pass
>>> f(**{u'a': 1})
TypeError: f() keywords must be strings

With the previous workaround in place, replace PythonObject.__call__ with:
def __call__(self, *args, **kw):
    args = ConvertToPy(args)
    kw = ConvertToPy(kw)
    return ConvertToIpy(self.real.Invoke(args, kw))

This is more straightforward than creating PyObject[].

-- 
Seo Sanghyeon


More information about the PythonDotNet mailing list