Advertisement

Instantiating Python classes from C++

Started by December 09, 2004 02:29 AM
13 comments, last by Ostsol 19 years, 9 months ago
I'm quite sketchy on this. The documentation doesn't seem to explicitly describe how to do this, though I'm sure I've missed something. I've noted a function in the C API for this, but I'm using Boost and would prefer some uniformity in my code. Boost, after all, has functionality for calling Python classes' member functions and extracting values from the main namespace. However, I can't find any documentation on instantiating a Python class. Here's what I've managed to hack together:
handle<> (PyRun_String ("retObject = CPythonClass ()", Py_file_input, oMainNamespace.ptr (), oMainNamespace.ptr ()));

object oRetObject = oMainNamespace["retObject"];
Basically, I instantiate the class in Python and retrieve the object. It doesn't seem particularly elegant and I'm hoping that there's a better way. Any tips, thoughts, etc? Background: I've posted before about my desire to incorporate a scripting language into my engine that will allow some capabilities similar to that provided by UnrealScript in Epic's Unreal Engine. I want all game content to be defined and scripted using the scripting language, each class being derived from a C++ class. As such, it becomes clear as to why I need to instantiate Python classes. The engine will keep track of all objects because it will need to access state from instances of the C++ base class and call certain functions, such as for drawing and triggering event handlers.
-Ostsol
There's always the hard way.
---New infokeeps brain running;must gas up!
Advertisement
Erg. . . Yeah, I'd found that, but I was kinda hoping for a Boost solution. :)
-Ostsol
Don't use Python for that, unless you want coders formatting the harddisk of your players. Python's not sandboxed, so your Python code can do *anything* to the user machine. Also, the Python code has some tricks with introspection that mean you could pleasantly bork your egine's Python interpreter quite easily.

In short: don't embed Python if you're running untrusted code.
-- Single player is masturbation.
Hmm. . . Fun. Is there a way to trim down Python to limit what it can do? If I could some how take away certain potentially dangerous capabilities and use Python as a static library (if that's possible), it should be more secure.
-Ostsol
Quote: Original post by Ostsol
Hmm. . . Fun. Is there a way to trim down Python to limit what it can do? If I could some how take away certain potentially dangerous capabilities and use Python as a static library (if that's possible), it should be more secure.


I've been trying to get a straight answer out of the Python people on that issue for months. Zope has a sandboxed Python interpreter, but its got some thoroughly ungoogleable name (I think they call it Python Script) and again, the Zope people won't reply to my questions about a separate redistributable (unless you think having a full webserver and database is worth it when all you want is the scripting language).
-- Single player is masturbation.
Advertisement
How about writing you own, sandboxed scripting language? You can use Boost::Spirit for the parsing :P

Actually that's what I prefer to do, but it is a lot of work getting a decent scripting language running. In my expirience one of the hardest part of creating your own language is to lay out a sane system that deals with a sandboxed environment, GC, functions & oop, optionally a typeless language and last but not least: exposure of c++ api to the script.

If you are up for the task, I'd say a homebrewn scripting language aimed at your specific game is the best solution. Just be prepared for a lot of work with it :)
In case you were wondering what to put in your next christian game; don't ask me, because I'm an atheist, an infidel, and all in all an antitheist. If that doesn't bother you, visit my site that has all kinds of small utilities and widgets.
Quote: Original post by Joakim_ar
How about writing you own, sandboxed scripting language? You can use Boost::Spirit for the parsing :P

Actually that's what I prefer to do, but it is a lot of work getting a decent scripting language running. In my expirience one of the hardest part of creating your own language is to lay out a sane system that deals with a sandboxed environment, GC, functions & oop, optionally a typeless language and last but not least: exposure of c++ api to the script.

If you are up for the task, I'd say a homebrewn scripting language aimed at your specific game is the best solution. Just be prepared for a lot of work with it :)


Or just save yourself an asslode of work and use Lua or some other language designed for it. Hell, Java might not be bad as a scripting language - its sandboxable and fast.
-- Single player is masturbation.
I don't like Lua's object oriented programming support. It's feels quite low-level.

Java wouldn't be bad, I suppose, though it does have a much larger runtime environment.

Still, in any case, does anyone have an answer to my original question? :)
-Ostsol
You have the sources of Python, and can do whatever you want...

I had a version with changed compilation Tag, in order to prevent direct reverse engineering, and to limit file access to a directory I set...


Hope it helps,

This topic is closed to new replies.

Advertisement