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

Skip to content

Commit 2b3ce3b

Browse files
committed
FSRef and EasyDialogs pathname support was pretty much broken in MacPython-OS9. Fixed.
1 parent 57c115c commit 2b3ce3b

3 files changed

Lines changed: 68 additions & 14 deletions

File tree

Lib/plat-mac/EasyDialogs.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import Carbon.File
3535
import macresource
3636
import os
37+
import sys
3738

3839
__all__ = ['Message', 'AskString', 'AskPassword', 'AskYesNoCancel',
3940
'GetArgv', 'AskFileForOpen', 'AskFileForSave', 'AskFolder',
@@ -635,9 +636,9 @@ def AskFileForOpen(
635636
if issubclass(tpwanted, Carbon.File.FSSpec):
636637
return tpwanted(rr.selection[0])
637638
if issubclass(tpwanted, str):
638-
return tpwanted(rr.selection_fsr[0].FSRefMakePath())
639+
return tpwanted(rr.selection_fsr[0].as_pathname())
639640
if issubclass(tpwanted, unicode):
640-
return tpwanted(rr.selection_fsr[0].FSRefMakePath(), 'utf8')
641+
return tpwanted(rr.selection_fsr[0].as_pathname(), 'utf8')
641642
raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
642643

643644
def AskFileForSave(
@@ -686,13 +687,16 @@ def AskFileForSave(
686687
if issubclass(tpwanted, Carbon.File.FSSpec):
687688
return tpwanted(rr.selection[0])
688689
if issubclass(tpwanted, (str, unicode)):
689-
# This is gross, and probably incorrect too
690-
vrefnum, dirid, name = rr.selection[0].as_tuple()
691-
pardir_fss = Carbon.File.FSSpec((vrefnum, dirid, ''))
692-
pardir_fsr = Carbon.File.FSRef(pardir_fss)
693-
pardir_path = pardir_fsr.FSRefMakePath() # This is utf-8
694-
name_utf8 = unicode(name, 'macroman').encode('utf8')
695-
fullpath = os.path.join(pardir_path, name_utf8)
690+
if sys.platform == 'mac':
691+
fullpath = rr.selection[0].as_pathname()
692+
else:
693+
# This is gross, and probably incorrect too
694+
vrefnum, dirid, name = rr.selection[0].as_tuple()
695+
pardir_fss = Carbon.File.FSSpec((vrefnum, dirid, ''))
696+
pardir_fsr = Carbon.File.FSRef(pardir_fss)
697+
pardir_path = pardir_fsr.FSRefMakePath() # This is utf-8
698+
name_utf8 = unicode(name, 'macroman').encode('utf8')
699+
fullpath = os.path.join(pardir_path, name_utf8)
696700
if issubclass(tpwanted, unicode):
697701
return unicode(fullpath, 'utf8')
698702
return tpwanted(fullpath)
@@ -741,14 +745,14 @@ def AskFolder(
741745
if issubclass(tpwanted, Carbon.File.FSSpec):
742746
return tpwanted(rr.selection[0])
743747
if issubclass(tpwanted, str):
744-
return tpwanted(rr.selection_fsr[0].FSRefMakePath())
748+
return tpwanted(rr.selection_fsr[0].as_pathname())
745749
if issubclass(tpwanted, unicode):
746-
return tpwanted(rr.selection_fsr[0].FSRefMakePath(), 'utf8')
750+
return tpwanted(rr.selection_fsr[0].as_pathname(), 'utf8')
747751
raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
748752

749753

750754
def test():
751-
import time, sys, macfs
755+
import time, macfs
752756

753757
Message("Testing EasyDialogs.")
754758
optionlist = (('v', 'Verbose'), ('verbose', 'Verbose as long option'),

Mac/Modules/file/_Filemodule.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,26 @@ static PyObject *FSRef_as_pathname(FSRefObject *_self, PyObject *_args)
18401840
{
18411841
PyObject *_res = NULL;
18421842

1843+
#if TARGET_API_MAC_OSX
1844+
if (!PyArg_ParseTuple(_args, ""))
1845+
return NULL;
18431846
_res = FSRef_FSRefMakePath(_self, _args);
1847+
#else
1848+
char strbuf[1024];
1849+
OSErr err;
1850+
FSSpec fss;
1851+
1852+
if (!PyArg_ParseTuple(_args, ""))
1853+
return NULL;
1854+
if ( !PyMac_GetFSSpec((PyObject *)_self, &fss))
1855+
return NULL;
1856+
err = PyMac_GetFullPathname(&fss, strbuf, sizeof(strbuf));
1857+
if ( err ) {
1858+
PyMac_Error(err);
1859+
return NULL;
1860+
}
1861+
_res = PyString_FromString(strbuf);
1862+
#endif
18441863
return _res;
18451864

18461865
}
@@ -3190,6 +3209,7 @@ int
31903209
PyMac_GetFSRef(PyObject *v, FSRef *fsr)
31913210
{
31923211
OSStatus err;
3212+
FSSpec fss;
31933213

31943214
if (FSRef_Check(v)) {
31953215
*fsr = ((FSRefObject *)v)->ob_itself;
@@ -3208,8 +3228,13 @@ PyMac_GetFSRef(PyObject *v, FSRef *fsr)
32083228
/* XXXX Should try unicode here too */
32093229
#endif
32103230
/* Otherwise we try to go via an FSSpec */
3231+
#if TARGET_API_MAC_OSX
32113232
if (FSSpec_Check(v)) {
3212-
if ((err=FSpMakeFSRef(&((FSSpecObject *)v)->ob_itself, fsr)) == 0)
3233+
fss = ((FSSpecObject *)v)->ob_itself;
3234+
#else
3235+
if (PyMac_GetFSSpec(v, &fss)) {
3236+
#endif
3237+
if ((err=FSpMakeFSRef(&fss, fsr)) == 0)
32133238
return 1;
32143239
PyMac_Error(err);
32153240
return 0;

Mac/Modules/file/filesupport.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def getargsCheck(self, name):
271271
PyMac_GetFSRef(PyObject *v, FSRef *fsr)
272272
{
273273
OSStatus err;
274+
FSSpec fss;
274275
275276
if (FSRef_Check(v)) {
276277
*fsr = ((FSRefObject *)v)->ob_itself;
@@ -289,8 +290,13 @@ def getargsCheck(self, name):
289290
/* XXXX Should try unicode here too */
290291
#endif
291292
/* Otherwise we try to go via an FSSpec */
293+
#if TARGET_API_MAC_OSX
292294
if (FSSpec_Check(v)) {
293-
if ((err=FSpMakeFSRef(&((FSSpecObject *)v)->ob_itself, fsr)) == 0)
295+
fss = ((FSSpecObject *)v)->ob_itself;
296+
#else
297+
if (PyMac_GetFSSpec(v, &fss)) {
298+
#endif
299+
if ((err=FSpMakeFSRef(&fss, fsr)) == 0)
294300
return 1;
295301
PyMac_Error(err);
296302
return 0;
@@ -808,7 +814,26 @@ def parseArgumentList(self, args):
808814
fsref_methods.append(f)
809815

810816
FSRef_as_pathname_body = """
817+
#if TARGET_API_MAC_OSX
818+
if (!PyArg_ParseTuple(_args, ""))
819+
return NULL;
811820
_res = FSRef_FSRefMakePath(_self, _args);
821+
#else
822+
char strbuf[1024];
823+
OSErr err;
824+
FSSpec fss;
825+
826+
if (!PyArg_ParseTuple(_args, ""))
827+
return NULL;
828+
if ( !PyMac_GetFSSpec((PyObject *)_self, &fss))
829+
return NULL;
830+
err = PyMac_GetFullPathname(&fss, strbuf, sizeof(strbuf));
831+
if ( err ) {
832+
PyMac_Error(err);
833+
return NULL;
834+
}
835+
_res = PyString_FromString(strbuf);
836+
#endif
812837
return _res;
813838
"""
814839
f = ManualGenerator("as_pathname", FSRef_as_pathname_body)

0 commit comments

Comments
 (0)