Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(126828)

Side by Side Diff: iOS/XCode-sample/sample/main.m

Issue 23670: Modifications to support iOS as a development platform
Patch Set: Created 3 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //
2 // main.m
3 // A main module for starting Python projects under iOS.
4 //
5
6 #import <Foundation/Foundation.h>
7 #import <UIKit/UIKit.h>
8 #include <Python/Python.h>
9 #include <dlfcn.h>
10
11 int main(int argc, char *argv[]) {
12 int ret = 0;
13 unsigned int i;
14 NSString *tmp_path;
15 NSString *python_home;
16 wchar_t *wpython_home;
17 const char* main_script;
18 wchar_t** python_argv;
19
20 @autoreleasepool {
21
22 NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
23
24 // Special environment to prefer .pyo; also, don't write bytecode
25 // because the process will not have write permissions on the device.
26 putenv("PYTHONOPTIMIZE=1");
27 putenv("PYTHONDONTWRITEBYTECODE=1");
28
29 python_home = [NSString stringWithFormat:@"%@/Library/Python.framework/R esources", resourcePath, nil];
30 NSLog(@"PythonHome is: %@", python_home);
31 wpython_home = _Py_char2wchar([python_home UTF8String], NULL);
32 Py_SetPythonHome(wpython_home);
33
34 // iOS provides a specific directory for temp files.
35 tmp_path = [NSString stringWithFormat:@"TMP=%@/tmp", resourcePath, nil];
36 putenv((char *)[tmp_path UTF8String]);
37
38 NSLog(@"Initializing Python runtime");
39 Py_Initialize();
40
41 // Set the name of the main script
42 main_script = [
43 [[NSBundle mainBundle] pathForResource:@"Library/Application Support /org.python.sample/app/sample/main"
44 ofType:@"py"] cStringUsingEncoding:N SUTF8StringEncoding];
45
46 if (main_script == NULL) {
47 NSLog(@"Unable to locate app/sample/main.py file");
48 exit(-1);
49 }
50
51 // Construct argv for the interpreter
52 python_argv = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
53
54 python_argv[0] = _Py_char2wchar(main_script, NULL);
55 for (i = 1; i < argc; i++) {
56 python_argv[i] = _Py_char2wchar(argv[i], NULL);
57 }
58
59 PySys_SetArgv(argc, python_argv);
60
61 // If other modules are using threads, we need to initialize them.
62 PyEval_InitThreads();
63
64 // Start the main.py script
65 NSLog(@"Running %s", main_script);
66
67 @try {
68 FILE* fd = fopen(main_script, "r");
69 if (fd == NULL) {
70 ret = 1;
71 NSLog(@"Unable to open main.py, abort.");
72 } else {
73 ret = PyRun_SimpleFileEx(fd, main_script, 1);
74 if (ret != 0) {
75 NSLog(@"Application quit abnormally!");
76 } else {
77 // In a normal iOS application, the following line is what
78 // actually runs the application. It requires that the
79 // Objective-C runtime environment has a class named
80 // "PythonAppDelegate". This project doesn't define
81 // one, because Objective-C bridging isn't something
82 // Python does out of the box. You'll need to use
83 // a library like Rubicon-ObjC [1], Pyobjus [2] or
84 // PyObjC [3] if you want to run an *actual* iOS app.
85 // [1] http://pybee.org/rubicon
86 // [2] http://pyobjus.readthedocs.org/
87 // [3] https://pythonhosted.org/pyobjc/
88
89 UIApplicationMain(argc, argv, nil, @"PythonAppDelegate");
90 }
91 }
92 }
93 @catch (NSException *exception) {
94 NSLog(@"Python runtime error: %@", [exception reason]);
95 }
96 @finally {
97 Py_Finalize();
98 }
99
100 PyMem_RawFree(wpython_home);
101 if (python_argv) {
102 for (i = 0; i < argc; i++) {
103 PyMem_RawFree(python_argv[i]);
104 }
105 PyMem_RawFree(python_argv);
106 }
107 NSLog(@"Leaving");
108 }
109
110 exit(ret);
111 return ret;
112 }
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+