Server Help

ASSS Questions - python to c interface

Plareplane - Thu Aug 03, 2006 6:30 am
Post subject: python to c interface
I have a python module that generates a random point within a region. I can use it from other python modules by importing. What do I need to do to use it from a c module?

Code attached.
Edit: needs a patch to run
Edit: previous patch and code were untested and were found to be rather deficient after some testing. this version is at least minimally tested and hopefully less deficient
Animate Dreams - Thu Aug 03, 2006 3:55 pm
Post subject:
Um... I don't think you can do that. As I understand it, there is no way to reference a Python module from a C module. You can do C from C, C from Python, Python from Python... but that's it.
Grelminar - Fri Aug 04, 2006 2:17 am
Post subject:
You can do it, it's just not very convenient. You need to make a .h file with your interface and appropriate pyint comments, making sure that the first pyint line contains "impl". Then recompile pymod (you might have to "make dep" first), and you'll be able to register your implementation from python and call it from C. See, for example, Iflaggame in include/flagcore.h and implementations in py/fg_wz.py and py/fg_turf.py.
Plareplane - Fri Aug 04, 2006 1:13 pm
Post subject:
I have this header:

Code: Show/Hide

#ifndef __RANDPT_H
#define __RANDPT_H
#define I_RANDPT "randpt-1"
typedef struct Irandpt
{
      INTERFACE_HEAD_DECL
      /* pyint: impl */
      void (*RandomPoint)(Arena *a, Region *rgn, int *x, int *y);
      /* pyint: arena, region, int out, int out -> void */
} Irandpt;
#endif


but py_interfaces.inc has this snippet:

Code: Show/Hide

/* implementing interface I_RANDPT in python {{{ */

local void  pyint_func_I_RANDPT_Check(Arena *arg1_in, Region * arg2_in, int *arg3_out, int *arg4_out)
{
   PyObject *args, *out;
   int arg3_out_v;
   int arg4_out_v;
   args = Py_BuildValue("(O&O&)", cvt_c2p_arena, arg1_in, cvt_c2p_region, arg2_in);
   if (!args)
   {
      log_py_exception(L_ERROR, "python error building args for "
            "function Check in interface I_RANDPT");
      return ;
   }
   out = call_gen_py_interface(PYINTPREFIX I_RANDPT, "Check", args, arg1_in);
   if (!out)
   {
      log_py_exception(L_ERROR, "python error calling "
            "function Check in interface I_RANDPT");
      return ;
   }

   if (!PyArg_ParseTuple(out, "ii", &arg3_out_v, &arg4_out_v))
   {
      Py_DECREF(out);
      log_py_exception(L_ERROR, "python error unpacking results of "
            "function Check in interface I_RANDPT");
      return ;
   }

   *arg3_out = arg3_out_v;
   *arg4_out = arg4_out_v;
   Py_DECREF(out);
   return ;
}

local struct Irandpt pyint_int_I_RANDPT = {
   INTERFACE_HEAD_INIT(I_RANDPT, "pyint-I_RANDPT")
   pyint_func_I_RANDPT_Check
};


/* }}} */


Did I do something to cause RandomPoint to be renamed to Check?
Grelminar - Sat Aug 05, 2006 4:54 am
Post subject:
The pymod parser is really really picky. You need exactly one tab character in front of "void". See the regexes at the top of pymod-process.py.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group