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

AttributeError: 'NoneType' object has no attribute 'throw_exception' #182

Closed
mexicarne opened this issue Jun 28, 2016 · 28 comments
Closed

Comments

@mexicarne
Copy link

Hello,

I'm trying to make python-iptables work on Ubuntu 16.04 x64. I did git clone from this repository, and built packages with dpkg-buildpackage using debian/ directory from repository. Then installed from created package. Getting this upon import iptc:

Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/iptc/__init__.py", line 10, in <module>
    from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target,
  File "/usr/lib/python2.7/dist-packages/iptc/ip4tc.py", line 13, in <module>
    from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
  File "/usr/lib/python2.7/dist-packages/iptc/xtables.py", line 709, in <module>
    _throw = _lib_xtwrapper.throw_exception
AttributeError: 'NoneType' object has no attribute 'throw_exception'
>>> 

It looks like it's because libxtwrapper installed as /usr/lib/python2.7/dist-packages/libxtwrapper.x86_64-linux-gnu.so, not as /usr/lib/python2.7/dist-packages/libxtwrapper.so

If I create symlink or just copy this file to libxtwrapper.so, then all works fine. Can I do something about this? I need to propagate the package to a few servers, and I don't want to create symlinks everywhere manually.

Thank you.

@mexicarne
Copy link
Author

Just for the reference, I saw issue #76 and tried to reboot and to build package with zip_safe = False in setup.py, but to not avail. Anything I was able to google doesn't resolves the issue for me.

@mexicarne
Copy link
Author

Also tried to build package with replacing

from setuptools import setup, Extension

with

from distutils.core import setup, Extension

but still getting the same error upon importing.

@mexicarne
Copy link
Author

Ok, I was able to find the problem. The problem is in utils.py._find_libraries(). It doesn't using standard module imp or something and hardcodes the library path like that:
[name, "lib" + name, name + ext, "lib" + name + ext]

So there is no way to work on ubuntu out of the box (w/o creating a symlink manually). May be it's better to switch to something more portable, like imp module:

>>> import imp
>>> imp.find_module('libxtwrapper')[1]
'libxtwrapper.x86_64-linux-gnu.so'

@mexicarne
Copy link
Author

mexicarne commented Jun 28, 2016

And here is a quick hack to make it working (for someone who may google this issue before the proper resolution found):

--- util.py.orig    2016-06-28 12:04:37.000000000 +0300
+++ util.py 2016-06-28 14:13:36.000000000 +0300
@@ -79,8 +79,9 @@
         ext = get_config_var("EXT_SUFFIX")
     else:
         ext = get_config_var('SO')
+    arch_ext = '.x86_64-linux-gnu'
     for name in names:
-        libnames = [name, "lib" + name, name + ext, "lib" + name + ext]
+        libnames = [name, "lib" + name, name + ext, "lib" + name + ext, "lib" + name + arch_ext + ext]
         libdir = os.environ.get('IPTABLES_LIBDIR', None)
         if libdir is not None:
             libdirs = libdir.split(':')

@ldx
Copy link
Owner

ldx commented Jun 28, 2016

@mexicarne interesting. Can we leverage imp.find_module() instead of the libnames array?

@mexicarne
Copy link
Author

I believe yes, but can't come with exact patch at the moment. I just throw the idea :). Anyway, it's not your fault that debian/ubuntu changing the installed filename, and this is why the problem is not reproducible via pip/easy_install. But it would be great to support this as an exception for debian/ubuntu userbase.

@lemsx1
Copy link

lemsx1 commented Aug 3, 2016

I just came across this issue running python 2.7.11 in RHEL 6.x (compiled my own).
Using the patch above (@mexicarne) did not work for me.

Downgrading to 0.10.0 worked. Only 0.11.0 has this issue.

@ldx
Copy link
Owner

ldx commented Aug 3, 2016

@lemsx1 can you post here the stack trace you get?

@lemsx1
Copy link

lemsx1 commented Aug 4, 2016

[root@server python-iptables-0.11.0]# python
Python 2.7.11 (default, Feb 26 2016, 13:57:20) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "iptc/__init__.py", line 10, in <module>
    from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target,
  File "iptc/ip4tc.py", line 13, in <module>
    from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
  File "iptc/xtables.py", line 709, in <module>
    _throw = _lib_xtwrapper.throw_exception

