Skip to content

gh-132983: Split _zstd_set_c_parameters - #133921

Merged
AA-Turner merged 17 commits into
python:mainfrom
AA-Turner:zstd-set-params
May 28, 2025
Merged

gh-132983: Split _zstd_set_c_parameters#133921
AA-Turner merged 17 commits into
python:mainfrom
AA-Turner:zstd-set-params

Conversation

@AA-Turner

@AA-TurnerAA-Turner commented May 12, 2025

Copy link
Copy Markdown
Member

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

One slight change worth noting is that the constructor won't raise an error when level 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 the level parameter. I can see reasonable arguments to keep the status quo, however, so I've not done this yet.

A

cc @Rogdham

Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c Outdated
Comment threadLib/test/test_zstd.py Outdated
Comment threadLib/test/test_zstd.py
@serhiy-storchaka

serhiy-storchaka commented May 12, 2025

Copy link
Copy Markdown
Member

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 as int on 32-bit platforms, so there is no good reason to use it here.

@emmatyping

This comment was marked as resolved.

@serhiy-storchaka

This comment was marked as resolved.

@emmatyping

This comment was marked as outdated.

@emmatyping

This comment was marked as outdated.

Comment threadLib/test/test_zstd.py Outdated
Comment threadLib/test/test_zstd.py Outdated
Comment threadLib/test/test_zstd.py Outdated
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/compressor.c
Comment threadModules/_zstd/compressor.c
Comment threadModules/_zstd/decompressor.c Outdated
@bedevere-app

Copy link
Copy Markdown

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

Comment threadLib/test/test_zstd.py Outdated
Comment threadModules/_zstd/compressor.c
Comment threadLib/test/test_zstd.py Outdated
Comment threadLib/test/test_zstd.py Outdated

@emmatypingemmatyping left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A few suggestions for wording of error messages, but I think otherwise this looks great. Thank you for refactoring this!

Comment threadModules/_zstd/_zstdmodule.c Outdated
Comment threadModules/_zstd/compressor.c Outdated
Comment threadModules/_zstd/decompressor.c

@emmatypingemmatyping left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this is a great refactor!

Comment threadModules/_zstd/compressor.c Outdated
Comment threadLib/test/test_zstd.py
Comment threadLib/test/test_zstd.py
Comment threadLib/test/test_zstd.py Outdated

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure about term "illegal", this is a question to native speakers, but the rest LGTM. Thank you.

Comment threadLib/test/test_zstd.py Outdated
AA-Turnerand others added 3 commits May 28, 2025 14:12
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@emmatyping

Copy link
Copy Markdown
Member

I am not sure about term "illegal"

It's not wrong, but perhaps invalid is better?

@AA-Turner
AA-Turner merged commit 11f7a93 into python:mainMay 28, 2025
@miss-islington-app

Copy link
Copy Markdown

Thanks @AA-Turner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 28, 2025
(cherry picked from commit 11f7a93)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@bedevere-app

Copy link
Copy Markdown

GH-134838 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label May 28, 2025
AA-Turner added a commit that referenced this pull request May 28, 2025
gh-132983: Split ``_zstd_set_c_parameters`` (GH-133921)
(cherry picked from commit 11f7a93)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
gostak-dd pushed a commit to gostak-dd/cpython that referenced this pull request Jun 2, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@AA-Turner@serhiy-storchaka@emmatyping