-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-30193: Allow to load buffer objects with json.loads() #1334
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
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 |
---|---|---|
|
@@ -242,12 +242,13 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, | |
|
||
|
||
def detect_encoding(b): | ||
bstartswith = b.startswith | ||
if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)): | ||
return 'utf-32' | ||
if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)): | ||
return 'utf-16' | ||
if bstartswith(codecs.BOM_UTF8): | ||
for prefix in (codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE): | ||
if b[:len(prefix)] == prefix: | ||
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. This doesn't work for e.g. 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. purpose of this change is to allow to use c-ext classes that supports buffer protocol. I can remove memoryview from allowed type, and if encoding detection fails then fail with TypeError. proper solution would be to write memoryview like object that supports only "b|B" format 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. any conclusion on this ticket? 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. Is it reasonable to require unicode arrays for this patch? They are deprecated after all. 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. I suggest to start simple and raise a TypeError if it's memory view with view.format != 'B'. |
||
return 'utf-32' | ||
for prefix in (codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE): | ||
if b[:len(prefix)] == prefix: | ||
return 'utf-16' | ||
if b[:len(codecs.BOM_UTF8)] == codecs.BOM_UTF8: | ||
return 'utf-8-sig' | ||
|
||
if len(b) >= 4: | ||
|
@@ -343,10 +344,14 @@ def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, | |
raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)", | ||
s, 0) | ||
else: | ||
if not isinstance(s, (bytes, bytearray)): | ||
raise TypeError('the JSON object must be str, bytes or bytearray, ' | ||
'not {!r}'.format(s.__class__.__name__)) | ||
s = s.decode(detect_encoding(s), 'surrogatepass') | ||
if not isinstance(s, (bytes, bytearray, memoryview)): | ||
try: | ||
s = memoryview(s) | ||
except TypeError: | ||
raise TypeError('the JSON object must be str, bytes or ' | ||
'bytearray or memoryview compatible object, ' | ||
'not {!r}'.format(s.__class__.__name__)) | ||
s = str(s, detect_encoding(s), 'surrogatepass') | ||
|
||
if (cls is None and object_hook is None and | ||
parse_int is None and parse_float is None and | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,6 +317,8 @@ Extension Modules | |
Library | ||
------- | ||
|
||
- bpo-30193: Allow to load buffer objects with ``json.loads()`` | ||
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. You should add a NEWS entry using the blurb tool, and revert this change. 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. |
||
|
||
- bpo-30101: Add support for curses.A_ITALIC. | ||
|
||
- bpo-29822: inspect.isabstract() now works during __init_subclass__. Patch | ||
|
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.
Why was this change made?
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.
memoryview does not provide "startswith" function
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.
D'oh, completely missed that, it's late here :) Anyway, support for bytes was added in https://bugs.python.org/issue17909 by Serhiy so it might make sense to ping him.