Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26ee126

Browse files
committed
Got rid of nfullpath in favor of PyMac_GetFullPath (which was pretty
much identical anyway). Initialize PyMac_ApplicationPath and PyMac_ApplicationFSSpec to point to the current application.
1 parent 8096daa commit 26ee126

2 files changed

Lines changed: 31 additions & 36 deletions

File tree

Mac/Python/macgetargv.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4646
#include <Dialogs.h>
4747
#include <Windows.h>
4848

49+
#include "Python.h"
50+
#include "macglue.h"
51+
4952
#ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
5053
#define HAVE_UNIVERSAL_HEADERS
5154
#endif
@@ -61,6 +64,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6164

6265
static int arg_count;
6366
static char *arg_vector[256];
67+
FSSpec PyMac_ApplicationFSSpec;
68+
char PyMac_ApplicationPath[256];
69+
static int applocation_inited;
6470

6571
/* Duplicate a string to the heap. We also export this since it isn't standard
6672
** and others use it
@@ -75,20 +81,27 @@ strdup(char *src)
7581
return dst;
7682
}
7783

78-
/* Return FSSpec of current application */
84+
/* Initialize FSSpec and full name of current application */
7985

8086
OSErr
81-
PyMac_process_location(FSSpec *applicationSpec)
87+
PyMac_init_process_location()
8288
{
8389
ProcessSerialNumber currentPSN;
8490
ProcessInfoRec info;
91+
OSErr err;
8592

93+
if ( applocation_inited ) return 0;
8694
currentPSN.highLongOfPSN = 0;
8795
currentPSN.lowLongOfPSN = kCurrentProcess;
8896
info.processInfoLength = sizeof(ProcessInfoRec);
8997
info.processName = NULL;
90-
info.processAppSpec = applicationSpec;
91-
return GetProcessInformation(&currentPSN, &info);
98+
info.processAppSpec = &PyMac_ApplicationFSSpec;
99+
if ( err=GetProcessInformation(&currentPSN, &info))
100+
return err;
101+
if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
102+
return err;
103+
applocation_inited = 1;
104+
return 0;
92105
}
93106

94107
/* Given an FSSpec, return the FSSpec of the parent folder */
@@ -114,8 +127,8 @@ get_folder_parent (FSSpec * fss, FSSpec * parent)
114127

115128
/* Given an FSSpec return a full, colon-separated pathname */
116129

117-
static OSErr
118-
get_full_path (FSSpec *fss, char *buf)
130+
OSErr
131+
PyMac_GetFullPath (FSSpec *fss, char *buf)
119132
{
120133
short err;
121134
FSSpec fss_parent, fss_current;
@@ -146,21 +159,6 @@ get_full_path (FSSpec *fss, char *buf)
146159
return 0;
147160
}
148161

149-
/* Return the full program name */
150-
151-
static char *
152-
get_application_name()
153-
{
154-
static char appname[256];
155-
FSSpec appspec;
156-
157-
if (PyMac_process_location(&appspec))
158-
return NULL;
159-
if (get_full_path(&appspec, appname))
160-
return NULL;
161-
return appname;
162-
}
163-
164162
/* Check that there aren't any args remaining in the event */
165163

166164
static OSErr
@@ -230,7 +228,7 @@ handle_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
230228
&keywd, &rttype, &fss, sizeof(fss), &size);
231229
if (err)
232230
break;
233-
get_full_path(&fss, path);
231+
PyMac_GetFullPath(&fss, path);
234232
arg_vector[arg_count++] = strdup(path);
235233
}
236234
return err;
@@ -301,7 +299,8 @@ PyMac_GetArgv(pargv, noevents)
301299
{
302300

303301
arg_count = 0;
304-
arg_vector[arg_count++] = strdup(get_application_name());
302+
(void)PyMac_init_process_location();
303+
arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
305304

306305
if( !noevents ) {
307306
set_ae_handlers();

Mac/Python/macgetpath.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ PyMac_OpenPrefFile()
147147
printf("Cannot create preferences file, error %d\n", ResError());
148148
exit(1);
149149
}
150-
if ( (err=PyMac_process_location(&dirspec)) != 0 ) {
151-
printf("Cannot get FSSpec for application, error %d\n", err);
150+
if ( (err=PyMac_init_process_location()) != 0 ) {
151+
printf("Cannot get application location, error %d\n", err);
152152
exit(1);
153153
}
154+
dirspec = PyMac_ApplicationFSSpec;
154155
dirspec.name[0] = 0;
155156
if ((err=NewAlias(NULL, &dirspec, &handle)) != 0 ) {
156157
printf("Cannot make alias to application directory, error %d\n", err);
@@ -212,7 +213,7 @@ PyMac_GetPythonDir()
212213
if ( prefrh != -1 ) CloseResFile(prefrh);
213214
UseResFile(oldrh);
214215

215-
if ( nfullpath(&dirspec, name) == 0 ) {
216+
if ( PyMac_GetFullPath(&dirspec, name) == 0 ) {
216217
strcat(name, ":");
217218
} else {
218219
/* If all fails, we return the current directory */
@@ -228,7 +229,6 @@ PyMac_GetPythonDir()
228229
static char *
229230
PyMac_GetPythonPath()
230231
{
231-
FSSpec dirspec;
232232
short oldrh, prefrh = -1;
233233
char *rv;
234234
int i, newlen;
@@ -289,20 +289,16 @@ PyMac_GetPythonPath()
289289
rv[newlen-1] = 0;
290290
} else if ( pathitem[0] >= 14 && strncmp((char *)pathitem+1, "$(APPLICATION)", 14) == 0 ) {
291291
/* This is the application itself */
292-
char fullname[256];
293292

294-
if ( (err=PyMac_process_location(&dirspec)) != 0 ) {
295-
printf("Cannot get FSSpec for application, error %d\n", err);
296-
exit(1);
297-
}
298-
if ( nfullpath(&dirspec, fullname) != 0 ) {
299-
printf("Cannot convert application fsspec to path\n");
293+
if ( (err=PyMac_init_process_location()) != 0 ) {
294+
printf("Cannot get application location, error %d\n", err);
300295
exit(1);
301296
}
302-
newlen = strlen(rv) + strlen(fullname) + 2;
297+
298+
newlen = strlen(rv) + strlen(PyMac_ApplicationPath) + 2;
303299
if( (rv=realloc(rv, newlen)) == NULL)
304300
goto out;
305-
strcpy(rv+strlen(rv), fullname);
301+
strcpy(rv+strlen(rv), PyMac_ApplicationPath);
306302
rv[newlen-2] = '\n';
307303
rv[newlen-1] = 0;
308304

0 commit comments

Comments
 (0)