-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-137899: Enhance shelve serializer validation with descriptive error messages #138768
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -106,6 +106,19 @@ def __init__(self, dict, protocol=None, writeback=False, | |||||
| self.serializer = serializer | ||||||
| self.deserializer = deserializer | ||||||
|
|
||||||
| @staticmethod | ||||||
| def _validate_serialized_value(serialized_value, original_value): | ||||||
| if (serialized_value is None or | ||||||
| not isinstance(serialized_value, (bytes, str))): | ||||||
| if serialized_value is None: | ||||||
| invalid_type = "None" | ||||||
| else: | ||||||
| invalid_type = type(serialized_value).__name__ | ||||||
| msg = (f"Serializer returned {invalid_type} for value " | ||||||
| f"{original_value!r} But database values must be " | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, 'But' should be lowercase unless it follows a period:
Suggested change
Adding a period before it is another option. |
||||||
| f"bytes or str, not {invalid_type}") | ||||||
|
Comment on lines
+117
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The repr of |
||||||
| raise ShelveError(msg) | ||||||
|
|
||||||
| def __iter__(self): | ||||||
| for k in self.dict.keys(): | ||||||
| yield k.decode(self.keyencoding) | ||||||
|
|
@@ -135,6 +148,7 @@ def __setitem__(self, key, value): | |||||
| if self.writeback: | ||||||
| self.cache[key] = value | ||||||
| serialized_value = self.serializer(value, self._protocol) | ||||||
| self._validate_serialized_value(serialized_value, value) | ||||||
| self.dict[key.encode(self.keyencoding)] = serialized_value | ||||||
|
|
||||||
| def __delitem__(self, key): | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.