ios bootstrap: if we dont detect xcode environment, fake stdout/stderr to not crash on print, Logger, and anything that could use sys.stdout/err.

This commit is contained in:
Mathieu Virbel 2013-04-26 12:13:23 +02:00
parent cc4577186a
commit da698b29a6

View file

@ -39,8 +39,8 @@ int main(int argc, char *argv[]) {
putenv("KIVY_NO_CONSOLELOG=1");
#endif
// Export orientation preferences for Kivy
export_orientation();
// Export orientation preferences for Kivy
export_orientation();
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
NSLog(@"PythonHome is: %s", (char *)[resourcePath UTF8String]);
@ -53,8 +53,8 @@ int main(int argc, char *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();
// Add an importer for builtin modules
load_custom_builtin_importer();
// Search and start main.py
const char * prog = [
@ -85,8 +85,8 @@ int main(int argc, char *argv[]) {
// 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"];
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];
@ -101,32 +101,41 @@ void export_orientation() {
}
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);
static const char *custom_builtin_importer = \
"import sys, imp\n" \
"from os import environ\n" \
"from os.path import exists, join\n" \
"# Fake redirection when we run the app without xcode\n" \
"if 'CFLOG_FORCE_STDERR' not in environ:\n" \
" class fakestd(object):\n" \
" def write(self, *args, **kw): pass\n" \
" def flush(self, *args, **kw): pass\n" \
" sys.stdout = fakestd()\n" \
" sys.stderr = fakestd()\n" \
"# Custom builtin importer for precompiled modules\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);
}