2828
2929SYSFS_ROOT = '/sys'
3030
31+ # Some VPDs contain weird characters...
32+ def sanitize_sysfs_value (value ):
33+ return value .strip ('\x00 ' ).encode ('ascii' , errors = 'replace' ).decode ()
34+
3135
3236class SysfsNode (object ):
3337 def __init__ (self , path = None ):
@@ -121,7 +125,7 @@ def get(self, pathname, default=None, ignore_errors=False, printable=True,
121125 raise KeyError ('Not found: %s' % path )
122126 result = default
123127
124- return result
128+ return sanitize_sysfs_value ( result )
125129
126130 def readlink (self , pathname , default = None , absolute = False ):
127131 if absolute :
@@ -177,7 +181,7 @@ def load(self):
177181 # The next five methods are requirements of the ABC.
178182
179183 def __setitem__ (self , key , value ):
180- self .values [key ] = value
184+ self .values [key ] = sanitize_sysfs_value ( value )
181185
182186 def get (self , key , default = None ):
183187 if not self .values .__contains__ (key ):
@@ -189,6 +193,7 @@ def get(self, key, default=None):
189193 else :
190194 raise AttributeError ("%r object has no attribute %r" %
191195 (self .__class__ .__name__ , key ))
196+
192197 return self .values [key ]
193198
194199 def __getitem__ (self , key ):
0 commit comments