Commits

Bill Bumgarner  committed 0d8ec14

Refactored intialization code, fixing a couple of bugs and moving a bunch of code out of __main__.py.

  • Participants
  • Parent commits 84a900b

Comments (0)

Files changed (4)

File pyobjc/Examples/WebServicesTool/Web Services Tool.pbproj/project.pbxproj

 		};
 		080E96DDFE201D6D7F000001 = {
 			children = (
-				F6D75F2C039008BF01A801C8,
 				F6646A4E0344927801F2B36F,
 				F6646A500344928B01F2B36F,
 				F6646A5C0344985501F2B36F,
 		};
 		29B97315FDCFA39411CA2CEA = {
 			children = (
-				29B97316FDCFA39411CA2CEA,
-				F6D75F2D0390096901A801C8,
 				F6AAC478035708BD01B76035,
 			);
 			isa = PBXGroup;
 			path = "";
 			refType = 4;
 		};
-		29B97316FDCFA39411CA2CEA = {
-			fileEncoding = 30;
-			isa = PBXFileReference;
-			path = "main-embedded-interpreter.m";
-			refType = 4;
-		};
 		29B97317FDCFA39411CA2CEA = {
 			children = (
 				F62CEFC10357250301718D22,
 			settings = {
 			};
 		};
-		F6D75F2C039008BF01A801C8 = {
-			indentWidth = 2;
-			isa = PBXFileReference;
-			path = Main.py;
-			refType = 4;
-			usesTabs = 0;
-		};
-		F6D75F2D0390096901A801C8 = {
-			indentWidth = 2;
-			isa = PBXFileReference;
-			path = "main-bin-python.m";
-			refType = 4;
-			usesTabs = 0;
-		};
 		F6ED47D803453061016073B7 = {
 			isa = PBXFileReference;
 			path = Preferences.png;

File pyobjc/Examples/WebServicesTool/__main__.py

 import sys
-import os.path
-
-sys.path.insert(0, os.path.join(sys.path[0], "pyobjc"))
 
 import objc
 import Foundation
 import AppKit
 
-# automatically load frameworks that were linked into the bootstrap tooll
-pyFrameworkPathsIndex = sys.argv.index("-PyFrameworkPaths")
-if not (pyFrameworkPathsIndex == -1):
-  import string
-  from Foundation import NSBundle
-  paths = string.split(sys.argv[pyFrameworkPathsIndex + 1], ":")
-  count = 0
-  for path in paths:
-    bundle = NSBundle.bundleWithPath_(path)
-    bundle.principalClass()
-    sys.path.insert(count, bundle.resourcePath())
-    count = count + 1
-    
-    initPath = bundle.pathForResource_ofType_( "Init", "py")
-    if initPath:
-      execfile(initPath, globals(), locals())
-
+# import classes required to start application
 import WSTApplicationDelegateClass
 import WSTConnectionWindowControllerClass
 

File pyobjc/Examples/WebServicesTool/bin-python-main.m

     char** childEnvp;
     char*  PYTHONPATH = NULL;
 
+    // set up paths to be prepended to the PYTHONPATH
+    const char *pythonPathInWrapper = [[NSString stringWithFormat: @"%@:%@",
+        [[NSBundle mainBundle] resourcePath],
+        [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"pyobjc"]] UTF8String];
+    
     // count entries in environment and find the PYTHONPATH setting, if present
     for (envc = 0; envp[envc] != NULL; envc++) {
         if (strncmp(envp[envc], "PYTHONPATH=", sizeof("PYTHONPATH=")-1) == 0) {
         }
     }
 
+    // copy the environment into a new array that will eventually also contain the PYTHONPATH
     childEnvp = alloca(sizeof(char*) * (envc + 10)); // enough for both PYTHONPATH and the DYLD stuff
     for (envc = 0; envp[envc] != NULL; envc ++) {
         if (strncmp(envp[envc], "PYTHONPATH=", sizeof("PYTHONPATH=")-1) == 0) {
-            childEnvp[envc] = (char *)[[NSString stringWithFormat: @"PYTHONPATH=%@:%s", [[NSBundle mainBundle] resourcePath], envp[envc]] UTF8String];
+            // already exisxts, prepend app wrapper paths
+            NSString *envValue = [NSString stringWithFormat: @"PYTHONPATH=%s:%s", pythonPathInWrapper, PYTHONPATH];
+            childEnvp[envc] = (char *)[envValue UTF8String];
         } else {
             childEnvp[envc] = envp[envc];
         }
     }
     if (PYTHONPATH) {
+        // already set in for() loop above
         childEnvp[envc] = NULL;
     } else {
-        childEnvp[envc] = (char *)[[NSString stringWithFormat: @"PYTHONPATH=%@", [[NSBundle mainBundle] resourcePath]] UTF8String];
+        // wasn't set -- add PYTHONPATH to child
+        NSString *envValue = [NSString stringWithFormat: @"PYTHONPATH=%s", pythonPathInWrapper];
+        childEnvp[envc] = (char *)[envValue UTF8String];
         envc++;
         childEnvp[envc] = NULL;
     }
             [bundlePaths addObject: [aBundle bundlePath]];
     }
 
+    // set an environment variable to contain the linked frameworks
+    childEnvp[envc++] = (char*)[[NSString stringWithFormat: @"PYOBJCFRAMEWORKS=%@", [bundlePaths componentsJoinedByString: @":"]] UTF8String];
+    childEnvp[envc++] = NULL;
+
     // figure out which python interpreter to use
     NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
     pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
         childArgv[i+1] = argv[i];
     i++; // compensate for i+1 in for() loop
 
-    // add an argument that lists all frameworks
-    childArgv[i++] = "-PyFrameworkPaths";
-    childArgv[i++] = [[bundlePaths componentsJoinedByString: @":"] UTF8String];
-
     // terminate the arg list
     childArgv[i++] = NULL;
 
         NSLog(@"Process ID is: %d (\n\tgdb %s %d\n to debug)", getpid(), pythonBinPathPtr, getpid());
 
     // pass control to the python interpreter
-    return execve(pythonBinPathPtr, (char **)childArgv, childEnvp);
+    if (execve(pythonBinPathPtr, (char **)childArgv, childEnvp) == -1)
+        perror("execve");
+    return 1;
 }
 
 int main(int argc, char * const *argv, char * const *envp)

File pyobjc/Lib/Foundation/__init__.py

 def NSStringFromClass(aClass):
 	return aClass.__name__
 
-# Define usefull utility methods here
+# Define useful utility methods here
 def load_bundle(path):
 	"""
 	Load the specified bundle/framework and return a list of classes 
 
 	classes = [ cls 
 		for cls in _objc.getClassList() 
-		if path == bundle_class.bundleForClass_(cls).bundlePath() ]
+		if bndl == bundle_class.bundleForClass_(cls) ]
 	return classes
 
 class_list = load_bundle('/System/Library/Frameworks/Foundation.framework')
 del cls
 del gl
 
+import os
+import sys
+import string
+if 'PYOBJCFRAMEWORKS' in os.environ:
+	paths = string.split(os.environ['PYOBJCFRAMEWORKS'], ":")
+	count = 0
+	for path in paths:
+		bundle = NSBundle.bundleWithPath_(path)
+		bundle.principalClass()
+		sys.path.insert(count, bundle.resourcePath())
+		count = count + 1
+    
+		initPath = bundle.pathForResource_ofType_( "Init", "py")
+		if initPath:
+			execfile(initPath, globals(), locals())
+
 from types import *
 def propertyListFromPythonCollection(aPyCollection, conversionHelper=None):
 	containerType = type(aPyCollection)