hercules-team / python-augeas

Python bindings for Augeas
GNU Lesser General Public License v2.1
44 stars 31 forks source link

transition to FFI broke set(path, None) #26

Closed pspacek closed 7 years ago

pspacek commented 7 years ago

As far as I can tell handling None values require special care. Right now an attempt to set a value to None produces following error:

t._aug.set("/files/tmp/hosts/#comment[4]", None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-d057463c301f> in <module>()
----> 1 t._aug.set("/files/tmp/hosts/#comment[4]", None)

/usr/lib/python3.6/site-packages/augeas/__init__.py in set(self, path, value)
    166 
    167         # Call the function
--> 168         ret = lib.aug_set(self.__handle, enc(path), enc(value))
    169         if ret != 0:
    170             raise ValueError("Unable to set value to path!")

TypeError: initializer for ctype 'char *' must be a bytes or list or tuple, not str

This is very unfortunate because setting values to None is used to implement touch command similar to augtool.

yan12125 commented 7 years ago

enc() should always return bytes. This works for me:

diff --git a/augeas/__init__.py b/augeas/__init__.py
index ab9a2b1..ccfcec0 100644
--- a/augeas/__init__.py
+++ b/augeas/__init__.py
@@ -57,7 +57,7 @@ def enc(st):
     if st:
         return st.encode(AUGENC)
     else:
-        return ''
+        return b''

 def dec(st):