Fix three PyChecker-detected gotchas.
[python/dscho.git] / Mac / Modules / scrap / Scrapmodule.c
blob436cbede62496fdacff737a9536cdbe14a6d66d8
2 /* ========================== Module Scrap ========================== */
4 #include "Python.h"
8 #include "macglue.h"
9 #include "pymactoolbox.h"
11 #include <Scrap.h>
14 ** Generate ScrapInfo records
16 static PyObject *
17 SCRRec_New(itself)
18 ScrapStuff *itself;
21 return Py_BuildValue("lO&hhO&", itself->scrapSize,
22 ResObj_New, itself->scrapHandle, itself->scrapCount, itself->scrapState,
23 PyMac_BuildStr255, itself->scrapName);
26 static PyObject *Scrap_Error;
28 static PyObject *Scrap_LoadScrap(_self, _args)
29 PyObject *_self;
30 PyObject *_args;
32 PyObject *_res = NULL;
33 OSStatus _err;
34 if (!PyArg_ParseTuple(_args, ""))
35 return NULL;
36 _err = LoadScrap();
37 if (_err != noErr) return PyMac_Error(_err);
38 Py_INCREF(Py_None);
39 _res = Py_None;
40 return _res;
43 static PyObject *Scrap_UnloadScrap(_self, _args)
44 PyObject *_self;
45 PyObject *_args;
47 PyObject *_res = NULL;
48 OSStatus _err;
49 if (!PyArg_ParseTuple(_args, ""))
50 return NULL;
51 _err = UnloadScrap();
52 if (_err != noErr) return PyMac_Error(_err);
53 Py_INCREF(Py_None);
54 _res = Py_None;
55 return _res;
58 #if !TARGET_API_MAC_CARBON
60 static PyObject *Scrap_InfoScrap(_self, _args)
61 PyObject *_self;
62 PyObject *_args;
64 PyObject *_res = NULL;
65 ScrapStuffPtr _rv;
66 if (!PyArg_ParseTuple(_args, ""))
67 return NULL;
68 _rv = InfoScrap();
69 _res = Py_BuildValue("O&",
70 SCRRec_New, _rv);
71 return _res;
73 #endif
75 #if !TARGET_API_MAC_CARBON
77 static PyObject *Scrap_GetScrap(_self, _args)
78 PyObject *_self;
79 PyObject *_args;
81 PyObject *_res = NULL;
82 long _rv;
83 Handle destination;
84 ScrapFlavorType flavorType;
85 SInt32 offset;
86 if (!PyArg_ParseTuple(_args, "O&O&",
87 ResObj_Convert, &destination,
88 PyMac_GetOSType, &flavorType))
89 return NULL;
90 _rv = GetScrap(destination,
91 flavorType,
92 &offset);
93 _res = Py_BuildValue("ll",
94 _rv,
95 offset);
96 return _res;
98 #endif
101 static PyObject *Scrap_ZeroScrap(_self, _args)
102 PyObject *_self;
103 PyObject *_args;
105 PyObject *_res = NULL;
106 OSStatus _err;
107 if (!PyArg_ParseTuple(_args, ""))
108 return NULL;
109 #if TARGET_API_MAC_CARBON
111 ScrapRef scrap;
113 _err = ClearCurrentScrap();
114 if (_err != noErr) return PyMac_Error(_err);
115 _err = GetCurrentScrap(&scrap);
117 #else
118 _err = ZeroScrap();
119 #endif
120 if (_err != noErr) return PyMac_Error(_err);
121 Py_INCREF(Py_None);
122 _res = Py_None;
123 return _res;
126 static PyObject *Scrap_PutScrap(_self, _args)
127 PyObject *_self;
128 PyObject *_args;
130 PyObject *_res = NULL;
131 OSStatus _err;
132 SInt32 sourceBufferByteCount;
133 ScrapFlavorType flavorType;
134 char *sourceBuffer__in__;
135 int sourceBuffer__len__;
136 int sourceBuffer__in_len__;
137 #if TARGET_API_MAC_CARBON
138 ScrapRef scrap;
139 #endif
141 if (!PyArg_ParseTuple(_args, "O&s#",
142 PyMac_GetOSType, &flavorType,
143 &sourceBuffer__in__, &sourceBuffer__in_len__))
144 return NULL;
145 sourceBufferByteCount = sourceBuffer__in_len__;
146 sourceBuffer__len__ = sourceBuffer__in_len__;
147 #if TARGET_API_MAC_CARBON
148 _err = GetCurrentScrap(&scrap);
149 if (_err != noErr) return PyMac_Error(_err);
150 _err = PutScrapFlavor(scrap, flavorType, 0, sourceBufferByteCount, sourceBuffer__in__);
151 #else
152 _err = PutScrap(sourceBufferByteCount,
153 flavorType,
154 sourceBuffer__in__);
155 #endif
156 if (_err != noErr) return PyMac_Error(_err);
157 Py_INCREF(Py_None);
158 _res = Py_None;
159 sourceBuffer__error__: ;
160 return _res;
163 #if TARGET_API_MAC_CARBON
165 static PyObject *Scrap_ClearCurrentScrap(_self, _args)
166 PyObject *_self;
167 PyObject *_args;
169 PyObject *_res = NULL;
170 OSStatus _err;
171 if (!PyArg_ParseTuple(_args, ""))
172 return NULL;
173 _err = ClearCurrentScrap();
174 if (_err != noErr) return PyMac_Error(_err);
175 Py_INCREF(Py_None);
176 _res = Py_None;
177 return _res;
179 #endif
181 #if TARGET_API_MAC_CARBON
183 static PyObject *Scrap_CallInScrapPromises(_self, _args)
184 PyObject *_self;
185 PyObject *_args;
187 PyObject *_res = NULL;
188 OSStatus _err;
189 if (!PyArg_ParseTuple(_args, ""))
190 return NULL;
191 _err = CallInScrapPromises();
192 if (_err != noErr) return PyMac_Error(_err);
193 Py_INCREF(Py_None);
194 _res = Py_None;
195 return _res;
197 #endif
199 static PyMethodDef Scrap_methods[] = {
200 {"LoadScrap", (PyCFunction)Scrap_LoadScrap, 1,
201 "() -> None"},
202 {"UnloadScrap", (PyCFunction)Scrap_UnloadScrap, 1,
203 "() -> None"},
205 #if !TARGET_API_MAC_CARBON
206 {"InfoScrap", (PyCFunction)Scrap_InfoScrap, 1,
207 "() -> (ScrapStuffPtr _rv)"},
208 #endif
210 #if !TARGET_API_MAC_CARBON
211 {"GetScrap", (PyCFunction)Scrap_GetScrap, 1,
212 "(Handle destination, ScrapFlavorType flavorType) -> (long _rv, SInt32 offset)"},
213 #endif
215 {"ZeroScrap", (PyCFunction)Scrap_ZeroScrap, 1,
216 "() -> None"},
218 {"PutScrap", (PyCFunction)Scrap_PutScrap, 1,
219 "(ScrapFlavorType flavorType, Buffer sourceBuffer) -> None"},
221 #if TARGET_API_MAC_CARBON
222 {"ClearCurrentScrap", (PyCFunction)Scrap_ClearCurrentScrap, 1,
223 "() -> None"},
224 #endif
226 #if TARGET_API_MAC_CARBON
227 {"CallInScrapPromises", (PyCFunction)Scrap_CallInScrapPromises, 1,
228 "() -> None"},
229 #endif
230 {NULL, NULL, 0}
236 void initScrap()
238 PyObject *m;
239 PyObject *d;
244 m = Py_InitModule("Scrap", Scrap_methods);
245 d = PyModule_GetDict(m);
246 Scrap_Error = PyMac_GetOSErrException();
247 if (Scrap_Error == NULL ||
248 PyDict_SetItemString(d, "Error", Scrap_Error) != 0)
249 return;
252 /* ======================== End module Scrap ======================== */