Skip to content

Commit

Permalink
Allow units to be defined by bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhassell committed Jan 30, 2024
1 parent fac74a5 commit 0d0cf7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Version 3.3.7
-------------

**2024-02-??**

* Allow units to be defined by `bytes`
(https://github.com/NCAS-CMS/cfunits/issues/54)

----

Version 3.3.6
-------------

Expand Down
4 changes: 4 additions & 0 deletions cfunits/test/test_Units.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ def test_Units__dask_tokenize__(self):
self.assertEqual(tokenize(u), tokenize(Units("bad units")))
self.assertNotEqual(tokenize(u), tokenize(Units("worse units")))

def test_Units_bytes(self):
"""Tests initializing with bytes."""
self.assertEqual(Units("m"), Units(b"m"))


if __name__ == "__main__":
print("cfunits version:", cfunits.__version__)
Expand Down
8 changes: 8 additions & 0 deletions cfunits/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,17 @@ def __init__(
# ----------------------------------------------------
ut_unit = _cached_ut_unit.get(units, None)
if ut_unit is None:
try:
# Try for units being bytes
units = units.decode("utf-8")
except AttributeError:
# units is not bytes
pass

ut_unit = _ut_parse(
_ut_system, _c_char_p(units.encode("utf-8")), _UT_ASCII
)

if not ut_unit:
ut_unit = None
self._isvalid = False
Expand Down

0 comments on commit 0d0cf7f

Please sign in to comment.