Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(178250)

Side by Side Diff: Modules/_testcapimodule.c

Issue 23392: Add tests for marshal FILE* API
Patch Set: Created 4 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_marshal.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * C Extension module to test Python interpreter C APIs. 2 * C Extension module to test Python interpreter C APIs.
3 * 3 *
4 * The 'test_*' functions exported by this module are run as part of the 4 * The 'test_*' functions exported by this module are run as part of the
5 * standard Python regression test, via Lib/test/test_capi.py. 5 * standard Python regression test, via Lib/test/test_capi.py.
6 */ 6 */
7 7
8 #define PY_SSIZE_T_CLEAN 8 #define PY_SSIZE_T_CLEAN
9 9
10 #include "Python.h" 10 #include "Python.h"
11 #include <float.h> 11 #include <float.h>
12 #include "structmember.h" 12 #include "structmember.h"
13 #include "datetime.h" 13 #include "datetime.h"
14 #include "marshal.h"
14 #include <signal.h> 15 #include <signal.h>
15 16
16 #ifdef WITH_THREAD 17 #ifdef WITH_THREAD
17 #include "pythread.h" 18 #include "pythread.h"
18 #endif /* WITH_THREAD */ 19 #endif /* WITH_THREAD */
19 static PyObject *TestError; /* set to exception object in init */ 20 static PyObject *TestError; /* set to exception object in init */
20 21
21 /* Raise TestError with test_name + ": " + msg, and return NULL. */ 22 /* Raise TestError with test_name + ": " + msg, and return NULL. */
22 23
23 static PyObject * 24 static PyObject *
(...skipping 3166 matching lines...) Expand 10 before | Expand all | Expand 10 after
3190 return NULL; 3191 return NULL;
3191 3192
3192 err = raise(signum); 3193 err = raise(signum);
3193 if (err) 3194 if (err)
3194 return PyErr_SetFromErrno(PyExc_OSError); 3195 return PyErr_SetFromErrno(PyExc_OSError);
3195 3196
3196 if (PyErr_CheckSignals() < 0) 3197 if (PyErr_CheckSignals() < 0)
3197 return NULL; 3198 return NULL;
3198 3199
3199 Py_RETURN_NONE; 3200 Py_RETURN_NONE;
3201 }
3202
3203 /* marshal */
3204
3205 static PyObject*
3206 pymarshal_write_long_to_file(PyObject* self, PyObject *args)
3207 {
3208 long value;
3209 char *filename;
3210 int version;
3211 FILE *fp;
3212
3213 if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file",
3214 &value, &filename, &version))
3215 return NULL;
3216
3217 fp = fopen(filename, "wb");
3218 if (fp == NULL) {
3219 PyErr_SetFromErrno(PyExc_OSError);
3220 return NULL;
3221 }
3222
3223 PyMarshal_WriteLongToFile(value, fp, version);
3224
3225 fclose(fp);
3226 if (PyErr_Occurred())
3227 return NULL;
3228 Py_RETURN_NONE;
3229 }
3230
3231 static PyObject*
3232 pymarshal_write_object_to_file(PyObject* self, PyObject *args)
3233 {
3234 PyObject *obj;
3235 char *filename;
3236 int version;
3237 FILE *fp;
3238
3239 if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file",
3240 &obj, &filename, &version))
3241 return NULL;
3242
3243 fp = fopen(filename, "wb");
3244 if (fp == NULL) {
3245 PyErr_SetFromErrno(PyExc_OSError);
3246 return NULL;
3247 }
3248
3249 PyMarshal_WriteObjectToFile(obj, fp, version);
3250
3251 fclose(fp);
3252 if (PyErr_Occurred())
3253 return NULL;
3254 Py_RETURN_NONE;
3255 }
3256
3257 static PyObject*
3258 pymarshal_read_short_from_file(PyObject* self, PyObject *args)
3259 {
3260 int value;
3261 long pos;
3262 char *filename;
3263 FILE *fp;
3264
3265 if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename))
3266 return NULL;
3267
3268 fp = fopen(filename, "rb");
3269 if (fp == NULL) {
3270 PyErr_SetFromErrno(PyExc_OSError);
3271 return NULL;
3272 }
3273
3274 value = PyMarshal_ReadShortFromFile(fp);
3275 pos = ftell(fp);
3276
3277 fclose(fp);
3278 if (PyErr_Occurred())
3279 return NULL;
3280 return Py_BuildValue("il", value, pos);
3281 }
3282
3283 static PyObject*
3284 pymarshal_read_long_from_file(PyObject* self, PyObject *args)
3285 {
3286 long value, pos;
3287 char *filename;
3288 FILE *fp;
3289
3290 if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename))
3291 return NULL;
3292
3293 fp = fopen(filename, "rb");
3294 if (fp == NULL) {
3295 PyErr_SetFromErrno(PyExc_OSError);
3296 return NULL;
3297 }
3298
3299 value = PyMarshal_ReadLongFromFile(fp);
3300 pos = ftell(fp);
3301
3302 fclose(fp);
3303 if (PyErr_Occurred())
3304 return NULL;
3305 return Py_BuildValue("ll", value, pos);
3306 }
3307
3308 static PyObject*
3309 pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
3310 {
3311 PyObject *obj;
3312 long pos;
3313 char *filename;
3314 FILE *fp;
3315
3316 if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filen ame))
3317 return NULL;
3318
3319 fp = fopen(filename, "rb");
3320 if (fp == NULL) {
3321 PyErr_SetFromErrno(PyExc_OSError);
3322 return NULL;
3323 }
3324
3325 obj = PyMarshal_ReadLastObjectFromFile(fp);
3326 pos = ftell(fp);
3327
3328 fclose(fp);
3329 return Py_BuildValue("Nl", obj, pos);
3330 }
3331
3332 static PyObject*
3333 pymarshal_read_object_from_file(PyObject* self, PyObject *args)
3334 {
3335 PyObject *obj;
3336 long pos;
3337 char *filename;
3338 FILE *fp;
3339
3340 if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename))
3341 return NULL;
3342
3343 fp = fopen(filename, "rb");
3344 if (fp == NULL) {
3345 PyErr_SetFromErrno(PyExc_OSError);
3346 return NULL;
3347 }
3348
3349 obj = PyMarshal_ReadObjectFromFile(fp);
3350 pos = ftell(fp);
3351
3352 fclose(fp);
3353 return Py_BuildValue("Nl", obj, pos);
3200 } 3354 }
3201 3355
3202 3356
3203 static PyMethodDef TestMethods[] = { 3357 static PyMethodDef TestMethods[] = {
3204 {"raise_exception", raise_exception, METH_VARARGS}, 3358 {"raise_exception", raise_exception, METH_VARARGS},
3205 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, 3359 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
3206 {"test_config", (PyCFunction)test_config, METH_NOARGS}, 3360 {"test_config", (PyCFunction)test_config, METH_NOARGS},
3207 {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, 3361 {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS},
3208 {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, 3362 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
3209 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, 3363 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
3339 docstring_with_signature_and_extra_newlines}, 3493 docstring_with_signature_and_extra_newlines},
3340 {"docstring_with_signature_with_defaults", 3494 {"docstring_with_signature_with_defaults",
3341 (PyCFunction)test_with_docstring, METH_NOARGS, 3495 (PyCFunction)test_with_docstring, METH_NOARGS,
3342 docstring_with_signature_with_defaults}, 3496 docstring_with_signature_with_defaults},
3343 {"raise_signal", 3497 {"raise_signal",
3344 (PyCFunction)test_raise_signal, METH_VARARGS}, 3498 (PyCFunction)test_raise_signal, METH_VARARGS},
3345 #ifdef WITH_THREAD 3499 #ifdef WITH_THREAD
3346 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, 3500 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
3347 PyDoc_STR("set_error_class(error_class) -> None")}, 3501 PyDoc_STR("set_error_class(error_class) -> None")},
3348 #endif 3502 #endif
3503 {"pymarshal_write_long_to_file",
3504 pymarshal_write_long_to_file, METH_VARARGS},
3505 {"pymarshal_write_object_to_file",
3506 pymarshal_write_object_to_file, METH_VARARGS},
3507 {"pymarshal_read_short_from_file",
3508 pymarshal_read_short_from_file, METH_VARARGS},
3509 {"pymarshal_read_long_from_file",
3510 pymarshal_read_long_from_file, METH_VARARGS},
3511 {"pymarshal_read_last_object_from_file",
3512 pymarshal_read_last_object_from_file, METH_VARARGS},
3513 {"pymarshal_read_object_from_file",
3514 pymarshal_read_object_from_file, METH_VARARGS},
3349 {NULL, NULL} /* sentinel */ 3515 {NULL, NULL} /* sentinel */
3350 }; 3516 };
3351 3517
3352 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); P y_DECREF(o);} 3518 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); P y_DECREF(o);}
3353 3519
3354 typedef struct { 3520 typedef struct {
3355 char bool_member; 3521 char bool_member;
3356 char byte_member; 3522 char byte_member;
3357 unsigned char ubyte_member; 3523 unsigned char ubyte_member;
3358 short short_member; 3524 short short_member;
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
3667 Py_INCREF(&PyInstanceMethod_Type); 3833 Py_INCREF(&PyInstanceMethod_Type);
3668 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); 3834 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
3669 3835
3670 PyModule_AddIntConstant(m, "the_number_three", 3); 3836 PyModule_AddIntConstant(m, "the_number_three", 3);
3671 3837
3672 TestError = PyErr_NewException("_testcapi.error", NULL, NULL); 3838 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
3673 Py_INCREF(TestError); 3839 Py_INCREF(TestError);
3674 PyModule_AddObject(m, "error", TestError); 3840 PyModule_AddObject(m, "error", TestError);
3675 return m; 3841 return m;
3676 } 3842 }
OLDNEW
« no previous file with comments | « Lib/test/test_marshal.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+