 |
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
Plareplane Newbie
Joined: Jul 09 2006 Posts: 14 Offline
|
Posted: 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
randpt.txt - 5.67 KB
File downloaded or viewed 9 time(s)
Last edited by Plareplane on Sun Aug 13, 2006 7:29 pm, edited 4 times in total |
|
Back to top |
|
 |
Animate Dreams Gotta buy them all! (Consumer whore)

Age:37 Gender: Joined: May 01 2004 Posts: 821 Location: Middle Tennessee Offline
|
Posted: 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.
|
|
Back to top |
|
 |
Grelminar Creator of Asss
Joined: Feb 26 2003 Posts: 378 Offline
|
Posted: 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.
|
|
Back to top |
|
 |
Plareplane Newbie
Joined: Jul 09 2006 Posts: 14 Offline
|
Posted: Fri Aug 04, 2006 1:13 pm Post subject: |
 |
|
|
|
I have this header:
#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:
/* 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?
|
|
Back to top |
|
 |
Grelminar Creator of Asss
Joined: Feb 26 2003 Posts: 378 Offline
|
Posted: 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.
|
|
Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Software by php BB © php BB Group Server Load: 36 page(s) served in previous 5 minutes.
|