Skip to content
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

accept bytearray and memoryview as input to write in s3 submodule #293

Merged
merged 6 commits into from
Apr 19, 2019
Merged

accept bytearray and memoryview as input to write in s3 submodule #293

merged 6 commits into from
Apr 19, 2019

Conversation

bmizhen-exos
Copy link
Contributor

@bmizhen-exos bmizhen-exos commented Apr 15, 2019

Hello, I'd like to add ability to handle memoryview objects.
https://docs.python.org/2/c-api/buffer.html

I encountered this when attempting to wrap a s3 stream in a io.BufferedWriter like this

class MD5BufferedWriter(io.BufferedWriter):
    def __init__(self, output_stream):
        super().__init__(output_stream)
        self.md5 = hashlib.md5()

    def digest(self):
        return self.md5.hexdigest()

    def write(self, b):
        self.md5.update(b)
        return super().write(b)

with MD5BufferedWriter(smart_open.open('s3://bucket/name', 'wb')) as bt:
       bt.write('a'.encode('utf-8'))

Copy link
Collaborator

@mpenkov mpenkov left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution. Left you a minor comment.

smart_open/s3.py Outdated
@@ -477,8 +477,14 @@ def write(self, b):

There's buffering happening under the covers, so this may not actually
do any HTTP transfer right away."""
if not isinstance(b, six.binary_type):
raise TypeError("input must be a binary string, got: %r", b)
import sys
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move this stuff to module scope, and make it an internal constant, e.g. _BINARY_TYPES.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

smart_open/s3.py Outdated Show resolved Hide resolved
@mpenkov mpenkov changed the title now able to handle memoryview accept bytearray and memoryview as input to write in s3 submodule Apr 16, 2019
@mpenkov
Copy link
Collaborator

mpenkov commented Apr 17, 2019

Looking good. Could you please add a unit test that exercises your new feature?

@bmizhen-exos
Copy link
Contributor Author

Added a test. Tested to make sure it fails without the fix.

Copy link
Collaborator

@mpenkov mpenkov left a comment

Choose a reason for hiding this comment

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

Looks good. Left you some minor nitpicks.

smart_open/tests/test_s3.py Outdated Show resolved Hide resolved
smart_open/tests/test_s3.py Outdated Show resolved Hide resolved
@bmizhen-exos
Copy link
Contributor Author

bmizhen-exos commented Apr 18, 2019

Hi Michael, can you give me some idea of when this might land in pypi?

@mpenkov
Copy link
Collaborator

mpenkov commented Apr 19, 2019

We just did a bugfix release, I suppose we could do another one in a week or so. Are you in a rush?

@bmizhen-exos
Copy link
Contributor Author

It would be helpful, otherwise I'll have to live off a branch for a bit.
BTW, do I need to do anything else in order to merge to master?

thanks!

@mpenkov
Copy link
Collaborator

mpenkov commented Apr 19, 2019

No, that's pretty much it, congrats on your first contribution to smart_open! 🥇

@mpenkov mpenkov merged commit 0996986 into piskvorky:master Apr 19, 2019
@bmizhen-exos
Copy link
Contributor Author

bmizhen-exos commented Apr 19, 2019 via email

@mpenkov
Copy link
Collaborator

mpenkov commented Apr 26, 2019

Released 1.8.3

@bmizhen-exos
Copy link
Contributor Author

bmizhen-exos commented Apr 26, 2019 via email

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

Successfully merging this pull request may close these issues.

2 participants