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

Skip to content

Commit 697842f

Browse files
committed
Replaced PyMac_FullPath by PyMac_FullPathname, which has an extra 'length'
parameter for the return string (as unix pathnames are not limited by the 255 char pstring limit). Implemented the function for MachO-Python, where it returns unix pathnames.
1 parent b0e8e9b commit 697842f

6 files changed

Lines changed: 74 additions & 35 deletions

File tree

Mac/Include/macglue.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ extern short PyMac_AppRefNum; /* RefNum of application rsrcfork (from macmain.
6262
extern FSSpec PyMac_ApplicationFSSpec; /* Application location (from macargv.c) */
6363
extern char PyMac_ApplicationPath[]; /* Application location (from macargv.c) */
6464
extern OSErr PyMac_init_application_location(void); /* Init the above */
65-
extern OSErr PyMac_GetFullPath(FSSpec *, char *); /* convert fsspec->path (macargv.c) */
6665
extern int PyMac_GetArgv(char ***, int); /* Get argc, argv (from macargv.c) */
6766
extern int PyMac_AppearanceCompliant; /* True if in appearance support mode */
6867

Mac/Modules/macfsmodule.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ extern PyObject *_PyMac_BuildFSRef(FSRef *);
5151
#endif
5252
static PyObject *ErrorObject;
5353

54+
#ifdef TARGET_API_MAC_OSX
55+
#define PATHNAMELEN 1024
56+
#else
57+
#define PATHNAMELEN 256
58+
#endif
59+
5460
/* ----------------------------------------------------- */
5561
/* Declarations for objects of type Alias */
5662

