bt2: clean available `bt2` package names
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 24 Jul 2019 20:36:30 +0000 (16:36 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 7 Aug 2019 18:05:19 +0000 (14:05 -0400)
commitc946c9de02e1a5a37945cb98bd0e499033336c55
treebe5b451b1b48f02ebab028be086e996c7e5ed9e7
parent2d83d56c9e5b8f909b2fb7ca79a612159a6f7723
bt2: clean available `bt2` package names

This patch makes the `bt2` package contain exactly the names that we
want to expose. When you do:

    import bt2
    dir(bt2)

the list, except for the standard names added by Python itself, contains
exactly what is meant to be used by the user. More specifically, it does
not contain the names of our internal modules, so that, for example,
`bt2.clock_class` is not available.

In `__init__.py`, doing

    from bt2.field import _EnumerationField

binds the `field` name in the package to the `bt2.field` module [1]:

> When a submodule is loaded using any mechanism (e.g. importlib APIs,
> the import or import-from statements, or built-in __import__()) a
> binding is placed in the parent module’s namespace to the submodule
> object.

All the `from bt2.xyz import Something` lines eventually bind some or
all module names in the `bt2` package, so `__init__.py` explicitly tries
to delete the module names just after. `__init__.py` also deletes the
`_init_and_register_exit` name which is private and used a single time
to initialize the native part.

Internally, within a module file, because the module names are not
available anymore in the `bt2` package, we cannot do

    import bt2.clock_class

as `clock_class` is not a name within `bt2`. This is why all those
private imports are replaced with, for example:

    from bt2 import clock_class as bt2_clock_class

Now `bt2_clock_class` is a name bound to the `bt2.clock_class` module.
Note that we cannot use the

    import bt2.clock_class as bt2_clock_class

because this leads to circular import issues with Python < 3.7 [2]. The
`from bt2 import clock_class` form does not create circular imports.

Doing this change in `__init__.py` revealed a lot of locations where
`bt2.some_module.SomeName` was used without explicitly importing
`bt2.some_module`. They are all fixed now.

[1]: https://docs.python.org/3/reference/import.html#submodules
[2]: https://bugs.python.org/issue30024

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib7d2b3ef3283ace19cbcb811d537a63b34690ada
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1774
Tested-by: jenkins <jenkins@lttng.org>
34 files changed:
src/bindings/python/bt2/bt2/__init__.py.in
src/bindings/python/bt2/bt2/clock_snapshot.py
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/connection.py
src/bindings/python/bt2/bt2/event.py
src/bindings/python/bt2/bt2/event_class.py
src/bindings/python/bt2/bt2/field.py
src/bindings/python/bt2/bt2/field_class.py
src/bindings/python/bt2/bt2/graph.py
src/bindings/python/bt2/bt2/message.py
src/bindings/python/bt2/bt2/message_iterator.py
src/bindings/python/bt2/bt2/packet.py
src/bindings/python/bt2/bt2/plugin.py
src/bindings/python/bt2/bt2/port.py
src/bindings/python/bt2/bt2/py_plugin.py
src/bindings/python/bt2/bt2/query_executor.py
src/bindings/python/bt2/bt2/stream.py
src/bindings/python/bt2/bt2/stream_class.py
src/bindings/python/bt2/bt2/trace.py
src/bindings/python/bt2/bt2/trace_class.py
src/bindings/python/bt2/bt2/trace_collection_message_iterator.py
src/bindings/python/bt2/bt2/utils.py
src/bindings/python/bt2/bt2/value.py
tests/bindings/python/bt2/test_component_class.py
tests/bindings/python/bt2/test_error.py
tests/bindings/python/bt2/test_field.py
tests/bindings/python/bt2/test_field_class.py
tests/bindings/python/bt2/test_graph.py
tests/bindings/python/bt2/test_message.py
tests/bindings/python/bt2/test_message_iterator.py
tests/bindings/python/bt2/test_plugin.py
tests/bindings/python/bt2/test_query_executor.py
tests/bindings/python/bt2/test_trace_class.py
tests/bindings/python/bt2/test_trace_collection_message_iterator.py
This page took 0.029072 seconds and 4 git commands to generate.