~pythonregexp2.7/python/issue2636-01

« back to all changes in this revision

Viewing changes to Modules/_ctypes/stgdict.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-06-09 14:37:21 UTC
  • mfrom: (39022.1.14 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080609143721-bj0g1mwta28038da
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#include <ffi.h>
7
7
#ifdef MS_WIN32
8
8
#include <windows.h>
 
9
#include <malloc.h>
9
10
#endif
10
11
#include "ctypes.h"
11
12
 
24
25
{
25
26
        if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
26
27
                return -1;
 
28
        self->format = NULL;
 
29
        self->ndim = 0;
 
30
        self->shape = NULL;
27
31
        return 0;
28
32
}
29
33
 
42
46
StgDict_dealloc(StgDictObject *self)
43
47
{
44
48
        StgDict_clear(self);
 
49
        PyMem_Free(self->format);
 
50
        PyMem_Free(self->shape);
45
51
        PyMem_Free(self->ffi_type_pointer.elements);
46
52
        PyDict_Type.tp_dealloc((PyObject *)self);
47
53
}
54
60
 
55
61
        StgDict_clear(dst);
56
62
        PyMem_Free(dst->ffi_type_pointer.elements);
 
63
        PyMem_Free(dst->format);
 
64
        dst->format = NULL;
 
65
        PyMem_Free(dst->shape);
 
66
        dst->shape = NULL;
57
67
        dst->ffi_type_pointer.elements = NULL;
58
68
 
59
69
        d = (char *)dst;
68
78
        Py_XINCREF(dst->restype);
69
79
        Py_XINCREF(dst->checker);
70
80
 
 
81
        if (src->format) {
 
82
                dst->format = PyMem_Malloc(strlen(src->format) + 1);
 
83
                if (dst->format == NULL)
 
84
                        return -1;
 
85
                strcpy(dst->format, src->format);
 
86
        }
 
87
        if (src->shape) {
 
88
                dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim);
 
89
                if (dst->shape == NULL)
 
90
                        return -1;
 
91
                memcpy(dst->shape, src->shape,
 
92
                       sizeof(Py_ssize_t) * src->ndim);
 
93
        }
 
94
 
71
95
        if (src->ffi_type_pointer.elements == NULL)
72
96
                return 0;
73
97
        size = sizeof(ffi_type *) * (src->length + 1);
349
373
                return -1;
350
374
        }
351
375
 
 
376
        if (stgdict->format) {
 
377
                PyMem_Free(stgdict->format);
 
378
                stgdict->format = NULL;
 
379
        }
 
380
 
352
381
        if (stgdict->ffi_type_pointer.elements)
353
382
                PyMem_Free(stgdict->ffi_type_pointer.elements);
354
383
 
387
416
                ffi_ofs = 0;
388
417
        }
389
418
 
 
419
        if (isStruct && !isPacked) {
 
420
                stgdict->format = alloc_format_string(NULL, "T{");
 
421
        } else {
 
422
                /* PEP3118 doesn't support union, or packed structures (well,
 
423
                   only standard packing, but we dont support the pep for
 
424
                   that). Use 'B' for bytes. */
 
425
                stgdict->format = alloc_format_string(NULL, "B");
 
426
        }
 
427
 
390
428
#define realdict ((PyObject *)&stgdict->dict)
391
429
        for (i = 0; i < len; ++i) {
392
430
                PyObject *name = NULL, *desc = NULL;
451
489
                        }
452
490
                } else
453
491
                        bitsize = 0;
 
492
                if (isStruct && !isPacked) {
 
493
                        char *fieldfmt = dict->format ? dict->format : "B";
 
494
                        char *fieldname = PyString_AsString(name);
 
495
                        char *ptr;
 
496
                        Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt);
 
497
                        char *buf = alloca(len + 2 + 1);
 
498
 
 
499
                        sprintf(buf, "%s:%s:", fieldfmt, fieldname);
 
500
 
 
501
                        ptr = stgdict->format;
 
502
                        stgdict->format = alloc_format_string(stgdict->format, buf);
 
503
                        PyMem_Free(ptr);
 
504
 
 
505
                        if (stgdict->format == NULL) {
 
506
                                Py_DECREF(pair);
 
507
                                return -1;
 
508
                        }
 
509
                }
454
510
                if (isStruct) {
455
511
                        prop = CField_FromDesc(desc, i,
456
512
                                               &field_size, bitsize, &bitofs,
481
537
                Py_DECREF(prop);
482
538
        }
483
539
#undef realdict
 
540
 
 
541
        if (isStruct && !isPacked) {
 
542
                stgdict->format = alloc_format_string(stgdict->format, "}");
 
543
                if (stgdict->format == NULL)
 
544
                        return -1;
 
545
        }
 
546
 
484
547
        if (!isStruct)
485
548
                size = union_size;
486
549