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