~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to src/bufferproxy.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#define PYGAMEAPI_BUFFERPROXY_INTERNAL
22
22
 
23
23
#include "pygame.h"
 
24
#include "pgcompat.h"
24
25
#include "pygamedocs.h"
25
26
 
26
27
static PyObject* _bufferproxy_new (PyTypeObject *type, PyObject *args,
33
34
static PyObject* _bufferproxy_write (PyBufferProxy *buffer, PyObject *args);
34
35
 
35
36
/* Buffer methods */
 
37
#if PY3
 
38
static int _bufferproxy_getbuffer (PyBufferProxy *self, Py_buffer *view,
 
39
                                   int flags);
 
40
static void _bufferproxy_releasebuffer (PyBufferProxy *self, Py_buffer *view);
 
41
#else
36
42
static Py_ssize_t _bufferproxy_getreadbuf (PyBufferProxy *buffer,
37
43
                                           Py_ssize_t _index,
38
44
                                           const void **ptr);
41
47
                                            const void **ptr);
42
48
static Py_ssize_t _bufferproxy_getsegcount (PyBufferProxy *buffer,
43
49
                                            Py_ssize_t *lenp);
 
50
#endif
44
51
 
45
52
/* C API interfaces */
46
53
static PyObject* PyBufferProxy_New (PyObject *parent, void *buffer,
79
86
/**
80
87
 * Buffer interface support for the PyBufferProxy.
81
88
 */
82
 
static PyBufferProcs _bufferproxy_as_buffer =
83
 
{
84
 
        (readbufferproc) _bufferproxy_getreadbuf,
85
 
        (writebufferproc) _bufferproxy_getwritebuf,
86
 
        (segcountproc) _bufferproxy_getsegcount,
87
 
        NULL,
88
 
};
 
89
#if PY3
 
90
static PyBufferProcs _bufferproxy_as_buffer =
 
91
{
 
92
    (getbufferproc) _bufferproxy_getbuffer,
 
93
    (releasebufferproc) _bufferproxy_releasebuffer
 
94
};
 
95
#else
 
96
static PyBufferProcs _bufferproxy_as_buffer =
 
97
{
 
98
    (readbufferproc) _bufferproxy_getreadbuf,
 
99
    (writebufferproc) _bufferproxy_getwritebuf,
 
100
    (segcountproc) _bufferproxy_getsegcount,
 
101
    NULL,
 
102
#if PY_VERSION_HEX >= 0x02060000
 
103
    NULL,
 
104
    NULL
 
105
#endif
 
106
};
 
107
#endif
89
108
 
90
109
static PyTypeObject PyBufferProxy_Type =
91
110
{
92
 
    PyObject_HEAD_INIT(NULL)
93
 
    0,
 
111
    TYPE_HEAD (NULL, 0)
94
112
    "pygame.bufferproxy.BufferProxy", /* tp_name */
95
113
    sizeof (PyBufferProxy),     /* tp_basicsize */
96
114
    0,                          /* tp_itemsize */
130
148
    0,                          /* tp_init */
131
149
    0,                          /* tp_alloc */
132
150
    _bufferproxy_new,           /* tp_new */
 
151
#ifndef __SYMBIAN32__    
133
152
    0,                          /* tp_free */
134
153
    0,                          /* tp_is_gc */
135
154
    0,                          /* tp_bases */
138
157
    0,                          /* tp_subclasses */
139
158
    0,                          /* tp_weaklist */
140
159
    0                           /* tp_del */
 
160
#endif    
141
161
};
142
162
 
143
163
/**
169
189
 
170
190
    Py_XDECREF (self->lock);
171
191
    Py_XDECREF (self->dict);
172
 
    self->ob_type->tp_free ((PyObject *) self);
 
192
    Py_TYPE(self)->tp_free ((PyObject *) self);
173
193
}
174
194
 
175
195
/**** Getter and setter access ****/
197
217
static PyObject*
198
218
_bufferproxy_get_raw (PyBufferProxy *self, void *closure)
199
219
{
200
 
    return PyString_FromStringAndSize (self->buffer, self->length);
 
220
    return Bytes_FromStringAndSize (self->buffer, self->length);
201
221
}
202
222
 
203
223
/**
221
241
#if PY_VERSION_HEX < 0x02050000
222
242
    return PyString_FromFormat("<BufferProxy(%d)>", self->length);
223
243
#else
224
 
    return PyString_FromFormat("<BufferProxy(%zd)>", self->length);
 
244
    return Text_FromFormat("<BufferProxy(%zd)>", self->length);
225
245
#endif
226
246
}
227
247
 
250
270
 
251
271
/**** Buffer interfaces ****/
252
272
 
 
273
#if PY3
 
274
static int
 
275
_bufferproxy_getbuffer (PyBufferProxy *self, Py_buffer *view, int flags)
 
276
{
 
277
    if (!view)
 
278
        return 0;
 
279
    Py_INCREF (self); /* Guarantee that the object does not get destroyed */
 
280
    return PyBuffer_FillInfo (view, (PyObject*)self, self->buffer,
 
281
                              self->length, 0, flags);
 
282
}
 
283
 
 
284
static void
 
285
_bufferproxy_releasebuffer (PyBufferProxy *self, Py_buffer *view)
 
286
{
 
287
    Py_DECREF (self);
 
288
}
 
289
#else /* PY3 */
253
290
static Py_ssize_t
254
291
_bufferproxy_getreadbuf (PyBufferProxy *buffer, Py_ssize_t _index,
255
292
                         const void **ptr)
305
342
        *lenp = buffer->length;
306
343
    return 1;
307
344
}
 
345
#endif /* PY3 */
308
346
 
309
347
static PyObject*
310
348
PyBufferProxy_New (PyObject *parent, void *buffer, Py_ssize_t length,
323
361
    return (PyObject *) buf;
324
362
}
325
363
 
326
 
PYGAME_EXPORT
327
 
void initbufferproxy (void)
 
364
/*DOC*/ static char _bufferproxy_doc[] =
 
365
/*DOC*/    "A generic proxy module that can spend arbitrary " \
 
366
/*DOC*/    "objects a buffer interface";
 
367
 
 
368
MODINIT_DEFINE (bufferproxy)
328
369
{
329
370
    PyObject *module;
330
371
    PyObject *dict;
331
372
    PyObject *apiobj;
 
373
    int ecode;
332
374
    static void* c_api[PYGAMEAPI_BUFFERPROXY_NUMSLOTS];
333
375
 
 
376
#if PY3
 
377
    static struct PyModuleDef _module = {
 
378
        PyModuleDef_HEAD_INIT,
 
379
        "bufferproxy",
 
380
        _bufferproxy_doc,
 
381
        -1,
 
382
        _bufferproxy_methods,
 
383
        NULL, NULL, NULL, NULL
 
384
    };
 
385
#endif
 
386
 
334
387
    if (PyType_Ready (&PyBufferProxy_Type) < 0)
335
 
        return;
 
388
        MODINIT_ERROR;
336
389
 
337
390
    /* create the module */
338
 
    module = Py_InitModule3 ("bufferproxy", NULL,
339
 
        "A generic proxy module that can spend arbitrary " \
340
 
        "objects a buffer interface");
 
391
#if PY3
 
392
    module = PyModule_Create (&_module);
 
393
#else
 
394
    module = Py_InitModule3 (MODPREFIX "bufferproxy", NULL, _bufferproxy_doc);
 
395
#endif
341
396
    PyBufferProxy_Type.tp_getattro = PyObject_GenericGetAttr;
342
397
    Py_INCREF (&PyBufferProxy_Type);
343
 
    PyModule_AddObject (module, "BufferProxy", (PyObject *)&PyBufferProxy_Type);
 
398
    if (PyModule_AddObject (module, "BufferProxy",
 
399
                            (PyObject *)&PyBufferProxy_Type) == -1) {
 
400
        Py_DECREF ((PyObject *)&PyBufferProxy_Type);
 
401
        DECREF_MOD (module);
 
402
        MODINIT_ERROR;
 
403
    }
344
404
    dict = PyModule_GetDict (module);
345
405
 
346
406
    c_api[0] = &PyBufferProxy_Type;
347
407
    c_api[1] = PyBufferProxy_New;
348
408
    apiobj = PyCObject_FromVoidPtr (c_api, NULL);
349
 
    PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
 
409
    if (apiobj == NULL) {
 
410
        DECREF_MOD (module);
 
411
        MODINIT_ERROR;
 
412
    }
 
413
    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
350
414
    Py_DECREF (apiobj);
 
415
    if (ecode == -1) {
 
416
        DECREF_MOD (module);
 
417
        MODINIT_ERROR;
 
418
    }
 
419
    MODINIT_RETURN (module);
351
420
}