Skip to content

Commit ef29538

Browse files
committed
Merge branch 'hotfix/1.3.3'
2 parents 6d2051f + 98c1130 commit ef29538

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
Changelog for eccodes-python
33
============================
44

5+
1.3.3 (2021-mm-dd)
6+
--------------------
7+
8+
- ECC-1246: UnicodeDecodeError when parsing BUFR file
9+
10+
511
1.3.2 (2021-04-16)
612
--------------------
713

814
- Restore the experimental high-level interface
915

16+
1017
1.3.1 (2021-04-16)
1118
--------------------
1219

gribapi/bindings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import cffi
2222

23-
__version__ = "1.3.2"
23+
__version__ = "1.3.3"
2424

2525
LOG = logging.getLogger(__name__)
2626

gribapi/gribapi.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def grib_get_string(msgid, key):
491491
length_p = ffi.new("size_t *", length)
492492
err = lib.grib_get_string(h, key.encode(ENC), values, length_p)
493493
GRIB_CHECK(err)
494-
return ffi.string(values, length_p[0]).decode(ENC)
494+
return _decode_bytes(values, length_p[0])
495495

496496

497497
@require(msgid=int, key=str, value=str)
@@ -1174,6 +1174,19 @@ def grib_get_double_array(msgid, key):
11741174
return arr
11751175

11761176

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+
11771190
@require(msgid=int, key=str)
11781191
def grib_get_string_array(msgid, key):
11791192
"""
@@ -1192,7 +1205,7 @@ def grib_get_string_array(msgid, key):
11921205
size_p = ffi.new("size_t *", size)
11931206
err = lib.grib_get_string_array(h, key.encode(ENC), values, size_p)
11941207
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])]
11961209

11971210

11981211
@require(msgid=int, key=str)

0 commit comments

Comments
 (0)