Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Python Magic API change #157

Merged
merged 3 commits into from
Sep 29, 2016

Conversation

StevenMaude
Copy link
Contributor

@StevenMaude StevenMaude commented Sep 28, 2016

Here, python-magic changed the behaviour of from_buffer().

This breaks a new install of messytables on Python 3.4, shown by tests failing:

  File ".../messytables/messytables/any.py", line 66, in get_mime
    mimetype = magic.from_buffer(header, mime=True).decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

The alternative fix would be to pin python-magic at some version <0.4.12.

To ensure subsequent fixes will work.
Current release version of html5lib (1.0b10) needs a newer setuptools
version than the one Travis provides.

Error is: html5lib requires setuptools version 18.5 or above

Travis attempts to install html5lib as prompted by setup.py, which then
installs the current version, which has this problem.
@StevenMaude StevenMaude changed the title Fix python magic from buffer Fix for Python Magic API change Sep 28, 2016
@StevenMaude
Copy link
Contributor Author

StevenMaude commented Sep 28, 2016

This also fixes the Travis tests, which is a separate html5lib issue (a change which can be revoked when they update PyPI, I think the fix is already merged on their GitHub).

Can separate this out into another PR if you prefer, though it meant I could demonstrate tests passing on this PR.

@frabcus
Copy link
Contributor

frabcus commented Sep 28, 2016

I get lots of errors like this, in Python 2 or 3, when running nosetests on this branch.

ERROR: test.test_any.test_simple({'tableset': <class 'messytables.excel.XLSTableSet'>, 'filename': 'bian-anal-mca-2005-dols-eng-1011-0312-tab3.xlsm'},)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/francis/code/fiddle/messytables-steve/test/test_any.py", line 42, in check_no_filename
    table_set = any_tableset(fh)
  File "/home/francis/code/fiddle/messytables-steve/messytables/any.py", line 152, in any_tableset
    magic_mime = get_mime(fileobj)
  File "/home/francis/code/fiddle/messytables-steve/messytables/any.py", line 66, in get_mime
    mimetype = magic.from_buffer(header, mime=True)
AttributeError: 'module' object has no attribute 'from_buffer'

@StevenMaude
Copy link
Contributor Author

@frabcus Weird. That looks like any.py can't see magic's from_buffer() at all.

Do you get the same issue running nosetests on master locally, or just on my branch?

Also worth checking if

python -c "from __future__ import print_function; import magic; print(magic.from_buffer)"

gives you an error.

@StevenMaude
Copy link
Contributor Author

StevenMaude commented Sep 28, 2016

@frabcus: long shot, you don't by any chance have filemagic installed and it's shadowing python-magic? (pip freeze should tell you.)

It uses the same module name, magic. When I installed this in my virtualenv containing python-magic, I got the AttributeError as above. After uninstalling filemagic, accessing from_buffer worked again.

@StevenMaude
Copy link
Contributor Author

The output of python -c "from __future__ import print_function; import magic; print(dir(magic))" would also be useful in diagnosing this.

Looks like:

['MAGIC_CHECK', 'MAGIC_COMPRESS', 'MAGIC_CONTINUE', 'MAGIC_DEBUG', 'MAGIC_DEVICES', 'MAGIC_ERROR', 'MAGIC_MIME', 'MAGIC_MIME_ENCODING', 'MAGIC_NONE', 'MAGIC_NO_CHECK_APPTYPE', 'MAGIC_NO_CHECK_ASCII', 'MAGIC_NO_CHECK_COMPRESS', 'MAGIC_NO_CHECK_ELF', 'MAGIC_NO_CHECK_FORTRAN', 'MAGIC_NO_CHECK_SOFT', 'MAGIC_NO_CHECK_TAR', 'MAGIC_NO_CHECK_TOKENS', 'MAGIC_NO_CHECK_TROFF', 'MAGIC_PRESERVE_ATIME', 'MAGIC_RAW', 'MAGIC_SYMLINK', 'Magic', 'MagicException', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_get_magic_type', '_instances', '_magic_buffer', '_magic_file', '_magic_load', 'c_char_p', 'c_int', 'c_size_t', 'c_void_p', 'coerce_filename', 'ctypes', 'dll', 'errorcheck_negative_one', 'errorcheck_null', 'from_buffer', 'from_file', 'glob', 'libmagic', 'magic_buffer', 'magic_check', 'magic_close', 'magic_compile', 'magic_errno', 'magic_error', 'magic_file', 'magic_load', 'magic_open', 'magic_setflags', 'magic_t', 'maybe_decode', 'os', 'sys', 'threading']

for me with python-magic==0.4.12.

StevenMaude added a commit to sensiblecodeio/xypath that referenced this pull request Sep 29, 2016
Install older version of python-magic which is compatible with
messytables. messytables just installs the latest >=0.4.6 but the latest
version isn't compatible with messytables right now.

See okfn/messytables#157 for more details.

This can be removed when the PR is merged, and a new version of
messytables containing it is released on PyPI.
@frabcus
Copy link
Contributor

frabcus commented Sep 29, 2016

Turned out the problem was my global (installed via an Ubuntu package) version of nosetests getting found before the one in my virtualenv. Fine now and merging.

@frabcus frabcus merged commit 38673cc into okfn:master Sep 29, 2016
StevenMaude added a commit to sensiblecodeio/xypath that referenced this pull request Sep 29, 2016
Resolves issues with python-magic that prevented a fresh install from
working on Python 3.

See okfn/messytables#157
@StevenMaude StevenMaude deleted the fix-python-magic-from-buffer branch September 29, 2016 15:13
StevenMaude added a commit to sensiblecodeio/xypath that referenced this pull request Sep 29, 2016
Resolves issues with python-magic that prevented a fresh install from
working on Python 3.

See okfn/messytables#157
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants