Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
python to c interface

 
Post new topic   Reply to topic Printable version
 View previous topic  Hosting Module (MMAV'S BOT) Post :: Post Keep up with flags(python)  View next topic  
Author Message
Plareplane
Newbie


Joined: Jul 09 2006
Posts: 14
Offline

PostPosted: Thu Aug 03, 2006 6:30 am    Post subject: python to c interface Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address MSN Messenger
Animate Dreams
Gotta buy them all!
(Consumer whore)


Age:37
Gender:Gender:Male
Joined: May 01 2004
Posts: 821
Location: Middle Tennessee
Offline

PostPosted: Thu Aug 03, 2006 3:55 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address MSN Messenger
Grelminar
Creator of Asss


Joined: Feb 26 2003
Posts: 378
Offline

PostPosted: Fri Aug 04, 2006 2:17 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email Visit posters website
Plareplane
Newbie


Joined: Jul 09 2006
Posts: 14
Offline

PostPosted: Fri Aug 04, 2006 1:13 pm    Post subject: Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List AIM Address MSN Messenger
Grelminar
Creator of Asss


Joined: Feb 26 2003
Posts: 378
Offline

PostPosted: Sat Aug 05, 2006 4:54 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> ASSS Questions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 36 page(s) served in previous 5 minutes.

phpBB Created this page in 0.441069 seconds : 31 queries executed (93.8%): GZIP compression disabled