@@ -449,22 +455,17 @@ PyMac_SetFileDates(FSSpec *fss, unsigned long crdat, unsigned long mddat,
449455
static PyObject *
450456
mfss_as_pathname(mfssobject *self, PyObject *args)
451457
{
452-
#if TARGET_API_MAC_OSX
453-
PyErr_SetString(PyExc_NotImplementedError, "FSSpec.as_pathname not supported on this platform");
454-
return 0;
455-
#else
456-
char strbuf[257];
458+
char strbuf[PATHNAMELEN];
457459
OSErr err;
458460

459461
if (!PyArg_ParseTuple(args, ""))
460462
return NULL;
461-
err = PyMac_GetFullPath(&self->fsspec, strbuf);
463+
err = PyMac_GetFullPathname(&self->fsspec, strbuf, PATHNAMELEN);
462464
if ( err ) {
463465
PyErr_Mac(ErrorObject, err);
464466
return NULL;
465467
}
466468
return PyString_FromString(strbuf);
467-
#endif
468469
}
469470

470471
static PyObject *

Mac/Modules/macosmodule.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4040

4141
static PyObject *MacOS_Error; /* Exception MacOS.Error */
4242

43+
#ifdef TARGET_API_MAC_OSX
44+
#define PATHNAMELEN 1024
45+
#else
46+
#define PATHNAMELEN 256
47+
#endif
48+
4349
#ifdef MPW
4450
#define bufferIsSmall -607 /*error returns from Post and Accept */
4551
#endif
@@ -596,15 +602,14 @@ MacOS_openrf(PyObject *self, PyObject *args)
596602

597603
err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
598604

599-
#if !TARGET_API_MAC_OSX
600605
if ( err == fnfErr ) {
601606
/* In stead of doing complicated things here to get creator/type
602607
** correct we let the standard i/o library handle it
603608
*/
604609
FILE *tfp;
605-
char pathname[257];
610+
char pathname[PATHNAMELEN];
606611

607-
if ( err=PyMac_GetFullPath(&fss, pathname) ) {
612+
if ( err=PyMac_GetFullPathname(&fss, pathname, PATHNAMELEN) ) {
608613
PyMac_Error(err);
609614
Py_DECREF(fp);
610615
return NULL;
@@ -618,7 +623,6 @@ MacOS_openrf(PyObject *self, PyObject *args)
618623
fclose(tfp);
619624
err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
620625
}
621-
#endif
622626
if ( err ) {
623627
Py_DECREF(fp);
624628
PyMac_Error(err);

Mac/Python/macgetargv.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@ typedef unsigned long refcontype;
5454
#include "Python.h"
5555
#include "macglue.h"
5656

57+
#ifdef TARGET_API_MAC_OSX
58+
#define PATHNAMELEN 1024
59+
#else
60+
#define PATHNAMELEN 256
61+
#endif
62+
5763
static int arg_count;
5864
static char *arg_vector[256];
5965
FSSpec PyMac_ApplicationFSSpec;
60-
char PyMac_ApplicationPath[256];
66+
char PyMac_ApplicationPath[PATHNAMELEN];
6167

6268
/* Duplicate a string to the heap. We also export this since it isn't standard
6369
** and others use it
@@ -73,22 +79,6 @@ strdup(const char *src)
7379
}
7480
#endif
7581

76-
#if TARGET_API_MAC_OSX
77-
OSErr
78-
PyMac_GetFullPath(FSSpec *fss, char *path)
79-
{
80-
FSRef fsr;
81-
OSErr err;
82-
83-
*path = '\0';
84-
err = FSpMakeFSRef(fss, &fsr);
85-
if ( err ) return err;
86-
err = (OSErr)FSRefMakePath(&fsr, path, 1024);
87-
if ( err ) return err;
88-
return 0;
89-
}
90-
#endif /* TARGET_API_MAC_OSX */
91-
9282

9383
#if !TARGET_API_MAC_OSX
9484
/* Initialize FSSpec and full name of current application */
@@ -109,7 +99,7 @@ PyMac_init_process_location(void)
10999
info.processAppSpec = &PyMac_ApplicationFSSpec;
110100
if ( err=GetProcessInformation(&currentPSN, &info))
111101
return err;
112-
if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
102+
if ( err=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath, PATHNAMELEN) )
113103
return err;
114104
applocation_inited = 1;
115105
return 0;
@@ -170,7 +160,7 @@ handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype r
170160
DescType rttype;
171161
long i, ndocs, size;
172162
FSSpec fss;
173-
char path[1024];
163+
char path[PATHNAMELEN];
174164

175165
got_one = 1;
176166
if ((err = AEGetParamDesc(theAppleEvent,
@@ -185,7 +175,7 @@ handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype r
185175
&keywd, &rttype, &fss, sizeof(fss), &size);
186176
if (err)
187177
break;
188-
PyMac_GetFullPath(&fss, path);
178+
PyMac_GetFullPathname(&fss, path, PATHNAMELEN);
189179
arg_vector[arg_count++] = strdup(path);
190180
}
191181
return err;

Mac/Python/macgetpath.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ PERFORMANCE OF THIS SOFTWARE.
3838
#include <unistd.h>
3939
#endif
4040

41+
#ifdef TARGET_API_MAC_OSX
42+
#define PATHNAMELEN 1024
43+
#else
44+
#define PATHNAMELEN 256
45+
#endif
4146

4247
/* Return the initial python search path. This is called once from
4348
** initsys() to initialize sys.path.
@@ -244,7 +249,7 @@ char *
244249
PyMac_GetPythonDir()
245250
{
246251
static int diditbefore = 0;
247-
static char name[256] = {':', '\0'};
252+
static char name[PATHNAMELEN] = {':', '\0'};
248253
AliasHandle handle;
249254
FSSpec dirspec;
250255
Boolean modified = 0;
@@ -285,7 +290,7 @@ PyMac_GetPythonDir()
285290
if ( prefrh != -1 ) CloseResFile(prefrh);
286291
UseResFile(oldrh);
287292

288-
if ( PyMac_GetFullPath(&dirspec, name) == 0 ) {
293+
if ( PyMac_GetFullPathname(&dirspec, name, PATHNAMELEN) == 0 ) {
289294
strcat(name, ":");
290295
} else {
291296
/* If all fails, we return the current directory */

Python/mactoolboxglue.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PyObject *PyMac_OSErrException;
9393

9494
/* Initialize and return PyMac_OSErrException */
9595
PyObject *
96-
PyMac_GetOSErrException()
96+
PyMac_GetOSErrException(void)
9797
{
9898
if (PyMac_OSErrException == NULL)
9999
PyMac_OSErrException = PyString_FromString("MacOS.Error");
@@ -127,6 +127,46 @@ PyMac_Error(OSErr err)
127127
return PyErr_Mac(PyMac_GetOSErrException(), err);
128128
}
129129

130+
131+
#if TARGET_API_MAC_OSX
132+
OSErr
133+
PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
134+
{
135+
FSRef fsr;
136+
OSErr err;
137+
138+
*path = '\0';
139+
err = FSpMakeFSRef(fss, &fsr);
140+
if ( err == fnfErr ) {
141+
/* FSSpecs can point to non-existing files, fsrefs can't. */
142+
FSSpec fss2;
143+
int tocopy;
144+
145+
err = FSMakeFSSpec(fss->vRefNum, fss->parID, "", &fss2);
146+
if ( err ) return err;
147+
err = FSpMakeFSRef(&fss2, &fsr);
148+
if ( err ) return err;
149+
err = (OSErr)FSRefMakePath(&fsr, path, len-1);
150+
if ( err ) return err;
151+
/* This part is not 100% safe: we append the filename part, but
152+
** I'm not sure that we don't run afoul of the various 8bit
153+
** encodings here. Will have to look this up at some point...
154+
*/
155+
strcat(path, "/");
156+
tocopy = fss->name[0];
157+
if ( strlen(path) + tocopy >= len )
158+
tocopy = len - strlen(path) - 1;
159+
if ( tocopy > 0 )
160+
strncat(path, fss->name+1, tocopy);
161+
} else {
162+
if ( err ) return err;
163+
err = (OSErr)FSRefMakePath(&fsr, path, len);
164+
if ( err ) return err;
165+
}
166+
return 0;
167+
}
168+
169+
#endif /* TARGET_API_MAC_OSX */
130170
/* Convert a 4-char string object argument to an OSType value */
131171
int
132172
PyMac_GetOSType(PyObject *v, OSType *pr)

0 commit comments

Comments
 (0)