Search code examples
pythonimportpybluez

Python module importing itself with leading underscore but there is no corresponding .py file


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?


Solution

  • 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.