ソースを参照

Cherry-pick upstream commit FILE5_30-37-g8a942980 "Retain python 2 compatibility". Closes: #902796

Christoph Biedl 6 年 前
コミット
e6f9e8dfce

+ 136 - 0
debian/patches/cherry-pick.FILE5_30-37-g8a942980.retain-python-2-compatibility-factoring-out-the-conversion-functions.patch

@@ -0,0 +1,136 @@
+Subject: Retain python 2 compatibility, factoring out the conversion functions
+Origin: FILE5_30-37-g8a942980 <https://github.com/file/file/commit/FILE5_30-37-g8a942980>
+Upstream-Author: Christos Zoulas <christos@zoulas.com>
+Date: Tue Apr 4 20:48:40 2017 +0000
+Bug-Debian: https://bugs.debian.org/902796
+
+--- a/python/magic.py
++++ b/python/magic.py
+@@ -117,30 +117,43 @@
+         """
+         _close(self._magic_t)
+ 
++    @staticmethod
++    def __tostr(s):
++        if s is None:
++            return None
++        if isinstance(s, str):
++            return s
++        try:  # keep Python 2 compatibility
++            return str(s, 'utf-8')
++        except TypeError:
++            return str(s)
++
++    @staticmethod
++    def __tobytes(b):
++        if b is None:
++            return None
++        if isinstance(b, bytes):
++            return b
++        try:  # keep Python 2 compatibility
++            return bytes(b, 'utf-8')
++        except TypeError:
++            return bytes(b)
++
+     def file(self, filename):
+         """
+         Returns a textual description of the contents of the argument passed
+         as a filename or None if an error occurred and the MAGIC_ERROR flag
+-        is set.  A call to errno() will return the numeric error code.
++        is set. A call to errno() will return the numeric error code.
+         """
+-        if isinstance(filename, bytes):
+-            bi = filename
+-        else:
+-            try:  # keep Python 2 compatibility
+-                bi = bytes(filename, 'utf-8')
+-            except TypeError:
+-                bi = bytes(filename)
+-        r = _file(self._magic_t, bi)
+-        if isinstance(r, str):
+-            return r
+-        else:
+-            return str(r, 'utf-8')
++        return Magic.__tostr(_file(self._magic_t, Magic.__tobytes(filename)))
+ 
+     def descriptor(self, fd):
+         """
+-        Like the file method, but the argument is a file descriptor.
++        Returns a textual description of the contents of the argument passed
++        as a file descriptor or None if an error occurred and the MAGIC_ERROR
++        flag is set. A call to errno() will return the numeric error code.
+         """
+-        return _descriptor(self._magic_t, fd)
++        return Magic.__tostr(_descriptor(self._magic_t, fd))
+ 
+     def buffer(self, buf):
+         """
+@@ -148,22 +161,14 @@
+         as a buffer or None if an error occurred and the MAGIC_ERROR flag
+         is set. A call to errno() will return the numeric error code.
+         """
+-        r = _buffer(self._magic_t, buf, len(buf))
+-        if isinstance(r, str):
+-            return r
+-        else:
+-            return str(r, 'utf-8')
++        return Magic.__tostr(_buffer(self._magic_t, buf, len(buf)))
+ 
+     def error(self):
+         """
+         Returns a textual explanation of the last error or None
+         if there was no error.
+         """
+-        e = _error(self._magic_t)
+-        if isinstance(e, str):
+-            return e
+-        else:
+-            return str(e, 'utf-8')
++        return Magic.__tostr(_error(self._magic_t))
+ 
+     def setflags(self, flags):
+         """
+@@ -184,35 +189,38 @@
+ 
+         Returns 0 on success and -1 on failure.
+         """
+-        return _load(self._magic_t, filename)
++        return _load(self._magic_t, Magic.__tobytes(filename))
+ 
+     def compile(self, dbs):
+         """
+         Compile entries in the colon separated list of database files
+         passed as argument or the default database file if no argument.
+-        Returns 0 on success and -1 on failure.
+         The compiled files created are named from the basename(1) of each file
+         argument with ".mgc" appended to it.
++
++        Returns 0 on success and -1 on failure.
+         """
+-        return _compile(self._magic_t, dbs)
++        return _compile(self._magic_t, Magic.__tobytes(dbs))
+ 
+     def check(self, dbs):
+         """
+         Check the validity of entries in the colon separated list of
+         database files passed as argument or the default database file
+         if no argument.
++
+         Returns 0 on success and -1 on failure.
+         """
+-        return _check(self._magic_t, dbs)
++        return _check(self._magic_t, Magic.__tobytes(dbs))
+ 
+     def list(self, dbs):
+         """
+         Check the validity of entries in the colon separated list of
+         database files passed as argument or the default database file
+         if no argument.
++
+         Returns 0 on success and -1 on failure.
+         """
+-        return _list(self._magic_t, dbs)
++        return _list(self._magic_t, Magic.__tobytes(dbs))
+ 
+     def errno(self):
+         """

+ 1 - 0
debian/patches/series

@@ -14,6 +14,7 @@ cherry-pick.FILE5_30-28-g393555f2.try-to-clean-this-up-the-vector-code-is-still-
 cherry-pick.FILE5_30-29-g76c2d4ae.several-fixes-in-cdf-parser.patch
 cherry-pick.FILE5_30-30-gc703aa9f.free-memory-on-error.patch
 cherry-pick.FILE5_30-34-g22067c96.simplify-the-property-info-copy-function-and-check-for-bounds.patch
+cherry-pick.FILE5_30-37-g8a942980.retain-python-2-compatibility-factoring-out-the-conversion-functions.patch
 cherry-pick.FILE5_30-38-gfd42e119.if-we-could-not-read-a-field-set-it-to-0-found-by-oss-fuzz.patch
 cherry-pick.FILE5_30-39-geb973428.limit-memory-usage-more-to-satisfy-oss-fuzz.patch
 cherry-pick.FILE5_30-41-g393dafa4.work-around-glibc-regex-msan-bug-regexec-returns-0-but-does-initialize-pmatch.patch