[apparmor] [PATCH]: raise exception in python bindings when something fails

Steve Beattie steve at nxnw.org
Fri Mar 7 19:52:17 UTC 2014


Hey Arkadiusz,

On Fri, Mar 07, 2014 at 01:02:32PM +0100, Arkadiusz Miśkiewicz wrote:
> Current libapparmor python bindings are very "unpythonic". Also lack
> ability to access "why" information in case of failure.
> 
> In python when something fail the normal behaviour is exception
> to occur. In case of apparmor functions die silently and require
> user to verify returned value.
> 
> And here comes second problem. In C api when return value is -1
> (and the same value is returned in python API) we can access errno
> to get information why this occured. Unfortunately in python there
> is no way to access the same information.  Pythonic way of accessing
> errno is via exception (which is never raised in python bindings currently).
> 
> So the patch adds exceptions on failures. First %exception creates
> a wrapper that swig adds to each function listed below. Empty %exception
> causes that the rest of code (beside listed functions) won't be wrapped.
> 
> How this works? Example on apparmor disabled system:
> 
> Before:
> >>> LibAppArmor.aa_change_hat(hat, random.randint(1, sys.maxint))
> -1
> 
> After:
> >>> LibAppArmor.aa_change_hat(hat, random.randint(1, sys.maxint))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> OSError: [Errno 22] Invalid argument
> 
> so pythonic way of accessing "why":
> >>> try:
> ...     LibAppArmor.aa_change_hat(hat, random.randint(1, sys.maxint))
> ... except OSError, e:
> ...     print e.errno
> ... 
> 22
> 
> Signed-off-by: Arkadiusz Miśkiewicz <arekm at maven.pl>

Very nice, applying to trunk. Acked-by: Steve Beattie <steve at nxnw.org>.

If anyone wants to dig into swig more, the handling of function
arguments around aa_getcon/aa_getpeercon and probably others could
use some love.

-- 
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: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20140307/fb06f24b/attachment.pgp>


More information about the AppArmor mailing list