Merge lp:~jelmer/brz/python3-xml into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merged at revision: 6982
Proposed branch: lp:~jelmer/brz/python3-xml
Merge into: lp:brz
Prerequisite: lp:~jelmer/brz/python3-b
Diff against target: 400 lines (+100/-49)
4 files modified
breezy/bzr/xml8.py (+4/-8)
breezy/bzr/xml_serializer.py (+12/-12)
breezy/tests/test_xml.py (+30/-29)
python3.passing (+54/-0)
To merge this branch: bzr merge lp:~jelmer/brz/python3-xml
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+346434@code.launchpad.net

Commit message

Port breezy.bzr.xml* to Python3.

Description of the change

Port breezy.bzr.xml* to Python3.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

I'm not sure on some of the bare encode() calls here, but it's probably on the caller if they pass non-ascii values?

review: Approve
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

> I'm not sure on some of the bare encode() calls here, but it's probably on the
> caller if they pass non-ascii values?
Yeah; this should be ascii only. At some point perhaps we can all just make them regular strings.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/bzr/xml8.py'
2--- breezy/bzr/xml8.py 2018-05-13 02:18:13 +0000
3+++ breezy/bzr/xml8.py 2018-05-22 02:02:43 +0000
4@@ -187,17 +187,15 @@
5 # them.
6 decode_utf8 = cache_utf8.decode
7 revision_id = rev.revision_id
8- if isinstance(revision_id, str):
9- revision_id = decode_utf8(revision_id)
10 format_num = self.format_num
11 if self.revision_format_num is not None:
12 format_num = self.revision_format_num
13 root = Element('revision',
14 committer = rev.committer,
15 timestamp = '%.3f' % rev.timestamp,
16- revision_id = revision_id,
17+ revision_id = decode_utf8(revision_id),
18 inventory_sha1 = rev.inventory_sha1,
19- format=format_num,
20+ format=format_num.decode(),
21 )
22 if rev.timezone is not None:
23 root.set('timezone', str(rev.timezone))
24@@ -212,9 +210,7 @@
25 _mod_revision.check_not_reserved_id(parent_id)
26 p = SubElement(pelts, 'revision_ref')
27 p.tail = '\n'
28- if isinstance(parent_id, str):
29- parent_id = decode_utf8(parent_id)
30- p.set('revision_id', parent_id)
31+ p.set('revision_id', decode_utf8(parent_id))
32 if rev.properties:
33 self._pack_revision_properties(rev, root)
34 return root
35@@ -248,7 +244,7 @@
36 if self.revision_format_num is not None:
37 format_num = self.revision_format_num
38 if format is not None:
39- if format != format_num:
40+ if format.encode() != format_num:
41 raise BzrError("invalid format version %r on revision"
42 % format)
43 get_cached = get_utf8_or_ascii
44
45=== modified file 'breezy/bzr/xml_serializer.py'
46--- breezy/bzr/xml_serializer.py 2018-02-18 15:21:06 +0000
47+++ breezy/bzr/xml_serializer.py 2018-05-22 02:02:43 +0000
48@@ -100,7 +100,7 @@
49 self._write_element(self._pack_revision(rev), f)
50
51 def write_revision_to_string(self, rev):
52- return tostring(self._pack_revision(rev)) + '\n'
53+ return tostring(self._pack_revision(rev)) + b'\n'
54
55 def read_revision(self, f):
56 return self._unpack_revision(self._read_element(f))
57@@ -110,7 +110,7 @@
58
59 def _write_element(self, elt, f):
60 ElementTree(elt).write(f, 'utf-8')
61- f.write('\n')
62+ f.write(b'\n')
63
64 def _read_element(self, f):
65 return ElementTree().parse(f)
66@@ -195,9 +195,9 @@
67 decode back into Unicode, and then use the XML escape code.
68 """
69 try:
70- return _map[match.group()]
71+ return _map[match.group().decode('ascii', 'replace')].encode()
72 except KeyError:
73- return ''.join('&#%d;' % ord(uni_chr)
74+ return b''.join(b'&#%d;' % ord(uni_chr)
75 for uni_chr in match.group().decode('utf8'))
76
77
78@@ -209,7 +209,7 @@
79 # to check if None, rather than try/KeyError
80 text = _map.get(unicode_or_utf8_str)
81 if text is None:
82- if unicode_or_utf8_str.__class__ is text_type:
83+ if isinstance(unicode_or_utf8_str, text_type):
84 # The alternative policy is to do a regular UTF8 encoding
85 # and then escape only XML meta characters.
86 # Performance is equivalent once you use cache_utf8. *However*
87@@ -217,7 +217,7 @@
88 # of bzr. So no net gain. (Perhaps the read code would handle utf8
89 # better than entity escapes, but cElementTree seems to do just fine
90 # either way)
91- text = bytes(_unicode_re.sub(_unicode_escape_replace, unicode_or_utf8_str)) + b'"'
92+ text = _unicode_re.sub(_unicode_escape_replace, unicode_or_utf8_str).encode() + b'"'
93 else:
94 # Plain strings are considered to already be in utf-8 so we do a
95 # slightly different method for escaping.
96@@ -312,11 +312,11 @@
97 parent_id)
98 ie.symlink_target = elt_get('symlink_target')
99 elif kind == 'tree-reference':
100- file_id = elt.attrib['file_id']
101+ file_id = get_utf8_or_ascii(elt.attrib['file_id'])
102 name = elt.attrib['name']
103- parent_id = elt.attrib['parent_id']
104- revision = elt.get('revision')
105- reference_revision = elt.get('reference_revision')
106+ parent_id = get_utf8_or_ascii(elt.attrib['parent_id'])
107+ revision = get_utf8_or_ascii(elt.get('revision'))
108+ reference_revision = get_utf8_or_ascii(elt.get('reference_revision'))
109 ie = inventory.TreeReference(file_id, name, parent_id, revision,
110 reference_revision)
111 else:
112@@ -346,7 +346,7 @@
113 if elt.tag != 'inventory':
114 raise errors.UnexpectedInventoryFormat('Root tag is %r' % elt.tag)
115 format = elt.get('format')
116- if format != format_num:
117+ if format.encode() != format_num:
118 raise errors.UnexpectedInventoryFormat('Invalid format version %r'
119 % format)
120 revision_id = elt.get('revision_id')
121@@ -387,7 +387,7 @@
122 b'text_sha1="%s" text_size="%d" />\n' % (
123 executable, encode_and_escape(ie.file_id),
124 encode_and_escape(ie.name), parent_str, parent_id,
125- encode_and_escape(ie.revision), ie.text_sha1,
126+ encode_and_escape(ie.revision), ie.text_sha1.encode(),
127 ie.text_size))
128 else:
129 append(b'<file%s file_id="%s name="%s%s%s />\n' % (
130
131=== modified file 'breezy/tests/test_xml.py'
132--- breezy/tests/test_xml.py 2018-03-25 11:39:36 +0000
133+++ breezy/tests/test_xml.py 2018-05-22 02:02:43 +0000
134@@ -27,6 +27,7 @@
135 )
136 from ..sixish import (
137 BytesIO,
138+ text_type,
139 )
140 from ..bzr.inventory import Inventory
141 from . import TestCase
142@@ -208,7 +209,7 @@
143 eq(len(rev.parent_ids), 1)
144 eq(rev.timezone, 36000)
145 eq(rev.parent_ids[0],
146- "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
147+ b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
148
149 def test_unpack_revision_5_utc(self):
150 inp = BytesIO(_revision_v5_utc)
151@@ -219,7 +220,7 @@
152 eq(len(rev.parent_ids), 1)
153 eq(rev.timezone, 0)
154 eq(rev.parent_ids[0],
155- "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
156+ b"mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
157
158 def test_unpack_inventory_5(self):
159 """Unpack canned new-style inventory"""
160@@ -227,9 +228,9 @@
161 inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
162 eq = self.assertEqual
163 eq(len(inv), 4)
164- ie = inv.get_entry('bar-20050824000535-6bc48cfad47ed134')
165+ ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
166 eq(ie.kind, 'file')
167- eq(ie.revision, 'mbp@foo-00')
168+ eq(ie.revision, b'mbp@foo-00')
169 eq(ie.name, 'bar')
170 eq(inv.get_entry(ie.parent_id).kind, 'directory')
171
172@@ -239,17 +240,17 @@
173 inv = breezy.bzr.xml5.serializer_v5.read_inventory(inp)
174 eq = self.assertEqual
175 eq(len(inv), 4)
176- eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
177- ie = inv.get_entry('bar-20050824000535-6bc48cfad47ed134')
178+ eq(inv.revision_id, b'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
179+ ie = inv.get_entry(b'bar-20050824000535-6bc48cfad47ed134')
180 eq(ie.kind, 'file')
181- eq(ie.revision, 'mbp@foo-00')
182+ eq(ie.revision, b'mbp@foo-00')
183 eq(ie.name, 'bar')
184 eq(inv.get_entry(ie.parent_id).kind, 'directory')
185
186 def test_unpack_inventory_5a(self):
187 inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_string(
188- _inventory_v5a, revision_id='test-rev-id')
189- self.assertEqual('test-rev-id', inv.root.revision)
190+ _inventory_v5a, revision_id=b'test-rev-id')
191+ self.assertEqual(b'test-rev-id', inv.root.revision)
192
193 def test_unpack_inventory_5a_cache_and_copy(self):
194 # Passing an entry_cache should get populated with the objects
195@@ -257,7 +258,7 @@
196 # False
197 entry_cache = fifo_cache.FIFOCache()
198 inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_string(
199- _inventory_v5a, revision_id='test-rev-id',
200+ _inventory_v5a, revision_id=b'test-rev-id',
201 entry_cache=entry_cache, return_from_cache=False)
202 for entry in inv.iter_just_entries():
203 key = (entry.file_id, entry.revision)
204@@ -273,7 +274,7 @@
205 # True
206 entry_cache = fifo_cache.FIFOCache()
207 inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_string(
208- _inventory_v5a, revision_id='test-rev-id',
209+ _inventory_v5a, revision_id=b'test-rev-id',
210 entry_cache=entry_cache, return_from_cache=True)
211 for entry in inv.iter_just_entries():
212 key = (entry.file_id, entry.revision)
213@@ -285,8 +286,8 @@
214
215 def test_unpack_inventory_5b(self):
216 inv = breezy.bzr.xml5.serializer_v5.read_inventory_from_string(
217- _inventory_v5b, revision_id='test-rev-id')
218- self.assertEqual('a-rev-id', inv.root.revision)
219+ _inventory_v5b, revision_id=b'test-rev-id')
220+ self.assertEqual(b'a-rev-id', inv.root.revision)
221
222 def test_repack_inventory_5(self):
223 inp = BytesIO(_committed_inv_v5)
224@@ -336,7 +337,7 @@
225 outp = BytesIO()
226 breezy.bzr.xml5.serializer_v5.write_revision(rev, outp)
227 outfile_contents = outp.getvalue()
228- self.assertEqual(outfile_contents[-1], '\n')
229+ self.assertEqual(outfile_contents[-1:], b'\n')
230 self.assertEqualDiff(outfile_contents, breezy.bzr.xml5.serializer_v5.write_revision_to_string(rev))
231 self.assertEqualDiff(outfile_contents, _expected_rev_v5)
232
233@@ -459,13 +460,13 @@
234 """Parsed revision_ids should all be utf-8 strings, not unicode."""
235 s_v5 = breezy.bzr.xml5.serializer_v5
236 rev = s_v5.read_revision_from_string(_revision_utf8_v5)
237- self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
238- self.assertIsInstance(rev.revision_id, str)
239- self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
240+ self.assertEqual(b'erik@b\xc3\xa5gfors-02', rev.revision_id)
241+ self.assertIsInstance(rev.revision_id, bytes)
242+ self.assertEqual([b'erik@b\xc3\xa5gfors-01'], rev.parent_ids)
243 for parent_id in rev.parent_ids:
244- self.assertIsInstance(parent_id, str)
245+ self.assertIsInstance(parent_id, bytes)
246 self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
247- self.assertIsInstance(rev.message, unicode)
248+ self.assertIsInstance(rev.message, text_type)
249
250 # ie.revision should either be None or a utf-8 revision id
251 inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
252@@ -481,21 +482,21 @@
253 (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
254 ]
255 self.assertEqual(rev_id_2, inv.revision_id)
256- self.assertIsInstance(inv.revision_id, str)
257+ self.assertIsInstance(inv.revision_id, bytes)
258
259 actual = list(inv.iter_entries_by_dir())
260 for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
261 (act_path, act_ie)) in zip(expected, actual):
262 self.assertEqual(exp_path, act_path)
263- self.assertIsInstance(act_path, unicode)
264+ self.assertIsInstance(act_path, text_type)
265 self.assertEqual(exp_file_id, act_ie.file_id)
266- self.assertIsInstance(act_ie.file_id, str)
267+ self.assertIsInstance(act_ie.file_id, bytes)
268 self.assertEqual(exp_parent_id, act_ie.parent_id)
269 if exp_parent_id is not None:
270- self.assertIsInstance(act_ie.parent_id, str)
271+ self.assertIsInstance(act_ie.parent_id, bytes)
272 self.assertEqual(exp_rev_id, act_ie.revision)
273 if exp_rev_id is not None:
274- self.assertIsInstance(act_ie.revision, str)
275+ self.assertIsInstance(act_ie.revision, bytes)
276
277 self.assertEqual(len(expected), len(actual))
278
279@@ -514,24 +515,24 @@
280 # are being used in xml attributes, and by returning it now, we have to
281 # do fewer string operations later.
282 val = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
283- self.assertEqual('foo bar"', val)
284+ self.assertEqual(b'foo bar"', val)
285 # The second time should be cached
286 val2 = breezy.bzr.xml_serializer.encode_and_escape('foo bar')
287 self.assertIs(val2, val)
288
289 def test_ascii_with_xml(self):
290- self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
291+ self.assertEqual(b'&amp;&apos;&quot;&lt;&gt;"',
292 breezy.bzr.xml_serializer.encode_and_escape('&\'"<>'))
293
294 def test_utf8_with_xml(self):
295 # u'\xb5\xe5&\u062c'
296- utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
297- self.assertEqual('&#181;&#229;&amp;&#1580;"',
298+ utf8_str = b'\xc2\xb5\xc3\xa5&\xd8\xac'
299+ self.assertEqual(b'&#181;&#229;&amp;&#1580;"',
300 breezy.bzr.xml_serializer.encode_and_escape(utf8_str))
301
302 def test_unicode(self):
303 uni_str = u'\xb5\xe5&\u062c'
304- self.assertEqual('&#181;&#229;&amp;&#1580;"',
305+ self.assertEqual(b'&#181;&#229;&amp;&#1580;"',
306 breezy.bzr.xml_serializer.encode_and_escape(uni_str))
307
308
309
310=== modified file 'python3.passing'
311--- python3.passing 2018-05-19 14:54:22 +0000
312+++ python3.passing 2018-05-22 02:02:43 +0000
313@@ -56,11 +56,35 @@
314 breezy.plugins.upload.tests.test_upload.TestBranchUploadLocations.test_set_push_location(BranchReferenceFormat)
315 breezy.plugins.upload.tests.test_upload.TestBranchUploadLocations.test_set_push_location(BzrBranchFormat7)
316 breezy.plugins.weave_fmt.test_bzrdir.TestBranchFormat4.test_no_metadir_support
317+breezy.plugins.weave_fmt.test_bzrdir.TestBranchFormat4.test_supports_bzrdir_6
318+breezy.plugins.weave_fmt.test_bzrdir.TestFormat5.test_can_convert
319+breezy.plugins.weave_fmt.test_bzrdir.TestFormat5.test_needs_conversion
320+breezy.plugins.weave_fmt.test_bzrdir.TestFormat5.test_same_lockfiles_between_tree_repo_branch
321+breezy.plugins.weave_fmt.test_bzrdir.TestFormat6.test_can_convert
322+breezy.plugins.weave_fmt.test_bzrdir.TestFormat6.test_needs_conversion
323+breezy.plugins.weave_fmt.test_bzrdir.TestFormat6.test_same_lockfiles_between_tree_repo_branch
324+breezy.plugins.weave_fmt.test_bzrdir.V4WeaveBundleTester.test_crlf_bundle
325+breezy.plugins.weave_fmt.test_bzrdir.V4WeaveBundleTester.test_malformed
326+breezy.plugins.weave_fmt.test_bzrdir.V4WeaveBundleTester.test_non_bundle
327+breezy.plugins.weave_fmt.test_repository.TestFormat6.test_attribute__fetch_order
328+breezy.plugins.weave_fmt.test_repository.TestFormat6.test_attribute__fetch_reconcile
329+breezy.plugins.weave_fmt.test_repository.TestFormat6.test_attribute__fetch_uses_deltas
330+breezy.plugins.weave_fmt.test_repository.TestFormat6.test_no_ancestry_weave
331+breezy.plugins.weave_fmt.test_repository.TestFormat6.test_supports_external_lookups
332+breezy.plugins.weave_fmt.test_repository.TestFormat7.test_attribute__fetch_order
333+breezy.plugins.weave_fmt.test_repository.TestFormat7.test_attribute__fetch_reconcile
334+breezy.plugins.weave_fmt.test_repository.TestFormat7.test_attribute__fetch_uses_deltas
335+breezy.plugins.weave_fmt.test_repository.TestFormat7.test_creates_lockdir
336+breezy.plugins.weave_fmt.test_repository.TestFormat7.test_supports_external_lookups
337+breezy.plugins.weave_fmt.test_repository.TestFormat7.test_uses_lockdir
338 breezy.plugins.weave_fmt.test_repository.TestInterWeaveRepo.test_is_compatible_and_registered
339+breezy.plugins.weave_fmt.test_repository.TestSerializer.test_canned_inventory
340 breezy.plugins.weave_fmt.test_repository.TestSerializer.test_registry
341+breezy.plugins.weave_fmt.test_repository.TestSerializer.test_unpack_revision
342 breezy.plugins.weave_fmt.test_store.TestCompressedTextStore.test_multiple_add
343 breezy.plugins.weave_fmt.test_store.TestCompressedTextStore.test_total_size
344 breezy.plugins.weave_fmt.test_store.TestInstrumentedTransportStore.test__add_records
345+breezy.plugins.weave_fmt.test_store.TestMemoryStore.test_add_and_retrieve
346 breezy.plugins.weave_fmt.test_store.TestMemoryStore.test_adding_fails_when_present
347 breezy.plugins.weave_fmt.test_store.TestMemoryStore.test_missing_is_absent
348 breezy.plugins.weave_fmt.test_store.TestMemoryStore.test_total_size
349@@ -79,6 +103,8 @@
350 breezy.plugins.weave_fmt.test_store.TestTransportStore.test__relpath_simple
351 breezy.plugins.weave_fmt.test_store.TestTransportStore.test__relpath_simple_suffixed
352 breezy.plugins.weave_fmt.test_store.TestTransportStore.test__relpath_unregister_suffixes
353+breezy.plugins.weave_fmt.test_store.TestVersionFileStore.test_get_weave_readonly_cant_write
354+breezy.plugins.weave_fmt.test_store.TestVersionFileStore.test_get_weave_registers_dirty_in_write
355 breezy.pyutils.get_named_object
356 breezy.symbol_versioning.deprecated_in
357 breezy.tests.commands.test_checkout.TestCheckout.test_checkout
358@@ -10513,6 +10539,7 @@
359 breezy.tests.test_ui.TestUIFactoryTests.test_test_ui_factory_progress
360 breezy.tests.test_ui.UITests.test_progress_construction
361 breezy.tests.test_ui.UITests.test_text_ui_non_terminal
362+breezy.tests.test_upgrade.TestUpgrade.test_convert_knit_dirstate_content
363 breezy.tests.test_upstream_import.TestImport.test_common_directory
364 breezy.tests.test_upstream_import.TestImport.test_get_archive_type
365 breezy.tests.test_upstream_import.TestImport.test_top_path
366@@ -10670,7 +10697,34 @@
367 breezy.tests.test_wsgi.TestWSGI.test_relpath_setter
368 breezy.tests.test_wsgi.TestWSGI.test_relpath_setter_bad_path_prefix
369 breezy.tests.test_wsgi.TestWSGI.test_relpath_setter_bad_path_suffix
370+breezy.tests.test_xml.TestEncodeAndEscape.test_ascii_with_xml
371+breezy.tests.test_xml.TestEncodeAndEscape.test_simple_ascii
372+breezy.tests.test_xml.TestEncodeAndEscape.test_unicode
373+breezy.tests.test_xml.TestEncodeAndEscape.test_utf8_with_xml
374 breezy.tests.test_xml.TestMisc.test_unescape_xml
375+breezy.tests.test_xml.TestSerializer.test_empty_property_value
376+breezy.tests.test_xml.TestSerializer.test_inventory_text_v8
377+breezy.tests.test_xml.TestSerializer.test_pack_revision_5
378+breezy.tests.test_xml.TestSerializer.test_repack_inventory_5
379+breezy.tests.test_xml.TestSerializer.test_repack_revision_5
380+breezy.tests.test_xml.TestSerializer.test_repack_revision_5_utc
381+breezy.tests.test_xml.TestSerializer.test_revision_ids_are_utf8
382+breezy.tests.test_xml.TestSerializer.test_revision_text_v6
383+breezy.tests.test_xml.TestSerializer.test_revision_text_v7
384+breezy.tests.test_xml.TestSerializer.test_revision_text_v8
385+breezy.tests.test_xml.TestSerializer.test_roundtrip_inventory_v6
386+breezy.tests.test_xml.TestSerializer.test_roundtrip_inventory_v7
387+breezy.tests.test_xml.TestSerializer.test_roundtrip_inventory_v8
388+breezy.tests.test_xml.TestSerializer.tests_serialize_inventory_v5_with_root
389+breezy.tests.test_xml.TestSerializer.test_tree_reference
390+breezy.tests.test_xml.TestSerializer.test_unpack_basis_inventory_5
391+breezy.tests.test_xml.TestSerializer.test_unpack_inventory_5
392+breezy.tests.test_xml.TestSerializer.test_unpack_inventory_5a
393+breezy.tests.test_xml.TestSerializer.test_unpack_inventory_5a_cache_and_copy
394+breezy.tests.test_xml.TestSerializer.test_unpack_inventory_5a_cache_no_copy
395+breezy.tests.test_xml.TestSerializer.test_unpack_inventory_5b
396+breezy.tests.test_xml.TestSerializer.test_unpack_revision_5
397+breezy.tests.test_xml.TestSerializer.test_unpack_revision_5_utc
398 breezy.tests.test_xml.TestSerializer.test_wrong_format_v7
399 breezy.timestamp.format_highres_date
400 breezy.transport.http.unhtml_roughly

Subscribers

People subscribed via source and target branches