]> granicus.if.org Git - file/commitdiff
update for python-3.1 from Kelly Anderson.
authorChristos Zoulas <christos@zoulas.com>
Wed, 15 Dec 2010 02:49:24 +0000 (02:49 +0000)
committerChristos Zoulas <christos@zoulas.com>
Wed, 15 Dec 2010 02:49:24 +0000 (02:49 +0000)
python/example.py
python/py_magic.c
python/setup.py

index fc41a86d7ed7bad82bc85f5204dd88e502708892..d78f15a8fcc6281a162a88b8157dd152014e2136 100644 (file)
@@ -1,16 +1,17 @@
+#! /usr/bin/python
+
 import magic
 
 ms = magic.open(magic.MAGIC_NONE)
 ms.load()
-type =  ms.file("/path/to/some/file")
-print type
+tp =  ms.file("/bin/ls")
+print (tp)
 
-f = file("/path/to/some/file", "r")
+f = open("/bin/ls", "r")
 buffer = f.read(4096)
 f.close()
 
-type = ms.buffer(buffer)
-print type
+tp = ms.buffer(buffer)
+print (tp)
 
 ms.close()
-
index 10a1b3807767752e19f34d0ff05ba7c0184264b3..e8c463f9ebb7fcbce7edf0e3439b774c5fbeac1b 100644 (file)
@@ -14,7 +14,7 @@
       documentation and/or other materials provided with the distribution.
    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.
-    
+
    THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    SUCH DAMAGE.
 */
-
 #include <Python.h>
 #include <magic.h>
-#include "py_magic.h"
 
-/* Exceptions raised by this module */
+typedef struct {
+       PyObject_HEAD
+       magic_t cookie;
+} magic_cookie_hnd;
+
+static int py_magic_init(PyObject *self, PyObject *args, PyObject *kwds);
+static PyObject *py_magic_open(PyObject *self, PyObject *args);
+static PyObject *py_magic_close(PyObject *self, PyObject *args);
+static PyObject *py_magic_error(PyObject *self, PyObject *args);
+static PyObject *py_magic_file(PyObject *self, PyObject *args);
+static PyObject *py_magic_buffer(PyObject *self, PyObject *args);
+static PyObject *py_magic_setflags(PyObject *self, PyObject *args);
+static PyObject *py_magic_check(PyObject *self, PyObject *args);
+static PyObject *py_magic_compile(PyObject *self, PyObject *args);
+static PyObject *py_magic_load(PyObject *self, PyObject *args);
+static PyObject *py_magic_errno(PyObject *self, PyObject *args);
+
+// The meta-type for PyQt classes.
+static PyTypeObject magic_cookie_type = {
+       PyVarObject_HEAD_INIT(0,0)
+       "Magic cookie",
+       sizeof (magic_cookie_hnd), /* tp_basicsize */
+       0,                      /* tp_itemsize */
+       py_magic_dealloc,       /* tp_dealloc */
+       0,                      /* tp_print */
+       0,                      /* tp_getattr */
+       0,                      /* tp_setattr */
+       0,                      /* tp_compare */
+       0,                      /* tp_repr */
+       0,                      /* tp_as_number */
+       0,                      /* tp_as_sequence */
+       0,                      /* tp_as_mapping */
+       0,                      /* tp_hash */
+       0,                      /* tp_call */
+       0,                      /* tp_str */
+       PyObject_GenericGetAttr,/* tp_getattro */
+       0,                      /* tp_setattro */
+       0,                      /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+       "Magic objects",        /* tp_doc */
+       0,                      /* tp_traverse */
+       0,                      /* tp_clear */
+       0,                      /* tp_richcompare */
+       0,                      /* tp_weaklistoffset */
+       0,                      /* tp_iter */
+       0,                      /* tp_iternext */
+       magic_cookie_hnd_methods, /* tp_methods */
+       /*0,                       tp_methods */
+       0,                      /* tp_members */
+       0,                      /* tp_getset */
+       0,                      /* tp_base */
+       0,                      /* tp_dict */
+       0,                      /* tp_descr_get */
+       0,                      /* tp_descr_set */
+       0,                      /* tp_dictoffset */
+       py_magic_init,          /* tp_init */
+       0,                      /* tp_alloc */
+       0,                      /* tp_new */
+       0,                      /* tp_free */
+       0,                      /* tp_is_gc */
+       0,                      /* tp_bases */
+       0,                      /* tp_mro */
+       0,                      /* tp_cache */
+       0,                      /* tp_subclasses */
+       0,                      /* tp_weaklist */
+       0,                      /* tp_del */
+#if PY_VERSION_HEX >= 0x02070000
+       0,                      /* tp_version_tag */
+#endif
+};
 
-PyObject* magic_error_obj;
+/* Documentation */
+static char _magic_close__doc__[] =
+"Closes the magic database and deallocates any resources used.\n";
+static char _magic_open__doc__[] =
+"Returns a magic cookie on success and None on failure.\n";
+static char _magic_error__doc__[] =
+"Returns a textual explanation of the last error or None \
+ if there was no error.\n";
+static char _magic_errno__doc__[] =
+"Returns a numeric error code. If return value is 0, an internal \
+ magic error occurred. If return value is non-zero, the value is \
+ an OS error code. Use the errno module or os.strerror() can be used \
+ to provide detailed error information.\n";
+static char _magic_file__doc__[] =
+"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.\n";
+static char _magic_buffer__doc__[] =
+"Returns a textual description of the contents of the argument passed \
+ 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.\n";
+static char _magic_setflags__doc__[] =
+"Set flags on the cookie object.\n \
+ Returns -1 on systems that don't support utime(2) or utimes(2) \
+ when MAGIC_PRESERVE_ATIME is set.\n";
+static char _magic_check__doc__[] =
+"Check the validity of entries in the colon separated list of \
+ database files passed as argument or the default database file \
+ if no argument.\n Returns 0 on success and -1 on failure.\n";
+static char _magic_compile__doc__[] =
+"Compile entries in the colon separated list of database files \
+ passed as argument or the default database file if no argument.\n \
+ Returns 0 on success and -1 on failure.\n \
+ The compiled files created are named from the basename(1) of each file \
+ argument with \".mgc\" appended to it.\n";
+static char _magic_load__doc__[] =
+"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 \
+ any magic queries can be performed.\n \
+ Returns 0 on success and -1 on failure.\n";
 
-/* Create a new magic_cookie_hnd object */
-PyObject* new_magic_cookie_handle(magic_t cookie)
-{
-    magic_cookie_hnd* mch;
+/* object methods */
 
-    mch = PyObject_New(magic_cookie_hnd, &magic_cookie_type);
+static PyMethodDef magic_cookie_hnd_methods[] = {
+       { "close",    py_magic_close,    METH_NOARGS,  _magic_close__doc__    },
+       { "error",    py_magic_error,    METH_NOARGS,  _magic_error__doc__    },
+       { "file",     py_magic_file,     METH_VARARGS, _magic_file__doc__     },
+       { "buffer",   py_magic_buffer,   METH_VARARGS, _magic_buffer__doc__   },
+       { "setflags", py_magic_setflags, METH_VARARGS, _magic_setflags__doc__ },
+       { "check",    py_magic_check,    METH_VARARGS, _magic_check__doc__    },
+       { "compile",  py_magic_compile,  METH_VARARGS, _magic_compile__doc__  },
+       { "load",     py_magic_load,     METH_VARARGS, _magic_load__doc__     },
+       { "errno",    py_magic_errno,    METH_NOARGS,  _magic_errno__doc__    },
+       { NULL,       NULL,              0,            NULL                   }
+};
 
-    mch->cookie = cookie;
+/* module level methods */
 
-    return (PyObject*)mch;
-}
+static PyMethodDef magic_methods[] = {
+       { "open",     py_magic_open,     METH_VARARGS, _magic_open__doc__     },
+       { NULL,       NULL,              0,            NULL                   }
+};
 
-static char _magic_open__doc__[] =
-"Returns a magic cookie on success and None on failure.\n";
-static PyObject* py_magic_open(PyObject* self, PyObject* args)
-{
-    int flags = 0;
-    magic_t cookie;
+/* Initialize constants */
 
-    if(!PyArg_ParseTuple(args, "i", &flags))
-        return NULL;
+static struct const_vals {
+           const char *name;
+           unsigned int value;
+} module_const_vals[] = {
+       { "MAGIC_NONE", MAGIC_NONE },
+       { "MAGIC_DEBUG", MAGIC_DEBUG },
+       { "MAGIC_SYMLINK", MAGIC_SYMLINK },
+       { "MAGIC_COMPRESS", MAGIC_COMPRESS },
+       { "MAGIC_DEVICES", MAGIC_DEVICES },
+       { "MAGIC_MIME_TYPE", MAGIC_MIME_TYPE },
+       { "MAGIC_CONTINUE", MAGIC_CONTINUE },
+       { "MAGIC_CHECK", MAGIC_CHECK },
+       { "MAGIC_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME },
+       { "MAGIC_RAW", MAGIC_RAW},
+       { "MAGIC_ERROR", MAGIC_ERROR},
+       { "MAGIC_MIME_ENCODING", MAGIC_MIME_ENCODING },
+       { "MAGIC_MIME", MAGIC_MIME },
+       { "MAGIC_APPLE", MAGIC_APPLE },
+       { "MAGIC_NO_CHECK_COMPRESS", MAGIC_NO_CHECK_COMPRESS },
+       { "MAGIC_NO_CHECK_TAR", MAGIC_NO_CHECK_TAR },
+       { "MAGIC_NO_CHECK_SOFT", MAGIC_NO_CHECK_SOFT },
+       { "MAGIC_NO_CHECK_APPTYPE", MAGIC_NO_CHECK_APPTYPE },
+       { "MAGIC_NO_CHECK_ELF", MAGIC_NO_CHECK_ELF },
+       { "MAGIC_NO_CHECK_TEXT", MAGIC_NO_CHECK_TEXT },
+       { "MAGIC_NO_CHECK_CDF", MAGIC_NO_CHECK_CDF },
+       { "MAGIC_NO_CHECK_TOKENS", MAGIC_NO_CHECK_TOKENS },
+       { "MAGIC_NO_CHECK_ENCODING", MAGIC_NO_CHECK_ENCODING },
+       { NULL, 0 }
+};
 
-    if(!(cookie = magic_open(flags))) {
-        PyErr_SetString(magic_error_obj, "failure initializing magic cookie");
-        return NULL;
-    }
+/* Exceptions raised by this module */
 
-    return new_magic_cookie_handle(cookie);
-}
+static PyObject *magic_error_obj;
 
-static char _magic_close__doc__[] =
-"Closes the magic database and deallocates any resources used.\n";
-static PyObject* py_magic_close(PyObject* self, PyObject* args)
+/* Create a new magic_cookie_hnd object */
+PyObject *
+new_magic_cookie_handle(magic_t cookie)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-
-    magic_close(hnd->cookie);
-
-    Py_INCREF(Py_None);
-    return Py_None;
+       magic_cookie_hnd *mch = PyObject_New(magic_cookie_hnd,
+           &magic_cookie_type);
+       mch->cookie = cookie;
+       return (PyObject *)mch;
 }
 
-static char _magic_error__doc__[] =
-"Returns a textual explanation of the last error or None \
- if there was no error.\n";
-static PyObject* py_magic_error(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_open(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    const char* message = NULL;
-    PyObject* result = Py_None;
+       int flags;
+       magic_t cookie;
 
-    message = magic_error(hnd->cookie);
+       if(!PyArg_ParseTuple(args, "i", &flags))
+               return NULL;
 
-    if(message != NULL)
-        result = PyString_FromString(message);
-    else
-        Py_INCREF(Py_None);
+       if((cookie = magic_open(flags)) == NULL) {
+               PyErr_SetString(magic_error_obj,
+                   "failure initializing magic cookie");
+               return NULL;
+       }
 
-    return result;
+       return new_magic_cookie_handle(cookie);
 }
 
-static char _magic_errno__doc__[] =
-"Returns a numeric error code. If return value is 0, an internal \
- magic error occurred. If return value is non-zero, the value is \
- an OS error code. Use the errno module or os.strerror() can be used \
- to provide detailed error information.\n";
-static PyObject* py_magic_errno(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_close(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    return PyInt_FromLong(magic_errno(hnd->cookie));
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       magic_close(hnd->cookie);
+
+       Py_INCREF(Py_None);
+       return Py_None;
 }
 
-static char _magic_file__doc__[] =
-"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.\n";
-static PyObject* py_magic_file(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_error(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    char* filename = NULL;
-    const char* message = NULL;
-    PyObject* result = Py_None;
-
-    if(!(PyArg_ParseTuple(args, "s", &filename)))
-        return NULL;
-
-    message = magic_file(hnd->cookie, filename);
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       const char *message = magic_error(hnd->cookie);
 
-    if(message != NULL)
-        result = PyString_FromString(message);
-    else
-        Py_INCREF(Py_None);
+       if (message)
+               return PyUnicode_FromString(message);
 
-    return result;
+       Py_INCREF(Py_None);
+       return Py_None;
 }
 
-static char _magic_buffer__doc__[] =
-"Returns a textual description of the contents of the argument passed \
- 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.\n";
-static PyObject* py_magic_buffer(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_errno(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    void* buffer = NULL;
-    int buffer_length = 0;
-    const char* message = NULL;
-    PyObject* result = Py_None;
+       magic_cookie_hnd *hnd = (magic_cookie_hnd*)self;
+       return PyLong_FromLong(magic_errno(hnd->cookie));
+}
 
-    if(!(PyArg_ParseTuple(args, "s#", (char**)&buffer, &buffer_length)))
-        return NULL;
+static PyObject *
+py_magic_file(PyObject *self, PyObject *args)
+{
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       char *filename;
+       const char *msg;
 
-    message = magic_buffer(hnd->cookie, buffer, buffer_length);
+       if (!PyArg_ParseTuple(args, "s", &filename))
+               return NULL;
 
-    if(message != NULL)
-        result = PyString_FromString(message);
-    else
-        Py_INCREF(Py_None);
+       if ((msg = magic_file(hnd->cookie, filename)) != NULL)
+               return PyUnicode_FromString(msg);
 
-    return result;
+       Py_INCREF(Py_None);
+       return Py_None;
 }
 
-static char _magic_setflags__doc__[] =
-"Set flags on the cookie object.\n \
- Returns -1 on systems that don't support utime(2) or utimes(2) \
- when MAGIC_PRESERVE_ATIME is set.\n";
-static PyObject* py_magic_setflags(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_buffer(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    int flags;
-    int result;
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       void *buf;
+       int buflen;
+       const char *msg;
 
-    if(!(PyArg_ParseTuple(args, "i", &flags)))
-        return NULL;
+       if (!PyArg_ParseTuple(args, "s#", (char **)&buf, &buflen))
+               return NULL;
 
-    result = magic_setflags(hnd->cookie, flags);
+       if ((msg = magic_buffer(hnd->cookie, buf, buflen)) != NULL)
+               return PyUnicode_FromString(msg);
 
-    return PyInt_FromLong(result);
+       Py_INCREF(Py_None);
+       return Py_None;
 }
 
-static char _magic_check__doc__[] =
-"Check the validity of entries in the colon separated list of \
- database files passed as argument or the default database file \
- if no argument.\n Returns 0 on success and -1 on failure.\n";
-static PyObject* py_magic_check(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_setflags(PyObject* self, PyObject* args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    char* filename = NULL;
-    int result;
-
-    if(!(PyArg_ParseTuple(args, "|s", &filename)))
-        return NULL;
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       int flags;
 
-    result = magic_check(hnd->cookie, filename);
+       if (!PyArg_ParseTuple(args, "i", &flags))
+               return 0;
 
-    return PyInt_FromLong(result);
+       return PyLong_FromLong(magic_setflags(hnd->cookie, flags));
 }
 
-static char _magic_compile__doc__[] =
-"Compile entries in the colon separated list of database files \
- passed as argument or the default database file if no argument.\n \
- Returns 0 on success and -1 on failure.\n \
- The compiled files created are named from the basename(1) of each file \
- argument with \".mgc\" appended to it.\n";
-static PyObject* py_magic_compile(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_check(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    char* filename = NULL;
-    int result;
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       char *filename;
 
-    if(!(PyArg_ParseTuple(args, "|s", &filename)))
-        return NULL;
+       if (!PyArg_ParseTuple(args, "|s", &filename))
+               return NULL;
 
-    result = magic_compile(hnd->cookie, filename);
-
-    return PyInt_FromLong(result);
+       return PyLong_FromLong(magic_check(hnd->cookie, filename));
 }
 
-static char _magic_load__doc__[] =
-"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 \
- any magic queries can be performed.\n \
- Returns 0 on success and -1 on failure.\n";
-static PyObject* py_magic_load(PyObject* self, PyObject* args)
+static PyObject *
+py_magic_compile(PyObject *self, PyObject *args)
 {
-    magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
-    char* filename = NULL;
-    int result;
-
-    if(!(PyArg_ParseTuple(args, "|s", &filename)))
-        return NULL;
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       char *filename;
 
-    result = magic_load(hnd->cookie, filename);
+       if (!PyArg_ParseTuple(args, "|s", &filename))
+               return NULL;
 
-    return PyInt_FromLong(result);
+       return PyLong_FromLong(magic_compile(hnd->cookie, filename));
 }
 
-/* object methods */
-
-static PyMethodDef magic_cookie_hnd_methods[] = {
-    { "close", (PyCFunction)py_magic_close,
-      METH_NOARGS, _magic_close__doc__ },
-    { "error", (PyCFunction)py_magic_error,
-      METH_NOARGS, _magic_error__doc__ },
-    { "file", (PyCFunction)py_magic_file,
-      METH_VARARGS, _magic_file__doc__ },
-    { "buffer", (PyCFunction)py_magic_buffer,
-      METH_VARARGS, _magic_buffer__doc__ },
-    { "setflags", (PyCFunction)py_magic_setflags,
-      METH_VARARGS, _magic_setflags__doc__ },
-    { "check", (PyCFunction)py_magic_check,
-      METH_VARARGS, _magic_check__doc__ },
-    { "compile", (PyCFunction)py_magic_compile,
-      METH_VARARGS, _magic_compile__doc__ },
-    { "load", (PyCFunction)py_magic_load,
-      METH_VARARGS, _magic_load__doc__ },
-    { "errno", (PyCFunction)py_magic_errno,
-      METH_NOARGS, _magic_errno__doc__ },
-    { NULL, NULL }
-};
-
-/* module level methods */
+static PyObject *
+py_magic_load(PyObject *self, PyObject *args)
+{
+       magic_cookie_hnd *hnd = (magic_cookie_hnd *)self;
+       char *filename;
 
-static PyMethodDef magic_methods[] = {
-    { "open", (PyCFunction)py_magic_open,
-      METH_VARARGS, _magic_open__doc__ },
-    { NULL, NULL }
-};
+       if (!PyArg_ParseTuple(args, "|s", &filename))
+               return NULL;
 
-static void py_magic_dealloc(PyObject* self)
-{
-    PyObject_Del(self);
+       return PyLong_FromLong(magic_load(hnd->cookie, filename));
 }
 
-static PyObject* py_magic_getattr(PyObject* self, char* attrname)
+static void
+py_magic_dealloc(PyObject *self)
 {
-    return Py_FindMethod(magic_cookie_hnd_methods, self, attrname);
+       PyObject_Del(self);
 }
 
-PyTypeObject magic_cookie_type = {
-    PyObject_HEAD_INIT(NULL)
-    0,
-    "Magic cookie",
-    sizeof(magic_cookie_hnd),
-    0,
-    py_magic_dealloc, /* tp_dealloc */
-    0,                /* tp_print */
-    py_magic_getattr, /* tp_getattr */
-    0,                /* tp_setattr */
-    0,                /* tp_compare */
-    0,                /* tp_repr */
-    0,                /* tp_as_number */
-    0,                /* tp_as_sequence */
-    0,                /* tp_as_mapping */
-    0,                /* tp_hash */
-};
 
-/* Initialize constants */
 
-static struct const_vals {
-    const char* const name;
-    unsigned int value;
-} module_const_vals[] = {
-    { "MAGIC_NONE", MAGIC_NONE },
-    { "MAGIC_DEBUG", MAGIC_DEBUG },
-    { "MAGIC_SYMLINK", MAGIC_SYMLINK },
-    { "MAGIC_COMPRESS", MAGIC_COMPRESS },
-    { "MAGIC_DEVICES", MAGIC_DEVICES },
-    { "MAGIC_MIME_TYPE", MAGIC_MIME_TYPE },
-    { "MAGIC_CONTINUE", MAGIC_CONTINUE },
-    { "MAGIC_CHECK", MAGIC_CHECK },
-    { "MAGIC_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME },
-    { "MAGIC_RAW", MAGIC_RAW},
-    { "MAGIC_ERROR", MAGIC_ERROR},
-    { "MAGIC_MIME_ENCODING", MAGIC_MIME_ENCODING },
-    { "MAGIC_MIME", MAGIC_MIME },
-    { "MAGIC_APPLE", MAGIC_APPLE },
-    { "MAGIC_NO_CHECK_COMPRESS", MAGIC_NO_CHECK_COMPRESS },
-    { "MAGIC_NO_CHECK_TAR", MAGIC_NO_CHECK_TAR },
-    { "MAGIC_NO_CHECK_SOFT", MAGIC_NO_CHECK_SOFT },
-    { "MAGIC_NO_CHECK_APPTYPE", MAGIC_NO_CHECK_APPTYPE },
-    { "MAGIC_NO_CHECK_ELF", MAGIC_NO_CHECK_ELF },
-    { "MAGIC_NO_CHECK_TEXT", MAGIC_NO_CHECK_TEXT },
-    { "MAGIC_NO_CHECK_CDF", MAGIC_NO_CHECK_CDF },
-    { "MAGIC_NO_CHECK_TOKENS", MAGIC_NO_CHECK_TOKENS },
-    { "MAGIC_NO_CHECK_ENCODING", MAGIC_NO_CHECK_ENCODING },
-    { NULL }
-};
 
-static void const_init(PyObject* dict)
+static int
+py_magic_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
-    struct const_vals* tmp;
-    PyObject *obj;
-
-    for(tmp = module_const_vals; tmp->name; ++tmp) {
-        obj = PyInt_FromLong(tmp->value);
-        PyDict_SetItemString(dict, tmp->name, obj);
-        Py_DECREF(obj);
-    }
-}
+       int rc;
 
-/*
- * Module initialization
- */
+       magic_cookie_type.tp_new = PyType_GenericNew;
+       rc = PyType_Ready(&magic_cookie_type);
 
-void initmagic(void)
-{
-    PyObject* module;
-    PyObject* dict;
+       if (rc < 0) {
+               Py_FatalError("magic: Failed to initialize magic_cookie type");
+               return rc;
+       }
 
-    /* Initialize module */
+       Py_INCREF(&magic_cookie_type);
 
-    module = Py_InitModule("magic", magic_methods);
-    dict = PyModule_GetDict(module);
+       return 0;
+}
 
-    magic_error_obj = PyErr_NewException("magic.error", NULL, NULL);
-    PyDict_SetItemString(dict, "error", magic_error_obj);
+static void
+const_init(PyObject *dict)
+{
+       struct const_vals *v = module_const_vals;
 
-    magic_cookie_type.ob_type = &PyType_Type;
+       for (; v->name; ++v) {
+               PyObject *obj = PyLong_FromLong(v->value);
+               PyDict_SetItemString(dict, v->name, obj);
+               Py_DECREF(obj);
+       }
+}
 
-    /* Initialize constants */
+/*
+ * Module initialization
+ */
 
-    const_init(dict);
+#ifndef PyMODINIT_FUNC
+#define PyMODINIT_FUNC void
+#endif
 
-    if(PyErr_Occurred())
-        Py_FatalError("can't initialize module magic");
+PyMODINIT_FUNC
+#if PY_MAJOR_VERSION >= 3
+PyInit_magic(void)
+#else
+initmagic(void)
+#endif
+{
+#if PY_MAJOR_VERSION >= 3
+       PyModuleDef modDef = {
+               PyModuleDef_HEAD_INIT,
+               "magic",                /* name */
+               "File magic module",    /* module doc */
+               -1,                     /* size of per-interpreter state of mod
+                                        * or -1 if the module keeps stat in
+                                        * global vars
+                                        */
+               magic_methods
+       };
+
+       PyObject *module = PyModule_Create(&modDef);
+       if (module == NULL)
+           Py_FatalError("module error");
+       PyObject *dict = PyModule_GetDict(module);
+       if (dict == NULL)
+           Py_FatalError("dict error");
+
+       magic_error_obj = PyErr_NewException("magic.error", 0, 0);
+       PyDict_SetItemString(dict, "error", magic_error_obj);
+
+       /* Initialize constants */
+
+       const_init(dict);
+
+       if (PyErr_Occurred())
+               Py_FatalError("can't initialize module magic");
+
+       return module;
+#else
+       PyObject *module = Py_InitModule4("magic", magic_methods,
+           "File magic module", (PyObject*)0, PYTHON_API_VERSION);
+       PyObject *dict = PyModule_GetDict(module);
+
+       magic_error_obj = PyErr_NewException("magic.error", 0, 0);
+       PyDict_SetItemString(dict, "error", magic_error_obj);
+
+       /* Initialize constants */
+
+       const_init(dict);
+
+       if (PyErr_Occurred())
+               Py_FatalError("can't initialize module magic");
+#endif
 }
index 7c86194844171bb83e4d82676b2393f29b28d4c2..83776c79062d826d1bd77ad21484796f84e997a6 100644 (file)
@@ -1,10 +1,17 @@
 # Python distutils build script for magic extension
+import sys, os
 from distutils.core import setup, Extension
 
+p = sys.prefix + '/lib64'
+if os.path.exists(sys.prefix + '/lib64'):
+    libdir = sys.prefix + '/lib64'
+elif os.path.exists(sys.prefix + '/lib'):
+    libdir = sys.prefix + '/lib'
+
 magic_module = Extension('magic',
     libraries = ['magic'],
-    library_dirs = ['./','../','../src','/usr/lib/'],
-    include_dirs = ['./','../','../src','/usr/include/'],
+    library_dirs = ['./', '../', '../src', libdir],
+    include_dirs = ['./', '../', '../src', sys.prefix + '/include' ],
     sources = ['py_magic.c'])
 
 setup (name = 'Magic file extensions',