Skip to contentSkip to conversation

sepolgen: Decode output from Popen in Python3

PATCH Applied Robert Kuska 2 participants 2 messages Aug 5, 2015 1 message not in the archive SELinux security module

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 sepolgen: Decode output from Popen in Python3

In reply to a message not in the archive

In Python3 output from Popen communicate function returns bytes, to handle output as a string it is needed to properly decode it.

---
 sepolgen/src/sepolgen/audit.py |  7 +++++++
 sepolgen/src/sepolgen/util.py  | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
sepolgen/src/sepolgen/audit.py +7 -0
index 83efac3..724d3ea 100644
@@ -22,6 +22,7 @@ import sys
 
 from . import refpolicy
 from . import access
+from . import util
 # Convenience functions
 
 def get_audit_boot_msgs():
@@ -42,6 +43,8 @@ def get_audit_boot_msgs():
     boottime = time.strftime("%X", s)
     output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR", "-ts", bootdate, boottime],
                               stdout=subprocess.PIPE).communicate()[0]
+    if util.PY3:
+        output = util.decode_input(output)
     return output
 
 def get_audit_msgs():
@@ -55,6 +58,8 @@ def get_audit_msgs():
     import subprocess
     output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR"],
                               stdout=subprocess.PIPE).communicate()[0]
+    if util.PY3:
+        output = util.decode_input(output)
     return output
 
 def get_dmesg_msgs():
@@ -66,6 +71,8 @@ def get_dmesg_msgs():
     import subprocess
     output = subprocess.Popen(["/bin/dmesg"],
                               stdout=subprocess.PIPE).communicate()[0]
+    if util.PY3:
+        output = util.decode_input(output)
     return output
 
 # Classes representing audit messages
sepolgen/src/sepolgen/util.py +14 -0
index 4934bec..16e7ca2 100644
@@ -103,6 +103,20 @@ def encode_input(text):
         encoded_text = text.encode('utf-8')
     return encoded_text
 
+def decode_input(text):
+    import locale
+    """Decode given text via preferred system encoding"""
+    # locale will often find out the correct encoding
+    encoding = locale.getpreferredencoding()
+    try:
+        decoded_text = text.decode(encoding)
+    except UnicodeError:
+    # if it fails to find correct encoding then ascii is used
+    # which may lead to UnicodeError if `text` contains non ascii signs
+    # utf-8 is our guess to fix the situation
+        decoded_text = text.decode('utf-8')
+    return decoded_text
+
 class Comparison():
     """Class used when implementing rich comparison.
 
Signature · 1 line
2.4.3
#2 lore raw
Quoted 5 lines · Robert Kuska

In Python3 output from Popen communicate function returns bytes, to handle output as a string it is needed to properly decode it.

Thanks, applied.

Quoted 70 lines
---
 sepolgen/src/sepolgen/audit.py |  7 +++++++
 sepolgen/src/sepolgen/util.py  | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
sepolgen/src/sepolgen/audit.py +7 -0
index 83efac3..724d3ea 100644
@@ -22,6 +22,7 @@ import sys
 
 from . import refpolicy
 from . import access
+from . import util
 # Convenience functions
 
 def get_audit_boot_msgs():
@@ -42,6 +43,8 @@ def get_audit_boot_msgs():
     boottime = time.strftime("%X", s)
     output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR", "-ts", bootdate, boottime],
                               stdout=subprocess.PIPE).communicate()[0]
+    if util.PY3:
+        output = util.decode_input(output)
     return output
 
 def get_audit_msgs():
@@ -55,6 +58,8 @@ def get_audit_msgs():
     import subprocess
     output = subprocess.Popen(["/sbin/ausearch", "-m", "AVC,USER_AVC,MAC_POLICY_LOAD,DAEMON_START,SELINUX_ERR"],
                               stdout=subprocess.PIPE).communicate()[0]
+    if util.PY3:
+        output = util.decode_input(output)
     return output
 
 def get_dmesg_msgs():
@@ -66,6 +71,8 @@ def get_dmesg_msgs():
     import subprocess
     output = subprocess.Popen(["/bin/dmesg"],
                               stdout=subprocess.PIPE).communicate()[0]
+    if util.PY3:
+        output = util.decode_input(output)
     return output
 
 # Classes representing audit messages
sepolgen/src/sepolgen/util.py +14 -0
index 4934bec..16e7ca2 100644
@@ -103,6 +103,20 @@ def encode_input(text):
         encoded_text = text.encode('utf-8')
     return encoded_text
 
+def decode_input(text):
+    import locale
+    """Decode given text via preferred system encoding"""
+    # locale will often find out the correct encoding
+    encoding = locale.getpreferredencoding()
+    try:
+        decoded_text = text.decode(encoding)
+    except UnicodeError:
+    # if it fails to find correct encoding then ascii is used
+    # which may lead to UnicodeError if `text` contains non ascii signs
+    # utf-8 is our guess to fix the situation
+        decoded_text = text.decode('utf-8')
+    return decoded_text
+
 class Comparison():
     """Class used when implementing rich comparison.