~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Python/getargs.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
45
45
                           size_t, PyObject **);
46
46
static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
 
47
static int getbuffer(PyObject *, Py_buffer *, char**);
47
48
 
48
49
static int vgetargskeywords(PyObject *, PyObject *,
49
50
                            const char *, char **, va_list *, int);
138
139
 
139
140
/* Handle cleanup of allocated memory in case of exception */
140
141
 
 
142
static void
 
143
cleanup_ptr(void *ptr)
 
144
{
 
145
        PyMem_FREE(ptr);
 
146
}
 
147
 
 
148
static void
 
149
cleanup_buffer(void *ptr)
 
150
{
 
151
        PyBuffer_Release((Py_buffer *) ptr);
 
152
}
 
153
 
141
154
static int
142
 
addcleanup(void *ptr, PyObject **freelist)
 
155
addcleanup(void *ptr, PyObject **freelist, void (*destr)(void *))
143
156
{
144
157
        PyObject *cobj;
145
158
        if (!*freelist) {
146
159
                *freelist = PyList_New(0);
147
160
                if (!*freelist) {
148
 
                        PyMem_FREE(ptr);
 
161
                        destr(ptr);
149
162
                        return -1;
150
163
                }
151
164
        }
152
 
        cobj = PyCObject_FromVoidPtr(ptr, NULL);
 
165
        cobj = PyCObject_FromVoidPtr(ptr, destr);
153
166
        if (!cobj) {
154
 
                PyMem_FREE(ptr);
 
167
                destr(ptr);
155
168
                return -1;
156
169
        }
157
170
        if (PyList_Append(*freelist, cobj)) {
158
 
                PyMem_FREE(ptr);
159
171
                Py_DECREF(cobj);
160
172
                return -1;
161
173
        }
166
178
static int
167
179
cleanreturn(int retval, PyObject *freelist)
168
180
{
169
 
        if (freelist) {
170
 
                if (retval == 0) {
171
 
                        Py_ssize_t len = PyList_GET_SIZE(freelist), i;
172
 
                        for (i = 0; i < len; i++)
173
 
                                PyMem_FREE(PyCObject_AsVoidPtr(
174
 
                                                PyList_GET_ITEM(freelist, i)));
175
 
                }
176
 
                Py_DECREF(freelist);
 
181
        if (freelist && retval != 0) {
 
182
                /* We were successful, reset the destructors so that they
 
183
                   don't get called. */
 
184
                Py_ssize_t len = PyList_GET_SIZE(freelist), i;
 
185
                for (i = 0; i < len; i++)
 
186
                        ((PyCObject *) PyList_GET_ITEM(freelist, i))
 
187
                                ->destructor = NULL;
177
188
        }
 
189
        Py_XDECREF(freelist);
178
190
        return retval;
179
191
}
180
192
 
773
785
        }
774
786
        
775
787
        case 's': {/* string */
776
 
                if (*format == '#') {
 
788
                if (*format == '*') {
 
789
                        Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
 
790
 
 
791
                        if (PyString_Check(arg)) {
 
792
                                PyBuffer_FillInfo(p, arg,
 
793
                                                  PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
 
794
                                                  1, 0);
 
795
                        }
 
796
#ifdef Py_USING_UNICODE
 
797
                        else if (PyUnicode_Check(arg)) {
 
798
                                uarg = UNICODE_DEFAULT_ENCODING(arg);
 
799
                                if (uarg == NULL)
 
800
                                        return converterr(CONV_UNICODE,
 
801
                                                          arg, msgbuf, bufsize);
 
802
                                PyBuffer_FillInfo(p, arg,
 
803
                                                  PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
 
804
                                                  1, 0);
 
805
                        }
 
806
#endif
 
807
                        else { /* any buffer-like object */
 
808
                                char *buf;
 
809
                                if (getbuffer(arg, p, &buf) < 0)
 
810
                                        return converterr(buf, arg, msgbuf, bufsize);
 
811
                        }
 
812
                        if (addcleanup(p, freelist, cleanup_buffer)) {
 
813
                                return converterr(
 
814
                                        "(cleanup problem)",
 
815
                                        arg, msgbuf, bufsize);
 
816
                        }
 
817
                        format++;
 
818
                } else if (*format == '#') {
777
819
                        void **p = (void **)va_arg(*p_va, char **);
778
820
                        FETCH_SIZE;
779
821
                        
823
865
        }
824
866
 
825
867
        case 'z': {/* string, may be NULL (None) */
826
 
                if (*format == '#') { /* any buffer-like object */
 
868
                if (*format == '*') {
 
869
                        Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
 
870
 
 
871
                        if (arg == Py_None)
 
872
                                PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
 
873
                        else if (PyString_Check(arg)) {
 
874
                                PyBuffer_FillInfo(p, arg,
 
875
                                                  PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
 
876
                                                  1, 0);
 
877
                        }
 
878
#ifdef Py_USING_UNICODE
 
879
                        else if (PyUnicode_Check(arg)) {
 
880
                                uarg = UNICODE_DEFAULT_ENCODING(arg);
 
881
                                if (uarg == NULL)
 
882
                                        return converterr(CONV_UNICODE,
 
883
                                                          arg, msgbuf, bufsize);
 
884
                                PyBuffer_FillInfo(p, arg,
 
885
                                                  PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
 
886
                                                  1, 0);
 
887
                        }
 
888
#endif
 
889
                        else { /* any buffer-like object */
 
890
                                char *buf;
 
891
                                if (getbuffer(arg, p, &buf) < 0)
 
892
                                        return converterr(buf, arg, msgbuf, bufsize);
 
893
                        }
 
894
                        if (addcleanup(p, freelist, cleanup_buffer)) {
 
895
                                return converterr(
 
896
                                        "(cleanup problem)",
 
897
                                        arg, msgbuf, bufsize);
 
898
                        }
 
899
                        format++;
 
900
                } else if (*format == '#') { /* any buffer-like object */
827
901
                        void **p = (void **)va_arg(*p_va, char **);
828
902
                        FETCH_SIZE;
829
903
                        
998
1072
                                                "(memory error)",
999
1073
                                                arg, msgbuf, bufsize);
1000
1074
                                }
1001
 
                                if (addcleanup(*buffer, freelist)) {
 
1075
                                if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1002
1076
                                        Py_DECREF(s);
1003
1077
                                        return converterr(
1004
1078
                                                "(cleanup problem)",
1043
1117
                                return converterr("(memory error)",
1044
1118
                                                  arg, msgbuf, bufsize);
1045
1119
                        }
1046
 
                        if (addcleanup(*buffer, freelist)) {
 
1120
                        if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1047
1121
                                Py_DECREF(s);
1048
1122
                                return converterr("(cleanup problem)",
1049
1123
                                                arg, msgbuf, bufsize);
1144
1218
                
1145
1219
        case 'w': { /* memory buffer, read-write access */
1146
1220
                void **p = va_arg(*p_va, void **);
 
1221
                void *res;
1147
1222
                PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1148
1223
                Py_ssize_t count;
1149
 
                        
1150
 
                if (pb == NULL || 
 
1224
 
 
1225
                if (pb && pb->bf_releasebuffer && *format != '*')
 
1226
                        /* Buffer must be released, yet caller does not use
 
1227
                           the Py_buffer protocol. */
 
1228
                        return converterr("pinned buffer", arg, msgbuf, bufsize);
 
1229
 
 
1230
                if (pb && pb->bf_getbuffer && *format == '*') {
 
1231
                        /* Caller is interested in Py_buffer, and the object
 
1232
                           supports it directly. */
 
1233
                        format++;
 
1234
                        if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
 
1235
                                PyErr_Clear();
 
1236
                                return converterr("read-write buffer", arg, msgbuf, bufsize);
 
1237
                        }
 
1238
                        if (addcleanup(p, freelist, cleanup_buffer)) {
 
1239
                                return converterr(
 
1240
                                        "(cleanup problem)",
 
1241
                                        arg, msgbuf, bufsize);
 
1242
                        }
 
1243
                        if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
 
1244
                                return converterr("contiguous buffer", arg, msgbuf, bufsize);
 
1245
                        break;
 
1246
                }
 
1247
 
 
1248
                if (pb == NULL ||
1151
1249
                    pb->bf_getwritebuffer == NULL ||
1152
1250
                    pb->bf_getsegcount == NULL)
1153
1251
                        return converterr("read-write buffer", arg, msgbuf, bufsize);
1154
1252
                if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1155
1253
                        return converterr("single-segment read-write buffer", 
1156
1254
                                          arg, msgbuf, bufsize);
1157
 
                if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
 
1255
                if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
1158
1256
                        return converterr("(unspecified)", arg, msgbuf, bufsize);
1159
 
                if (*format == '#') {
1160
 
                        FETCH_SIZE;
1161
 
                        STORE_SIZE(count);
 
1257
                if (*format == '*') {
 
1258
                        PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
1162
1259
                        format++;
1163
1260
                }
 
1261
                else {
 
1262
                        *p = res;
 
1263
                        if (*format == '#') {
 
1264
                                FETCH_SIZE;
 
1265
                                STORE_SIZE(count);
 
1266
                                format++;
 
1267
                        }
 
1268
                }
1164
1269
                break;
1165
1270
        }
1166
1271
                
1186
1291
                                "string or single-segment read-only buffer",
1187
1292
                                arg, msgbuf, bufsize);
1188
1293
 
 
1294
                if (pb->bf_releasebuffer)
 
1295
                        return converterr(
 
1296
                                "string or pinned buffer",
 
1297
                                arg, msgbuf, bufsize);
 
1298
 
1189
1299
                count = pb->bf_getcharbuffer(arg, 0, p);
1190
1300
                if (count < 0)
1191
1301
                        return converterr("(unspecified)", arg, msgbuf, bufsize);
1212
1322
        Py_ssize_t count;
1213
1323
        if (pb == NULL ||
1214
1324
            pb->bf_getreadbuffer == NULL ||
1215
 
            pb->bf_getsegcount == NULL) {
 
1325
            pb->bf_getsegcount == NULL ||
 
1326
            pb->bf_releasebuffer != NULL) {
1216
1327
                *errmsg = "string or read-only buffer";
1217
1328
                return -1;
1218
1329
        }
1226
1337
        return count;
1227
1338
}
1228
1339
 
 
1340
static int
 
1341
getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
 
1342
{
 
1343
        void *buf;
 
1344
        Py_ssize_t count;
 
1345
        PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
 
1346
        if (pb == NULL) {
 
1347
                *errmsg = "string or buffer";
 
1348
                return -1;
 
1349
        }
 
1350
        if (pb->bf_getbuffer) {
 
1351
                if (pb->bf_getbuffer(arg, view, 0) < 0) {
 
1352
                        *errmsg = "convertible to a buffer";
 
1353
                        return -1;
 
1354
                }
 
1355
                if (!PyBuffer_IsContiguous(view, 'C')) {
 
1356
                        *errmsg = "contiguous buffer";
 
1357
                        return -1;
 
1358
                }
 
1359
                return 0;
 
1360
        }
 
1361
 
 
1362
        count = convertbuffer(arg, &buf, errmsg);
 
1363
        if (count < 0) {
 
1364
                *errmsg = "convertible to a buffer";
 
1365
                return count;
 
1366
        }
 
1367
        PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
 
1368
        return 0;
 
1369
}
 
1370
 
1229
1371
/* Support for keyword arguments donated by
1230
1372
   Geoff Philbrick <philbric@delphi.hks.com> */
1231
1373
 
1566
1708
                                else
1567
1709
                                        (void) va_arg(*p_va, int *);
1568
1710
                                format++;
 
1711
                        } else if ((c == 's' || c == 'z') && *format == '*') {
 
1712
                                format++;
1569
1713
                        }
1570
1714
                        break;
1571
1715
                }