1

In the PyBluez source code I've noticed a couple things I haven't seen before. This file named widcomm.py starts with the following:

from .btcommon import *
import socket
import struct
import threading
import os
import _widcomm

In the previous directory, there is no _widcomm.py or another widcomm.py. I've read that modules with a leading underscore might be "private" or accelerated, but I can't find anything about a module seemingly importing itself with an underscore.

A few lines under that you get this interesting function:

def dbg (*args):
    return
    sys.stdout.write (*args)
    sys.stdout.write ("\n")

Am I correct in thinking the code under return has no way of ever being executed? As far as I can tell this function serves no purpose.

What exactly is going on here?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Temerity
  • 13
  • 3
  • 1
    Yes, the code below `return` won't be executed. However, this has nothing to do with the `_widcomm` import. – mkrieger1 Jul 20 '22 at 14:43
  • It may be a C extension module. When writing C extensions, its common to have a .py file for stuff that doesn't need C level code that imports the stuff that does need C. As for `return`, its just a quick and dirty way of doing debug - you are meant to comment out the `return` to enable it. – tdelaney Jul 20 '22 at 14:48

2 Answers2

0

Regarding the obscure return statement.

You can push the blame-button on the github you linked and there you'll see following comment for the return:

suppress debugging messages

Below that return there are said debugging-messages printing the arguments to stdout. So the return is there to not execute those messages that where there before the return. Like you guessed and @mkrieger1 commented those statements will never be executed as long the return is in place.

MangoNrFive
  • 1,541
  • 1
  • 10
  • 23
0

According to the setup file of this Python package,

ext_modules.append(Extension('bluetooth._widcomm',
        include_dirs=["%s\\Inc" % WC_BASE],
        define_macros=[('_BTWLIB', None)],
        library_dirs=["%s\\Release" % WC_BASE],
        libraries=["WidcommSdklib", "ws2_32", "version", "user32",
                   "Advapi32", "Winspool", "ole32", "oleaut32"],
        sources=["widcomm\\_widcomm.cpp",
                 "widcomm\\inquirer.cpp",
                 "widcomm\\rfcommport.cpp",
                 "widcomm\\rfcommif.cpp",
                 "widcomm\\l2capconn.cpp",
                 "widcomm\\l2capif.cpp",
                 "widcomm\\sdpservice.cpp",
                 "widcomm\\util.cpp"]))

the _widcomm import refers to an extension module which is built from the _widcomm.cpp and related C++ source files.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65