marshal.c.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(marshal_dump__doc__,
  9. "dump($module, value, file, version=version, /)\n"
  10. "--\n"
  11. "\n"
  12. "Write the value on the open file.\n"
  13. "\n"
  14. " value\n"
  15. " Must be a supported type.\n"
  16. " file\n"
  17. " Must be a writeable binary file.\n"
  18. " version\n"
  19. " Indicates the data format that dump should use.\n"
  20. "\n"
  21. "If the value has (or contains an object that has) an unsupported type, a\n"
  22. "ValueError exception is raised - but garbage data will also be written\n"
  23. "to the file. The object will not be properly read back by load().");
  24. #define MARSHAL_DUMP_METHODDEF \
  25. {"dump", _PyCFunction_CAST(marshal_dump), METH_FASTCALL, marshal_dump__doc__},
  26. static PyObject *
  27. marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file,
  28. int version);
  29. static PyObject *
  30. marshal_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  31. {
  32. PyObject *return_value = NULL;
  33. PyObject *value;
  34. PyObject *file;
  35. int version = Py_MARSHAL_VERSION;
  36. if (!_PyArg_CheckPositional("dump", nargs, 2, 3)) {
  37. goto exit;
  38. }
  39. value = args[0];
  40. file = args[1];
  41. if (nargs < 3) {
  42. goto skip_optional;
  43. }
  44. version = _PyLong_AsInt(args[2]);
  45. if (version == -1 && PyErr_Occurred()) {
  46. goto exit;
  47. }
  48. skip_optional:
  49. return_value = marshal_dump_impl(module, value, file, version);
  50. exit:
  51. return return_value;
  52. }
  53. PyDoc_STRVAR(marshal_load__doc__,
  54. "load($module, file, /)\n"
  55. "--\n"
  56. "\n"
  57. "Read one value from the open file and return it.\n"
  58. "\n"
  59. " file\n"
  60. " Must be readable binary file.\n"
  61. "\n"
  62. "If no valid value is read (e.g. because the data has a different Python\n"
  63. "version\'s incompatible marshal format), raise EOFError, ValueError or\n"
  64. "TypeError.\n"
  65. "\n"
  66. "Note: If an object containing an unsupported type was marshalled with\n"
  67. "dump(), load() will substitute None for the unmarshallable type.");
  68. #define MARSHAL_LOAD_METHODDEF \
  69. {"load", (PyCFunction)marshal_load, METH_O, marshal_load__doc__},
  70. PyDoc_STRVAR(marshal_dumps__doc__,
  71. "dumps($module, value, version=version, /)\n"
  72. "--\n"
  73. "\n"
  74. "Return the bytes object that would be written to a file by dump(value, file).\n"
  75. "\n"
  76. " value\n"
  77. " Must be a supported type.\n"
  78. " version\n"
  79. " Indicates the data format that dumps should use.\n"
  80. "\n"
  81. "Raise a ValueError exception if value has (or contains an object that has) an\n"
  82. "unsupported type.");
  83. #define MARSHAL_DUMPS_METHODDEF \
  84. {"dumps", _PyCFunction_CAST(marshal_dumps), METH_FASTCALL, marshal_dumps__doc__},
  85. static PyObject *
  86. marshal_dumps_impl(PyObject *module, PyObject *value, int version);
  87. static PyObject *
  88. marshal_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  89. {
  90. PyObject *return_value = NULL;
  91. PyObject *value;
  92. int version = Py_MARSHAL_VERSION;
  93. if (!_PyArg_CheckPositional("dumps", nargs, 1, 2)) {
  94. goto exit;
  95. }
  96. value = args[0];
  97. if (nargs < 2) {
  98. goto skip_optional;
  99. }
  100. version = _PyLong_AsInt(args[1]);
  101. if (version == -1 && PyErr_Occurred()) {
  102. goto exit;
  103. }
  104. skip_optional:
  105. return_value = marshal_dumps_impl(module, value, version);
  106. exit:
  107. return return_value;
  108. }
  109. PyDoc_STRVAR(marshal_loads__doc__,
  110. "loads($module, bytes, /)\n"
  111. "--\n"
  112. "\n"
  113. "Convert the bytes-like object to a value.\n"
  114. "\n"
  115. "If no valid value is found, raise EOFError, ValueError or TypeError. Extra\n"
  116. "bytes in the input are ignored.");
  117. #define MARSHAL_LOADS_METHODDEF \
  118. {"loads", (PyCFunction)marshal_loads, METH_O, marshal_loads__doc__},
  119. static PyObject *
  120. marshal_loads_impl(PyObject *module, Py_buffer *bytes);
  121. static PyObject *
  122. marshal_loads(PyObject *module, PyObject *arg)
  123. {
  124. PyObject *return_value = NULL;
  125. Py_buffer bytes = {NULL, NULL};
  126. if (PyObject_GetBuffer(arg, &bytes, PyBUF_SIMPLE) != 0) {
  127. goto exit;
  128. }
  129. if (!PyBuffer_IsContiguous(&bytes, 'C')) {
  130. _PyArg_BadArgument("loads", "argument", "contiguous buffer", arg);
  131. goto exit;
  132. }
  133. return_value = marshal_loads_impl(module, &bytes);
  134. exit:
  135. /* Cleanup for bytes */
  136. if (bytes.obj) {
  137. PyBuffer_Release(&bytes);
  138. }
  139. return return_value;
  140. }
  141. /*[clinic end generated code: output=12082d61d2942473 input=a9049054013a1b77]*/