1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
26 /* cfm68k InterfaceLib exports GetEventQueue, but Events.h doesn't know this
27 ** and defines it as GetEvQHdr (which is correct for PPC). This fix is for
28 ** CW9, check that the workaround is still needed for the next release.
30 #define GetEvQHdr GetEventQueue
31 #endif /* __CFM68K__ */
35 #if TARGET_API_MAC_CARBON
36 /* Unfortunately this call is probably slower... */
37 #define LMGetTicks() TickCount()
42 #endif /* __CFM68K__ */
51 #include "pythonresources.h"
53 #include <OSUtils.h> /* for Set(Current)A5 */
55 #include <StandardFile.h>
56 #include <Resources.h>
60 #include <Processes.h>
63 #include <TextUtils.h>
66 extern void SIOUXSetupMenus(void);
67 extern void SIOUXDoAboutBox(void);
70 /* Functions we redefine because they're in obscure libraries */
71 extern void SpinCursor(short x
);
72 extern void RotateCursor(short x
);
73 extern pascal void PLstrcpy(unsigned char *, unsigned char *);
74 extern pascal int PLstrcmp(unsigned char *, unsigned char *);
75 extern pascal unsigned char *PLstrrchr(unsigned char *, unsigned char);
80 #include <TFileSpec.h> /* For Path2FSSpec */
85 /* The ID of the Sioux apple menu */
86 #define SIOUX_APPLEID 32000
92 ** When less than this amount of stackspace is left we
93 ** raise a MemoryError.
95 #ifndef MINIMUM_STACK_SIZE
97 #define MINIMUM_STACK_SIZE 8192
99 #define MINIMUM_STACK_SIZE 4096
103 #if TARGET_API_MAC_CARBON
105 ** On MacOSX StackSpace() lies: it gives the distance from heap end to stack pointer,
106 ** but the stack cannot grow that far due to rlimit values. We cannot get at this value
107 ** from Carbon, so we set a maximum to the stack here that is based on the default
108 ** stack limit of 512K.
110 #define MAXIMUM_STACK_SIZE (256*1024)
114 ** We have to be careful, since we can't handle
115 ** things like updates (and they'll keep coming back if we don't
116 ** handle them). Note that we don't know who has windows open, so
117 ** even handing updates off to SIOUX under MW isn't going to work.
119 #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask)
123 /* XXX We should include Errors.h here, but it has a name conflict
124 ** with the python errors.h. */
127 /* Declared in macfsmodule.c: */
128 extern FSSpec
*mfs_GetFSSpecFSSpec(PyObject
*);
129 extern PyObject
*newmfssobject(FSSpec
*);
131 /* Interrupt code variables: */
132 static int interrupted
; /* Set to true when cmd-. seen */
133 static RETSIGTYPE
intcatcher(int);
135 static int PyMac_Yield(void);
138 ** These are the real scheduling parameters that control what we check
139 ** in the event loop, and how often we check. The values are initialized
140 ** from pyMac_SchedParamStruct.
143 struct real_sched_param_struct
{
144 int check_interrupt
; /* if true check for command-dot */
145 int process_events
; /* if nonzero enable evt processing, this mask */
146 int besocial
; /* if nonzero be a little social with CPU */
147 unsigned long check_interval
; /* how often to check, in ticks */
148 unsigned long bg_yield
; /* yield so long when in background */
149 /* these are computed from previous and clock and such */
150 int enabled
; /* check_interrupt OR process_event OR yield */
151 unsigned long next_check
; /* when to check/yield next, in ticks */
154 static struct real_sched_param_struct schedparams
=
155 { 1, MAINLOOP_EVENTMASK
, 1, 15, 15, 1, 0};
158 ** Workaround for sioux/gusi combo: set when we are exiting
160 int PyMac_ConsoleIsDead
;
163 ** Sioux menu bar, saved early so we can restore it
165 static MenuBarHandle sioux_mbar
;
168 ** Some stuff for our GetDirectory and PromptGetFile routines
171 int selectcur_hit
; /* Set to true when "select current" selected */
172 char *prompt
; /* The prompt */
174 #if TARGET_API_MAC_CARBON
175 /* The StandardFile hooks don't exist in Carbon. This breaks GetDirectory,
176 ** but the macfsn code will replace it by a NavServices version anyway.
178 #define myhook_upp NULL
180 static DlgHookYDUPP myhook_upp
;
181 static int upp_inited
= 0;
185 ** The python-code event handler
187 static PyObject
*python_event_handler
;
190 ** Set to true if we're appearance-compliant
192 int PyMac_AppearanceCompliant
;
195 ** Find out what the current script is.
196 ** Donated by Fredrik Lund.
198 char *PyMac_getscript()
200 int font
, script
, lang
;
203 script
= FontToScript(font
);
206 lang
= GetScriptVariable(script
, smScriptLang
);
207 if (lang
== langIcelandic
)
208 return "mac-iceland";
209 else if (lang
== langTurkish
)
210 return "mac-turkish";
211 else if (lang
== langGreek
)
221 return "mac-cyrillic";
223 return "mac-roman"; /* better than nothing */
230 ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of
231 ** the working directory. Hence, we call this routine after each call
232 ** to chdir() to rectify things.
240 if ( Path2FSSpec(":x", &curdirfss
) != noErr
)
243 /* Set MacOS "working directory" */
245 pb
.ioVRefNum
= curdirfss
.vRefNum
;
246 pb
.ioWDDirID
= curdirfss
.parID
;
247 if (PBHSetVolSync(&pb
) != noErr
)
254 ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we
255 ** provide a dummy here.
257 void SpinCursor(short x
) { /* Dummy */ }
258 void RotateCursor(short x
) { /* Dummy */ }
261 ** Replacement GUSI Spin function
265 PyMac_GUSISpin(spin_msg msg
, long arg
)
267 static Boolean inForeground
= true;
268 int maxsleep
= 6; /* 6 ticks is "normal" sleeptime */
270 if (PyMac_ConsoleIsDead
) return 0;
273 SpinCursor(msg
== SP_AUTO_SPIN
? short(arg
) : 1);
276 if (interrupted
) return -1;
278 if ( msg
== SP_AUTO_SPIN
)
280 if ( msg
==SP_SLEEP
||msg
==SP_SELECT
)
283 PyMac_DoYield(maxsleep
, 0); /* XXXX or is it safe to call python here? */
289 PyMac_SetGUSISpin() {
290 GUSISetHook(GUSI_SpinHook
, (GUSIHook
)PyMac_GUSISpin
);
294 /* Called at exit() time thru atexit(), to stop event processing */
296 PyMac_StopGUSISpin() {
297 PyMac_ConsoleIsDead
= 1;
300 #if !TARGET_API_MAC_CARBON
302 ** Replacement routines for the PLstr... functions so we don't need
306 PLstrcpy(unsigned char *to
, unsigned char *fr
)
308 memcpy(to
, fr
, fr
[0]+1);
312 PLstrcmp(unsigned char *s1
, unsigned char *s2
)
315 int l
= s1
[0] < s2
[0] ? s1
[0] : s2
[0];
317 res
= memcmp(s1
+1, s2
+1, l
);
323 else if ( s1
[0] > s2
[0] )
329 pascal unsigned char *
330 PLstrrchr(unsigned char *str
, unsigned char chr
)
332 unsigned char *ptr
= 0;
335 for(p
=str
+1; p
<str
+str
[0]; p
++)
341 #endif /* !TARGET_API_MAC_CARBON */
342 #endif /* USE_GUSI */
345 /* Convert C to Pascal string. Returns pointer to static buffer. */
355 buf
[0] = (unsigned char)len
;
356 strncpy((char *)buf
+1, str
, len
);
360 #if !TARGET_API_MAC_CARBON
362 c2pstrcpy(unsigned char *dst
, const char *src
)
367 if ( len
> 255 ) len
= 255;
368 strncpy((char *)dst
+1, src
, len
);
371 #endif /* !TARGET_API_MAC_CARBON */
373 /* Like strerror() but for Mac OS error numbers */
374 char *PyMac_StrError(int err
)
376 static char buf
[256];
380 h
= GetResource('Estr', err
);
384 memcpy(buf
, str
+1, (unsigned char)str
[0]);
385 buf
[(unsigned char)str
[0]] = '\0';
389 sprintf(buf
, "Mac OS error code %d", err
);
394 /* Exception object shared by all Mac specific modules for Mac OS errors */
395 PyObject
*PyMac_OSErrException
;
397 /* Initialize and return PyMac_OSErrException */
399 PyMac_GetOSErrException()
401 if (PyMac_OSErrException
== NULL
)
402 PyMac_OSErrException
= PyString_FromString("Mac OS Error");
403 return PyMac_OSErrException
;
406 /* Set a MAC-specific error from errno, and return NULL; return None if no error */
408 PyErr_Mac(PyObject
*eobj
, int err
)
413 if (err
== 0 && !PyErr_Occurred()) {
417 if (err
== -1 && PyErr_Occurred())
419 msg
= PyMac_StrError(err
);
420 v
= Py_BuildValue("(is)", err
, msg
);
421 PyErr_SetObject(eobj
, v
);
426 /* Call PyErr_Mac with PyMac_OSErrException */
428 PyMac_Error(OSErr err
)
430 return PyErr_Mac(PyMac_GetOSErrException(), err
);
433 #ifdef USE_STACKCHECK
434 /* Check for stack overflow */
439 static char *sentinel
= 0;
440 static PyThreadState
*thread_for_sentinel
= 0;
442 if ( sentinel
== 0 ) {
443 unsigned long stackspace
= StackSpace();
445 #ifdef MAXIMUM_STACK_SIZE
446 /* See the comment at the definition */
447 if ( stackspace
> MAXIMUM_STACK_SIZE
)
448 stackspace
= MAXIMUM_STACK_SIZE
;
450 sentinel
= &here
- stackspace
+ MINIMUM_STACK_SIZE
;
452 if ( thread_for_sentinel
== 0 ) {
453 thread_for_sentinel
= PyThreadState_Get();
455 if ( &here
< sentinel
) {
456 if (thread_for_sentinel
== PyThreadState_Get()) {
460 /* Else we are unsure... */
461 fprintf(stderr
, "Stackcheck in other thread (was %x now %x)\n", thread_for_sentinel
,PyThreadState_Get());
467 #endif /* USE_STACKCHECK */
469 /* The catcher routine (which may not be used for all compilers) */
475 signal(SIGINT
, intcatcher
);
479 PyOS_InitInterrupts()
481 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
482 signal(SIGINT
, intcatcher
);
486 PyOS_FiniInterrupts()
491 ** This routine scans the event queue looking for cmd-.
492 ** This is the only way to get an interrupt under THINK (since it
493 ** doesn't do SIGINT handling), but is also used under MW, when
494 ** the full-fledged event loop is disabled. This way, we can at least
495 ** interrupt a runaway python program.
498 scan_event_queue(flush
)
501 #if TARGET_API_MAC_CARBON
502 if ( CheckEventQueueForUserCancel() )
507 q
= (EvQElPtr
) LMGetEventQueue()->qHead
;
509 for (; q
; q
= (EvQElPtr
)q
->qLink
) {
510 if (q
->evtQWhat
== keyDown
&&
511 (char)q
->evtQMessage
== '.' &&
512 (q
->evtQModifiers
& cmdKey
) != 0) {
514 FlushEvents(keyDownMask
, 0);
525 if (schedparams
.enabled
) {
526 if ( (unsigned long)LMGetTicks() > schedparams
.next_check
) {
527 if ( PyMac_Yield() < 0)
529 schedparams
.next_check
= (unsigned long)LMGetTicks()
530 + schedparams
.check_interval
;
532 scan_event_queue(1); /* Eat events up to cmd-. */
534 PyErr_SetNone(PyExc_KeyboardInterrupt
);
544 ** This routine is called if we know that an external library yielded
545 ** to background tasks, so we shouldn't count that time in our computation
546 ** of how much CPU we used.
547 ** This happens with SIOUX, and the routine is called from our modified
551 PyMac_LibraryDidYield(int howlong
)
553 unsigned long maxnextcheck
= (unsigned long)LMGetTicks() + schedparams
.check_interval
;
555 schedparams
.next_check
= schedparams
.next_check
+ howlong
;
556 if (schedparams
.next_check
> maxnextcheck
)
557 schedparams
.next_check
= maxnextcheck
;
562 PyOS_InterruptOccurred()
567 /* Check whether we are in the foreground */
569 PyMac_InForeground(void)
571 static ProcessSerialNumber ours
;
573 ProcessSerialNumber curfg
;
577 (void)GetCurrentProcess(&ours
);
580 if ( GetFrontProcess(&curfg
) < 0 )
582 else if ( SameProcess(&ours
, &curfg
, &eq
) < 0 )
589 PyMac_SetEventHandler(PyObject
*evh
)
591 if ( evh
&& python_event_handler
) {
592 PyErr_SetString(PyExc_RuntimeError
, "Python event handler already set");
595 if ( python_event_handler
)
596 Py_DECREF(python_event_handler
);
599 python_event_handler
= evh
;
604 ** Handle an event, either one found in the mainloop eventhandler or
605 ** one passed back from the python program.
608 PyMac_HandleEventIntern(evp
)
611 #if !TARGET_API_MAC_CARBON
612 if ( evp
->what
== mouseDown
) {
615 if ( FindWindow(evp
->where
, &wp
) == inSysWindow
) {
616 SystemClick(evp
, wp
);
625 /* If SIOUX wants it we're done */
626 siouxdidit
= SIOUXHandleOneEvent(evp
);
631 /* Other compilers are just unlucky... */
632 #endif /* !__MWERKS__ */
636 ** Handle an event, either through HandleEvent or by passing it to the Python
640 PyMac_HandleEvent(evp
)
645 if ( python_event_handler
) {
646 rv
= PyObject_CallFunction(python_event_handler
, "(O&)",
647 PyMac_BuildEventRecord
, evp
);
651 return -1; /* Propagate exception */
653 PyMac_HandleEventIntern(evp
);
659 ** Yield the CPU to other tasks without processing events.
662 PyMac_DoYield(int maxsleep
, int maycallpython
)
666 long latest_time_ready
;
667 static int in_here
= 0;
671 ** First check for interrupts, if wanted.
672 ** This sets a flag that will be picked up at an appropriate
673 ** moment in the mainloop.
675 if (schedparams
.check_interrupt
)
678 /* XXXX Implementing an idle routine goes here */
681 ** Check which of the eventloop cases we have:
683 ** - don't process events but do yield
686 if( in_here
> 1 || !schedparams
.process_events
||
687 (python_event_handler
&& !maycallpython
) ) {
688 #if !TARGET_API_MAC_CARBON
689 if ( maxsleep
>= 0 ) {
694 latest_time_ready
= LMGetTicks() + maxsleep
;
696 /* XXXX Hack by Jack.
697 ** In time.sleep() you can click to another application
698 ** once only. If you come back to Python you cannot get away
701 gotone
= WaitNextEvent(schedparams
.process_events
, &ev
, maxsleep
, NULL
);
702 /* Get out quickly if nothing interesting is happening */
703 if ( !gotone
|| ev
.what
== nullEvent
)
705 if ( PyMac_HandleEvent(&ev
) < 0 ) {
709 maxsleep
= latest_time_ready
- LMGetTicks();
710 } while ( maxsleep
> 0 );
717 ** Process events and/or yield the CPU to other tasks if opportune
721 unsigned long maxsleep
;
723 if( PyMac_InForeground() )
726 maxsleep
= schedparams
.bg_yield
;
728 return PyMac_DoYield(maxsleep
, 1);
732 ** Return current scheduler parameters
735 PyMac_GetSchedParams(PyMacSchedParams
*sp
)
737 sp
->check_interrupt
= schedparams
.check_interrupt
;
738 sp
->process_events
= schedparams
.process_events
;
739 sp
->besocial
= schedparams
.besocial
;
740 sp
->check_interval
= schedparams
.check_interval
/ 60.0;
741 sp
->bg_yield
= schedparams
.bg_yield
/ 60.0;
745 ** Set current scheduler parameters
748 PyMac_SetSchedParams(PyMacSchedParams
*sp
)
750 schedparams
.check_interrupt
= sp
->check_interrupt
;
751 schedparams
.process_events
= sp
->process_events
;
752 schedparams
.besocial
= sp
->besocial
;
753 schedparams
.check_interval
= (unsigned long)(sp
->check_interval
*60);
754 schedparams
.bg_yield
= (unsigned long)(sp
->bg_yield
*60);
755 if ( schedparams
.check_interrupt
|| schedparams
.process_events
||
756 schedparams
.besocial
)
757 schedparams
.enabled
= 1;
759 schedparams
.enabled
= 0;
760 schedparams
.next_check
= 0; /* Check immedeately */
764 ** Install our menu bar.
769 MenuHandle applemenu
;
771 if ( (sioux_mbar
=GetMenuBar()) == NULL
) {
772 /* Sioux menu not installed yet. Do so */
774 if ( (sioux_mbar
=GetMenuBar()) == NULL
)
777 if ( (applemenu
=GetMenuHandle(SIOUX_APPLEID
)) == NULL
) return;
778 SetMenuItemText(applemenu
, 1, "\pAbout Python...");
782 ** Restore sioux menu bar
785 PyMac_RestoreMenuBar()
788 /* This doesn't seem to work anymore? Or only for Carbon? */
789 MenuBarHandle curmenubar
;
791 curmenubar
= GetMenuBar();
793 SetMenuBar(sioux_mbar
);
804 ** Our replacement about box
807 #include "patchlevel.h"
810 SIOUXDoAboutBox(void)
817 if( (theDialog
= GetNewDialog(ABOUT_ID
, NULL
, (WindowPtr
)-1)) == NULL
)
819 theWindow
= GetDialogWindow(theDialog
);
820 SetPortWindowPort(theWindow
);
821 GetFNum("\pPython-Sans", &fontID
);
823 fontID
= kFontIDGeneva
;
826 ParamText(Pstring(PATCHLEVEL
), "\p", "\p", "\p");
827 ShowWindow(theWindow
);
828 ModalDialog(NULL
, &item
);
829 DisposeDialog(theDialog
);
834 PyMac_FileExists(char *name
)
838 if ( FSMakeFSSpec(0, 0, Pstring(name
), &fss
) == noErr
)
845 ** Helper routine for GetDirectory
848 myhook_proc(short item
, DialogPtr theDialog
, struct hook_args
*dataptr
)
850 if ( item
== sfHookFirstCall
&& dataptr
->prompt
) {
855 GetDialogItem(theDialog
, PROMPT_ITEM
, &type
, &prompth
, &rect
);
857 SetDialogItemText(prompth
, (unsigned char *)dataptr
->prompt
);
859 if ( item
== SELECTCUR_ITEM
) {
860 item
= sfItemCancelButton
;
861 dataptr
->selectcur_hit
= 1;
865 #if !TARGET_API_MAC_CARBON
867 ** Ask the user for a directory. I still can't understand
868 ** why Apple doesn't provide a standard solution for this...
871 PyMac_GetDirectory(dirfss
, prompt
)
875 static SFTypeList list
= {'fldr', 0, 0, 0};
876 static Point where
= {-1, -1};
877 StandardFileReply reply
;
878 struct hook_args hook_args
;
881 myhook_upp
= NewDlgHookYDProc(myhook_proc
);
884 if ( prompt
&& *prompt
)
885 hook_args
.prompt
= (char *)Pstring(prompt
);
887 hook_args
.prompt
= NULL
;
888 hook_args
.selectcur_hit
= 0;
889 CustomGetFile((FileFilterYDUPP
)0, 1, list
, &reply
, GETDIR_ID
, where
, myhook_upp
,
890 NULL
, NULL
, NULL
, (void *)&hook_args
);
892 reply
.sfFile
.name
[0] = 0;
893 if( FSMakeFSSpec(reply
.sfFile
.vRefNum
, reply
.sfFile
.parID
, reply
.sfFile
.name
, dirfss
) )
895 return hook_args
.selectcur_hit
;
899 ** Slightly extended StandardGetFile: accepts a prompt */
900 void PyMac_PromptGetFile(short numTypes
, ConstSFTypeListPtr typeList
,
901 StandardFileReply
*reply
, char *prompt
)
903 static Point where
= {-1, -1};
904 struct hook_args hook_args
;
907 myhook_upp
= NewDlgHookYDProc(myhook_proc
);
910 if ( prompt
&& *prompt
)
911 hook_args
.prompt
= (char *)Pstring(prompt
);
913 hook_args
.prompt
= NULL
;
914 hook_args
.selectcur_hit
= 0;
915 CustomGetFile((FileFilterYDUPP
)0, numTypes
, typeList
, reply
, GETFILEPROMPT_ID
, where
,
916 myhook_upp
, NULL
, NULL
, NULL
, (void *)&hook_args
);
918 #endif /* TARGET_API_MAC_CARBON */
920 /* Convert a 4-char string object argument to an OSType value */
922 PyMac_GetOSType(PyObject
*v
, OSType
*pr
)
924 if (!PyString_Check(v
) || PyString_Size(v
) != 4) {
925 PyErr_SetString(PyExc_TypeError
,
926 "OSType arg must be string of 4 chars");
929 memcpy((char *)pr
, PyString_AsString(v
), 4);
933 /* Convert an OSType value to a 4-char string object */
935 PyMac_BuildOSType(OSType t
)
937 return PyString_FromStringAndSize((char *)&t
, 4);
940 /* Convert an NumVersion value to a 4-element tuple */
942 PyMac_BuildNumVersion(NumVersion t
)
944 return Py_BuildValue("(hhhh)", t
.majorRev
, t
.minorAndBugRev
, t
.stage
, t
.nonRelRev
);
948 /* Convert a Python string object to a Str255 */
950 PyMac_GetStr255(PyObject
*v
, Str255 pbuf
)
953 if (!PyString_Check(v
) || (len
= PyString_Size(v
)) > 255) {
954 PyErr_SetString(PyExc_TypeError
,
955 "Str255 arg must be string of at most 255 chars");
959 memcpy((char *)(pbuf
+1), PyString_AsString(v
), len
);
963 /* Convert a Str255 to a Python string object */
965 PyMac_BuildStr255(Str255 s
)
968 PyErr_SetString(PyExc_SystemError
, "Str255 pointer is NULL");
971 return PyString_FromStringAndSize((char *)&s
[1], (int)s
[0]);
975 PyMac_BuildOptStr255(Str255 s
)
981 return PyString_FromStringAndSize((char *)&s
[1], (int)s
[0]);
986 ** Convert a Python object to an FSSpec.
987 ** The object may either be a full pathname or a triple
988 ** (vrefnum, dirid, path).
989 ** NOTE: This routine will fail on pre-sys7 machines.
990 ** The caller is responsible for not calling this routine
991 ** in those cases (which is fine, since everyone calling
992 ** this is probably sys7 dependent anyway).
995 PyMac_GetFSSpec(PyObject
*v
, FSSpec
*fs
)
1003 /* first check whether it already is an FSSpec */
1004 fs2
= mfs_GetFSSpecFSSpec(v
);
1006 (void)FSMakeFSSpec(fs2
->vRefNum
, fs2
->parID
, fs2
->name
, fs
);
1009 if ( PyString_Check(v
) ) {
1010 /* It's a pathname */
1011 if( !PyArg_Parse(v
, "O&", PyMac_GetStr255
, &path
) )
1013 refnum
= 0; /* XXXX Should get CurWD here?? */
1016 if( !PyArg_Parse(v
, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)",
1017 &refnum
, &parid
, PyMac_GetStr255
, &path
)) {
1021 err
= FSMakeFSSpec(refnum
, parid
, path
, fs
);
1022 if ( err
&& err
!= fnfErr
) {
1029 /* Convert FSSpec to PyObject */
1030 PyObject
*PyMac_BuildFSSpec(FSSpec
*v
)
1032 return newmfssobject(v
);
1035 /* Convert a Python object to a Rect.
1036 The object must be a (left, top, right, bottom) tuple.
1037 (This differs from the order in the struct but is consistent with
1038 the arguments to SetRect(), and also with STDWIN). */
1040 PyMac_GetRect(PyObject
*v
, Rect
*r
)
1042 return PyArg_Parse(v
, "(hhhh)", &r
->left
, &r
->top
, &r
->right
, &r
->bottom
);
1045 /* Convert a Rect to a Python object */
1047 PyMac_BuildRect(Rect
*r
)
1049 return Py_BuildValue("(hhhh)", r
->left
, r
->top
, r
->right
, r
->bottom
);
1053 /* Convert a Python object to a Point.
1054 The object must be a (h, v) tuple.
1055 (This differs from the order in the struct but is consistent with
1056 the arguments to SetPoint(), and also with STDWIN). */
1058 PyMac_GetPoint(PyObject
*v
, Point
*p
)
1060 return PyArg_Parse(v
, "(hh)", &p
->h
, &p
->v
);
1063 /* Convert a Point to a Python object */
1065 PyMac_BuildPoint(Point p
)
1067 return Py_BuildValue("(hh)", p
.h
, p
.v
);
1071 /* Convert a Python object to an EventRecord.
1072 The object must be a (what, message, when, (v, h), modifiers) tuple. */
1074 PyMac_GetEventRecord(PyObject
*v
, EventRecord
*e
)
1076 return PyArg_Parse(v
, "(hll(hh)h)",
1085 /* Convert a Rect to an EventRecord object */
1087 PyMac_BuildEventRecord(EventRecord
*e
)
1089 return Py_BuildValue("(hll(hh)h)",
1098 /* Convert Python object to Fixed */
1100 PyMac_GetFixed(PyObject
*v
, Fixed
*f
)
1104 if( !PyArg_Parse(v
, "d", &d
))
1106 *f
= (Fixed
)(d
* 0x10000);
1110 /* Convert a Point to a Python object */
1112 PyMac_BuildFixed(Fixed f
)
1118 return Py_BuildValue("d", d
);
1121 /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */
1123 PyMac_Getwide(PyObject
*v
, wide
*rv
)
1125 if (PyInt_Check(v
)) {
1127 rv
->lo
= PyInt_AsLong(v
);
1128 if( rv
->lo
& 0x80000000 )
1132 return PyArg_Parse(v
, "(ll)", &rv
->hi
, &rv
->lo
);
1137 PyMac_Buildwide(wide
*w
)
1139 if ( (w
->hi
== 0 && (w
->lo
& 0x80000000) == 0) ||
1140 (w
->hi
== -1 && (w
->lo
& 0x80000000) ) )
1141 return PyInt_FromLong(w
->lo
);
1142 return Py_BuildValue("(ll)", w
->hi
, w
->lo
);