home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / Modules / res / ressupport.py < prev   
Encoding:
Text File  |  1996-07-31  |  3.1 KB  |  142 lines  |  [TEXT/Pyth]

  1. # This script will generate the Resources interface for Python.
  2. # It uses the "bgen" package to generate C code.
  3. # It execs the file resgen.py which contain the function definitions
  4. # (resgen.py was generated by resscan.py, scanning the <Resources.h> header file).
  5.  
  6. import addpack
  7. addpack.addpack(':Tools:bgen:bgen')
  8.  
  9. from macsupport import *
  10.  
  11.  
  12. class ResMixIn:
  13.  
  14.     def checkit(self):
  15.         OutLbrace()
  16.         Output("OSErr _err = ResError();")
  17.         Output("if (_err != noErr) return PyMac_Error(_err);")
  18.         OutRbrace()
  19.         FunctionGenerator.checkit(self) # XXX
  20.  
  21. class ResFunction(ResMixIn, FunctionGenerator): pass
  22. class ResMethod(ResMixIn, MethodGenerator): pass
  23.  
  24. # includestuff etc. are imported from macsupport
  25.  
  26. includestuff = includestuff + """
  27. #include <Resources.h>
  28.  
  29. #define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
  30. """
  31.  
  32. finalstuff = finalstuff + """
  33.  
  34. /* Alternative version of ResObj_New, which returns None for null argument */
  35. PyObject *OptResObj_New(itself)
  36.     Handle itself;
  37. {
  38.     if (itself == NULL) {
  39.         Py_INCREF(Py_None);
  40.         return Py_None;
  41.     }
  42.     return ResObj_New(itself);
  43. }
  44.  
  45. OptResObj_Convert(v, p_itself)
  46.     PyObject *v;
  47.     Handle *p_itself;
  48. {
  49.     if ( v == Py_None ) {
  50.         *p_itself = NULL;
  51.         return 1;
  52.     }
  53.     if (!ResObj_Check(v))
  54.     {
  55.         PyErr_SetString(PyExc_TypeError, "Resource required");
  56.         return 0;
  57.     }
  58.     *p_itself = ((ResourceObject *)v)->ob_itself;
  59.     return 1;
  60. }
  61.  
  62. """
  63.  
  64. initstuff = initstuff + """
  65. """
  66.  
  67. module = MacModule('Res', 'Res', includestuff, finalstuff, initstuff)
  68.  
  69. getattrHookCode = """
  70. if (strcmp(name, "size") == 0)
  71.     return PyInt_FromLong(GetHandleSize(self->ob_itself));
  72. if (strcmp(name, "data") == 0) {
  73.     PyObject *res;
  74.     char state;
  75.     state = HGetState(self->ob_itself);
  76.     HLock(self->ob_itself);
  77.     res = PyString_FromStringAndSize(
  78.         *self->ob_itself,
  79.         GetHandleSize(self->ob_itself));
  80.     HUnlock(self->ob_itself);
  81.     HSetState(self->ob_itself, state);
  82.     return res;
  83. }
  84. if (strcmp(name, "__members__") == 0)
  85.     return Py_BuildValue("[ss]", "data", "size");
  86. """
  87.  
  88. setattrCode = """
  89. static int
  90. ResObj_setattr(self, name, value)
  91.     ResourceObject *self;
  92.     char *name;
  93.     PyObject *value;
  94. {
  95.     char *data;
  96.     long size;
  97.     
  98.     if (strcmp(name, "data") != 0 || value == NULL )
  99.         return -1;
  100.     if ( !PyString_Check(value) )
  101.         return -1;
  102.     size = PyString_Size(value);
  103.     data = PyString_AsString(value);
  104.     /* XXXX Do I need the GetState/SetState calls? */
  105.     SetHandleSize(self->ob_itself, size);
  106.     if ( MemError())
  107.         return -1;
  108.     HLock(self->ob_itself);
  109.     memcpy((char *)*self->ob_itself, data, size);
  110.     HUnlock(self->ob_itself);
  111.     /* XXXX Should I do the Changed call immedeately? */
  112.     return 0;
  113. }
  114. """
  115.  
  116. class ResDefiniton(GlobalObjectDefinition):
  117.  
  118.     def outputCheckNewArg(self):
  119.         Output("if (itself == NULL) return PyMac_Error(resNotFound);")
  120.  
  121.     def outputGetattrHook(self):
  122.         Output(getattrHookCode)
  123.         
  124.     def outputSetattr(self):
  125.         Output(setattrCode)
  126.  
  127.  
  128. resobject = ResDefiniton('Resource', 'ResObj', 'Handle')
  129. module.addobject(resobject)
  130.  
  131. functions = []
  132. resmethods = []
  133.  
  134. execfile('resgen.py')
  135. execfile('resedit.py')
  136.  
  137. for f in functions: module.add(f)
  138. for f in resmethods: resobject.add(f)
  139.  
  140. SetOutputFileName('Resmodule.c')
  141. module.generate()
  142.