Commits

Ronald Oussoren committed 6c643e4

Set PYTHONPATH in main.m

  • Participants
  • Parent commits 8cf74aa

Comments (0)

Files changed (2)

File pyobjc/Examples/TableModel2/TableModel2.pbproj/project.pbxproj

 				FRAMEWORK_SEARCH_PATHS = "";
 				HEADER_SEARCH_PATHS = /sw/include/python2.2;
 				INSTALL_PATH = "$(HOME)/Applications";
-				LIBRARY_SEARCH_PATHS = "/sw/lib/python2.2/config/ /sw/lib/python2.2/config";
+				LIBRARY_SEARCH_PATHS = "";
 				OTHER_CFLAGS = "";
 				OTHER_LDFLAGS = "-force_flat_namespace -undefined suppress";
 				PREBINDING = NO;

File pyobjc/Examples/TableModel2/main.m

   NSBundle *aBundle;
   NSMutableArray *bundlePaths = [[NSMutableArray array] retain];
   int i;
+  int envc;
+  char** childEnvp;
+  char*  PYTHONPATH = NULL;
+
+  for (envc = 0; envp[envc] != NULL; envc++) {
+  	if (strncmp(envp[envc], "PYTHONPATH=", sizeof("PYTHONPATH=")-1) == 0) {
+		PYTHONPATH=envp[envc] + sizeof("PYTHONPATH=") - 1;
+		/* No break, we also want to know how large envp is */
+	}
+  } 
+
+  childEnvp = alloca(sizeof(char*) * (envc + 2));
+  for (envc = 0; envp[envc] != NULL; envc ++) {
+  	if (strncmp(envp[envc], "PYTHONPATH=", sizeof("PYTHONPATH=")-1) == 0) {
+		const char* s = [[[NSBundle mainBundle] resourcePath] UTF8String];
+		childEnvp[envc] = alloca(strlen(envp[envc]) + strlen(s) + 2);
+		sprintf(childEnvp[envc], "%s:%s", envp[envc], s);
+
+	} else {
+		childEnvp[envc] = envp[i];
+	}
+  }
+  if (PYTHONPATH) {
+	envp[envc] = NULL;
+  } else {
+	const char* s = [[[NSBundle mainBundle] resourcePath] UTF8String];
+	childEnvp[envc] = alloca(sizeof("PYTHONPATH=") + strlen(s));
+	sprintf(childEnvp[envc], "PYTHONPATH=%s", s);
+	childEnvp[envc+1] = NULL;
+  }
+
 
   while ( aBundle = [bundleEnumerator nextObject] ) {
     if ( [[[aBundle bundlePath] pathExtension] isEqualToString: @"framework"] )
   childArgv[i+2] = [[bundlePaths componentsJoinedByString: @":"] UTF8String];
   childArgv[i+3] = NULL;
 
-  return execve(pythonBinPathPtr, (char **)childArgv, envp);
+  i = execve(pythonBinPathPtr, (char **)childArgv, (char**) childEnvp);
+  if (i == -1) perror("execv");
+  return 1;
 }
 
 int main(int argc, char * const *argv, char *envp[])