From 91f2c030b57793b996353681b04ecf2f1763ff76 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Thu, 22 Sep 2011 16:58:37 +0000 Subject: [PATCH] add python3 changes. --- ChangeLog | 4 ++++ python/magic.py | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index a87d70c0..28f87aab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2011-09-22 12:57 Christos Zoulas + + * Python3 binding fixes from Kelly Anderson + 2011-09-20 11:32 Christos Zoulas * If a string type magic entry is marked as text or binary diff --git a/python/magic.py b/python/magic.py index 41a42ee5..bd42f565 100644 --- a/python/magic.py +++ b/python/magic.py @@ -109,13 +109,17 @@ class Magic(object): """ _close(self._magic_t) - def file(self, file): + 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. """ - return _file(self._magic_t, file) + try: # attempt python3 approach first + bi = bytes(filename, 'utf-8') + return str(_file(self._magic_t, bi), 'utf-8') + except: + return _file(self._magic_t, filename) def descriptor(self, fd): """ @@ -129,14 +133,20 @@ class Magic(object): 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. """ - return _buffer(self._magic_t, buf, len(buf)) + try: # attempt python3 approach first + return str(_buffer(self._magic_t, buf, len(buf)), 'utf-8') + except: + return _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. """ - return _error(self._magic_t) + try: # attempt python3 approach first + return str(_error(self._magic_t), 'utf-8') + except: + return _error(self._magic_t) def setflags(self, flags): """ @@ -149,7 +159,7 @@ class Magic(object): """ return _setflags(self._magic_t, flags) - def load(self, file=None): + def load(self, filename=None): """ Must be called to load entries in the colon separated list of database files passed as argument or the default database file if no argument before @@ -157,7 +167,7 @@ class Magic(object): Returns 0 on success and -1 on failure. """ - return _load(self._magic_t, file) + return _load(self._magic_t, filename) def compile(self, dbs): """ -- 2.49.0