Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-132983: Split_zstd_set_c_parameters#133921

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AA-Turner wants to merge4 commits intopython:main
base:main
Choose a base branch
Loading
fromAA-Turner:zstd-set-params

Conversation

AA-Turner
Copy link
Member

@AA-TurnerAA-Turner commentedMay 12, 2025
edited by bedevere-appbot
Loading

The current_zstd_set_c_parameters shares little between the two possible (int or dict) paths. By splitting the function, we can now use thePy_ssize_t AC converter.

One slight change worth noting is that the constructor won't raise an error whenlevel is$-2^{63}$ but will just silently use the default. cc@erlend-aasland if there's a better method/sentinel we can use in the clinic to detect if the value has been set or not.

>>> _zstd.ZstdCompressor(level=-(1<<63)+1)Traceback (most recent call last):  File "<python-input-17>", line 1, in <module>    _zstd.ZstdCompressor(level=-(1<<63)+1)    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ValueError: compression level -9223372036854775807 not in valid range -131072 <= level <= 22.>>> _zstd.ZstdCompressor(level=-(1<<63))<compression.zstd.ZstdCompressor object at 0x7f61875b2210>

I also think it might be worth considering removing the level XOR options check, as we could adopt the rule that a compression level set in an options dict overrides thelevel parameter. I can see reasonable arguments to keep the status quo, however, so I've not done this yet.

A

cc@Rogdham

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commentedMay 12, 2025
edited
Loading

if there's a better method/sentinel we can use in the clinic to detect if the value has been set or not

No, there is not. You cannot distinguish the passed value from the default value. It is better to not using Argument Clinic for conversion of this parameter.

BTW,Py_ssize_t is the same asint on 32-bit platforms, so there is no good reason to use it here.

@emmatyping
Copy link
Member

If we can't use AC converters due to the default value not being expressible in C, I'm not sure splitting the function makes sense.

@serhiy-storchaka
Copy link
Member

Setting "level" and setting "options" do not share code. Why do they even use the same function?

@emmatyping
Copy link
Member

They were originally one argument in the pyzstd API, so I suppose splitting it is still useful since they are separate arguments. But I do think we should just takeobject and not usePy_ssize_t forlevel.

@emmatyping
Copy link
Member

@AA-Turner similarly, would you like to pick this up again as well? I'm happy to take this over if you don't have time. Want to get the last of the zstd items done before b2.

@@ -196,10 +196,17 @@ def test_simple_compress_bad_args(self):
self.assertRaises(TypeError,ZstdCompressor,zstd_dict=b"abcd1234")
self.assertRaises(TypeError,ZstdCompressor,zstd_dict={1:2,3:4})

# valid compression level range is [-(1<<17), 22]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What are the error messages for out of range values?

Comment on lines +404 to +408
ZstdDecompressor(options={2**31:100})
withself.assertRaises(ValueError):
ZstdDecompressor(options={2**1000:100})
withself.assertRaises(ValueError):
ZstdDecompressor(options={-(2**31)-1:100})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You perhaps meant to test for2**31-1 and-2**31.

Comment on lines +446 to +449
options[DecompressionParameter.window_log_max]=2**31
withself.assertRaises(ValueError):
decompress(b'',options=options)
options[DecompressionParameter.window_log_max]=-(2**32)-1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same as above.

@@ -49,98 +49,108 @@ typedef struct {
#include"clinic/compressor.c.h"

staticint
_zstd_set_c_parameters(ZstdCompressor*self,PyObject*level_or_options,
constchar*arg_name,constchar*arg_type)
_zstd_set_c_level(ZstdCompressor*self,constintlevel)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

const is redundant here.

return-1;
}

Py_INCREF(key);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It is never decrefed.

"Key of options dict should be either a "
"CompressionParameter attribute or an int.");
return-1;
"dictionary key must be less than 2**31");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This error message does not look quite useful (first, you can get OverflowError for negative values whichare less than2**31). What error do you get if the key value is in the range of C int, but is not valid? Try to imitate it. If it is too difficult, leave an OverflowError, it can be improved in future.

BTW, the old error message was about the type of the key, not its value. TypeError is more suitable for it. Do you have tests for non-integer key or value?

PyErr_SetString(PyExc_ValueError,
"Value of options dict should be an int.");
return-1;
"dictionary value must be less than 2**31");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same as for error message for key.

if (level!=PY_SSIZE_T_MIN&&options!=Py_None) {
PyErr_SetString(PyExc_RuntimeError,"Only one of level or options should be used.");
if (level!=Py_None&&options!=Py_None) {
PyErr_SetString(PyExc_RuntimeError,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

TypeError is more suitable for calling with conflicting arguments.

Comment on lines +389 to +391
PyErr_Format(PyExc_ValueError,
"%zd not in valid range %d <= compression level <= %d.",
level,ZSTD_minCLevel(),ZSTD_maxCLevel());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is a good example for enhanced ValueError message. But%zd is not valid format forPyObject *. Taking into account that string representation oflevel can be arbitrary long, it is better to not include it in the error message.

Comment on lines +116 to +132
Py_INCREF(key);
intkey_v=PyLong_AsInt(key);
if (key_v==-1&&PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
"Key of options dict should be either a "
"DecompressionParameter attribute or an int.");
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_SetString(PyExc_ValueError,
"dictionary key must be less than 2**31");
}
return-1;
}

Py_INCREF(value);
intvalue_v=PyLong_AsInt(value);
if (value_v==-1&&PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
"Value of options dict should be an int.");
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_SetString(PyExc_ValueError,
"dictionary value must be less than 2**31");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same issues as for compressor.

@bedevere-app
Copy link

When you're done making the requested changes, leave the comment:I have made the requested changes; please review again.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka requested changes

@emmatypingemmatypingAwaiting requested review from emmatyping

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@AA-Turner@serhiy-storchaka@emmatyping

[8]ページ先頭

©2009-2025 Movatter.jp