archrelease: copy trunk to extra-x86_64
[arch-packages.git] / itstool / trunk / 0001-Fix-the-crash-from-912099.patch
blob326b849ee125854450500d80c6a13f8a9532d432
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Tanguy Ortolo <tanguy+debian@ortolo.eu>
3 Date: Fri, 7 Dec 2018 00:00:00 +0000
4 Subject: [PATCH] Fix the crash from #912099
6 ITS Tool 2.0.4 crashes when building some documentation, as reported in #912099.
7 This comes from translations with invalid XML markup, which ITS Tool fails to
8 merge (which is not abnormal), and to report these issues, needlessly encodes
9 the original msgstr from unicode to bytes, causing it to be recoded using the
10 default ascii codec, which fails when the msgstr contains anything out of ascii.
12 This patch removes the useless decoding, avoiding the failing subsequent
13 recoding. It also explicitly encodes the output strings to be able to print them
14 in all cases, even when the output encoding cannot be detected.
16 Bug: https://github.com/itstool/itstool/issues/25
17 Bug-Debian: https://bugs.debian.org/912099
18 Forwarded: https://github.com/itstool/itstool/issues/25
19 ---
20 itstool.in | 21 +++++++++++++++++----
21 1 file changed, 17 insertions(+), 4 deletions(-)
23 diff --git a/itstool.in b/itstool.in
24 index c21ad4bfeb86..f34673581c88 100755
25 --- a/itstool.in
26 +++ b/itstool.in
27 @@ -44,9 +44,22 @@ if PY3:
28 else:
29 return str(s)
30 ustr_type = str
31 + def pr_str(s):
32 + """Return a string that can be safely print()ed"""
33 + # Since print works on both bytes and unicode, just return the argument
34 + return s
35 else:
36 string_types = basestring,
37 ustr = ustr_type = unicode
38 + def pr_str(s):
39 + """Return a string that can be safely print()ed"""
40 + if isinstance(s, str):
41 + # Since print works on str, just return the argument
42 + return s
43 + else:
44 + # print may not work on unicode if the output encoding cannot be
45 + # detected, so just encode with UTF-8
46 + return unicode.encode(s, 'utf-8')
48 NS_ITS = 'http://www.w3.org/2005/11/its'
49 NS_ITST = 'http://itstool.org/extensions/'
50 @@ -1077,36 +1090,36 @@ class Document (object):
51 if strict:
52 raise
53 else:
54 - sys.stderr.write('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
55 + sys.stderr.write(pr_str('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
56 (lang + ' ') if lang is not None else '',
57 - msgstr.encode('utf-8')))
58 + msgstr)))
59 self._xml_err = ''
60 return node
61 def scan_node(node):
62 children = [child for child in xml_child_iter(node)]
63 for child in children:
64 if child.type != 'element':
65 continue
66 if child.ns() is not None and child.ns().content == NS_BLANK:
67 ph_node = msg.get_placeholder(child.name).node
68 if self.has_child_elements(ph_node):
69 self.merge_translations(translations, None, ph_node, strict=strict)
70 newnode = ph_node.copyNode(1)
71 newnode.setTreeDoc(self._doc)
72 child.replaceNode(newnode)
73 else:
74 repl = self.get_translated(ph_node, translations, strict=strict, lang=lang)
75 child.replaceNode(repl)
76 scan_node(child)
77 try:
78 scan_node(trnode)
79 except:
80 if strict:
81 raise
82 else:
83 - sys.stderr.write('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
84 + sys.stderr.write(pr_str('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
85 (lang + ' ') if lang is not None else '',
86 - msgstr.encode('utf-8')))
87 + msgstr)))
88 self._xml_err = ''
89 ctxt.doc().freeDoc()
90 return node