How was class instantiated?

naseeam
naseeam used Ask the Experts™
on
Bitbake is a python program. It was downloaded from:  https://github.com/openembedded/bitbake
bitbake 1.40.0 branch was selected for download.

What instantiated class ConfigParameters(object)?  I see that __init__ or constructor was executed but exactly what python code instantiated this class?


object created
filename:  cookerdata.py
class ConfigParameters(object):
    def __init__(self, argv=sys.argv):
        self.options, targets = self.parseCommandLine(argv)
        self.environment = self.parseEnvironment()

Open in new window


filename:  bitbake
if __name__ == "__main__":
    if __version__ != bb.__version__:
        sys.exit("Bitbake core version and program version mismatch!")
    try:
        sys.exit(bitbake_main(BitBakeConfigParameters(sys.argv),
                              cookerdata.CookerConfiguration()))

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I think cookerdata.CookerConfiguration() instantiated derived class configParameters(object), but how?
if you are in the python debugger you can type bt to see the back trace which means the complete chain of function calls.

you can then press u or d to go up or down in the back trace

if you have a class ConfigParameters, then somewhere in the code somebody created an object of type ConfigParameters or of a class that inherited from ConfigParameters

in your case it seems, that line 5 of bitbake instantiates
BitBakeConfigParameters(sys.argv)
I guess, that if you look at the rest of the code you will seel, that BitBakeConfigParameters inherits from ConfigParameters

If you add following line before line 5, then you will see all classes from which BitBakeConfigParameters inherited.

print(BitBakeConfigParameters.mro())

Open in new window

Author

Commented:
Above filename bitbake, line 5, statement BitBakeConfigParameters(sys.argv) instantiates child class BitBakeConfigParameters( ) in filename main.py.  Another words, child class is instantiated with statement BitBakeConfigParameters(sys.argv) because above debugger snapshot shows execution of this statement calls the initializer in parent class ConfigParameters( ) in filename cookerdata.py.
Instantiating child class calls initializer in parent class because child class didn't overload the initializer.
When child class is instantiated, there is no object variable.  Why?  There is no need for object variable because BitBakeConfigParameters(sys.argv) argument is passed to bitbake_main( ) in main.py ?

filename:  main.py
class BitBakeConfigParameters(cookerdata.ConfigParameters):

    def parseCommandLine(self, argv=sys.argv):
        parser = optparse.OptionParser(
            formatter=BitbakeHelpFormatter(),
            version="BitBake Build Tool Core version %s" % bb.__version__,
            usage="""%prog [options] [recipename/target recipe:do_task ...]

    Executes the specified task (default is 'build') for a given set of target recipes (.bb files).
    It is assumed there is a conf/bblayers.conf available in cwd or in BBPATH which
    will provide the layer, BBFILES and other configuration information.""")

        parser.add_option("-b", "--buildfile", action="store", dest="buildfile", default=None,
                          help="Execute tasks from a specific .bb recipe directly. WARNING: Does "
                               "not handle any dependencies from other recipes.")

        parser.add_option("-k", "--continue", action="store_false", dest="abort",

Open in new window



filename main.py
def bitbake_main(configParams, configuration):

    # Python multiprocessing requires /dev/shm on Linux
    if sys.platform.startswith('linux') and not os.access('/dev/shm', os.W_OK | os.X_OK):
        raise BBMainException("FATAL: /dev/shm does not exist or is not writable")

    # Unbuffer stdout to avoid log truncation in the event
    # of an unorderly exit as well as to provide timely
    # updates to log files for use with tail
    try:
        if sys.stdout.name == '<stdout>':
            # Reopen with O_SYNC (unbuffered)
            fl = fcntl.fcntl(sys.stdout.fileno(), fcntl.F_GETFL)
            fl |= os.O_SYNC
            fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, fl)
    except:
        pass

    configuration.setConfigParameters(configParams)

    if configParams.server_only and configParams.remote_server:
            raise BBMainException("FATAL: The '--server-only' option conflicts with %s.\n" %
                                  ("the BBSERVER environment variable" if "BBSERVER" in os.environ \
                                   else "the '--remote-server' option"))

    if configParams.observe_only and not (configParams.remote_server or configParams.bind):
        raise BBMainException("FATAL: '--observe-only' can only be used by UI clients "
                              "connecting to a server.\n")

    if "BBDEBUG" in os.environ:
        level = int(os.environ["BBDEBUG"])
        if level > configuration.debug:
            configuration.debug = level

    bb.msg.init_msgconfig(configParams.verbose, configuration.debug,
                          configuration.debug_domains)

    server_connection, ui_module = setup_bitbake(configParams, configuration)
    # No server connection
    if server_connection is None:
        if configParams.status_only:
            return 1
        if configParams.kill_server:
            return 0

    if not configParams.server_only:
        if configParams.status_only:
            server_connection.terminate()
            return 0

        try:
            for event in bb.event.ui_queue:
                server_connection.events.queue_event(event)
            bb.event.ui_queue = []

            return ui_module.main(server_connection.connection, server_connection.events,
                                  configParams)
        finally:
            server_connection.terminate()
    else:
        return 0

    return 1

Open in new window

How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
Thank you for helping me figure out where the class is instantiated.
exactly you can directly create an instance and pass it to a function (without assigning it before to a variable)

In that case it will be the parameter variable of the function, that will keep a reference to the created object.

Author

Commented:
Thank you for this confirmation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial