API class creation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kman3048

    #1

    API class creation


    Hello,

    as a relative newcomer to Python API programming I've got a problem:

    To extend Python:
    - there is an API C call to create a module
    - there is also a API C call to create a method
    - there is an API C call to create a Class instance

    Now, I need to create a Class and fill it with Methods and Variables.
    There are means to create (and attache) methods and variables.
    However, I have not found how to create a Class within a Module. Or do
    I have to use a low level API function to allocate an Object from Heap?

    One possible solution I think is not very elegant:
    - define a module and class within Python scripting
    - use this object by creating Instances of this object via API

    Is there no better way (however, I don't know if above works anyway).

    Thanks,
    Matt


  • Peter Maas

    #2
    Re: API class creation

    kman3048 schrieb:[color=blue]
    > Now, I need to create a Class and fill it with Methods and Variables.
    > There are means to create (and attache) methods and variables.
    > However, I have not found how to create a Class within a Module. Or do[/color]

    import aModule
    c = aClassGenerator ()
    setattr(aModule ,'c',c)
    ci = aModule.c()

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
    E-mail 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
    -------------------------------------------------------------------

    Comment

    • Daniel Dittmar

      #3
      Re: API class creation

      kman3048 wrote:[color=blue]
      > Hello,
      >
      > as a relative newcomer to Python API programming I've got a problem:
      >
      > To extend Python:
      > - there is an API C call to create a module
      > - there is also a API C call to create a method
      > - there is an API C call to create a Class instance
      >
      > Now, I need to create a Class and fill it with Methods and Variables.
      > There are means to create (and attache) methods and variables.
      > However, I have not found how to create a Class within a Module. Or do
      > I have to use a low level API function to allocate an Object from Heap?[/color]

      static PyMethodDef moduleMethods [] = {
      ...
      };

      statichere PyTypeObject MyClassType = {
      PyObject_HEAD_I NIT (NULL)
      ...
      };

      initmymodule ()
      {
      PyObject* module;
      PyObject* dict;

      module = Py_InitModule4 ("mymodule", moduleMethods,
      "doc string", NULL, PYTHON_API_VERS ION);
      if (module == NULL) {
      return;
      }
      dict = PyModule_GetDic t (module);
      PyDict_SetItemS tring (dict, "MyClass"),
      (PyObject*) &MyClassType ));
      }

      Daniel

      Comment

      • kman3048

        #4
        Re: API class creation


        Thanks, that helped a lot.

        Anybody who wants to attempt the same might find the
        source code tree of Python i.e. file src/Modules/cdmodule.c
        helpful.

        --Matt


        Daniel Dittmar wrote:[color=blue]
        > kman3048 wrote:[color=green]
        > > Hello,
        > >
        > > as a relative newcomer to Python API programming I've got a problem:
        > >
        > > To extend Python:
        > > - there is an API C call to create a module
        > > - there is also a API C call to create a method
        > > - there is an API C call to create a Class instance
        > >
        > > Now, I need to create a Class and fill it with Methods and Variables.
        > > There are means to create (and attache) methods and variables.
        > > However, I have not found how to create a Class within a Module. Or do
        > > I have to use a low level API function to allocate an Object from Heap?[/color]
        >
        > static PyMethodDef moduleMethods [] = {
        > ...
        > };
        >
        > statichere PyTypeObject MyClassType = {
        > PyObject_HEAD_I NIT (NULL)
        > ...
        > };
        >
        > initmymodule ()
        > {
        > PyObject* module;
        > PyObject* dict;
        >
        > module = Py_InitModule4 ("mymodule", moduleMethods,
        > "doc string", NULL, PYTHON_API_VERS ION);
        > if (module == NULL) {
        > return;
        > }
        > dict = PyModule_GetDic t (module);
        > PyDict_SetItemS tring (dict, "MyClass"),
        > (PyObject*) &MyClassType ));
        > }
        >
        > Daniel[/color]

        Comment

        Working...