Using Pyrex To Write Components

First stab at writing Kamaelia Components using Pyrex. This code uses Pyrex 0.9.3, and has some issues that need resolving, but this does actually work.

File: pyrexcomponent.pyx

    #
    # Simple pyrex component
    #

    import Axon.Component.component

    cdef class test(Axon.Component.component):
      def mainBody(self):
        if self.dataReady("inbox"):
          data = self.recv("inbox")
          self.send(data, "outbox")
        return 1

File: Axon.Component.pxd

    class component:
      pass

File: setup.py

    from distutils.core import setup
    from distutils.extension import Extension
    from Pyrex.Distutils import build_ext

    setup(
      name = 'PyrexComponent',
      ext_modules=list(
        Extension("pyrexcomponent",
          ["pyrexcomponent.pyx"],
        ),
      ),
      cmdclass = {'build_ext': build_ext}
    )

Build

    > sudo python setup.py install
    Password:
    running install
    running build
    running build_ext
    building 'pyrexcomponent' extension
    .../pyrex/Axon.Component.pxd:1:0: class definition not allowed here
    .../pyrex/pyrexcomponent.pyx:7:5: 'component' is not declared
    > sudo python setup.py install
    running install
    running build
    running build_ext
    building 'pyrexcomponent' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -g -fPIC -I/usr/include/python2.4 -c
    pyrexcomponent.c -o build/temp.linux-i686-2.4/pyrexcomponent.o
    pyrexcomponent.c: In function `__pyx_tp_new_14pyrexcomponent_test':
    pyrexcomponent.c:187: warning: unused variable `p'
    pyrexcomponent.c: In function `__pyx_tp_dealloc_14pyrexcomponent_test':
    pyrexcomponent.c:192: warning: unused variable `p'
    pyrexcomponent.c: In function `__pyx_tp_traverse_14pyrexcomponent_test':
    pyrexcomponent.c:197: warning: unused variable `e'
    pyrexcomponent.c:198: warning: unused variable `p'
    pyrexcomponent.c: In function `__pyx_tp_clear_14pyrexcomponent_test':
    pyrexcomponent.c:203: warning: unused variable `p'
    pyrexcomponent.c: At top level:
    pyrexcomponent.c:12: warning: `__Pyx_UnpackItem' declared `static' but never defined
    pyrexcomponent.c:13: warning: `__Pyx_EndUnpack' declared `static' but never defined
    pyrexcomponent.c:14: warning: `__Pyx_PrintItem' declared `static' but never defined
    pyrexcomponent.c:15: warning: `__Pyx_PrintNewline' declared `static' but never defined
    pyrexcomponent.c:16: warning: `__Pyx_Raise' declared `static' but never defined
    pyrexcomponent.c:17: warning: `__Pyx_ReRaise' declared `static' but never defined
    pyrexcomponent.c:19: warning: `__Pyx_GetExcValue' declared `static' but never defined
    pyrexcomponent.c:20: warning: `__Pyx_ArgTypeTest' declared `static' but never defined
    pyrexcomponent.c:21: warning: `__Pyx_TypeTest' declared `static' but never defined
    pyrexcomponent.c:22: warning: `__Pyx_GetStarArgs' declared `static' but never defined
    pyrexcomponent.c:23: warning: `__Pyx_WriteUnraisable' declared `static' but never defined
    pyrexcomponent.c:25: warning: `__Pyx_ImportType' declared `static' but never defined
    pyrexcomponent.c:26: warning: `__Pyx_SetVtable' declared `static' but never defined
    pyrexcomponent.c:27: warning: `__Pyx_GetVtable' declared `static' but never defined
    pyrexcomponent.c:28: warning: `__Pyx_CreateClass' declared `static' but never defined
    pyrexcomponent.c:31: warning: `__Pyx_GetName' declared `static' but never defined
    gcc -pthread -shared build/temp.linux-i686-2.4/pyrexcomponent.o -o build/lib.linux-i686-2.4/pyrexcomponent.so
    running install_lib
    copying build/lib.linux-i686-2.4/pyrexcomponent.so -> /usr/lib/python2.4/site-packages

Creating an instance

    > python
    Python 2.4 (#1, Mar 22 2005, 21:42:42)
    [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from Axon.Component import component
    >>> import pyrexcomponent
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "pyrexcomponent.pyx", line 12, in pyrexcomponent
    import Axon.Component.component
    ImportError: No module named component
    >>> import pyrexcomponent
    >>> pyrexcomponent
    <module 'pyrexcomponent' (built-in)>

    >>> class foo(pyrexcomponent.test, component): pass
    ...
    >>> x=foo()
    >>> x
    >>> component.__init__(x)
    >>> x.inboxes
    >>> x.mainBody
    <built-in method mainBody of foo object at 0x404a4a04>
    >>> x.mainBody.__doc__
    >>> x.activate()
    <__main__.foo object at 0x404a4a04>
    >>> x.mainBody.__doc__
    >>> x._deliver("hello")
    >>> x._deliver("world")
    >>> x.next()
    1
    >>> x.next()
    1
    >>> x.next()
    1
    >>> x._collect()
    'hello'
    >>> x._collect()
    'world'

Summary

There's clearly some minor issues here and scope for improvement, but this clearly shows that you can write components in pyrex, which therefore means you can pull in any library with a C interface with relative ease.

 

Kamaelia is an open source project originated from and guided by BBC Research. For more information browse the site or get in contact.

This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)

(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved

This web site is powered by the same code created for the bicker manor project. For more details, contact Michael Sparks at BBC Research directly (cf contact)