@@ -491,7 +491,7 @@ def grib_get_string(msgid, key):
491
491
length_p = ffi .new ("size_t *" , length )
492
492
err = lib .grib_get_string (h , key .encode (ENC ), values , length_p )
493
493
GRIB_CHECK (err )
494
- return ffi . string (values , length_p [0 ]). decode ( ENC )
494
+ return _decode_bytes (values , length_p [0 ])
495
495
496
496
497
497
@require (msgid = int , key = str , value = str )
@@ -1174,6 +1174,19 @@ def grib_get_double_array(msgid, key):
1174
1174
return arr
1175
1175
1176
1176
1177
+ # See ECC-1246
1178
+ def _decode_bytes (binput , maxlen = None ):
1179
+ if maxlen :
1180
+ a_str = ffi .string (binput , maxlen )
1181
+ else :
1182
+ a_str = ffi .string (binput )
1183
+ # Check for a MISSING value i.e., each character has all its bits=1
1184
+ if all (x == 255 for x in a_str ):
1185
+ return ""
1186
+ # Replace with a suitable replacement character rather than throw an exception
1187
+ return a_str .decode (ENC , "replace" )
1188
+
1189
+
1177
1190
@require (msgid = int , key = str )
1178
1191
def grib_get_string_array (msgid , key ):
1179
1192
"""
@@ -1192,7 +1205,7 @@ def grib_get_string_array(msgid, key):
1192
1205
size_p = ffi .new ("size_t *" , size )
1193
1206
err = lib .grib_get_string_array (h , key .encode (ENC ), values , size_p )
1194
1207
GRIB_CHECK (err )
1195
- return [ffi . string (values [i ]). decode ( ENC ) for i in range (size_p [0 ])]
1208
+ return [_decode_bytes (values [i ]) for i in range (size_p [0 ])]
1196
1209
1197
1210
1198
1211
@require (msgid = int , key = str )
0 commit comments