cherry-pick.FILE5_30-37-g8a942980.retain-python-2-compatibility-factoring-out-the-conversion-functions.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. Subject: Retain python 2 compatibility, factoring out the conversion functions
  2. Origin: FILE5_30-37-g8a942980 <https://github.com/file/file/commit/FILE5_30-37-g8a942980>
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Tue Apr 4 20:48:40 2017 +0000
  5. Bug-Debian: https://bugs.debian.org/902796
  6. --- a/python/magic.py
  7. +++ b/python/magic.py
  8. @@ -117,30 +117,43 @@
  9. """
  10. _close(self._magic_t)
  11. + @staticmethod
  12. + def __tostr(s):
  13. + if s is None:
  14. + return None
  15. + if isinstance(s, str):
  16. + return s
  17. + try: # keep Python 2 compatibility
  18. + return str(s, 'utf-8')
  19. + except TypeError:
  20. + return str(s)
  21. +
  22. + @staticmethod
  23. + def __tobytes(b):
  24. + if b is None:
  25. + return None
  26. + if isinstance(b, bytes):
  27. + return b
  28. + try: # keep Python 2 compatibility
  29. + return bytes(b, 'utf-8')
  30. + except TypeError:
  31. + return bytes(b)
  32. +
  33. def file(self, filename):
  34. """
  35. Returns a textual description of the contents of the argument passed
  36. as a filename or None if an error occurred and the MAGIC_ERROR flag
  37. - is set. A call to errno() will return the numeric error code.
  38. + is set. A call to errno() will return the numeric error code.
  39. """
  40. - if isinstance(filename, bytes):
  41. - bi = filename
  42. - else:
  43. - try: # keep Python 2 compatibility
  44. - bi = bytes(filename, 'utf-8')
  45. - except TypeError:
  46. - bi = bytes(filename)
  47. - r = _file(self._magic_t, bi)
  48. - if isinstance(r, str):
  49. - return r
  50. - else:
  51. - return str(r, 'utf-8')
  52. + return Magic.__tostr(_file(self._magic_t, Magic.__tobytes(filename)))
  53. def descriptor(self, fd):
  54. """
  55. - Like the file method, but the argument is a file descriptor.
  56. + Returns a textual description of the contents of the argument passed
  57. + as a file descriptor or None if an error occurred and the MAGIC_ERROR
  58. + flag is set. A call to errno() will return the numeric error code.
  59. """
  60. - return _descriptor(self._magic_t, fd)
  61. + return Magic.__tostr(_descriptor(self._magic_t, fd))
  62. def buffer(self, buf):
  63. """
  64. @@ -148,22 +161,14 @@
  65. as a buffer or None if an error occurred and the MAGIC_ERROR flag
  66. is set. A call to errno() will return the numeric error code.
  67. """
  68. - r = _buffer(self._magic_t, buf, len(buf))
  69. - if isinstance(r, str):
  70. - return r
  71. - else:
  72. - return str(r, 'utf-8')
  73. + return Magic.__tostr(_buffer(self._magic_t, buf, len(buf)))
  74. def error(self):
  75. """
  76. Returns a textual explanation of the last error or None
  77. if there was no error.
  78. """
  79. - e = _error(self._magic_t)
  80. - if isinstance(e, str):
  81. - return e
  82. - else:
  83. - return str(e, 'utf-8')
  84. + return Magic.__tostr(_error(self._magic_t))
  85. def setflags(self, flags):
  86. """
  87. @@ -184,35 +189,38 @@
  88. Returns 0 on success and -1 on failure.
  89. """
  90. - return _load(self._magic_t, filename)
  91. + return _load(self._magic_t, Magic.__tobytes(filename))
  92. def compile(self, dbs):
  93. """
  94. Compile entries in the colon separated list of database files
  95. passed as argument or the default database file if no argument.
  96. - Returns 0 on success and -1 on failure.
  97. The compiled files created are named from the basename(1) of each file
  98. argument with ".mgc" appended to it.
  99. +
  100. + Returns 0 on success and -1 on failure.
  101. """
  102. - return _compile(self._magic_t, dbs)
  103. + return _compile(self._magic_t, Magic.__tobytes(dbs))
  104. def check(self, dbs):
  105. """
  106. Check the validity of entries in the colon separated list of
  107. database files passed as argument or the default database file
  108. if no argument.
  109. +
  110. Returns 0 on success and -1 on failure.
  111. """
  112. - return _check(self._magic_t, dbs)
  113. + return _check(self._magic_t, Magic.__tobytes(dbs))
  114. def list(self, dbs):
  115. """
  116. Check the validity of entries in the colon separated list of
  117. database files passed as argument or the default database file
  118. if no argument.
  119. +
  120. Returns 0 on success and -1 on failure.
  121. """
  122. - return _list(self._magic_t, dbs)
  123. + return _list(self._magic_t, Magic.__tobytes(dbs))
  124. def errno(self):
  125. """