Sign in to follow this  

Embedding python

This topic is 3103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After some reading I've Embedded Python in cpp (not a real game/application yet) I've been able to solve most of my problems myself but there're 3 problems that I don't get solved. - 1 - The c function PyRun_SimpleFile (sometimes) craches on windows, that's what I found out myself (with a little help of google [wink]). Is there a way around this problem or do I have to ready the file myself and use PyRun_SimpleString? - 2 - To access a Python function from the __main__ scope I wrote this:
PyObject* ModuleName = Py_BuildValue("s", "__main__");

if (ModuleName == NULL)
{
    //error
    Py_DECREF(ModuleName);
}

PyObject* Module = PyImport_Import(ModuleName);
//load and call the function



But because the __main__ scope already exists I think it unnessesary to (re)import it. Are there other ways to get a PyObject of the __main__ scope? - 3 - I have a python function declared:
def greet():
   return "Hello world"



and I call it like:
PyObject* Function = PyObject_GetAttrString(Module, "greet");

if (Function == NULL || !PyCallable_Check(Function))
{
    Py_DECREF(Module);
    //error
    return EXIT_FAILURE;
}

PyObject* Value;

Value = PyObject_CallObject(Function, NULL);



but Value (returned by CallObject) appears to be a unicode string (I thought "Hello world" was just a 8-bit string in python) and I can't get it converted it to a ancii string. How do you let Python return a 8-bit string or convert a Unicode string to a ASCII string (in c++)? That were all my problems I hope somebody can help me. Thanks in advance. [Edited by - flammable on June 9, 2009 11:43:40 AM]

Share this post


Link to post
Share on other sites
sorry I think this topic is more something for Scripting Languages and Game Mods can someone move it?

edit: Thanks [Smile]

[Edited by - flammable on June 11, 2009 10:39:32 AM]

Share this post


Link to post
Share on other sites
1) No idea. Try asking the people who you found with this problem on Google...

2)
i) That DECREF is pointless and possibly harmful - how can a null pointer have references?

ii) Once it's imported into your current namespace, it's imported. You don't need 'another way' to get an object from that scope, so your question doesn't really match the code you posted. If you're really asking how you retrieve a module that is already imported, then you'll find that is just a Python object in the namespace, which you can get from the relevant dict I presume.

3)
i) What version of Python are you using?

ii) What makes you you think it's a Unicode string?

iii) If it really is Unicode then you have to consider how you're going to handle non-ASCII characters. http://www.diveintopython.org/xml_processing/unicode.html is a reasonable place to start reading about this sort of thing.

Share this post


Link to post
Share on other sites
2)
i) Thanks, that's a stupid mistake, I'll remove it.
ii) ok.

3)
i) 3.0.1
ii) PyUnicode_Check makes me think that (I should have told that).
iii) I'll have a look at it right now.

Thanks, I know enough to go on for now.

Share this post


Link to post
Share on other sites
Python 3 has strings as Unicode by default. I would recommend you use Python 2.6 if you're new to Python. That has 8-bit strings by default and is more widely supported by 3rd party libraries. Both Python 2.6 and 3.0 are current - 3.0 does not replace 2.6, as the 2 branches are being developed in parallel for now. But it's up to you.

Share this post


Link to post
Share on other sites

This topic is 3103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this