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

Documented that nothing should be defined outside the plugin class in Python storage plugins #867

17 changes: 17 additions & 0 deletions doc/user_guide/storage_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
"""Template for Python storage plugins."""

# NOTE:
# Please, do not define any variables or functions outside the scope
# of the plugin class.
#
# The reason for this requirement is that all plugins will be loaded
# into the same shared scope within the built-in interpreter.
# Hence, variables or functions outside the plugin class may interfere
# with other plugins, resulting in hard-to-find bugs.


class plugin_driver_name(dlite.DLiteStorageBase):
"""General description of the Python storage plugin."""
Expand Down Expand Up @@ -80,3 +89,11 @@ def to_bytes(cls, inst):
Returns:
The bytes (or bytearray) object that the instance is saved to.
"""

def _example_help_method(self, *args):
"""Example help method.

If you need a help function, please make it a class method to
avoid possible naming conflicts with help functions in other
storage plugins.
"""
10 changes: 10 additions & 0 deletions doc/user_guide/storage_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ See the [Python storage plugin template] for how to write a Python storage plugi
A complete example can be found in the [Python storage plugin example].


:::{danger}
**When writing a Python storage plugin, do not define any variables or functions outside the `DLiteStorageBase` subclass!**

The reason for this requirement is that all plugins will be loaded into the same shared scope within the built-in interpreter.
Hence, variables or functions outside the plugin class may interfere with other plugins, resulting in confusing and hard-to-find bugs.
:::

Comment on lines +178 to +184
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, but can we add an example in https://github.com/SINTEF/dlite/blob/master/doc/user_guide/storage_plugin.py (doen't matter if it is not used), and in the template?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, but can we add an example in https://github.com/SINTEF/dlite/blob/master/doc/user_guide/storage_plugin.py (doen't matter if it is not used), and in the template?

Do you mean the note added on line 3 in https://github.com/SINTEF/dlite/blob/81261950afd235dd4c280103bcca798c46ec9463/doc/user_guide/storage_plugin.py?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I was thinking more along the lines
class plugin....:
....
...

def _local_help_function():
""" An optional class method or a function if desired.
Note that no functions or variables should be set outside the class
for a DLite plugin python file.
""

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an example help method to the storage plugin template




Working with storages from C and Fortran
----------------------------------------
The C API for storages is documented in the [C reference manual].
Expand Down