@ldx
Copy link
Owner

ldx commented Aug 4, 2016

@lemsx1 testing in a CentOS6.5, after manually installing Python 2.7.11 and pip installing python-iptables:

[vagrant@vagrant-centos65 ~]$ sudo pip install python-iptables
You are using pip version 7.1.0, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting python-iptables
  Using cached python-iptables-0.11.0.tar.gz
Installing collected packages: python-iptables
  Running setup.py install for python-iptables
Successfully installed python-iptables-0.11.0
[vagrant@vagrant-centos65 ~]$ sudo /usr/local/bin/python
Python 2.7.11 (default, Aug  4 2016, 23:32:54)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
>>>

@FedericoCeratto
Copy link
Contributor

Hi @ldx - I'm in the process of packaging python-iptables for Debian and I'm running into the same bug when the .deb package is installed. Anything I can do to help?

@ldx
Copy link
Owner

ldx commented Aug 15, 2016

@FedericoCeratto can you tell me more about your environment? OS, iptables version, python version, python-iptables version?

@teracow
Copy link

teracow commented Aug 27, 2016

Hi @ldx - I have the same issue as the OP.

My configuration:
Ubuntu Server v14.04.5 LTS
iptables v1.4.21
Python v2.7.6
python-iptables - cloned a few minutes ago...

danc@dlserver ~/python-iptables $ sudo python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "iptc/__init__.py", line 10, in <module>
    from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target,
  File "iptc/ip4tc.py", line 13, in <module>
    from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
  File "iptc/xtables.py", line 709, in <module>
    _throw = _lib_xtwrapper.throw_exception
AttributeError: 'NoneType' object has no attribute 'throw_exception'
>>> 

I'm happy to assist if you need anything further.

@ldx
Copy link
Owner

ldx commented Sep 13, 2016

@teracow can you try building and installing it? Python-iptables also include a C library that needs to be compiled.

$ python setup.py build && python setup.py install

should do the trick.

@teracow
Copy link

teracow commented Sep 13, 2016

Hi,

I think I got past this error by copying libxtwrapper.so into /usr/lib/python2.7/dist-packages/ but this was some time ago, and I'm not 100% that that's all I did.

I then switched projects and this kinda got dumped. If anyone had responded to my query at the time, I could have been more helpful.

Sorry.

@FedericoCeratto
Copy link
Contributor

Symlinking the shared obj to /usr/lib/python2.7/dist-packages/libxtwrapper.so helped but then the import fails like in #90 with:

  ...
  File "/usr/lib/python2.7/dist-packages/iptc/xtables.py", line 8, in <module>
    from . import version
ImportError: cannot import name version

@ldx
Copy link
Owner

ldx commented Sep 20, 2016

@FedericoCeratto instead of symlinking, can you give a shot to the setup.py commands in my comment above?

@mexicarne
Copy link
Author

Would you please show the output of this command? I'm asking those who have this 'throw_exception' error.

python -c "import imp; print(imp.find_module('libxtwrapper')[1])"

Please post the output and your operating system info: name, version, architecture.

@mexicarne
Copy link
Author

mexicarne commented Oct 7, 2016

I now think that it is not right to change find_library() or _find_library(), because these functions are looking for system libraries (libc etc), not only for libxtwrapper. So I come up with another solution to xtables.py that works for me and should be universal for any iptables-capable system.

diff --git a/xtables.py.orig b/xtables.py
index 2cfccbb..0cf2493 100644
--- a/xtables.py.orig
+++ b/xtables.py
@@ -2,6 +2,7 @@

 import ctypes as ct
 import os
+import imp
 import sys
 import weakref

@@ -704,7 +705,7 @@ if _xtables_libdir is None:
     raise XTablesError("can't find directory with extensions; "
                        "please set XTABLES_LIBDIR")

-_lib_xtwrapper, _ = find_library("xtwrapper")
+_lib_xtwrapper, _ = find_library(imp.find_module('libxtwrapper')[1])

 _throw = _lib_xtwrapper.throw_exception

Please let me know if it works for you. It works fine at Ubuntu 16.04 x64

@ldx
Copy link
Owner

