Skip to content

gh-137899: Enhance shelve serializer validation with descriptive error messages#138768

Open
furkanonder wants to merge 10 commits intopython:mainfrom
furkanonder:gh-137899
Open

gh-137899: Enhance shelve serializer validation with descriptive error messages#138768
furkanonder wants to merge 10 commits intopython:mainfrom
furkanonder:gh-137899

Conversation

@furkanonder
Copy link
Copy Markdown
Contributor

@furkanonder furkanonder commented Sep 10, 2025

@furkanonder furkanonder requested a review from picnixz September 10, 2025 19:22
@furkanonder furkanonder added type-feature A feature request or enhancement extension-modules C modules in the Modules dir labels Sep 10, 2025
Comment thread Lib/shelve.py
self.deserializer = deserializer

@staticmethod
def _validate_serialized_value(serialized_value, original_value):
Copy link
Copy Markdown
Member

@picnixz picnixz Sep 10, 2025

Choose a reason for hiding this comment

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

Validation is also at the level of the C extension (in dbmmodule.c) so you should also change the message out there.

Comment thread Lib/shelve.py Outdated
@encukou
Copy link
Copy Markdown
Member

encukou commented Oct 17, 2025

I've sent some suggestions as a PR to your branch: furkanonder#3

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I am not sure this is the best solution of the original issue. Making error messages in the underlying database more specific would help too.

Comment thread Lib/shelve.py Outdated
Comment on lines +117 to +119
msg = (f"Serializer returned {invalid_type} for value "
f"{original_value!r} But database values must be "
f"bytes or str, not {invalid_type}")
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.

invalid_type is repeated twice. This is unnecessary.

The repr of original_value can be very large, it should not be included in the error message. Error most likely does not depend on the original value.

Improve dbm/gdbm error messages
@furkanonder
Copy link
Copy Markdown
Contributor Author

I've sent some suggestions as a PR to your branch: furkanonder#3

Thanks a lot for the PR!

@furkanonder
Copy link
Copy Markdown
Contributor Author

I am not sure this is the best solution of the original issue. Making error messages in the underlying database more specific would help too.

I'm not entirely clear on the specific approach you're suggesting. Could you provide an example of what you have in mind?
One potential improvement I'm considering is to make the error messages more specific by indicating which database backend raised the error.
For instance:

dbm.dumb: Serializer must return bytes or str, not NoneType
dbm.gnu: Serializer must return bytes or str, not NoneType

This would help users immediately identify which underlying database implementation is involved in the error. Would this address your concern about making the error messages in the underlying database more specific, or were you thinking of a different approach?

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review extension-modules C modules in the Modules dir stale Stale PR or inactive for long period of time. type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants