Skip to content

Commit 8e6df3e

Browse files
committed
pythongh-91349: add support for libdeflate library.
for now, this only exposes the crc32 function.
1 parent 576cc32 commit 8e6df3e

File tree

6 files changed

+3198
-4112
lines changed

6 files changed

+3198
-4112
lines changed

Modules/Setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ PYTHONPATH=$(COREPYTHONPATH)
200200
#_gdbm _gdbmmodule.c -lgdbm
201201
#_lzma _lzmamodule.c -llzma
202202
#_uuid _uuidmodule.c -luuid
203+
#deflate deflatemodule.c -ldeflate
203204
#zlib zlibmodule.c -lz
204205

205206
# The readline module also supports libeditline (-leditline).

Modules/Setup.stdlib.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
@MODULE_BINASCII_TRUE@binascii binascii.c
6767
@MODULE__BZ2_TRUE@_bz2 _bz2module.c
6868
@MODULE__LZMA_TRUE@_lzma _lzmamodule.c
69+
@MODULE_DEFLATE_TRUE@deflate deflatemodule.c
6970
@MODULE_ZLIB_TRUE@zlib zlibmodule.c
7071

7172
# dbm/gdbm

Modules/clinic/deflatemodule.c.h

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/deflatemodule.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* deflate module */
2+
3+
/* This module provides an interface to deflate library. */
4+
5+
/* deflate objects */
6+
7+
#ifndef Py_BUILD_CORE_BUILTIN
8+
# define Py_BUILD_CORE_MODULE 1
9+
#endif
10+
11+
#include "Python.h"
12+
#include "clinic/deflatemodule.c.h"
13+
#include "libdeflate.h"
14+
15+
/*[clinic input]
16+
module deflate
17+
[clinic start generated code]*/
18+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e8fe2232b5d8509a]*/
19+
20+
/*[clinic input]
21+
deflate.crc32 -> unsigned_int
22+
23+
data: Py_buffer
24+
value: unsigned_int(bitwise=True) = 0
25+
Starting value of the checksum.
26+
/
27+
28+
Compute a CRC-32 checksum of data.
29+
30+
The returned checksum is an integer.
31+
[clinic start generated code]*/
32+
33+
static unsigned int
34+
deflate_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
35+
/*[clinic end generated code: output=1509d54a8b6329c0 input=50ff444788572aa2]*/
36+
{
37+
/* Releasing the GIL for very small buffers is inefficient
38+
and may lower performance */
39+
if (data->len > 1024*5) {
40+
Py_BEGIN_ALLOW_THREADS
41+
value = libdeflate_crc32((uint32_t)value, data->buf, (size_t)data->len);
42+
Py_END_ALLOW_THREADS
43+
} else {
44+
value = libdeflate_crc32((uint32_t)value, data->buf, (size_t)data->len);
45+
}
46+
return value;
47+
}
48+
49+
static PyMethodDef deflate_methods[] = {
50+
DEFLATE_CRC32_METHODDEF
51+
{NULL}
52+
};
53+
54+
static struct PyModuleDef deflate_module = {
55+
PyModuleDef_HEAD_INIT,
56+
.m_name = "deflate",
57+
.m_doc = "Module that provides a CRC32 function.",
58+
.m_size = -1,
59+
.m_methods = deflate_methods,
60+
};
61+
62+
PyMODINIT_FUNC
63+
PyInit_deflate(void)
64+
{
65+
return PyModule_Create(&deflate_module);
66+
}

0 commit comments

Comments
 (0)