NCAS-CMS / cfunits

A Python interface to UNIDATA’s UDUNITS-2 library with CF extensions:
http://ncas-cms.github.io/cfunits
MIT License
11 stars 8 forks source link

Allow units to be defined by `bytes` #54

Closed davidhassell closed 7 months ago

davidhassell commented 7 months ago

Currently (v3.3.6), a Units instance cannot be defined by bytes:

>>> from cfunits import Units
>>> Units('m')  # Initialize with str: OK
<Units: m>
>>> Units(b'm')  # Initialize with bytes: NOT OK
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[5], line 1
----> 1 Units(b'm')

File ~/miniconda3/lib/python3.11/site-packages/cfunits/units.py:805, in Units.__init__(self, units, calendar, formatted, names, definition, _ut_unit)
    802 ut_unit = _cached_ut_unit.get(units, None)
    803 if ut_unit is None:
    804     ut_unit = _ut_parse(
--> 805         _ut_system, _c_char_p(units.encode("utf-8")), _UT_ASCII
    806     )
    807     if not ut_unit:
    808         ut_unit = None

AttributeError: 'bytes' object has no attribute 'encode'

>>>

It would be good to fix this!