diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Modules/binascii.c | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Modules/binascii.c')
-rw-r--r-- | contrib/tools/python3/src/Modules/binascii.c | 504 |
1 files changed, 252 insertions, 252 deletions
diff --git a/contrib/tools/python3/src/Modules/binascii.c b/contrib/tools/python3/src/Modules/binascii.c index 1f3248b604..1c6b3e5053 100644 --- a/contrib/tools/python3/src/Modules/binascii.c +++ b/contrib/tools/python3/src/Modules/binascii.c @@ -61,17 +61,17 @@ #include "zlib.h" #endif -typedef struct binascii_state { - PyObject *Error; - PyObject *Incomplete; -} binascii_state; - -static binascii_state * -get_binascii_state(PyObject *module) -{ - return (binascii_state *)PyModule_GetState(module); -} - +typedef struct binascii_state { + PyObject *Error; + PyObject *Incomplete; +} binascii_state; + +static binascii_state * +get_binascii_state(PyObject *module) +{ + return (binascii_state *)PyModule_GetState(module); +} + /* ** hqx lookup table, ascii->binary. */ @@ -136,7 +136,7 @@ static const unsigned char table_a2b_hqx[256] = { static const unsigned char table_b2a_hqx[] = "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr"; -static const unsigned char table_a2b_base64[] = { +static const unsigned char table_a2b_base64[] = { -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, @@ -144,16 +144,16 @@ static const unsigned char table_a2b_base64[] = { -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, - 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1, - - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1, + + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, }; #define BASE64_PAD '=' @@ -280,7 +280,7 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data) unsigned int leftchar = 0; PyObject *rv; Py_ssize_t ascii_len, bin_len; - binascii_state *state; + binascii_state *state; ascii_data = data->buf; ascii_len = data->len; @@ -312,11 +312,11 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data) ** '`' as zero instead of space. */ if ( this_ch < ' ' || this_ch > (' ' + 64)) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "Illegal char"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Illegal char"); Py_DECREF(rv); return NULL; } @@ -344,11 +344,11 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data) /* Extra '`' may be written as padding in some cases */ if ( this_ch != ' ' && this_ch != ' '+64 && this_ch != '\n' && this_ch != '\r' ) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "Trailing garbage"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Trailing garbage"); Py_DECREF(rv); return NULL; } @@ -376,7 +376,7 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick) int leftbits = 0; unsigned char this_ch; unsigned int leftchar = 0; - binascii_state *state; + binascii_state *state; Py_ssize_t bin_len, out_len; _PyBytesWriter writer; @@ -385,11 +385,11 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick) bin_len = data->len; if ( bin_len > 45 ) { /* The 45 is a limit that appears in all uuencode's */ - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "At most 45 bytes at once"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "At most 45 bytes at once"); return NULL; } @@ -441,92 +441,92 @@ static PyObject * binascii_a2b_base64_impl(PyObject *module, Py_buffer *data) /*[clinic end generated code: output=0628223f19fd3f9b input=5872acf6e1cac243]*/ { - assert(data->len >= 0); + assert(data->len >= 0); - const unsigned char *ascii_data = data->buf; - size_t ascii_len = data->len; + const unsigned char *ascii_data = data->buf; + size_t ascii_len = data->len; - /* Allocate the buffer */ - Py_ssize_t bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ - _PyBytesWriter writer; + /* Allocate the buffer */ + Py_ssize_t bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ + _PyBytesWriter writer; _PyBytesWriter_Init(&writer); - unsigned char *bin_data = _PyBytesWriter_Alloc(&writer, bin_len); + unsigned char *bin_data = _PyBytesWriter_Alloc(&writer, bin_len); if (bin_data == NULL) return NULL; - unsigned char *bin_data_start = bin_data; + unsigned char *bin_data_start = bin_data; - int quad_pos = 0; - unsigned char leftchar = 0; - int pads = 0; - for (size_t i = 0; i < ascii_len; i++) { - unsigned char this_ch = ascii_data[i]; + int quad_pos = 0; + unsigned char leftchar = 0; + int pads = 0; + for (size_t i = 0; i < ascii_len; i++) { + unsigned char this_ch = ascii_data[i]; /* Check for pad sequences and ignore ** the invalid ones. */ if (this_ch == BASE64_PAD) { - if (quad_pos >= 2 && quad_pos + ++pads >= 4) { + if (quad_pos >= 2 && quad_pos + ++pads >= 4) { /* A pad sequence means no more input. ** We've already interpreted the data ** from the quad at this point. */ - goto done; + goto done; } - continue; + continue; } - this_ch = table_a2b_base64[this_ch]; - if (this_ch >= 64) { + this_ch = table_a2b_base64[this_ch]; + if (this_ch >= 64) { continue; - } - pads = 0; - - switch (quad_pos) { - case 0: - quad_pos = 1; - leftchar = this_ch; - break; - case 1: - quad_pos = 2; - *bin_data++ = (leftchar << 2) | (this_ch >> 4); - leftchar = this_ch & 0x0f; - break; - case 2: - quad_pos = 3; - *bin_data++ = (leftchar << 4) | (this_ch >> 2); - leftchar = this_ch & 0x03; - break; - case 3: - quad_pos = 0; - *bin_data++ = (leftchar << 6) | (this_ch); - leftchar = 0; - break; + } + pads = 0; + + switch (quad_pos) { + case 0: + quad_pos = 1; + leftchar = this_ch; + break; + case 1: + quad_pos = 2; + *bin_data++ = (leftchar << 2) | (this_ch >> 4); + leftchar = this_ch & 0x0f; + break; + case 2: + quad_pos = 3; + *bin_data++ = (leftchar << 4) | (this_ch >> 2); + leftchar = this_ch & 0x03; + break; + case 3: + quad_pos = 0; + *bin_data++ = (leftchar << 6) | (this_ch); + leftchar = 0; + break; } } - if (quad_pos != 0) { - binascii_state *state = PyModule_GetState(module); - if (state == NULL) { - /* error already set, from PyModule_GetState */ - } else if (quad_pos == 1) { + if (quad_pos != 0) { + binascii_state *state = PyModule_GetState(module); + if (state == NULL) { + /* error already set, from PyModule_GetState */ + } else if (quad_pos == 1) { /* ** There is exactly one extra valid, non-padding, base64 character. ** This is an invalid length, as there is no possible input that ** could encoded into such a base64 string. */ - PyErr_Format(state->Error, + PyErr_Format(state->Error, "Invalid base64-encoded string: " - "number of data characters (%zd) cannot be 1 more " + "number of data characters (%zd) cannot be 1 more " "than a multiple of 4", (bin_data - bin_data_start) / 3 * 4 + 1); } else { - PyErr_SetString(state->Error, "Incorrect padding"); + PyErr_SetString(state->Error, "Incorrect padding"); } _PyBytesWriter_Dealloc(&writer); return NULL; } -done: +done: return _PyBytesWriter_Finish(&writer, bin_data); } @@ -553,7 +553,7 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline) unsigned int leftchar = 0; Py_ssize_t bin_len, out_len; _PyBytesWriter writer; - binascii_state *state; + binascii_state *state; bin_data = data->buf; bin_len = data->len; @@ -562,11 +562,11 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline) assert(bin_len >= 0); if ( bin_len > BASE64_MAXBIN ) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "Too much data for base64 line"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Too much data for base64 line"); return NULL; } @@ -619,11 +619,11 @@ static PyObject * binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data) /*[clinic end generated code: output=4d6d8c54d54ea1c1 input=0d914c680e0eed55]*/ { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "binascii.a2b_hqx() is deprecated", 1) < 0) { - return NULL; - } - + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "binascii.a2b_hqx() is deprecated", 1) < 0) { + return NULL; + } + const unsigned char *ascii_data; unsigned char *bin_data; int leftbits = 0; @@ -633,7 +633,7 @@ binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data) Py_ssize_t len; int done = 0; _PyBytesWriter writer; - binascii_state *state; + binascii_state *state; ascii_data = data->buf; len = data->len; @@ -657,11 +657,11 @@ binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data) if ( this_ch == SKIP ) continue; if ( this_ch == FAIL ) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "Illegal char"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Illegal char"); _PyBytesWriter_Dealloc(&writer); return NULL; } @@ -682,11 +682,11 @@ binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data) } if ( leftbits && !done ) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Incomplete, + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Incomplete, "String has incomplete number of bytes"); _PyBytesWriter_Dealloc(&writer); return NULL; @@ -712,11 +712,11 @@ static PyObject * binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data) /*[clinic end generated code: output=393d79338f5f5629 input=e1f1712447a82b09]*/ { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "binascii.rlecode_hqx() is deprecated", 1) < 0) { - return NULL; - } - + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "binascii.rlecode_hqx() is deprecated", 1) < 0) { + return NULL; + } + const unsigned char *in_data; unsigned char *out_data; unsigned char ch; @@ -779,11 +779,11 @@ static PyObject * binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data) /*[clinic end generated code: output=d0aa5a704bc9f7de input=9596ebe019fe12ba]*/ { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "binascii.b2a_hqx() is deprecated", 1) < 0) { - return NULL; - } - + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "binascii.b2a_hqx() is deprecated", 1) < 0) { + return NULL; + } + unsigned char *ascii_data; const unsigned char *bin_data; int leftbits = 0; @@ -839,11 +839,11 @@ static PyObject * binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data) /*[clinic end generated code: output=9826619565de1c6c input=54cdd49fc014402c]*/ { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "binascii.rledecode_hqx() is deprecated", 1) < 0) { - return NULL; - } - + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "binascii.rledecode_hqx() is deprecated", 1) < 0) { + return NULL; + } + const unsigned char *in_data; unsigned char *out_data; unsigned char in_byte, in_repeat; @@ -853,7 +853,7 @@ binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data) in_data = data->buf; in_len = data->len; _PyBytesWriter_Init(&writer); - binascii_state *state; + binascii_state *state; assert(in_len >= 0); @@ -878,11 +878,11 @@ binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data) #define INBYTE(b) \ do { \ if ( --in_len < 0 ) { \ - state = PyModule_GetState(module); \ - if (state == NULL) { \ - return NULL; \ - } \ - PyErr_SetString(state->Incomplete, ""); \ + state = PyModule_GetState(module); \ + if (state == NULL) { \ + return NULL; \ + } \ + PyErr_SetString(state->Incomplete, ""); \ goto error; \ } \ b = *in_data++; \ @@ -904,11 +904,11 @@ binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data) /* Note Error, not Incomplete (which is at the end ** of the string only). This is a programmer error. */ - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "Orphaned RLE code at start"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Orphaned RLE code at start"); goto error; } *out_data++ = RUNCHAR; @@ -958,7 +958,7 @@ error: /*[clinic input] -binascii.crc_hqx +binascii.crc_hqx data: Py_buffer crc: unsigned_int(bitwise=True) @@ -967,9 +967,9 @@ binascii.crc_hqx Compute CRC-CCITT incrementally. [clinic start generated code]*/ -static PyObject * +static PyObject * binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc) -/*[clinic end generated code: output=2fde213d0f547a98 input=56237755370a951c]*/ +/*[clinic end generated code: output=2fde213d0f547a98 input=56237755370a951c]*/ { const unsigned char *bin_data; Py_ssize_t len; @@ -982,7 +982,7 @@ binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc) crc = ((crc<<8)&0xff00) ^ crctab_hqx[(crc>>8)^*bin_data++]; } - return PyLong_FromUnsignedLong(crc); + return PyLong_FromUnsignedLong(crc); } #ifndef USE_ZLIB_CRC32 @@ -1155,33 +1155,33 @@ binascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc) binascii.b2a_hex data: Py_buffer - sep: object = NULL - An optional single character or byte to separate hex bytes. - bytes_per_sep: int = 1 - How many bytes between separators. Positive values count from the - right, negative values count from the left. + sep: object = NULL + An optional single character or byte to separate hex bytes. + bytes_per_sep: int = 1 + How many bytes between separators. Positive values count from the + right, negative values count from the left. Hexadecimal representation of binary data. The return value is a bytes object. This function is also available as "hexlify()". - -Example: ->>> binascii.b2a_hex(b'\xb9\x01\xef') -b'b901ef' ->>> binascii.hexlify(b'\xb9\x01\xef', ':') -b'b9:01:ef' ->>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2) -b'b9_01ef' + +Example: +>>> binascii.b2a_hex(b'\xb9\x01\xef') +b'b901ef' +>>> binascii.hexlify(b'\xb9\x01\xef', ':') +b'b9:01:ef' +>>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2) +b'b9_01ef' [clinic start generated code]*/ static PyObject * -binascii_b2a_hex_impl(PyObject *module, Py_buffer *data, PyObject *sep, - int bytes_per_sep) -/*[clinic end generated code: output=a26937946a81d2c7 input=ec0ade6ba2e43543]*/ +binascii_b2a_hex_impl(PyObject *module, Py_buffer *data, PyObject *sep, + int bytes_per_sep) +/*[clinic end generated code: output=a26937946a81d2c7 input=ec0ade6ba2e43543]*/ { - return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len, - sep, bytes_per_sep); + return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len, + sep, bytes_per_sep); } /*[clinic input] @@ -1189,17 +1189,17 @@ binascii.hexlify = binascii.b2a_hex Hexadecimal representation of binary data. -The return value is a bytes object. This function is also -available as "b2a_hex()". +The return value is a bytes object. This function is also +available as "b2a_hex()". [clinic start generated code]*/ static PyObject * -binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep, - int bytes_per_sep) -/*[clinic end generated code: output=d12aa1b001b15199 input=bc317bd4e241f76b]*/ +binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep, + int bytes_per_sep) +/*[clinic end generated code: output=d12aa1b001b15199 input=bc317bd4e241f76b]*/ { - return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len, - sep, bytes_per_sep); + return _Py_strhex_bytes_with_sep((const char *)data->buf, data->len, + sep, bytes_per_sep); } /*[clinic input] @@ -1223,7 +1223,7 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr) PyObject *retval; char* retbuf; Py_ssize_t i, j; - binascii_state *state; + binascii_state *state; argbuf = hexstr->buf; arglen = hexstr->len; @@ -1235,11 +1235,11 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr) * raise an exception. */ if (arglen % 2) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, "Odd-length string"); + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Odd-length string"); return NULL; } @@ -1249,14 +1249,14 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr) retbuf = PyBytes_AS_STRING(retval); for (i=j=0; i < arglen; i += 2) { - unsigned int top = _PyLong_DigitValue[Py_CHARMASK(argbuf[i])]; - unsigned int bot = _PyLong_DigitValue[Py_CHARMASK(argbuf[i+1])]; - if (top >= 16 || bot >= 16) { - state = PyModule_GetState(module); - if (state == NULL) { - return NULL; - } - PyErr_SetString(state->Error, + unsigned int top = _PyLong_DigitValue[Py_CHARMASK(argbuf[i])]; + unsigned int bot = _PyLong_DigitValue[Py_CHARMASK(argbuf[i+1])]; + if (top >= 16 || bot >= 16) { + state = PyModule_GetState(module); + if (state == NULL) { + return NULL; + } + PyErr_SetString(state->Error, "Non-hexadecimal digit found"); goto finally; } @@ -1312,7 +1312,7 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header) /* We allocate the output same size as input, this is overkill. */ - odata = (unsigned char *) PyMem_Calloc(1, datalen); + odata = (unsigned char *) PyMem_Calloc(1, datalen); if (odata == NULL) { PyErr_NoMemory(); return NULL; @@ -1343,9 +1343,9 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header) (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') || (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) { /* hexval */ - ch = _PyLong_DigitValue[ascii_data[in]] << 4; + ch = _PyLong_DigitValue[ascii_data[in]] << 4; in++; - ch |= _PyLong_DigitValue[ascii_data[in]]; + ch |= _PyLong_DigitValue[ascii_data[in]]; in++; odata[out++] = ch; } @@ -1497,7 +1497,7 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs, /* We allocate the output same size as input, this is overkill. */ - odata = (unsigned char *) PyMem_Calloc(1, odatalen); + odata = (unsigned char *) PyMem_Calloc(1, odatalen); if (odata == NULL) { PyErr_NoMemory(); return NULL; @@ -1605,82 +1605,82 @@ static struct PyMethodDef binascii_module_methods[] = { /* Initialization function for the module (*must* be called PyInit_binascii) */ PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII"); -static int -binascii_exec(PyObject *module) { - int result; - binascii_state *state = PyModule_GetState(module); - if (state == NULL) { - return -1; - } - - state->Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL); - if (state->Error == NULL) { - return -1; - } - Py_INCREF(state->Error); - result = PyModule_AddObject(module, "Error", state->Error); - if (result == -1) { - Py_DECREF(state->Error); - return -1; - } - - state->Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); - if (state->Incomplete == NULL) { - return -1; - } - Py_INCREF(state->Incomplete); - result = PyModule_AddObject(module, "Incomplete", state->Incomplete); - if (result == -1) { - Py_DECREF(state->Incomplete); - return -1; - } - - return 0; -} - -static PyModuleDef_Slot binascii_slots[] = { - {Py_mod_exec, binascii_exec}, - {0, NULL} -}; - -static int -binascii_traverse(PyObject *module, visitproc visit, void *arg) -{ - binascii_state *state = get_binascii_state(module); - Py_VISIT(state->Error); - Py_VISIT(state->Incomplete); - return 0; -} - -static int -binascii_clear(PyObject *module) -{ - binascii_state *state = get_binascii_state(module); - Py_CLEAR(state->Error); - Py_CLEAR(state->Incomplete); - return 0; -} - -static void -binascii_free(void *module) -{ - binascii_clear((PyObject *)module); -} - +static int +binascii_exec(PyObject *module) { + int result; + binascii_state *state = PyModule_GetState(module); + if (state == NULL) { + return -1; + } + + state->Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL); + if (state->Error == NULL) { + return -1; + } + Py_INCREF(state->Error); + result = PyModule_AddObject(module, "Error", state->Error); + if (result == -1) { + Py_DECREF(state->Error); + return -1; + } + + state->Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); + if (state->Incomplete == NULL) { + return -1; + } + Py_INCREF(state->Incomplete); + result = PyModule_AddObject(module, "Incomplete", state->Incomplete); + if (result == -1) { + Py_DECREF(state->Incomplete); + return -1; + } + + return 0; +} + +static PyModuleDef_Slot binascii_slots[] = { + {Py_mod_exec, binascii_exec}, + {0, NULL} +}; + +static int +binascii_traverse(PyObject *module, visitproc visit, void *arg) +{ + binascii_state *state = get_binascii_state(module); + Py_VISIT(state->Error); + Py_VISIT(state->Incomplete); + return 0; +} + +static int +binascii_clear(PyObject *module) +{ + binascii_state *state = get_binascii_state(module); + Py_CLEAR(state->Error); + Py_CLEAR(state->Incomplete); + return 0; +} + +static void +binascii_free(void *module) +{ + binascii_clear((PyObject *)module); +} + static struct PyModuleDef binasciimodule = { PyModuleDef_HEAD_INIT, "binascii", doc_binascii, - sizeof(binascii_state), + sizeof(binascii_state), binascii_module_methods, - binascii_slots, - binascii_traverse, - binascii_clear, - binascii_free + binascii_slots, + binascii_traverse, + binascii_clear, + binascii_free }; PyMODINIT_FUNC PyInit_binascii(void) { - return PyModuleDef_Init(&binasciimodule); + return PyModuleDef_Init(&binasciimodule); } |