ekonda / kutana

The library for developing systems for messengers and social networks
MIT License
72 stars 17 forks source link

Fix import module when load plugins from file to allow some pickle cases #76

Closed acjh closed 1 year ago

acjh commented 1 year ago

Description

  1. Fix the name of the module to be imported in load_plugins_from_file.
  2. Fix the missing step in import_module that inserts the module into sys.modules as shown in the recipe https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly.

Motivation and Context

Pickling a plugin that has a reference to a function or class defined within the plugin module fails.
See https://stackoverflow.com/questions/74841508/cant-pickle-class-import-of-module-failed.

How Has This Been Tested?

Added test_pickle_dumps in test_loaders.py, which fails before this fix and passes after this fix.

Limitations:

Types of changes

Checklist:

michaelkryukov commented 1 year ago

In relation to other issues that currently is being solved (#74), I don't think this this PR will land into master. And generally pickling objects that are not supposed to be pickled - is not a good idea. Plugin objects should never be serialized. Why is that needed?

acjh commented 1 year ago

I can't read #74 and it doesn't make sense when Google-translated.

For why, see the linked Stack Overflow question. Note that it's not my question and I don't use Kutana, so I can't strongly justify the use case.

Regardless of whether pickling is supported, this fixes the incorrect and incomplete import helpers.

michaelkryukov commented 1 year ago

Sorry for mixed languages. I'm in process of rewriting loaders.py, where importing will be implemented properly (so that classes inside loaded modules will be available to pickle). That's what what basically means #74