Skip to content

Instantly share code, notes, and snippets.

@ygmpkk
Last active December 16, 2015 16:49
Show Gist options
  • Save ygmpkk/5465802 to your computer and use it in GitHub Desktop.
Save ygmpkk/5465802 to your computer and use it in GitHub Desktop.
kivy for ios crash lanuch
Apr 26 16:46:27 7suntekizhuantou kernel[0] <Debug>: launchd[416] Builtin profile: container (sandbox)
Apr 26 16:46:27 7suntekizhuantou kernel[0] <Debug>: launchd[416] Container: /private/var/mobile/Applications/46F8871A-A51F-4D34-9D7B-CBCF38D6399B (sandbox)
Apr 26 16:46:27 7suntekizhuantou python-for-ios[416] <Warning>: *** Assertion failure in void UIApplicationInstantiateSingleton(Class)(), /SourceCache/UIKit/UIKit-2380.17/UIApplication.m:2037
Apr 26 16:46:27 7suntekizhuantou python-for-ios[416] <Error>: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
*** First throw call stack:
(0x31f462a3 0x39de697f 0x31f4615d 0x3281bb13 0x33da279d 0x33da2109 0xf225b 0x3a7ffd 0x32854277 0x31f1b5df 0x31f1b291 0x31f19f01 0x31e8cebd 0x31e8cd49 0x35a562eb 0x33da2301 0x3a7c3b 0x3a21db20)
Apr 26 16:46:28 7suntekizhuantou ReportCrash[417] <Notice>: Formulating crash report for process python-for-ios[416]
Apr 26 16:46:28 7suntekizhuantou com.apple.launchd[1] (UIKitApplication:com.zenkore.python-for-ios[0x4d6b][416]) <Warning>: (UIKitApplication:com.zenkore.python-for-ios[0x4d6b]) Job appears to have crashed: Abort trap: 6
Apr 26 16:46:28 7suntekizhuantou backboardd[26] <Warning>: Application 'UIKitApplication:com.zenkore.python-for-ios[0x4d6b]' exited abnormally with signal 6: Abort trap: 6
Apr 26 16:46:28 7suntekizhuantou ReportCrash[417] <Error>: libMobileGestalt copySystemVersionDictionaryValue: Could not lookup ReleaseType from system version dictionary
Apr 26 16:46:28 7suntekizhuantou ReportCrash[417] <Notice>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/python-for-ios_2013-04-26-164627_7suntekizhuantou.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0
//
// main.m
// iOS-python-test
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <python2.7/Python.h>
#include "SDL/SDL_main.h"
#include <dlfcn.h>
void export_orientation();
void load_custom_builtin_importer();
int main(int argc, char *argv[]) {
NSLog(@"argc: %d", argc);
int ret = 0;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Change the executing path to YourApp
chdir("YourApp");
// Special environment to prefer .pyo, and don't write bytecode if .py are found
// because the process will not have write attribute on the device.
putenv("PYTHONOPTIMIZE=2");
putenv("PYTHONDONTWRITEBYTECODE=1");
putenv("PYTHONNOUSERSITE=1");
putenv("PYTHONPATH=.");
//putenv("PYTHONVERBOSE=1");
// Kivy environment to prefer some implementation on ios platform
putenv("KIVY_BUILD=ios");
putenv("KIVY_NO_CONFIG=1");
putenv("KIVY_NO_FILELOG=1");
putenv("KIVY_WINDOW=sdl");
putenv("KIVY_IMAGE=imageio,tex");
//putenv("KIVY_IMAGE=osxcoreimage");
putenv("KIVY_AUDIO=sdl");
#ifndef DEBUG
putenv("KIVY_NO_CONSOLELOG=1");
#endif
// Export orientation preferences for Kivy
export_orientation();
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
NSLog(@"PythonHome is: %s", (char *)[resourcePath UTF8String]);
//Py_SetProgramName((char*)[[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"] UTF8String]);
Py_SetPythonHome((char *)[resourcePath UTF8String]);
NSLog(@"Initializing python");
Py_Initialize();
PySys_SetArgv(argc, argv);
// If other modules are using thread, we need to initialize them before.
PyEval_InitThreads();
// Add an importer for builtin modules
load_custom_builtin_importer();
// Search and start main.py
const char * prog = [
[[NSBundle mainBundle] pathForResource:@"YourApp/main" ofType:@"py"] cStringUsingEncoding:
NSUTF8StringEncoding];
NSLog(@"Running main.py: %s", prog);
FILE* fd = fopen(prog, "r");
if ( fd == NULL ) {
ret = 1;
NSLog(@"Unable to open main.pyo, abort.");
} else {
ret = PyRun_SimpleFileEx(fd, prog, 1);
NSLog(@"Python return: %d", ret);
if (ret != 0)
NSLog(@"Application quit abnormally!");
}
Py_Finalize();
NSLog(@"Leaving");
[pool release];
// Look like the app still runn even when we leaved here.
exit(ret);
return ret;
}
// This method read available orientations from the Info.plist, and share them
// in an environment variable. Kivy will automatically set the orientation
// according to this environment value, if exist.
void export_orientation() {
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSArray *orientations = [info objectForKey:@"UISupportedInterfaceOrientations"];
NSString *result = [[NSString alloc] initWithString:@"KIVY_ORIENTATION="];
for (int i = 0; i < [orientations count]; i++) {
NSString *item = [orientations objectAtIndex:i];
item = [item substringFromIndex:22];
if (i > 0)
result = [result stringByAppendingString:@" "];
result = [result stringByAppendingString:item];
}
putenv((char *)[result UTF8String]);
//NSLog(@"Available orientation: %@", result);
}
void load_custom_builtin_importer() {
static const char *custom_builtin_importer = \
"import sys, imp\n" \
"from os.path import exists, join\n" \
"class CustomBuiltinImporter(object):\n" \
" def find_module(self, fullname, mpath=None):\n" \
" if '.' not in fullname:\n" \
" return\n" \
" if mpath is None:\n" \
" return\n" \
" part = fullname.rsplit('.')[-1]\n" \
" fn = join(mpath[0], '{}.so'.format(part))\n" \
" if exists(fn):\n" \
" return self\n" \
" return\n" \
" def load_module(self, fullname):\n" \
" f = fullname.replace('.', '_')\n" \
" mod = sys.modules.get(f)\n" \
" if mod is None:\n" \
" #print 'LOAD DYNAMIC', f\n" \
" try:\n" \
" mod = imp.load_dynamic(f, f)\n" \
" except ImportError:\n" \
" #print 'LOAD DYNAMIC FALLBACK', fullname\n" \
" mod = imp.load_dynamic(fullname, fullname)\n" \
" return mod\n" \
" return mod\n" \
"sys.meta_path.append(CustomBuiltinImporter())";
PyRun_SimpleString(custom_builtin_importer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment