Skip to content

Commit

Permalink
Plugins: Merge pre and post load codes better
Browse files Browse the repository at this point in the history
* Was not making sure that future imports are at the start of the
  file, giving errors when multiple plugins try to do that.
  • Loading branch information
kayhayen committed Apr 21, 2023
1 parent e75a8f9 commit 98d368e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
55 changes: 31 additions & 24 deletions nuitka/plugins/Plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,10 @@ def _createTriggerLoadedModule(cls, module, trigger_name, code, flags):
is_fake=module_name,
hide_syntax_error=False,
)
except SyntaxError:
except SyntaxError as e:
plugins_logger.sysexit(
"SyntaxError in plugin provided source code for '%s'." % module_name
"SyntaxError in plugin provided source code for '%s': %s."
% (module_name, e)
)

if trigger_module.getCompilationMode() == "bytecode":
Expand All @@ -758,7 +759,7 @@ def _createTriggerLoadedModule(cls, module, trigger_name, code, flags):
@classmethod
def onModuleDiscovered(cls, module):
# We offer plugins many ways to provide extra stuff
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
# pylint: disable=too-many-locals,too-many-statements

full_name = module.getFullName()

Expand Down Expand Up @@ -816,23 +817,41 @@ def _untangleFakeDesc(description):
_untangleFakeDesc(description=plugin.createFakeModuleDependency(module))
)

if pre_module_load_descriptions:
total_code = []
def combineLoadCodes(module_load_descriptions, mode):
future_imports_code = []
normal_code_code = []
total_flags = OrderedSet()
reasons = []

for plugin, pre_code, reason, flags in pre_module_load_descriptions:
if pre_code:
for plugin, code, reason, flags in module_load_descriptions:
if code:
plugin.info(
"Injecting pre-module load code for module '%s':" % full_name
"Injecting %s-module load code for module '%s':"
% (mode, full_name)
)
for line in reason.split("\n"):
plugin.info(" " + line)

total_code.append(pre_code)
for line in code.splitlines():
line = line + "\n"

if line.startswith("from __future__"):
future_imports_code.append(line)
else:
normal_code_code.append(line)

total_flags.update(flags)
reasons.append(reason)

total_code = future_imports_code + normal_code_code

return total_code, reasons, total_flags

if pre_module_load_descriptions:
total_code, reasons, total_flags = combineLoadCodes(
module_load_descriptions=pre_module_load_descriptions, mode="pre"
)

if total_code:
assert full_name not in pre_modules

Expand All @@ -845,21 +864,9 @@ def _untangleFakeDesc(description):
pre_modules_reasons[full_name] = tuple(reasons)

if post_module_load_descriptions:
total_code = []
total_flags = OrderedSet()
reasons = []

for plugin, post_code, reason, flags in post_module_load_descriptions:
if post_code:
plugin.info(
"Injecting post-module load code for module '%s':" % full_name
)
for line in reason.split("\n"):
plugin.info(" " + line)

total_code.append(post_code)
total_flags.update(flags)
reasons.append(reason)
total_code, reasons, total_flags = combineLoadCodes(
module_load_descriptions=post_module_load_descriptions, mode="post"
)

if total_code:
assert full_name not in post_modules
Expand Down
1 change: 0 additions & 1 deletion nuitka/plugins/standard/PySidePyQtPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,6 @@ def createPostModuleLoadCode(self, module):
os.path.dirname(__file__),
"qml"
)
""" % {
"package_name": full_name
}
Expand Down

0 comments on commit 98d368e

Please sign in to comment.