Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing on Windows systems results in an AttributeError #56

Closed
koenvervloesem opened this issue Apr 2, 2023 · 5 comments · Fixed by #57
Closed

Importing on Windows systems results in an AttributeError #56

koenvervloesem opened this issue Apr 2, 2023 · 5 comments · Fixed by #57
Labels
bug Something isn't working

Comments

@koenvervloesem
Copy link
Contributor

Describe the bug

Importing the module on Windows systems results in an AttributeError because bluetooth-adapters tries to access D-Bus.

To Reproduce

Run from bluetooth_adapters import get_adapters on Windows.

Minimal example:

import asyncio
from bluetooth_adapters import get_adapters


async def diagnostics():
    bluetooth_adapters = get_adapters()
    await bluetooth_adapters.refresh()
    print(f"Default adapter: {bluetooth_adapters.default_adapter}")


if __name__ == "__main__":
    asyncio.run(diagnostics())

Get a stack trace like this one (from other code than the above):

Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\TheengsGateway\diagnose.py", line 157, in
asyncio.run(diagnostics())
File "C:\Program Files\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\asyncio\runners.py", line 118, in run
return self.loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\TheengsGateway\diagnose.py", line 153, in diagnostics
await adapters()
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\TheengsGateway\diagnose.py", line 129, in adapters
from bluetooth_adapters import get_adapters
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\bluetooth_adapters_init.py", line 12, in
from .dbus import (
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\bluetooth_adapters\dbus.py", line 10, in
from dbus_fast import BusType, Message, MessageType, unpack_variants
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\dbus_fast_init.py", line 1, in
from . import aio, glib, introspection, message_bus, proxy_object, service
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\dbus_fast\aio_init.py", line 1, in
from .message_bus import MessageBus
File "C:\Users\JC\AppData\Roaming\Python\Python311\site-packages\dbus_fast\aio\message_bus.py", line 29, in
from .message_reader import build_message_reader
File "src\dbus_fast\aio\message_reader.py", line 1, in init dbus_fast.aio.message_reader
File "src\dbus_fast_private\unmarshaller.py", line 16, in init dbus_fast._private.unmarshaller
AttributeError: module 'socket' has no attribute 'CMSG_LEN'

Additional context

Removing lines 12-17 from bluetooth_adapters/__init__.py makes the code work on Windows:

from .dbus import (
    BlueZDBusObjects,
    get_bluetooth_adapter_details,
    get_bluetooth_adapters,
    get_dbus_managed_objects,
)

The code should probably only import from the dbus module on Linux?

@koenvervloesem koenvervloesem added the bug Something isn't working label Apr 2, 2023
@bdraco
Copy link
Member

bdraco commented Apr 2, 2023

This library is mostly used for Home Assistant which only supports WSL.

I don't have a windows machine to test with but I'm happy to merge a fix as long as it doesn't break WSL

@koenvervloesem
Copy link
Contributor Author

koenvervloesem commented Apr 2, 2023

Ok, I'll also test this on macOS and add a check on the operating system for a conditional import. I'll open a PR soon.

@koenvervloesem
Copy link
Contributor Author

There's no problem on macOS. However, looking at it deeper, it seems the problem on Windows is only because socket.CMSG_LEN isn't defined on Windows and is used in dbus-fast's unmarshaller module.

Wouldn't the cleanest way to fix this be an alternative implementation for UNIX_FDS_CMSG_LENGTH in dbus-fast? I don't know anything about Windows socket programming, though, so I don't know what this value should be.

@bdraco
Copy link
Member

bdraco commented Apr 3, 2023

There's no problem on macOS. However, looking at it deeper, it seems the problem on Windows is only because socket.CMSG_LEN isn't defined on Windows and is used in dbus-fast's unmarshaller module.

Wouldn't the cleanest way to fix this be an alternative implementation for UNIX_FDS_CMSG_LENGTH in dbus-fast? I don't know anything about Windows socket programming, though, so I don't know what this value should be.

dbus will likely never be installed on windows so ideally its never imported there

@koenvervloesem
Copy link
Contributor Author

Got it. Fixed and tested on Linux, Windows and macOS.

@bdraco bdraco closed this as completed in #57 Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants