Opened 11 years ago

Closed 11 years ago

#21059 closed Bug (invalid)

TypeError when trying to use python magic in Django shell

Reported by: murphys.irish.stout@… Owned by: grue
Component: Uncategorized Version: 1.5
Severity: Normal Keywords: magic django
Cc: murphys.irish.stout@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Running:
Python version 2.7.3
Django version 1.5.2
I have a django view that indirectly makes use of python's magic library (basically a ctypes wrapper with access to libmagic) which results in the following error

Traceback (most recent call last):

File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/magic.py", line 170, in load

return _load(self._magic_t, filename)

ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected CString instance instead of str

In isolation the error is repeatable using the following steps:

./manage.py shell

import magic
m = magic.open(magic.MAGIC_NONE)
m.load("/usr/share/file/magic.mgc")

Traceback (most recent call last):

File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/magic.py", line 170, in load

return _load(self._magic_t, filename)

ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected CString instance instead of str

Issuing the same commands from a standard python shell does not result in the same error

Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import magic
m=magic.open(magic.MAGIC_NONE)
m.load("/usr/share/file/magic.mgc")

0

Google searches for the extact typeerror have proved useless, I even attempted to modify magic.py and cast the filename with a ctypes.create_string_buffer() call (AFAIK there is no ctypes.CString type) but that still failed.

The same code works fine running
Python v. 2.7.3
Django v. 1.4.3

Thanks in advance

Change History (6)

comment:1 by grue, 11 years ago

Owner: changed from nobody to grue
Status: newassigned

comment:2 by Baptiste Mispelon, 11 years ago

Hi,

Where is the magic module coming from? Is it from python.magic[1]?

If so, which version are you running. The code you provided does not work with the latest release of python-magic.
Is the issue still present with the latest version?

Thanks.

[1] https://pypi.python.org/pypi/python-magic/0.4.3

comment:3 by murphys.irish.stout@…, 11 years ago

Yes, magic is python.magic.

I was previously using magic 0.2.0. After downloading and installing magic 0.4.3, using the new python magic syntax I had the following results:

in a standard python shell:

Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import magic
m = magic.Magic()
m

<magic.Magic instance at 0x7fed111067a0>

in the django ./manage.py shell:

Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

import magic
m = magic.Magic()

Traceback (most recent call last):

File "<console>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/magic.py", line 51, in init

magic_load(self.cookie, magic_file)

ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected CString instance instead of NoneType

and when specifying a magic file:

in a python shell:

import magic
m = magic.Magic("/usr/share/file/magic.mgc")
m

<magic.Magic instance at 0x7fec10ed57a0>

in the django ./manage.py shell

m = magic.Magic("/usr/share/file/magic.mgc")

Traceback (most recent call last):

File "<console>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/magic.py", line 51, in init

magic_load(self.cookie, magic_file)

ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected CString instance instead of NoneType

comment:4 by Baptiste Mispelon, 11 years ago

I can't reproduce the issue on my machine.

Can you try with manage.py shell --plain if the issue is still present?

comment:5 by murphys.irish.stout@…, 11 years ago

manage.py shell --plain produced the same error

however I did notice a difference in my initial error with magic 0.2.0 and the error reported in 0.4.3

ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected CString instance instead of str

as opposed to

ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected CString instance instead of NoneType

googling the error with NoneType turned up this bug report for the haystack module which is part of my INSTALLED_APPS

sudo pip uninstall haystack
sudo pip install django-haystack

fixed the problem - didn't look into it any further and from the bug report link above doesn't seem like the haystack developers are too concerned about it

sorry for the confusion thanks for the help, hopefully this report will at least help others in the future

comment:6 by murphys.irish.stout@…, 11 years ago

Resolution: invalid
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top