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

@include: Only include modules from the current package #1485

Conversation

SitiSchu
Copy link
Contributor

@SitiSchu SitiSchu commented May 7, 2023

Fixes #1483

The previously used SourceFileLoader(name, filepath).load_module() adds the imported module to sys.modules. When a second module is imported with the same name, the previous module gets overwritten. In the case of #1483 this happened:

  • Pkg A utils gets imported and added to the module cache with its hash
    • Module gets added to sys.modules
  • Pkg B utils gets imported and added to the module cache with its hash.
    • This replaces A utils in sys.modules which causes the module cache to now have Pkg B's utils for all utils hashes
  • Pkg C utils' hash is in the module cache from Pkg A, so it gets used from the cache. However, Pkg B utils replaced all utils modules so this returns Pkg B's utils.

Using importlib.util.module_from_spec avoids this by not adding imported modules to sys.modules so nothing gets overwritten even if modules with the same name get imported multiple times.

Since the @include functionality works by placing the module in the globals for exec, not having the module in sys.modules should be fine.

@SitiSchu SitiSchu requested review from nerdvegas and a team as code owners May 7, 2023 02:58
@SitiSchu SitiSchu force-pushed the 1483-wrong-include-module branch from 088b530 to 35336f7 Compare May 7, 2023 03:00
Copy link
Member

@JeanChristopheMorinPerso JeanChristopheMorinPerso left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @SitiSchu! Do you think you could add a simple unit tests there to make sure this is well covered?

@JeanChristopheMorinPerso
Copy link
Member

JeanChristopheMorinPerso commented May 7, 2023

Note to TSC: CLA is correctly signed and setup. I'm not too sure why the EasyCLA bot didn't create a comment. I checked in PCC and I see the signature, so everything is good.

@SitiSchu
Copy link
Contributor Author

SitiSchu commented May 7, 2023

@JeanChristopheMorinPerso Sure can do, what would you like tested? I guess making sure it doesn't actually end up in sys.modules and maybe ensuring all defined functions and variables are accessible?

@JeanChristopheMorinPerso
Copy link
Member

I'd test get_module_from_file, which basically means testing that I can import from a given file and that it doesn't end up in sys.modules.

@JeanChristopheMorinPerso JeanChristopheMorinPerso added this to the Next milestone May 7, 2023
@SitiSchu SitiSchu force-pushed the 1483-wrong-include-module branch from f3f4b92 to 78c3f34 Compare May 7, 2023 18:18
@SitiSchu
Copy link
Contributor Author

SitiSchu commented May 7, 2023

I've added a test case for this.

To make sure it works, I ran the test without my fix and it did fail correctly.

@SitiSchu
Copy link
Contributor Author

SitiSchu commented May 7, 2023

It looks like the test fails with Python 2.7, should I limit the test to Python 3 only or should I try and fix the issue for py2 too(despite it being deprecated for years now)?

@JeanChristopheMorinPerso
Copy link
Member

We need to support both python 2 and 3. So if we fix python 3, we also need a fix for py 2.

@SitiSchu
Copy link
Contributor Author

SitiSchu commented May 7, 2023

Locally the copyright fix added a empty line to the utils file which made git complain about the extra whitespace. So I added a comment. Now it doesn't complain locally anymore but somehow the CI still fails.

@JeanChristopheMorinPerso
Copy link
Member

Don't worry about this check for now. We can take care of that.

Thanks for adding a test!

SitiSchu added 4 commits May 27, 2023 16:47
Signed-off-by: SitiSchu <admin@sitischu.com>
Signed-off-by: SitiSchu <admin@sitischu.com>
Signed-off-by: SitiSchu <admin@sitischu.com>
Signed-off-by: Jean-Christophe Morin <jean_christophe_morin@hotmail.com>
Signed-off-by: Jean-Christophe Morin <jean_christophe_morin@hotmail.com>
Signed-off-by: Jean-Christophe Morin <jean_christophe_morin@hotmail.com>
@JeanChristopheMorinPerso
Copy link
Member

There you go @SitiSchu. I fixed the copyright issue. I remove the dummy python module and I'm now creating it on the fly in the tests. I wasn't able to find why the change_copyright isn't doing what it's supposed to.

Copy link
Member

@JeanChristopheMorinPerso JeanChristopheMorinPerso left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot for this!

@JeanChristopheMorinPerso JeanChristopheMorinPerso requested a review from a team May 27, 2023 21:12
@JeanChristopheMorinPerso JeanChristopheMorinPerso changed the title Change method to load module from source files. Fixes #1483 Change method to load module from source files Sep 6, 2023
@JeanChristopheMorinPerso JeanChristopheMorinPerso changed the title Change method to load module from source files @include: Change method to load module from source files Sep 6, 2023
@JeanChristopheMorinPerso JeanChristopheMorinPerso changed the title @include: Change method to load module from source files @include: Change method used to load module from source files Sep 9, 2023
@JeanChristopheMorinPerso JeanChristopheMorinPerso changed the title @include: Change method used to load module from source files @include: Make sure it includes modules from the current package instead of other packages Sep 9, 2023
@JeanChristopheMorinPerso JeanChristopheMorinPerso changed the title @include: Make sure it includes modules from the current package instead of other packages @include: Only include modules from the current package Sep 9, 2023
@JeanChristopheMorinPerso JeanChristopheMorinPerso merged commit c4cdc7e into AcademySoftwareFoundation:master Sep 9, 2023
@JeanChristopheMorinPerso
Copy link
Member

Thanks you @SitiSchu !

@SitiSchu SitiSchu deleted the 1483-wrong-include-module branch September 11, 2023 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@include uses file from different package
3 participants