Created
May 14, 2014 19:53
-
-
Save justinian/cbfb502a53a60b828a8b to your computer and use it in GitHub Desktop.
Simple C app to run a python file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <Python.h> | |
#ifndef PYTHON_FILE | |
#error "Please define PYTHON_FILE." | |
#endif | |
int main(int argc, char **argv) | |
{ | |
// Clear out the environment to disallow | |
// PYTHONPATH changes, or PYTHONSTARTUP, etc | |
clearenv(); | |
Py_SetProgramName(argv[0]); | |
Py_Initialize(); | |
PySys_SetArgv(argc, argv); | |
const char* filename = PYTHON_FILE; | |
// Disallow non-absolute script paths | |
if( *filename != '/' ) { | |
fprintf(stderr, PYTHON_FILE " is not an absolute path, please recompile.\n"); | |
return 1; | |
} | |
FILE* file = fopen(filename, "r"); | |
int result = 1; | |
if(file) | |
result = PyRun_SimpleFileEx(file, filename, 1); | |
Py_Finalize(); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment