Commits

Bill Bumgarner  committed 90f5190

Added ability to be run with Apple supplied python.

  • Participants
  • Parent commits 8935356

Comments (0)

Files changed (5)

File pyobjc/Examples/WebServicesTool/Main.py

+#!/usr/bin/python
+
+import sys
 import objc
+from AppKit import NSApplicationMain
 
 import WSTApplicationDelegateClass
 import WSTConnectionWindowControllerClass
+
+sys.exit(NSApplicationMain(sys.argv))

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

 		29B97315FDCFA39411CA2CEA = {
 			children = (
 				29B97316FDCFA39411CA2CEA,
+				F6AAC478035708BD01B76035,
 			);
 			isa = PBXGroup;
 			name = "Other Sources";
 		29B97316FDCFA39411CA2CEA = {
 			fileEncoding = 30;
 			isa = PBXFileReference;
-			path = main.m;
+			path = "main-embedded-interpreter.m";
 			refType = 4;
 		};
 		29B97317FDCFA39411CA2CEA = {
 		29B9732BFDCFA39411CA2CEA = {
 			buildActionMask = 2147483647;
 			files = (
-				29B9732CFDCFA39411CA2CEA,
+				F6AAC47A035708BD01B76035,
 			);
 			isa = PBXSourcesBuildPhase;
 			runOnlyForDeploymentPostprocessing = 0;
 		};
-		29B9732CFDCFA39411CA2CEA = {
-			fileRef = 29B97316FDCFA39411CA2CEA;
-			isa = PBXBuildFile;
-			settings = {
-				ATTRIBUTES = (
-				);
-			};
-		};
 		29B9732DFDCFA39411CA2CEA = {
 			buildActionMask = 2147483647;
 			files = (
 			settings = {
 			};
 		};
+		F6AAC478035708BD01B76035 = {
+			isa = PBXFileReference;
+			path = "main-bin-python.m";
+			refType = 4;
+		};
+		F6AAC47A035708BD01B76035 = {
+			fileRef = F6AAC478035708BD01B76035;
+			isa = PBXBuildFile;
+			settings = {
+			};
+		};
 		F6CD0DC5033C279E0126A167 = {
 			children = (
 				F6CD0DC6033C279E0126A167,

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

+/*
+ This main file uses execve() to transfer control of execution to the standard command line python interpreter.   As such, compiled classes in the project will not actually be linked into the runtime as execve() effectively overlays the existing process with the process being called -- in this case the python command line tool.
+
+ To use compiled classes with this main, create a separate bundle target and load the bundle in the Main.py file.
+
+ This style of execution works with the Apple provided version of Python.
+ */
+
+#import <Foundation/Foundation.h>
+#import <sys/param.h>
+#import <unistd.h>
+
+int pyobjc_main(int argc, char * const *argv, char *envp[])
+{
+  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+  const char **childArgv = alloca(sizeof(char *) * (argc + 3));
+  const char *pythonBinPathPtr;
+  const char *mainPyPathPtr;
+  int i;
+  
+  NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
+
+  pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
+  [pythonBinPath retain];
+  pythonBinPathPtr = [pythonBinPath cString];
+  
+  NSString *mainPyFile = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"PrincipalPythonFile"];
+  NSString *mainPyPath = nil;
+
+  if (mainPyFile)
+    mainPyPath = [[NSBundle mainBundle] pathForResource: mainPyFile ofType: nil];
+
+  if ( !mainPyPath )
+    mainPyPath = [[NSBundle mainBundle] pathForResource: @"Main.py" ofType: nil];
+
+  if ( !mainPyPath )
+    [NSException raise: NSInternalInconsistencyException
+                format: @"%s:%d pyobjc_main() Failed to find main python entry point for application.  Exiting.", __FILE__, __LINE__];
+  [mainPyPath retain];
+  mainPyPathPtr = [mainPyPath cString];
+
+  childArgv[0] = argv[0];
+  childArgv[1] = mainPyPathPtr;
+  for (i = 1; i<argc; i++)
+    childArgv[i+2] = argv[i];
+  childArgv[i+2] = NULL;
+  
+  [pool release];
+
+  return execve(pythonBinPathPtr, (char **)childArgv, envp);
+}
+
+int main(int argc, char * const *argv, char *envp[])
+{
+  return pyobjc_main(argc, argv, envp);
+}

File pyobjc/Examples/WebServicesTool/main-embedded-interpreter.m

+/*
+ This main file can be used in cases where the developer desires to embed the python interpreter directly into the application.   The pyobjc_main() function initializes the python interpreter in an embedded context.
+
+ This is useful if the application has a mix of ObjC and Python.  The ObjC can be compiled/linked without having to load a series of bundles (which is considerably slower than launching a normal application).
+
+ The point is somewhat moot in that the PyObjC module has to dynamically load the AppKit and related frameworks anyway.
+
+ As of 10.2.1 [6d52], This style of build will not work with the python supplied with OS X as Apple did not provide a python library or framework to link against.
+ */
+
+#import <Python.h>
+#import <Foundation/Foundation.h>
+#import <Cocoa/Cocoa.h>
+#import <sys/param.h>
+
+int pyobjc_main(int argc, const char *argv[])
+{
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
+
+    pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
+    [pythonBinPath retain];
+    Py_SetProgramName((char *)[pythonBinPath cString]);
+    Py_Initialize();
+
+    NSString *mainPyFile = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"PrincipalPythonFile"];
+    NSString *mainPyPath = nil;
+
+    if (mainPyFile)
+        mainPyPath = [[NSBundle mainBundle] pathForResource: mainPyFile ofType: nil];
+
+    if ( !mainPyPath )
+        mainPyPath = [[NSBundle mainBundle] pathForResource: @"Main.py" ofType: nil];
+
+    if ( !mainPyPath )
+        [NSException raise: NSInternalInconsistencyException
+                    format: @"%s:%d pyobjc_main() Failed to find main python entry point for application.  Exiting.", __FILE__, __LINE__];
+    [mainPyPath retain];
+
+    FILE *mainPy = fopen([mainPyPath cString], "r");
+    int result = PyRun_SimpleFile(mainPy, (char *)[[mainPyPath lastPathComponent] cString]);
+
+    if ( result != 0 )
+        [NSException raise: NSInternalInconsistencyException
+                    format: @"%s:%d pyobjc_main() PyRun_SimpleFile failed with file '%@'.  See console for errors.", __FILE__, __LINE__, mainPyPath];
+
+    [pool release];
+
+    return result;
+}
+
+int main(int argc, const char *argv[])
+{
+    return pyobjc_main(argc, argv);
+}

File pyobjc/Examples/WebServicesTool/main.m

-#import <Python.h>
-#import <Foundation/Foundation.h>
-#import <Cocoa/Cocoa.h>
-#import <sys/param.h>
-
-int pyobjc_main(int argc, const char *argv[])
-{
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
-
-    pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
-    [pythonBinPath retain];
-    Py_SetProgramName((char *)[pythonBinPath cString]);
-    Py_Initialize();
-
-    NSString *mainPyFile = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"PrincipalPythonFile"];
-    NSString *mainPyPath = nil;
-
-    if (mainPyFile)
-        mainPyPath = [[NSBundle mainBundle] pathForResource: mainPyFile ofType: nil];
-
-    if ( !mainPyPath )
-        mainPyPath = [[NSBundle mainBundle] pathForResource: @"Main.py" ofType: nil];
-
-    if ( !mainPyPath )
-        [NSException raise: NSInternalInconsistencyException
-                    format: @"%s:%d pyobjc_main() Failed to find main python entry point for application.  Exiting.", __FILE__, __LINE__];
-    [mainPyPath retain];
-
-    FILE *mainPy = fopen([mainPyPath cString], "r");
-    int result = PyRun_SimpleFile(mainPy, (char *)[[mainPyPath lastPathComponent] cString]);
-
-    if ( result != 0 )
-        [NSException raise: NSInternalInconsistencyException
-                    format: @"%s:%d pyobjc_main() PyRun_SimpleFile failed with file '%@'.  See console for errors.", __FILE__, __LINE__, mainPyPath];
-
-    [pool release];
-
-    return NSApplicationMain(argc, argv);
-}
-
-int main(int argc, const char *argv[])
-{
-    return pyobjc_main(argc, argv);
-}