Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views


Sign in to follow this  


So I'm designing the interface between Python<->C++. Now, there already is an interface. But you have to code your functions specifically so they match the typedef -

PyObject* ( *func )( PyObject*, PyObject* )

Which is bad, because we want to integrate Python directly into our program without having to specificially mark functions like THIS_IS_A_PYTHON_FUNCTION_LOL because that's silly.

So, what I've got in mind is an intermediate function which intercepts the call from Python, parses the tuple that the arguments come packed in, then calls the appropriate C-function.

The problem is the syntax. The only way I can figure out how to do it is to store the C-functions in a string-to-functor map. Which means, from python, you'd use something like -

module.call( "funcname", arg1, arg2, arg3, etc )

Which is, essentially, the same method I'm using to call Python functions from C++ -

python::call( "funcname", arg1, arg2, arg3, etc )

Now, ideally, you'd want to bind your functions directly to uniquely-named identifiers in Python (which can be done). The problem with this is the fact that the intermediate function needs to be called to resolve the arguments (from either direction, C->Python calls need to have their arguments packed into a tuple then passed to Python too).

Likewise, you also need to expose functionality between the two languages. This is done with the expose function -

module.expose( some_function, "func_name_to_bind_to" )

python::expose( some_functor, "func_name_to_bind_to" )

Again, its very symmetric, which is a good thing and a bad thing. Its good because its somewhat consistant. Its bad because it doesn't make full use of the power of Python; its essentially limited by the bounds of what C++ can do.

What I really need is a way to retrieve the name of the function being called from Python, so if, say -


Were called, I'd be able to retrieve the string somefunc. This would allow me to go beyond the call/expose pattern by actually adding functions into the module dictionary (which you can't really do in C++).

Anyway. I have to go implement all of this... after lunch. Mmmm I need lunch.
Sign in to follow this  

1 Comment

Recommended Comments

Guest Anonymous Poster


You might be able to do the string thing with some special method names. I'm not sure if that applies to modules, though... maybe it only works for classes.
I think that a better way to go would be to create a wrapper function and associate a closure with it. The closure would store a pointer to a functor or a function, which the wrapper would then call with the unpacked arguments. Check the docs.
Disclaimer: I've never actually tried this :P. Good luck!

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!