ldx commented Oct 15, 2016

@mexicarne works here. Can you open a PR? Thanks!

@dadittoz
Copy link

Although the fix by @mexicarne seem to work on python 3.5 (though I don't have any problem even without this fix), it didn't solve the problem for 2.7:
Here's the log with this fix implemented.

# python
Python 2.7.12 (default, Jul  1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/iptc/__init__.py", line 10, in <module>
  File "build/bdist.linux-x86_64/egg/iptc/ip4tc.py", line 13, in <module>
  File "build/bdist.linux-x86_64/egg/iptc/xtables.py", line 708, in <module>
ImportError: No module named libxtwrapper

@mexicarne
Copy link
Author

@dadittoz please let us know what OS and which version you are using. How you installed iptc (package, pip) and what the full name of libxtwrapper.so in your system (should be somewhere in /usr/lib/python2.7/dist-packages). Thank you!

@dadittoz
Copy link

Okay, I got it figured out. If I install python-iptables by adding to install_requires in my own project it doesn't work. But if I remove it and then use pip2 install python-iptables, it works fine. This is relevant to Python 2.7 only. Python 3.5 installation works fine either from install_requires or pip3 install. I really don't understand what black magic happens here.

My OS is: Ubuntu 16.04.1 LTS x64

Let me know if you would like some install logs.

I guess you can easily reproduce it by using python2, creating setup.py with

install_requires=["python-iptables"]

and then running

python2 setup.py build
python2 setup.py install

Produced installation will render the error as described above.

@ldx
Copy link
Owner

ldx commented Jan 3, 2017

Has anyone else been able to reproduce this problem on 16.04?

@jllorente
Copy link
Collaborator

I came here because I ran into the same problems as OP with a fresh cloned repo, but then I realized I had not installed the dependencies. I usually run python3 so I didn't have the module installed for python2, ergo the dependency was missing.

I tried the following, to install my local version:

#pip2 install -e .
#python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
>>>

However, as @ldx indicated, I also tried buidling and installing with setup.py, but that didn't work:

#python2 setup.py build && python2 setup.py install
...
#python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "iptc/__init__.py", line 10, in <module>
    from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target,
  File "iptc/ip4tc.py", line 13, in <module>
    from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
  File "iptc/xtables.py", line 709, in <module>
    _throw = _lib_xtwrapper.throw_exception
AttributeError: 'NoneType' object has no attribute 'throw_exception'

By the way, installing via pip2 worked perfectly as well!

A bit of info of my system

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial

$ uname -ar
Linux ces1 4.4.0-57-generic #78-Ubuntu SMP Fri Dec 9 23:50:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

$ iptables --version
iptables v1.6.0

Let me know if I can help anyhow ;)

@ldx
Copy link
Owner

ldx commented Jan 9, 2017

Yeah, just checked this and I can confirm what @jllorente wrote above.

The missing step is:

# python2.7 setup.py build && python2.7 setup.py install && python2.7 setup.py install_lib

@ldx ldx closed this as completed Jan 9, 2017
@drzraf
Copy link

drzraf commented Feb 26, 2019

I think there is an underlying issue worth reopening because trying to pyinstall a project containing python-iptables fails the exact same way as OP:

$ pip3 install -U pyinstaller
$ echo import iptc >> foo.py
$ pyinstaller ./foo.py -F
$ ./dist/foo
Traceback (most recent call last):
  File "foo.py", line 1, in <module>
    import iptc
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/home/.local/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "iptc/__init__.py", line 10, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/home/.local/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "iptc/ip4tc.py", line 13, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/home/.local/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "iptc/xtables.py", line 819, in <module>
AttributeError: 'NoneType' object has no attribute 'throw_exception'
[5158] Failed to execute script foo

Same with cythonize and this happens because the dependency is not made clear.
Using nuitka, one would need to explicitly pass --include-module=libxtwrapper in order to actually include this module which would other be overlooked by the bundling process.

@mcccs
Copy link

mcccs commented Jul 7, 2019

Yeah, just checked this and I can confirm what @jllorente wrote above.

The missing step is:

# python2.7 setup.py build && python2.7 setup.py install && python2.7 setup.py install_lib

it works for me

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

No branches or pull requests

9 participants