#import "Python/Python.h"
http://stackoverflow.com/questions/9749260/python-for-ios-interpreter
A little trip to Ubuntu 16.04
Install Python 3.5
1
sudo apt-get install python3 python3-dev
helloworld.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <Python.h> int main(int argc, char *argv[]) { wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); exit(1); } Py_SetProgramName(program); /* optional but recommended */ Py_Initialize(); PyRun_SimpleString("from time import time,ctime\n" "print('Today is', ctime(time()))\n"); Py_Finalize(); PyMem_RawFree(program); return 0; }
build
1 3
gcc -c hellowworld.c gcc helloworld.o $(/usr/bin/python3.5-config --ldflags)
Calling Python from C and fetching return values
Python: sample.py
1 2 3
# Returns the sum of two numbers. def add(a, b): return a+b
sample.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <python3.5/Python.h> #include <stdio.h>
int main(int argc, char* argv[])
{ char localPath[40] = "\/home\/XXX\/XXX\/"; printf("Calling Python to find the sum of 2 and 2.\n"); // Initialize the Python interpreter. Py_Initialize(); // Create some Python objects that will later be assigned values. PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue; // Convert the file name to a Python string. pName = PyUnicode_FromString("sample"); PyObject *sys_path = PySys_GetObject("path"); PyList_Append(sys_path, PyUnicode_FromString(localPath)); // Import the file as a Python module. pModule = PyImport_Import(pName); // Create a dictionary for the contents of the module. pDict = PyModule_GetDict(pModule); // Get the add method from the dictionary. pFunc = PyDict_GetItemString(pDict, "add"); // Create a Python tuple to hold the arguments to the method. pArgs = PyTuple_New(2); // Convert 2 to a Python integer. pValue = PyLong_FromLong(2); // Set the Python int as the first and second arguments to the method. PyTuple_SetItem(pArgs, 0, pValue); PyTuple_SetItem(pArgs, 1, pValue); // Call the function with the arguments. PyObject* pResult = PyObject_CallObject(pFunc, pArgs); // Print a message if calling the method failed. if(pResult == NULL) printf("Calling the add method failed.\n"); // Convert the result to a long from a Python object. long result = PyLong_AsLong(pResult); // Destroy the Python interpreter. Py_Finalize(); // Print the result. printf("The result is %d.\n", result); //std::cin.ignore(); return 0; }
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
- (void)helloWorld { printf("Calling Python to find the sum of 2 and 2.\n"); NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; PyObject *sys_path = PySys_GetObject("path"); PyList_Append(sys_path, PyUnicode_FromString([resourcePath UTF8String])); // Special environment to avoid writing bytecode because // the process will not have write attribute on the device. putenv("PYTHONDONTWRITEBYTECODE=1"); NSString *python_home = [NSString stringWithFormat:@"%@/Library/Python.framework/Resources", resourcePath, nil]; NSLog(@"PythonHome is: %@", python_home); wchar_t *wpython_home = Py_DecodeLocale([python_home UTF8String], NULL); Py_SetPythonHome(wpython_home); // iOS provides a specific directory for temp files. NSString *tmp_path = [NSString stringWithFormat:@"TMP=%@/tmp", resourcePath, nil]; putenv((char *)[tmp_path UTF8String]); // Initialize the Python interpreter. Py_Initialize(); // Create some Python objects that will later be assigned values. PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue; // Convert the file name to a Python string. pName = PyUnicode_FromString("sample"); // Import the file as a Python module. pModule = PyImport_Import(pName); // Create a dictionary for the contents of the module. pDict = PyModule_GetDict(pModule); // Get the add method from the dictionary. pFunc = PyDict_GetItemString(pDict, "add"); // Create a Python tuple to hold the arguments to the method. pArgs = PyTuple_New(2); // Convert 2 to a Python integer. pValue = PyLong_FromLong(2); // Set the Python int as the first and second arguments to the method. PyTuple_SetItem(pArgs, 0, pValue); PyTuple_SetItem(pArgs, 1, pValue); // Call the function with the arguments. PyObject* pResult = PyObject_CallObject(pFunc, pArgs); // Print a message if calling the method failed. if(pResult == NULL) printf("Calling the add method failed.\n"); // Convert the result to a long from a Python object. long result = PyLong_AsLong(pResult); // Destroy the Python interpreter. Py_Finalize(); // Print the result. printf("The result is %d.\n", result); }
No comments:
Post a Comment