python commmand line params from c++

W

Wesley Henwood

What is the proper way to pass command line parameters to a python
script called from C++? I'm tryng this:

path = "c:\\someDir\\someScript.py param1 param2 param3";
PyRun_SimpleFile(PyFile_AsFile( PyFile_FromString( path, "r")),
"someScript.py");

I'm getting a "format error someScript.py, line 1" when the code is
executed.

Note: The strange appearannce of the 3 python function calls nested is
not a bug, but required to prevent a run-time error.
 
D

Denis S. Otkidach

What is the proper way to pass command line parameters to a python
script called from C++? I'm tryng this:

Have you tried PySys_SetArgv?
path = "c:\\someDir\\someScript.py param1 param2 param3";
PyRun_SimpleFile(PyFile_AsFile( PyFile_FromString( path, "r")),
"someScript.py");

This code looks strange: you open file and create Python file object
from its descriptor (PyFile_FromString), then get descripto back
(PyFile_AsFile) to pass it to PyRun_SimpleFile. Why don't you just use
C fopen function?
I'm getting a "format error someScript.py, line 1" when the code is
executed.

Note: The strange appearannce of the 3 python function calls nested is
not a bug, but required to prevent a run-time error.

I believe it's due to PyFile_FromString failing to open non-existent
file. You must check return value of it.
 
W

Wesley Henwood

Thanks Denis. PySys_SetArgv should do the trick. The nested function
calls are there because the FILE structrure produced by fopen is not
always compatible with the FILE structure produced by the Python C++
functions. Something do with the Python C being built with different
version ofthe c run-time libs.

The problem is a bit strange and should be adressed, it's documented
somewhere on the Python site, but no solution is given. That I had to
find by trial and error. Even better would be a function such as
PyRun_File(char *filename), and do away with handling the FILE
structure altogether.
 
E

ej

Ummm, just a guess from looking at the output (not familiar with C++
interface)...
the error seems to complain about what's in someScript.py, not the call to
it.
What's in someScript.py?
 
W

Wesley Henwood

Thanks Denis. PySys_SetArgv should do the trick. The nested function
calls are there because the FILE structrure produced by fopen is not
always compatible with the FILE structure produced by the Python C++
functions. Something do with the Python C being built with different
version ofthe c run-time libs.


The FILE problem is a bit strange and should be adressed, it's
documented
somewhere on the Python site, but no solution is given. That I had to
find by trial and error. Even better would be a function such as
PyRun_File(char *filename), and do away with handling the FILE
structure altogether.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top