Merge lp:~james-w/pkgme/unicode-output into lp:pkgme

Proposed by James Westby
Status: Merged
Approved by: Jonathan Lange
Approved revision: 115
Merged at revision: 115
Proposed branch: lp:~james-w/pkgme/unicode-output
Merge into: lp:pkgme
Diff against target: 34 lines (+14/-1)
2 files modified
pkgme/tests/test_write.py (+10/-0)
pkgme/write.py (+4/-1)
To merge this branch: bzr merge lp:~james-w/pkgme/unicode-output
Reviewer Review Type Date Requested Status
Jonathan Lange Approve
Review via email: mp+111018@code.launchpad.net

Commit message

Encode unicode before writing it to a file.

Description of the change

Hi,

As spotted by Dave when the package details contain unicode
then we error trying to write the packaging as we don't encode
to utf-8 before writing the files.

This adds a test that demonstrates that and a fix.

I'm not sure that handling str or unicode in that function is the
right thing to do.

Thanks,

James

To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

Thanks! Please make sure there's a card for this on the kanban.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pkgme/tests/test_write.py'
2--- pkgme/tests/test_write.py 2011-07-15 09:22:47 +0000
3+++ pkgme/tests/test_write.py 2012-06-19 13:57:54 +0000
4@@ -119,3 +119,13 @@
5 filename, contents=new_contents, overwrite=False)
6 writer.write([package_file], tempdir.path)
7 self.assertThat(target_path, FileContains(original_contents))
8+
9+ def test_write_encodes_unicode(self):
10+ writer = self.get_writer()
11+ tempdir = self.useFixture(TempdirFixture())
12+ contents = u"\x80"
13+ filename = self.getUniqueString()
14+ target_path = tempdir.abspath(filename)
15+ package_file = SimplePackageFile(filename, contents=contents)
16+ writer.write([package_file], tempdir.path)
17+ self.assertThat(target_path, FileContains(contents.encode('utf-8')))
18
19=== modified file 'pkgme/write.py'
20--- pkgme/write.py 2011-07-29 14:07:35 +0000
21+++ pkgme/write.py 2012-06-19 13:57:54 +0000
22@@ -15,8 +15,11 @@
23
24 def write_file(path, content):
25 ensure_containing_directory_exists(path)
26+ encoded_content = content
27+ if isinstance(content, unicode):
28+ encoded_content = content.encode('utf-8')
29 with open(path, "w") as f:
30- f.write(content)
31+ f.write(encoded_content)
32
33
34 class Writer(object):

Subscribers

People subscribed via source and target branches

to all changes: