Changeset 391 for python/trunk/Mac/Modules/scrap
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Mac/Modules/scrap/_Scrapmodule.c
r2 r391 11 11 /* Macro to test whether a weak-loaded CFM function exists */ 12 12 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\ 13 14 15 13 PyErr_SetString(PyExc_NotImplementedError, \ 14 "Not available in this shared library/OS version"); \ 15 return NULL; \ 16 16 }} while(0) 17 17 … … 28 28 29 29 typedef struct ScrapObject { 30 31 30 PyObject_HEAD 31 ScrapRef ob_itself; 32 32 } ScrapObject; 33 33 34 34 PyObject *ScrapObj_New(ScrapRef itself) 35 35 { 36 37 38 39 40 36 ScrapObject *it; 37 it = PyObject_NEW(ScrapObject, &Scrap_Type); 38 if (it == NULL) return NULL; 39 it->ob_itself = itself; 40 return (PyObject *)it; 41 41 } 42 42 int ScrapObj_Convert(PyObject *v, ScrapRef *p_itself) 43 43 { 44 45 46 47 48 49 50 44 if (!ScrapObj_Check(v)) 45 { 46 PyErr_SetString(PyExc_TypeError, "Scrap required"); 47 return 0; 48 } 49 *p_itself = ((ScrapObject *)v)->ob_itself; 50 return 1; 51 51 } 52 52 53 53 static void ScrapObj_dealloc(ScrapObject *self) 54 54 { 55 56 55 /* Cleanup of self->ob_itself goes here */ 56 self->ob_type->tp_free((PyObject *)self); 57 57 } 58 58 59 59 static PyObject *ScrapObj_GetScrapFlavorFlags(ScrapObject *_self, PyObject *_args) 60 60 { 61 62 63 64 65 66 67 68 69 70 71 72 73 74 61 PyObject *_res = NULL; 62 OSStatus _err; 63 ScrapFlavorType flavorType; 64 ScrapFlavorFlags flavorFlags; 65 if (!PyArg_ParseTuple(_args, "O&", 66 PyMac_GetOSType, &flavorType)) 67 return NULL; 68 _err = GetScrapFlavorFlags(_self->ob_itself, 69 flavorType, 70 &flavorFlags); 71 if (_err != noErr) return PyMac_Error(_err); 72 _res = Py_BuildValue("l", 73 flavorFlags); 74 return _res; 75 75 } 76 76 77 77 static PyObject *ScrapObj_GetScrapFlavorSize(ScrapObject *_self, PyObject *_args) 78 78 { 79 80 81 82 83 84 85 86 87 88 89 90 91 92 79 PyObject *_res = NULL; 80 OSStatus _err; 81 ScrapFlavorType flavorType; 82 Size byteCount; 83 if (!PyArg_ParseTuple(_args, "O&", 84 PyMac_GetOSType, &flavorType)) 85 return NULL; 86 _err = GetScrapFlavorSize(_self->ob_itself, 87 flavorType, 88 &byteCount); 89 if (_err != noErr) return PyMac_Error(_err); 90 _res = Py_BuildValue("l", 91 byteCount); 92 return _res; 93 93 } 94 94 95 95 static PyObject *ScrapObj_GetScrapFlavorData(ScrapObject *_self, PyObject *_args) 96 96 { 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 97 PyObject *_res = NULL; 98 OSStatus _err; 99 ScrapFlavorType flavorType; 100 Size byteCount; 101 102 if (!PyArg_ParseTuple(_args, "O&", 103 PyMac_GetOSType, &flavorType)) 104 return NULL; 105 _err = GetScrapFlavorSize(_self->ob_itself, 106 flavorType, 107 &byteCount); 108 if (_err != noErr) return PyMac_Error(_err); 109 _res = PyString_FromStringAndSize(NULL, (int)byteCount); 110 if ( _res == NULL ) return NULL; 111 _err = GetScrapFlavorData(_self->ob_itself, 112 flavorType, 113 &byteCount, 114 PyString_AS_STRING(_res)); 115 if (_err != noErr) { 116 Py_XDECREF(_res); 117 return PyMac_Error(_err); 118 } 119 return _res; 120 120 } 121 121 122 122 static PyObject *ScrapObj_PutScrapFlavor(ScrapObject *_self, PyObject *_args) 123 123 { 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 124 PyObject *_res = NULL; 125 OSStatus _err; 126 ScrapFlavorType flavorType; 127 ScrapFlavorFlags flavorFlags; 128 char *flavorData__in__; 129 int flavorData__in_len__; 130 if (!PyArg_ParseTuple(_args, "O&Ks#", 131 PyMac_GetOSType, &flavorType, 132 &flavorFlags, 133 &flavorData__in__, &flavorData__in_len__)) 134 return NULL; 135 _err = PutScrapFlavor(_self->ob_itself, 136 flavorType, 137 flavorFlags, 138 (Size)flavorData__in_len__, 139 flavorData__in__); 140 if (_err != noErr) return PyMac_Error(_err); 141 Py_INCREF(Py_None); 142 _res = Py_None; 143 return _res; 144 144 } 145 145 146 146 static PyObject *ScrapObj_GetScrapFlavorCount(ScrapObject *_self, PyObject *_args) 147 147 { 148 149 150 151 152 153 154 155 156 157 158 148 PyObject *_res = NULL; 149 OSStatus _err; 150 UInt32 infoCount; 151 if (!PyArg_ParseTuple(_args, "")) 152 return NULL; 153 _err = GetScrapFlavorCount(_self->ob_itself, 154 &infoCount); 155 if (_err != noErr) return PyMac_Error(_err); 156 _res = Py_BuildValue("l", 157 infoCount); 158 return _res; 159 159 } 160 160 161 161 static PyObject *ScrapObj_GetScrapFlavorInfoList(ScrapObject *_self, PyObject *_args) 162 162 { 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 163 PyObject *_res = NULL; 164 PyObject *item; 165 OSStatus _err; 166 UInt32 infoCount; 167 ScrapFlavorInfo *infolist = NULL; 168 int i; 169 170 if (!PyArg_ParseTuple(_args, "")) 171 return NULL; 172 _err = GetScrapFlavorCount(_self->ob_itself, 173 &infoCount); 174 if (_err != noErr) return PyMac_Error(_err); 175 if (infoCount == 0) return Py_BuildValue("[]"); 176 177 if ((infolist = (ScrapFlavorInfo *)malloc(infoCount*sizeof(ScrapFlavorInfo))) == NULL ) 178 return PyErr_NoMemory(); 179 180 _err = GetScrapFlavorInfoList(_self->ob_itself, &infoCount, infolist); 181 if (_err != noErr) { 182 free(infolist); 183 return NULL; 184 } 185 if ((_res = PyList_New(infoCount)) == NULL ) { 186 free(infolist); 187 return NULL; 188 } 189 for(i=0; i<infoCount; i++) { 190 item = Py_BuildValue("O&l", PyMac_BuildOSType, infolist[i].flavorType, 191 infolist[i].flavorFlags); 192 if ( !item || PyList_SetItem(_res, i, item) < 0 ) { 193 Py_DECREF(_res); 194 free(infolist); 195 return NULL; 196 } 197 } 198 free(infolist); 199 return _res; 200 200 } 201 201 202 202 static PyMethodDef ScrapObj_methods[] = { 203 204 205 206 207 208 209 210 211 212 213 214 215 203 {"GetScrapFlavorFlags", (PyCFunction)ScrapObj_GetScrapFlavorFlags, 1, 204 PyDoc_STR("(ScrapFlavorType flavorType) -> (ScrapFlavorFlags flavorFlags)")}, 205 {"GetScrapFlavorSize", (PyCFunction)ScrapObj_GetScrapFlavorSize, 1, 206 PyDoc_STR("(ScrapFlavorType flavorType) -> (Size byteCount)")}, 207 {"GetScrapFlavorData", (PyCFunction)ScrapObj_GetScrapFlavorData, 1, 208 PyDoc_STR("(ScrapFlavorType flavorType, Buffer destination) -> (Size byteCount)")}, 209 {"PutScrapFlavor", (PyCFunction)ScrapObj_PutScrapFlavor, 1, 210 PyDoc_STR("(ScrapFlavorType flavorType, ScrapFlavorFlags flavorFlags, Size flavorSize, Buffer flavorData) -> None")}, 211 {"GetScrapFlavorCount", (PyCFunction)ScrapObj_GetScrapFlavorCount, 1, 212 PyDoc_STR("() -> (UInt32 infoCount)")}, 213 {"GetScrapFlavorInfoList", (PyCFunction)ScrapObj_GetScrapFlavorInfoList, 1, 214 PyDoc_STR("() -> ([(ScrapFlavorType, ScrapFlavorInfo), ...])")}, 215 {NULL, NULL, 0} 216 216 }; 217 217 … … 220 220 static PyObject *ScrapObj_getattr(ScrapObject *self, char *name) 221 221 { 222 222 return Py_FindMethodInChain(&ScrapObj_chain, (PyObject *)self, name); 223 223 } 224 224 … … 232 232 233 233 PyTypeObject Scrap_Type = { 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 234 PyObject_HEAD_INIT(NULL) 235 0, /*ob_size*/ 236 "_Scrap.Scrap", /*tp_name*/ 237 sizeof(ScrapObject), /*tp_basicsize*/ 238 0, /*tp_itemsize*/ 239 /* methods */ 240 (destructor) ScrapObj_dealloc, /*tp_dealloc*/ 241 0, /*tp_print*/ 242 (getattrfunc) ScrapObj_getattr, /*tp_getattr*/ 243 (setattrfunc) ScrapObj_setattr, /*tp_setattr*/ 244 (cmpfunc) ScrapObj_compare, /*tp_compare*/ 245 (reprfunc) ScrapObj_repr, /*tp_repr*/ 246 (PyNumberMethods *)0, /* tp_as_number */ 247 (PySequenceMethods *)0, /* tp_as_sequence */ 248 (PyMappingMethods *)0, /* tp_as_mapping */ 249 (hashfunc) ScrapObj_hash, /*tp_hash*/ 250 250 }; 251 251 … … 254 254 static PyObject *Scrap_LoadScrap(PyObject *_self, PyObject *_args) 255 255 { 256 257 258 259 260 261 262 263 264 256 PyObject *_res = NULL; 257 OSStatus _err; 258 if (!PyArg_ParseTuple(_args, "")) 259 return NULL; 260 _err = LoadScrap(); 261 if (_err != noErr) return PyMac_Error(_err); 262 Py_INCREF(Py_None); 263 _res = Py_None; 264 return _res; 265 265 } 266 266 267 267 static PyObject *Scrap_UnloadScrap(PyObject *_self, PyObject *_args) 268 268 { 269 270 271 272 273 274 275 276 277 269 PyObject *_res = NULL; 270 OSStatus _err; 271 if (!PyArg_ParseTuple(_args, "")) 272 return NULL; 273 _err = UnloadScrap(); 274 if (_err != noErr) return PyMac_Error(_err); 275 Py_INCREF(Py_None); 276 _res = Py_None; 277 return _res; 278 278 } 279 279 280 280 static PyObject *Scrap_GetCurrentScrap(PyObject *_self, PyObject *_args) 281 281 { 282 283 284 285 286 287 288 289 290 291 282 PyObject *_res = NULL; 283 OSStatus _err; 284 ScrapRef scrap; 285 if (!PyArg_ParseTuple(_args, "")) 286 return NULL; 287 _err = GetCurrentScrap(&scrap); 288 if (_err != noErr) return PyMac_Error(_err); 289 _res = Py_BuildValue("O&", 290 ScrapObj_New, scrap); 291 return _res; 292 292 } 293 293 294 294 static PyObject *Scrap_ClearCurrentScrap(PyObject *_self, PyObject *_args) 295 295 { 296 297 298 299 300 301 302 303 304 296 PyObject *_res = NULL; 297 OSStatus _err; 298 if (!PyArg_ParseTuple(_args, "")) 299 return NULL; 300 _err = ClearCurrentScrap(); 301 if (_err != noErr) return PyMac_Error(_err); 302 Py_INCREF(Py_None); 303 _res = Py_None; 304 return _res; 305 305 } 306 306 307 307 static PyObject *Scrap_CallInScrapPromises(PyObject *_self, PyObject *_args) 308 308 { 309 310 311 312 313 314 315 316 317 309 PyObject *_res = NULL; 310 OSStatus _err; 311 if (!PyArg_ParseTuple(_args, "")) 312 return NULL; 313 _err = CallInScrapPromises(); 314 if (_err != noErr) return PyMac_Error(_err); 315 Py_INCREF(Py_None); 316 _res = Py_None; 317 return _res; 318 318 } 319 319 #endif /* __LP64__ */ … … 321 321 static PyMethodDef Scrap_methods[] = { 322 322 #ifndef __LP64__ 323 324 325 326 327 328 329 330 331 332 323 {"LoadScrap", (PyCFunction)Scrap_LoadScrap, 1, 324 PyDoc_STR("() -> None")}, 325 {"UnloadScrap", (PyCFunction)Scrap_UnloadScrap, 1, 326 PyDoc_STR("() -> None")}, 327 {"GetCurrentScrap", (PyCFunction)Scrap_GetCurrentScrap, 1, 328 PyDoc_STR("() -> (ScrapRef scrap)")}, 329 {"ClearCurrentScrap", (PyCFunction)Scrap_ClearCurrentScrap, 1, 330 PyDoc_STR("() -> None")}, 331 {"CallInScrapPromises", (PyCFunction)Scrap_CallInScrapPromises, 1, 332 PyDoc_STR("() -> None")}, 333 333 #endif /* __LP64__ */ 334 334 {NULL, NULL, 0} 335 335 }; 336 336 … … 340 340 void init_Scrap(void) 341 341 { 342 342 PyObject *m; 343 343 #ifndef __LP64__ 344 344 PyObject *d; 345 345 #endif /* __LP64__ */ 346 346 … … 348 348 349 349 350 350 m = Py_InitModule("_Scrap", Scrap_methods); 351 351 #ifndef __LP64__ 352 353 354 355 356 357 358 359 360 352 d = PyModule_GetDict(m); 353 Scrap_Error = PyMac_GetOSErrException(); 354 if (Scrap_Error == NULL || 355 PyDict_SetItemString(d, "Error", Scrap_Error) != 0) 356 return; 357 Scrap_Type.ob_type = &PyType_Type; 358 Py_INCREF(&Scrap_Type); 359 if (PyDict_SetItemString(d, "ScrapType", (PyObject *)&Scrap_Type) != 0) 360 Py_FatalError("can't initialize ScrapType"); 361 361 #endif /* __LP64__ */ 362 362 }
Note:
See TracChangeset
for help on using the changeset viewer.