8000 gh-132983: Convert zstd __new__ methods to Argument Clinic by AA-Turner · Pull Request #133860 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132983: Convert zstd __new__ methods to Argument Clinic #133860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
post-merge tweaks
  • Loading branch information
AA-Turner committed May 11, 2025
commit 15fc9cee2d76a73bb4fc1f5f2ae4b1698e10235f
12 changes: 8 additions & 4 deletions Lib/test/test_zstd.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def test_decompress_epilogue_flags(self):
self.assertFalse(d.needs_input)

def test_decompressor_arg(self):
zd = ZstdDict(b'12345678', True)
zd = ZstdDict(b'12345678', is_raw=True)

with self.assertRaises(TypeError):
d = ZstdDecompressor(zstd_dict={})
Expand Down Expand Up @@ -1021,6 +1021,10 @@ def test_decompressor_skippable(self):
class ZstdDictTestCase(unittest.TestCase):

def test_is_raw(self):
# must be passed as a keyword argument
with self.assertRaises(TypeError):
ZstdDict(bytes(8), True)

# content < 8
b = b'1234567'
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -1096,7 +1100,7 @@ def test_train_dict(self):


TRAINED_DICT = train_dict(SAMPLES, DICT_SIZE1)
ZstdDict(TRAINED_DICT.dict_content, False)
ZstdDict(TRAINED_DICT.dict_content, is_raw=False)

self.assertNotEqual(TRAINED_DICT.dict_id, 0)
self.assertGreater(len(TRAINED_DICT.dict_content), 0)
Expand Down Expand Up @@ -1250,7 +1254,7 @@ def _nbytes(dat):
def test_as_prefix(self):
# V1
V1 = THIS_FILE_BYTES
zd = ZstdDict(V1, True)
zd = ZstdDict(V1, is_raw=True)

# V2
mid = len(V1) // 2
Expand All @@ -1266,7 +1270,7 @@ def test_as_prefix(self):
self.assertEqual(decompress(dat, zd.as_prefix), V2)

# use wrong prefix
zd2 = ZstdDict(SAMPLES[0], True)
zd2 = ZstdDict(SAMPLES[0], is_raw=True)
try:
decompressed = decompress(dat, zd2.as_prefix)
except ZstdError: # expected
Expand Down
4 changes: 2 additions & 2 deletions Modules/_zstd/clinic/compressor.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 12 additions & 14 deletions Modules/_zstd/clinic/zstddict.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions Modules/_zstd/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,8 @@ _zstd_load_c_dict(ZstdCompressor *self, PyObject *dict)
/*[clinic input]
@classmethod
_zstd.ZstdCompressor.__new__ as _zstd_ZstdCompressor_new

level: object = None
The compression level to use, defaults to ZSTD_CLEVEL_DEFAULT.
The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.
options: object = None
A dict object that contains advanced compression parameters.
zstd_dict: object = None
Expand All @@ -335,17 +334,15 @@ function instead.
static PyObject *
_zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
PyObject *options, PyObject *zstd_dict)
/*[clinic end generated code: output=cdef61eafecac3d7 input=a9e9d73f246d6588]*/
/*[clinic end generated code: output=cdef61eafecac3d7 input=b9ea61ecafbb1b1e]*/
{
ZstdCompressor* self = PyObject_GC_New(ZstdCompressor, type);
if (self == NULL) {
goto error;
}

self->dict = NULL;
self->use_multithread = 0;


/* Compression context */
self->cctx = ZSTD_createCCtx();
if (self->cctx == NULL) {
Expand Down Expand Up @@ -378,18 +375,17 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
}
}

/* Load dictionary to compression context */
/* Load zstd dictionary to compression context */
self->dict = NULL;
if (zstd_dict != Py_None) {
if (_zstd_load_c_dict(self, zstd_dict) < 0) {
goto error;
}

/* Py_INCREF the dict */
Py_INCREF(zstd_dict);
self->dict = zstd_dict;
}

// We can only start tracking self with the GC once self->dict is set.
// We can only start GC tracking once self->dict is set.
PyObject_GC_Track(self);

return (PyObject*)self;
Expand Down
11 changes: 4 additions & 7 deletions Modules/_zstd/decompressor.c
6D40
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ stream_decompress(ZstdDecompressor *self, Py_buffer *data, Py_ssize_t max_length
/*[clinic input]
@classmethod
_zstd.ZstdDecompressor.__new__ as _zstd_ZstdDecompressor_new

zstd_dict: object = None
A ZstdDict object, a pre-trained zstd dictionary.
options: object = None
Expand All @@ -545,14 +544,13 @@ function instead.
static PyObject *
_zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
PyObject *options)
/*[clinic end generated code: output=590ca65c1102ff4a input=e73db62a54e25e4b]*/
/*[clinic end generated code: output=590ca65c1102ff4a input=0f161edb33d83216]*/
{
ZstdDecompressor* self = PyObject_GC_New(ZstdDecompressor, type);
if (self == NULL) {
goto error;
}

self->dict = NULL;
self->input_buffer = NULL;
self->input_buffer_size = 0;
self->in_begin = -1;
Expand All @@ -574,13 +572,12 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
goto error;
}

/* Load dictionary to decompression context */
/* Load zstd dictionary to decompression context */
self->dict = NULL;
if (zstd_dict != Py_None) {
if (_zstd_load_d_dict(self, zstd_dict) < 0) {
goto error;
}

/* Py_INCREF the dict */
Py_INCREF(zstd_dict);
self->dict = zstd_dict;
}
Expand All @@ -592,7 +589,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
}
}

// We can only start tracking self with the GC once self->dict is set.
// We can only start GC tracking once self->dict is set.
PyObject_GC_Track(self);

return (PyObject*)self;
Expand Down
19 changes: 8 additions & 11 deletions Modules/_zstd/zstddict.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class _zstd.ZstdDict "ZstdDict *" "&zstd_dict_type_spec"
@classmethod
_zstd.ZstdDict.__new__ as _zstd_ZstdDict_new
dict_content: object
A bytes-like object, dictionary's content.
The content of a Zstandard dictionary as a bytes-like object.
/
*
is_raw: bool = False
This parameter is for advanced user. True means dict_content
argument is a "raw content" dictionary, free of any format
restriction. False means dict_content argument is an ordinary
zstd dictionary, was created by zstd functions, follow a
specified format.
If true, perform no checks on *dict_content*, useful for some
advanced cases. Otherwise, check that the content represents
a Zstandard dictionary created by the zstd functions.

Represents a zstd dictionary, which can be used for compression/decompression.

Expand All @@ -46,7 +46,7 @@ ZstdDecompressor objects.
static PyObject *
_zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content,
int is_raw)
/*[clinic end generated code: output=3ebff839cb3be6d7 input=a0356fa7336dea1c]*/
/*[clinic end generated code: output=3ebff839cb3be6d7 input=e9e22fdc68fa04cc]*/
{
ZstdDict* self = PyObject_GC_New(ZstdDict, type);
if (self == NULL) {
Expand Down Expand Up @@ -86,10 +86,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content,
if (!is_raw && self->dict_id == 0) {
char *msg = "The dict_content argument is not a valid zstd "
"dictionary. The first 4 bytes of a valid zstd dictionary "
"should be a magic number: b'\\x37\\xA4\\x30\\xEC'.\n"
"If you are an advanced user, and can be sure that "
"dict_content argument is a \"raw content\" zstd "
"dictionary, set is_raw parameter to True.";
"should be a magic number: b'\\x37\\xA4\\x30\\xEC'.\n";
PyErr_SetString(PyExc_ValueError, msg);
goto error;
}
Expand Down
0