itstool-2.0.5-fix-crash-wrong-encoding.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Description: Fix the crash from #912099
  2. ITS Tool 2.0.4 crashes when building some documentation, as reported in
  3. #912099. This comes from translations with invalid XML markup, which ITS Tool
  4. fails to merge (which is not abnormal), and to report these issues, needlessly
  5. encodes the original msgstr from unicode to bytes, causing it to be recoded
  6. using the default ascii codec, which fails when the msgstr contains anything
  7. out of ascii.
  8. .
  9. This patch removes the useless decoding, avoiding the failing subsequent
  10. recoding. It also explicitly encodes the output strings to be able to print
  11. them in all cases, even when the output encoding cannot be detected.
  12. Bug: https://github.com/itstool/itstool/issues/25
  13. Bug-Debian: https://bugs.debian.org/912099
  14. Forwarded: https://github.com/itstool/itstool/issues/25
  15. Author: Tanguy Ortolo <tanguy+debian@ortolo.eu>
  16. Last-Update: 2018-12-071
  17. Index: itstool/itstool.in
  18. ===================================================================
  19. --- itstool.orig/itstool.in 2018-12-10 18:31:23.762143539 +0100
  20. +++ itstool/itstool.in 2018-12-10 18:38:03.496777117 +0100
  21. @@ -44,9 +44,22 @@
  22. else:
  23. return str(s)
  24. ustr_type = str
  25. + def pr_str(s):
  26. + """Return a string that can be safely print()ed"""
  27. + # Since print works on both bytes and unicode, just return the argument
  28. + return s
  29. else:
  30. string_types = basestring,
  31. ustr = ustr_type = unicode
  32. + def pr_str(s):
  33. + """Return a string that can be safely print()ed"""
  34. + if isinstance(s, str):
  35. + # Since print works on str, just return the argument
  36. + return s
  37. + else:
  38. + # print may not work on unicode if the output encoding cannot be
  39. + # detected, so just encode with UTF-8
  40. + return unicode.encode(s, 'utf-8')
  41. NS_ITS = 'http://www.w3.org/2005/11/its'
  42. NS_ITST = 'http://itstool.org/extensions/'
  43. @@ -1060,9 +1073,9 @@
  44. if strict:
  45. raise
  46. else:
  47. - sys.stderr.write('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
  48. + sys.stderr.write(pr_str('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
  49. (lang + ' ') if lang is not None else '',
  50. - msgstr.encode('utf-8')))
  51. + msgstr)))
  52. self._xml_err = ''
  53. return node
  54. def scan_node(node):
  55. @@ -1087,9 +1100,9 @@
  56. if strict:
  57. raise
  58. else:
  59. - sys.stderr.write('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
  60. + sys.stderr.write(pr_str('Warning: Could not merge %stranslation for msgid:\n%s\n' % (
  61. (lang + ' ') if lang is not None else '',
  62. - msgstr.encode('utf-8')))
  63. + msgstr)))
  64. self._xml_err = ''
  65. ctxt.doc().freeDoc()
  66. return node