3

I've initialized the Python environment by

Py_Initialize();

I have no external Python module imported into the environment and everything works well. But when I need to pass a C string into this environment, I am lost...

I was thinking to add a function in the environment to assign the variable like the following code do.

char *str;
str="\
def assign_var(str):\
    global string\
    string = str";
PyRun_SimpleString(str);

And then call this function in C and pass the converted C string as the arguments.

I don't think all I mentioned above is a good way solve the problem...

How can I make this work?

Solution:

Finally, here's the solution with Peter Mortensen's help. (Thanks Peter Mortensen!)

As the python environment I've initialized is a pure empty environment(without any imported modules). I use

py_main = PyImport_AddModule("__main__");

to get a hook to the main environment. and then call

PyModule_AddStringConstant(py_main, "string_name", str);

to bring the C string into the python environment.

To verify everything is done, just try:

PyRun_SimpleString("print dir()");
PyRun_SimpleString("print string_name");

and you'll see you "string_name" string appears in the dir() list and make it print by python!

3
  • Perhaps a variation on this would work? Commented Jun 8, 2011 at 16:34
  • @senderle sorry, that case, he already has a variable g in the module, but in my case I've none... thanks to your comment, I've reprecised the title of my question(added 'creat') Commented Jun 8, 2011 at 16:45
  • my thought was that you might be able to acquire a reference to __main__ using PySys_GetObject. But without knowing exactly what you're doing it's a bit hard to tell what you want to happen. Commented Jun 9, 2011 at 3:56

1 Answer 1

2

This should do what you want:

char *cStr = "Some text here.";

PyObject *pyStr = Py_BuildValue("s", cStr);

http://docs.python.org/c-api/arg.html#Py_BuildValue

Of course if you're using Python 3 (or use it in the future), there may be situations where you'd want to use "y" instead of "s" and get a bytes object rather than a str.

UPDATE: Woops, I forgot the even easier way of doing it.

PyObject *pyStr = PyString_FromString(cStr);

http://docs.python.org/c-api/string.html#PyString_FromString

(It'd be PyBytes_FromString() in Python 3.)

You might want to take a look at http://docs.python.org/extending/embedding.html for some more information.


Here's something else you might want to try. See

http://docs.python.org/c-api/module.html#PyModule_AddObject
Or possibly
http://docs.python.org/c-api/module.html#PyModule_AddStringConstant

With the former it'd be something like

errorcheck = PyModule_AddObject(embmodule, "str", pyStr);

And with the latter, something like

errorcheck = PyModule_AddStringConstant(embmodule, "str", cStr);
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, this is not what I'm looking for... all the functions you mentioned here return always PyObject* and nothing happens in the python environment. The python string stays still in C environment not in the python one. Do you have any idea about transfering the python string into python environment from the C one?
Look at docs.python.org/extending/…. You'll need to write a function that returns the string as a PyObject using the same PyString_FromString() function I showed, make an array of PyMethodDef objects, and then initialize a module with that array of methods. You'd then import your module from the Python side and call the methods of the module to access the data.
PyMethodDef structs, rather.
Hi, sorry for my late response. I've finally found a way to creat and assign a string. As you mentioned, I'm using Module_AddStringConstant(mod, "name", cStr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.