apmorton / pyhidapi

hidapi bindings in ctypes
MIT License
112 stars 42 forks source link

json.dumps dies with TypeError: Object of type BusType is not JSON serializable on hid.enumerate() on 1.0.6 #60

Open RodoMa92 opened 5 months ago

RodoMa92 commented 5 months ago

While debugging the Steam Deck updater program I discovered that #59 causes a regression making it unable to parse the new added field back with the following backtrace:

Traceback (most recent call last):
  File "/var/home/marco/Desktop/jupiter_controller_fw_updater/./d20bootloader.py", line 912, in <module>
    cli()
  File "/usr/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/home/marco/Desktop/jupiter_controller_fw_updater/./d20bootloader.py", line 765, in getdevicesjson
    print(json.dumps(devs))
          ^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type BusType is not JSON serializable

If you need to take a look at the source code of the software, the code is here: https://gitlab.com/evlaV/jupiter-hw-support/-/tree/master/usr/share/jupiter_controller_fw_updater?ref_type=heads

Downgrading back to 1.0.5 makes the program works correctly.

apmorton commented 5 months ago

I think this should be reported to https://gitlab.com/evlaV/jupiter-hw-support/-/issues

It's unfortunate 1.0.6 broke that software, but I don't think there is a good way to fix this in pyhidapi without causing even more breakage in other applications which now use BusType.

jupiter-hw-support can either:

antheas commented 5 months ago

You could always cast bustype as an int before returning it. Unless there is proof it breaks say e.g., match statements

Also, it appears that software is affected more than that print statement. It refuses to work now. We will investigate it.