Code

Opened 8 months ago

Closed 8 months 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

Attachments (0)

Change History (6)

comment:1 Changed 8 months ago by grue

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to grue
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 8 months ago by bmispelon

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 Changed 8 months ago by murphys.irish.stout@…

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 Changed 8 months ago by bmispelon

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 Changed 8 months ago by murphys.irish.stout@…

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 Changed 8 months ago by murphys.irish.stout@…

  • Resolution set to invalid
  • Status changed from assigned to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.