Suporte a Troca de D’ados

Essas rotinas permitem que o código C trabalhe com objetos serializados usando o mesmo formato de dados que o módulo marshal. Existem funções para gravar dados no formato de serialização e funções adicionais que podem ser usadas para ler os dados novamente. Os arquivos usados para armazenar dados empacotados devem ser abertos no modo binário.

Os valores numéricos são armazenados primeiro com o byte menos significativo.

The module supports two versions of the data format: version 0 is the historical version, version 1 shares interned strings in the file, and upon unmarshalling. Version 2 uses a binary format for floating point numbers. Py_MARSHAL_VERSION indicates the current file format (currently 2).

void PyMarshal_WriteLongToFile(long value, FILE *file, int version)

Aplica marshalling em um inteiro long, value, para file. Isso escreverá apenas os 32 bits menos significativos de value; independentemente do tamanho do tipo nativo long. version indica o formato do arquivo.

void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)

Aplica marshalling em um objeto Python, value, para file. version indica o formato do arquivo.

PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
Return value: New reference.

Retorna um objeto de bytes que contém a representação pós-marshalling de value. version indica o formato do arquivo.

As seguintes funções permitem que os valores pós-marshalling sejam lidos novamente.

XXX What about error detection? It appears that reading past the end of the file will always result in a negative numeric value (where that’s relevant), but it’s not clear that negative values won’t be handled properly when there’s no error. What’s the right way to tell? Should only non-negative values be written using these routines?

long PyMarshal_ReadLongFromFile(FILE *file)

Retorna um long C do fluxo de dados em um FILE* aberto para leitura. Somente um valor de 32 bits pode ser lido usando essa função, independentemente do tamanho nativo de long.

On error, raise an exception and return -1.

int PyMarshal_ReadShortFromFile(FILE *file)

Retorna um short C do fluxo de dados em um FILE* aberto para leitura. Somente um valor de 16 bits pode ser lido usando essa função, independentemente do tamanho nativo de short.

On error, raise an exception and return -1.

PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
Return value: New reference.

Retorna um objeto Python do fluxo de dados em um FILE* aberto para leitura.

On error, sets the appropriate exception (EOFError or TypeError) and returns NULL.

PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
Return value: New reference.

Retorna um objeto Python do fluxo de dados em um FILE* aberto para leitura. Diferentemente de PyMarshal_ReadObjectFromFile(), essa função presume que nenhum objeto adicional será lido do arquivo, permitindo que ele carregue agressivamente os dados do arquivo na memória, para que a desserialização possa operar a partir de dados na memória em vez de ler um byte por vez do arquivo. Use essas variantes apenas se tiver certeza de que não estará lendo mais nada do arquivo.

On error, sets the appropriate exception (EOFError or TypeError) and returns NULL.

PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
Return value: New reference.

Retorna um objeto Python do fluxo de dados em um buffer de bytes contendo len bytes apontados por data.

On error, sets the appropriate exception (EOFError or TypeError) and returns NULL.