[apparmor] [patch v2] fix python LibAppArmor import failures with swig > 3.0.8

Steve Beattie steve at nxnw.org
Tue Sep 20 18:45:23 UTC 2016


On Wed, Sep 14, 2016 at 11:32:06PM +0200, Christian Boltz wrote:
> Am Mittwoch, 14. September 2016, 12:41:33 CEST schrieb Steve Beattie:
> > Ugh, I *really* don't like the 'from LibAppArmor.LibAppArmor import *'
> > bit.
> 
> Do you have a better solution? ;-)
> 
> I asked about this on the swig-user mailinglist some weeks ago, and this 
> was the best solution I received. Actually it's the only solution I 
> received (besides some more general answers, documentation pointers 
> etc.). An interesting detail is that I received this solution off-list 
> for whatever reason.
> 
> My patch has a big advantage: it works ;-) (and it's already in 
> Tumbleweed and Leap 42.2 beta to un-break the aa-* tools)

Except for where it doesn't, which is python2.7, where the __init__.py
import fails.

I spent some time poking at swig 3.10 trying to get it to work some
other way, but the documentation seems to lie, as I couldn't make swig
generate python code that did a relative import; no amount of futzing
with -relativeimport or -py3 or -modern or setting the package name
in the libapparmor.i seemed to make any difference in the _LibAppArmor
import code generated by swig. So as far as I can see, a variant of the
import * solution will have to suffice, as follows:

---
 .bzrignore                                    |    2 +-
 libraries/libapparmor/swig/python/Makefile.am |    3 +--
 libraries/libapparmor/swig/python/__init__.py |    6 ++++++
 3 files changed, 8 insertions(+), 3 deletions(-)

Index: b/.bzrignore
===================================================================
--- a/.bzrignore
+++ b/.bzrignore
@@ -88,7 +88,7 @@ libraries/libapparmor/swig/perl/MYMETA.y
 libraries/libapparmor/swig/perl/blib
 libraries/libapparmor/swig/perl/libapparmor_wrap.c
 libraries/libapparmor/swig/perl/pm_to_blib
-libraries/libapparmor/swig/python/__init__.py
+libraries/libapparmor/swig/python/LibAppArmor.py
 libraries/libapparmor/swig/python/build/
 libraries/libapparmor/swig/python/libapparmor_wrap.c
 libraries/libapparmor/swig/python/Makefile
Index: b/libraries/libapparmor/swig/python/Makefile.am
===================================================================
--- a/libraries/libapparmor/swig/python/Makefile.am
+++ b/libraries/libapparmor/swig/python/Makefile.am
@@ -6,9 +6,8 @@ SUBDIRS = test
 
 libapparmor_wrap.c: $(srcdir)/../SWIG/libapparmor.i
 	$(SWIG) -python -I$(srcdir)/../../include -module LibAppArmor -o $@ $(srcdir)/../SWIG/libapparmor.i
-	mv LibAppArmor.py __init__.py
 
-MOSTLYCLEANFILES=libapparmor_wrap.c __init__.py
+MOSTLYCLEANFILES=libapparmor_wrap.c LibAppArmor.py
 
 all-local: libapparmor_wrap.c setup.py
 	if test ! -f libapparmor_wrap.c; then cp $(srcdir)/libapparmor_wrap.c . ; fi
Index: b/libraries/libapparmor/swig/python/__init__.py
===================================================================
--- /dev/null
+++ b/libraries/libapparmor/swig/python/__init__.py
@@ -0,0 +1,6 @@
+import sys
+
+if sys.version_info[0] > 3:
+    from LibAppArmor.LibAppArmor import *
+else:
+    from .LibAppArmor import *

Once we kill python2.7 support, then we can go with the "simple" form of
the __init__.py file.

> > make maintainer-clean, I believe (but it's only supported by the
> > libapparmor subdirectory).
> 
> # make maintainer-clean
> ...
> # bzr ignored | awkcol 1
> libraries/libapparmor/swig/python/Makefile.in          <----------
> 
> So it cleans up a lot, but not everything.
> 
> Needless to say that .../swig/python/Makefile.in contains code from 
> Makefile.am and needs to be updated ;-)

Changes made to Makefile.am will cause autotools to regenerate
Makefile.in.

-- 
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20160920/caf3b191/attachment.pgp>


More information about the AppArmor mailing list