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

Account for bytes in Python 3 #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ test:
rm -f .coverage
nosetests --rednose --exe --cover-package=s3po --with-coverage --cover-branches --logging-clear-handlers -v

test3:
rm -f .coverage
python3 -m nose --rednose --exe --cover-package=s3po --with-coverage --cover-branches --logging-clear-handlers -v

clean:
# Remove the build
rm -rf build dist
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ vagrant ssh

# On the vagrant instance
make test
make test3 # To run the tests under Python 3
```
7 changes: 7 additions & 0 deletions s3po/backends/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def __init__(self):
def download(self, bucket, key, fobj, retries, headers=None):
'''Download the contents of bucket/key to fobj'''
obj = self.buckets[bucket].get(key)

# if we stored a bytes object, decode it into a string
try:
obj = obj.decode('ascii')
Copy link
Contributor

Choose a reason for hiding this comment

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

I think instead of this you should change the mode for download_file to wb so that the file object will be in binary mode as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wish that was the fix. The test failure that needed fixing was on https://github.com/seomoz/s3po/blob/master/test/test_connection.py#L29 where it checks the result of self.conn.download. For the memory backend, download() grabs the actual python object that is stored, so when upload_file runs with mode rb, the value that the memory backend stores is bytes.

https://github.com/seomoz/s3po/blob/master/s3po/backends/memory.py#L20 is trying to write those bytes to into fobj which is a StringIO from https://github.com/seomoz/s3po/blob/master/s3po/connection.py#L79.

I'm totally not sure what the "right way" to handle the memory backend is in the face of string vs. bytes.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the default for StringIO is to use the system locale via this method:

locale.getpreferredencoding(False)

I think that's preferable to specifying ascii since that could throw away bytes greater than 0x7f.

except AttributeError:
pass

if not obj:
raise DownloadException('%s / %s not found' % (bucket, key))
else:
Expand Down
2 changes: 1 addition & 1 deletion s3po/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def upload_file(self, bucket, key, path, headers=None, extra=None, retries=3):
'''Upload the file at path to bucket/key. This method is important for
use in batch mode, so that the file object can be used with the right
context management'''
with open(os.path.abspath(path)) as fobj:
with open(os.path.abspath(path), 'rb') as fobj:
return self.upload(
bucket, key, fobj,
headers=headers, extra=extra, retries=retries)
Expand Down
4 changes: 3 additions & 1 deletion scripts/vagrant/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

set -e

sudo apt-get install -y python-pip python-dev
sudo apt-get update
sudo apt-get install -y python-pip python-dev python3-pip python3-dev

echo '[Credentials]
aws_access_key_id = not-a-real-id
Expand All @@ -11,6 +12,7 @@ aws_secret_access_key = not-a-real-key' > ~/.boto
(
cd /vagrant
sudo pip install -r requirements.txt
sudo pip3 install -r requirements.txt
)

echo $'\ncd /vagrant' >> ~/.profile