Skip to content

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

Closed
wants to merge 1 commit into from
Closed

bpo-30193: Allow to load buffer objects with json.loads() #1334

wants to merge 1 commit into from

Conversation

fafhrd91
Copy link
Contributor

it is not possible to load buffer object with json.loads()

@mention-bot
Copy link

@fafhrd91, thanks for your PR! By analyzing the history of the files in this pull request, we identified @tiran, @benjaminp and @ezio-melotti to be potential reviewers.

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):
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

@DimitrisJim DimitrisJim Apr 27, 2017

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.

for prefix in (codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE):
if b[0:len(prefix)] == prefix:
return 'utf-16'
if b[0:len(codecs.BOM_UTF8)] == codecs.BOM_UTF8:
Copy link
Member

Choose a reason for hiding this comment

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

Replace [0:len(...)] with [:len(...)].

@@ -20,6 +21,15 @@ def test_empty_objects(self):
self.assertEqual(self.loads('[]'), [])
self.assertEqual(self.loads('""'), "")

def test_memoryview(self):
data = b'{"key": "val"}'
Copy link
Member

Choose a reason for hiding this comment

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

Please create the memoryview() on that line to make the test more explicit.

Misc/NEWS Outdated
@@ -102,6 +102,8 @@ Core and Builtins

- bpo-29546: Improve from-import error message with location

- bpo-30193: Allow to load buffer objects with ``json.loads()``
Copy link
Member

Choose a reason for hiding this comment

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

The NEWS entry should be move at the top the Library section.

@fafhrd91
Copy link
Contributor Author

@Haypo updated according comments

return 'utf-16'
if bstartswith(codecs.BOM_UTF8):
for prefix in (codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE):
if b[:len(prefix)] == prefix:
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't work for e.g. memoryview(array.array('u', '\ufeff[1,2,3]')).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

any conclusion on this ticket?

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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'.

@brettcannon
Copy link
Member

To try and help move older pull requests forward, we are going through and backfilling 'awaiting' labels on pull requests that are lacking the label. Based on the current reviews, the best we can tell in an automated fashion is that a core developer requested changes to be made to this pull request.

If/when the requested changes have been made, please leave a comment that says, I have made the requested changes; please review again. That will trigger a bot to flag this pull request as ready for a follow-up review.

@jakirkham
Copy link

It looks like all comments were addressed or the OP was seeking further feedback from core on the requested changes. Would it be possible for someone from core to review this again?

@flavianh
Copy link

@jakirkham Revived in #14977

@@ -317,6 +317,8 @@ Extension Modules
Library
-------

- bpo-30193: Allow to load buffer objects with ``json.loads()``
Copy link
Member

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

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

Looks like that is being done in @flavianh's follow-up PR ( #14977 ).

@csabella
Copy link
Contributor

csabella commented Feb 6, 2020

As this is from an unknown repository, I'm going to close this PR. A replacement PR has already been created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.