embedded python example: PyString_FromString doesnt work?

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

    #1

    embedded python example: PyString_FromString doesnt work?

    I'm trying the embedded python example here:


    int
    main(int argc, char *argv[])
    {
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
    fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
    return 1;
    }


    Py_Initialize() ;
    pName = PyString_FromSt ring(argv[1]);

    .....
    }

    But it seems to break at the line;
    pName = PyString_FromSt ring(argv[1]);

    it always seems to return null whatever string is used as a parameter?
    Can anyone explain or provide a working example?

    TIA

  • Brano Zarnovican

    #2
    Re: embedded python example: PyString_FromSt ring doesnt work?

    Hi David !

    I cannot see anything wrong on your code. So, I'm posting my working
    example.

    Hint: try to determine, why it is returning NULL (the PyErr_Print()
    call)

    BranoZ


    #include <Python.h>

    int
    main(int argc, char *argv[]) {
    PyObject *s;
    int ret;

    if (argc < 2)
    return -1;

    Py_Initialize() ;

    s = PyString_FromSt ring(argv[1]);
    if (s == NULL) {
    PyErr_Print();
    return -1;
    }
    ret = PyObject_Print( s, stdout, 0);
    Py_XDECREF(s);
    if (ret < 0) {
    PyErr_Print();
    return -1;
    }

    return 0;
    }

    $ cc test_String.c -o test_String -I /usr/include/python2.3 -lpython2.3
    $ ./test_String
    $ ./test_String hello
    'hello'

    Comment

    • David Harris

      #3
      Re: embedded python example: PyString_FromSt ring doesnt work?

      On Thu, 24 Mar 2005 06:39:47 -0800, Brano Zarnovican wrote:
      [color=blue]
      > Hi David !
      >
      > I cannot see anything wrong on your code. So, I'm posting my working
      > example.
      >
      > Hint: try to determine, why it is returning NULL (the PyErr_Print()
      > call)
      >
      > BranoZ
      >[/color]

      OK your example works fine. I inserted it in the original code:
      int
      main(int argc, char *argv[])
      {
      PyObject *pName, *s, *pModule, *pDict, *pFunc;
      PyObject *pArgs, *pValue;
      int i;

      if (argc < 3) {
      fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
      return 1;
      }


      Py_Initialize() ;
      pName = PyString_FromSt ring(argv[1]);
      pModule = PyImport_Import (pName);

      if (pModule == NULL) {
      printf( "import went bang...\n");
      PyErr_Print();
      return -1;
      }
      ....
      }

      Te test script (again cut and pasted from the python site example) is just
      this:

      def multiply(a,b):
      print "Will compute", a, "times", b
      c = 0
      for i in range(0, a):
      c = c + b
      return c


      Calling the program gives an error;
      "david@palm er:~/source/python> ./test_String script1.py multiply 4 5
      import went bang...
      ImportError: No module named script1.py"
      script1.py exists and it is in the same directory as the executable so its
      not a path error or that kind of stuff.

      wierd. does: http://www.python.org/doc/2.3.2/ext/pure-embedding.html work
      for you ?

      David


      Comment

      • Denis S. Otkidach

        #4
        Re: embedded python example: PyString_FromSt ring doesnt work?

        On Thu, 24 Mar 2005 21:12:18 +0000 David Harris wrote:

        DH> int
        DH> main(int argc, char *argv[])
        DH> {
        DH> PyObject *pName, *s, *pModule, *pDict, *pFunc;
        DH> PyObject *pArgs, *pValue;
        DH> int i;
        DH>
        DH> if (argc < 3) {
        DH> fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
        DH> return 1;
        DH> }
        DH>
        DH>
        DH> Py_Initialize() ;
        [...]
        DH> Calling the program gives an error;
        DH> "david@palm er:~/source/python> ./test_String script1.py multiply 4
        DH> 5 import went bang...
        DH> ImportError: No module named script1.py"
        DH> script1.py exists and it is in the same directory as the executable
        DH> so its not a path error or that kind of stuff.

        I believe you have to call Py_SetProgramNa me before Py_Initialize, or
        otherwise let Python know that it should look for modules in this
        directory. See http://python.org/doc/current/api/embedding.html#l2h-40

        --
        Denis S. Otkidach
        http://www.python.ru/ [ru]

        Comment

        • Brano Zarnovican

          #5
          Re: embedded python example: PyString_FromSt ring doesnt work?

          > wierd. does:
          http://www.python.org/doc/2.3.­2/ext...embedding.html work[color=blue]
          > for you ?[/color]

          Yes. It does.
          [color=blue]
          > ./test_String script1.py multiply 4 5[/color]

          Don't run it with the ".py" suffix. The argv[1] is a module name, not a
          filename..

          Even if you do, it may not find the module. Depending of what you have
          in PYTHONPATH. Try also:

          export PYTHONPATH=.
          [color=blue]
          > I believe you have to call Py_SetProgramNa me before Py_Initialize,[/color]

          Yes. This is another way to affect the search path.

          BranoZ

          Comment

          • David Harris

            #6
            Re: embedded python example: PyString_FromSt ring doesnt work?

            On Fri, 25 Mar 2005 11:09:35 +0300, Denis S. Otkidach wrote:
            [color=blue]
            > DH> Calling the program gives an error;
            > DH> "david@palm er:~/source/python> ./test_String script1.py multiply 4
            > DH> 5 import went bang...
            > DH> ImportError: No module named script1.py"
            > DH> script1.py exists and it is in the same directory as the executable
            > DH> so its not a path error or that kind of stuff.
            >
            > I believe you have to call Py_SetProgramNa me before Py_Initialize, or
            > otherwise let Python know that it should look for modules in this
            > directory. See http://python.org/doc/current/api/embedding.html#l2h-40[/color]

            excellent!! that was the tip (+ "PySys_SetArgv" ) the worked ...

            This now works:

            int
            main(int argc, char *argv[])
            {
            PyObject *pName, *s, *pModule, *pDict, *pFunc;
            PyObject *pArgs, *pValue;
            int i;

            Py_SetProgramNa me(argv[0]);
            Py_Initialize() ;
            PySys_SetArgv(a rgc, argv);
            pName = PyString_FromSt ring(argv[1]);
            ....
            }

            Thanks a bunch denis :)

            Comment

            Working...