About Python's C API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Link

    About Python's C API

    Dear sir,

    I am used to developing C program on MSVC7.0 platform.
    This is my first time to use Python for calling by C program.
    Now, it is hard to deal with the problem about extracting the variable
    which be definied in Python. That how can I do?

    I have gotten the JPython 's sample that the following code can be looked.
    Have anyone help me to obtain same results that be called by Python's C API?
    What do I mean?

    See attached the following code.

    // JPython code
    =============== =============== =========
    import org.python.util .PythonInterpre ter;
    import org.python.core .*;

    public class SimpleEmbedded {
    public static void main(String []args)
    throws PyException
    {
    PythonInterpret er interp =
    new PythonInterpret er();

    System.out.prin tln("Hello, brave new world");
    interp.exec("im port sys");
    interp.exec("pr int sys");

    interp.set("a", new PyInteger(42));
    interp.exec("pr int a");
    interp.exec("x = 2+2");
    PyObject x = interp.get("x") ;

    System.out.prin tln("x: "+x);
    System.out.prin tln("Goodbye, cruel world");
    }
    }


    I would like to re-write those program to Python.

    // C Language code =============== ===============
    void main() {
    Py_Initialize() ;
    printf("Hello, brave new world");

    PyRun_SimpleStr ing("import sys");
    PyRun_SimpleStr ing("print sys");
    // interp.set("a", new PyInteger(42));

    PyRun_SimpleStr ing("print a");
    PyRun_SimpleStr ing("x = 2+2");
    // PyObject x = interp.get("x") ;
    // System.out.prin tln("x: "+x);

    Py_Finalize();
    printf("Goodbye , cruel world");
    }

    See the above C Language code,

    After running the statement PyRun_SimpleStr ing("x = 2+2"),
    the variable x can be definied and be assigned value 4 in Python.

    Meanwhile, next one, I would like to get the x porinter.

    Could you help me how can I do?
    Or show me the other way to reach my purpose.

    Sincerely yours,
    Link


  • Jeff Epler

    #2
    Re: About Python's C API

    Let me start by saying I'm not a pro with the C API of Python.

    With PyRun_SimpleStr ing(), everything takes place in the special
    __main__ module:
    PyRun_SimpleStr ingFlags(const char *command, PyCompilerFlags *flags)
    {
    PyObject *m, *d, *v;
    m = PyImport_AddMod ule("__main__") ;
    if (m == NULL)
    return -1;
    d = PyModule_GetDic t(m);
    v = PyRun_StringFla gs(command, Py_file_input, d, d, flags);
    if (v == NULL) {
    PyErr_Print();
    return -1;
    }
    Py_DECREF(v);
    if (Py_FlushLine() )
    PyErr_Clear();
    return 0;
    }

    So, to get the attribute x from __main__, convert it to a string, and
    printf() it, you'd use this sequence [all untested]:
    PyObject *m, *d, *x, *s;

    /* m is a "new" reference */
    m = PyImport_AddMod ule("__main__") ;
    if (m == NULL)
    return -1;
    /* d is a "borrowed" reference */
    d = PyModule_GetDic t(m);
    Py_DECREF(m); m = NULL; /* Done with m -- decref and set to NULL */
    x = PyDict_GetItemS tring(d, "x")
    /* x is a "borrowed" reference */
    if (x == NULL) {
    PyErr_Print();
    return;
    }
    /* s is a "new" reference */
    s = PyObject_Str(x) ;
    if (s == NULL) {
    PyErr_Print();
    return;
    }
    printf("x: %s\n", PyString_AsStri ng(s));
    Py_DECREF(s); s = NULL; /* Done with s -- decref and set to NULL */

    If you're programming in C++, you might benefit from checking out boost.python at

    I'm not actually a boost.python user, but I've heard good things about it.

    Jeff

    Comment

    • Link

      #3
      Re: About Python's C API

      Jeff,

      First, I need to say you are so smart.
      I really appreciated your help that of course good idea to solve about my
      extraction's prblem.

      Following your advise, i obtained the value of variable from Python in
      developing C/API.

      By the way, if I had added the statement "Py_DECREF( m); m = NULL;",
      It would be crash during running program.
      So that need to remove this statement.

      Shall I ask you any problem if no this statment at here?
      Any trouble will be happened?

      With best regards.
      Link

      "Jeff Epler" <jepler@unpytho nic.net>
      ???????:mailman .1063850265.961 [email protected] ...[color=blue]
      > Let me start by saying I'm not a pro with the C API of Python.
      >
      > With PyRun_SimpleStr ing(), everything takes place in the special
      > __main__ module:
      > PyRun_SimpleStr ingFlags(const char *command, PyCompilerFlags *flags)
      > {
      > PyObject *m, *d, *v;
      > m = PyImport_AddMod ule("__main__") ;
      > if (m == NULL)
      > return -1;
      > d = PyModule_GetDic t(m);
      > v = PyRun_StringFla gs(command, Py_file_input, d, d, flags);
      > if (v == NULL) {
      > PyErr_Print();
      > return -1;
      > }
      > Py_DECREF(v);
      > if (Py_FlushLine() )
      > PyErr_Clear();
      > return 0;
      > }
      >
      > So, to get the attribute x from __main__, convert it to a string, and
      > printf() it, you'd use this sequence [all untested]:
      > PyObject *m, *d, *x, *s;
      >
      > /* m is a "new" reference */
      > m = PyImport_AddMod ule("__main__") ;
      > if (m == NULL)
      > return -1;
      > /* d is a "borrowed" reference */
      > d = PyModule_GetDic t(m);
      > Py_DECREF(m); m = NULL; /* Done with m -- decref and set to NULL */
      > x = PyDict_GetItemS tring(d, "x")
      > /* x is a "borrowed" reference */
      > if (x == NULL) {
      > PyErr_Print();
      > return;
      > }
      > /* s is a "new" reference */
      > s = PyObject_Str(x) ;
      > if (s == NULL) {
      > PyErr_Print();
      > return;
      > }
      > printf("x: %s\n", PyString_AsStri ng(s));
      > Py_DECREF(s); s = NULL; /* Done with s -- decref and set to NULL */
      >
      > If you're programming in C++, you might benefit from checking out[/color]
      boost.python at[color=blue]
      > http://boost.org/libs/python/doc/index.html
      > I'm not actually a boost.python user, but I've heard good things about it.
      >
      > Jeff
      >[/color]


      Comment

      • Ted Holden

        #4
        Re: About Python's C API

        Link wrote:
        [color=blue]
        > Dear sir,
        >
        > I am used to developing C program on MSVC7.0 platform.
        > This is my first time to use Python for calling by C program.[/color]


        Much easier to use swig than to code that sort of thing directly.


        Comment

        Working...