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

Replace use of imp library with importlib. #3159

Closed
wants to merge 3 commits into from
Closed

Replace use of imp library with importlib. #3159

wants to merge 3 commits into from

Conversation

mcepl
Copy link

@mcepl mcepl commented Jul 25, 2018

imp library has been deprecated since 3.4 and in 3.7 it finally breaks builds.

Preserving compatibility with python >= 2.7.

  • I have created a new test or updated the unit tests to cover the new/changed functionality.
    No functionality has been changed or added.
  • I have updated master/src/CHANGES.txt directory (and read the README.txt in that directory)
  • I have updated the appropriate documentation

@bdbaddog
Copy link
Contributor

we've never supported python 3.4, so please remove support code for that.
It's Python 2.7.x & >= 3.5

# module's dictionary, so anything that code defines ends
# up adding to that module. This is really short, but all
# the error checking makes it longer.
loader_details = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a brief comment as to what this does

Copy link
Author

Choose a reason for hiding this comment

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

Well, the same what the previous code ... opens module found in the particular location. See https://stackoverflow.com/a/37124336/164233

OK, I will mention it.

Copy link
Author

Choose a reason for hiding this comment

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

Also, this doesn't work.

[  106s] + python3 bootstrap.py build/scons
[  106s] /usr/bin/python3 /home/abuild/rpmbuild/BUILD/scons-3.0.1/bootstrap/src/script/scons.py build/scons
[  107s] *** Error loading site_init file './site_scons/site_init.py':
[  107s] AttributeError: 'NoneType' object has no attribute 'loader':
[  107s]   File "/home/abuild/rpmbuild/BUILD/scons-3.0.1/bootstrap/src/engine/SCons/Script/Main.py", line 1401:
[  107s]     _exec_main(parser, values)
[  107s]   File "/home/abuild/rpmbuild/BUILD/scons-3.0.1/bootstrap/src/engine/SCons/Script/Main.py", line 1364:
[  107s]     _main(parser)
[  107s]   File "/home/abuild/rpmbuild/BUILD/scons-3.0.1/bootstrap/src/engine/SCons/Script/Main.py", line 991:
[  107s]     _load_all_site_scons_dirs(d.get_internal_path())
[  107s]   File "/home/abuild/rpmbuild/BUILD/scons-3.0.1/bootstrap/src/engine/SCons/Script/Main.py", line 844:
[  107s]     _load_site_scons_dir(d)
[  107s]   File "/home/abuild/rpmbuild/BUILD/scons-3.0.1/bootstrap/src/engine/SCons/Script/Main.py", line 782:
[  107s]     mod = importlib.util.module_from_spec(spec)
[  107s]   File "<frozen importlib._bootstrap>", line 580:

@@ -136,7 +137,7 @@ def _tool_module(self):
sys.path = self.toolpath + sys.path
# sys.stderr.write("Tool:%s\nPATH:%s\n"%(self.name,sys.path))

if sys.version_info[0] < 3 or (sys.version_info[0] == 3 and sys.version_info[1] in (0,1,2,3,4)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. the py3 < 3.5 code shouldn't have ever been here..

Copy link
Author

Choose a reason for hiding this comment

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

I will gladly remove the other part of the condition, but that's what I've found in your code. Should I?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. No code needed to support < 3.5 in the py 3 series.
This code is left over from when I did the bulk of the port before the decision was made to only support 3.5+

Copy link
Author

Choose a reason for hiding this comment

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

Well, this is primarily for Py2 code.

@bdbaddog
Copy link
Contributor

We are getting almost 100% passing with py3.7 without these changes?
http://buildbot.scons.org/#/builders/13/builds/10/steps/1/logs/stdio

But if imp is obsolete, definitely worth moving forward!

@mcepl
Copy link
Author

mcepl commented Jul 25, 2018

We had hundreds of build errors like this one.
_log.txt

@mcepl
Copy link
Author

mcepl commented Jul 25, 2018

But if imp is obsolete, definitely worth moving forward!

https://docs.python.org/3/library/imp.html … “Deprecated since version 3.4: The imp package is pending deprecation in favor of importlib.”

@bdbaddog
Copy link
Contributor

We are getting almost 100% passing with py3.7 without these changes?
http://buildbot.scons.org/#/builders/13/builds/10/steps/1/logs/stdio

But if imp is obsolete, definitely worth moving forward!

Looks like a bunch of test broke with py3.5 and 3.6. Can you take a look and resolve?

@mcepl
Copy link
Author

mcepl commented Jul 30, 2018

Could we get this merged and then fix bugs which were already there?

@bdbaddog
Copy link
Contributor

No. The policy is that tests should pass before merge.
Can you not fix it in your branch?
Commits there will update the Pull Request and initiate another regression run on travis.

It's currently breaking site_scons, which is pretty important.

@mcepl
Copy link
Author

mcepl commented Jul 30, 2018

No. The policy is that tests should pass before merge.

Did tests pass without applying my patch? It seems to me that most of these failed tests are not my fault. Are they?

@bdbaddog
Copy link
Contributor

Yes.
See the latest travis run from master:
https://travis-ci.org/SCons/scons/builds/408108763

@bdbaddog
Copy link
Contributor

bdbaddog commented Aug 1, 2018

@mcepl Are you able to reproduce the test success without your pass with the various python versions?

@mcepl
Copy link
Author

mcepl commented Aug 4, 2018

@mcepl Are you able to reproduce the test success without your pass with the various python versions?

I don't understand. Do you want me to play manually Travis/Appveyor? What's the point?

@bdbaddog
Copy link
Contributor

bdbaddog commented Aug 4, 2018

SCons' regression tests pass on all versions of python (except pypy) without your change.
With your change the tests fail on PY3.5 and PY3.6

You asked if the tests passed without your changes and so I pointed you to travis-ci results which showed the failures from your changes.

If you look at this run: https://travis-ci.org/SCons/scons/builds/408108763
You'll see all the tests pass without your changes.

So please resolve the failures with your changes under py3.5 and 3.6 (and likely 3.7) or we cannot accept the pull request.

@mcepl
Copy link
Author

mcepl commented Aug 6, 2018

SCons' regression tests pass on all versions of python (except pypy) without your change.

When running tests for master branch on my RHEL, I get these tests failing:

Failed the following 6 tests:
	test/packaging/option--package-type.py
	test/packaging/rpm/cleanup.py
	test/packaging/rpm/internationalization.py
	test/packaging/rpm/multipackage.py
	test/packaging/rpm/package.py
	test/packaging/rpm/tagging.py

See full log

@bdbaddog
Copy link
Contributor

bdbaddog commented Aug 6, 2018 via email

@mwichmann
Copy link
Collaborator

@mcepl: I have fixes for the rpm stuff in #3172, if you could try those it would be useful since we don't have a builder that will exercise that at present; hopefully taking those out of the equation would simplify sorting what's going on with the actual content of this patch.

@mwichmann
Copy link
Collaborator

Adding comment from IRC which may shed some light on where problems occur

<rekado> Hi, I’m trying to package Scons for GNU Guix and I’m having some trouble with the bootstrap phase.
<rekado> I’m running python bootstrap.py build/scons with Python 3.7 and the first thing that happens is a complaint from Python about the use of “str” when a “byte-like” object is expected.
<rekado> The cause of this is rename_module, so I patched src/engine/SCons/compat/__init__.py to use __import__ instead of imp.load_module

@mwichmann
Copy link
Collaborator

Fwiw, this issue (use of "imp") completely blows up running the testsuite with Python 3.8 (still in alpha state), which has dialed up the intensity on warnings - on a large number of tests, the deprecation warning ends up in the stderr and so expected-output matches fail.

@mwichmann mwichmann mentioned this pull request Mar 6, 2019
@@ -122,8 +121,17 @@ def Package(env, target=None, source=None, **kw):
# load the needed packagers.
def load_packager(type):
try:
file,path,desc=imp.find_module(type, __path__)
return imp.load_module(type, file, path, desc)
if PY2:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I have an alternate solution for this location... we just want a relative import as we're already in SCons/Tool/Packaging. For that, the following seems to work (for 2.7 as well)

        try:
            return importlib.import_module("." + type, __name__)

src/engine/SCons/compat/__init__.py Outdated Show resolved Hide resolved
src/engine/SCons/Platform/__init__.py Outdated Show resolved Hide resolved
@mcepl
Copy link
Author

mcepl commented Mar 13, 2019

Rebased on the top of the current master (387e1f3) and included ideas from the resolved reviews here.

@mwichmann
Copy link
Collaborator

My suggestions seem to have broken everything... odd because I thought I was running with the same things here and having it work.

@mcepl
Copy link
Author

mcepl commented Mar 13, 2019

@mwichmann I really have problem here. I can do Python work pretty well (I am the lead Python maintainer for SUSE), but SCons seems to me to be so complicated I am really lost. If anybody who knows her way around SCons internals wants to take over this PR and lead it to some useable end, I would be more than happy.

@mwichmann
Copy link
Collaborator

I'll do some work on it. I can't really disagree with you, it's starting to feel comfortable for me in many parts now (and others remain complete mysteries), and that's after about two years of fiddling with it on and off. I don't want to offend Bill and the original developers, but it is too complex, sigh.

mwichmann added a commit to mwichmann/scons that referenced this pull request Mar 14, 2019
Several locations with simple usage of deprecated "imp" module
changed to use "importlib". These match with work in SCons#3159,
but this is not a complete implementation of SCons#3159.

More regex patterns are changed to be raw strings.

Some strings which did not seem appropriate to change to raw
strings (e.g. contain embedded tabs, which Python should honor)
had backslashes escaped to avoid accidental Python interpretation.
Example:
   '\t<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.targets" />\n'
Python 3.8 was Warning \M was an unknown escape.

More open().write(), open().read() style usage changed to use
context managers so the file is closed.

WIP part: even with Python 3.7, the tests which call sconsign.py
fail; oddly they do not fail without the patch to compat.py.
sconsign.py does an import using imp module (which is what
generates the errors) so needs to be updated anyway.  It does not
quite fit the "simple usage" pattern - can't do a simple relative
import since sconsign is normally located elsewhere in the tree than
the main scons code body.

With this version of the patch, 700 tests now pass with 3.8, and
Warning messages reduced to 2800 (current master has 200 pass,
9000 warns)

Signed-off-by: Mats Wichmann <mats@linux.com>
@mwichmann mwichmann mentioned this pull request Mar 14, 2019
3 tasks
mwichmann added a commit to mwichmann/scons that referenced this pull request Mar 30, 2019
Several locations with simple usage of deprecated "imp" module
changed to use "importlib". These match with work in SCons#3159,
but this is not a complete implementation of SCons#3159.

More regex patterns are changed to be raw strings.

Some strings which did not seem appropriate to change to raw
strings (e.g. contain embedded tabs, which Python should honor)
had backslashes escaped to avoid accidental Python interpretation.
Example:
   '\t<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.targets" />\n'
Python 3.8 was Warning \M was an unknown escape.

More open().write(), open().read() style usage changed to use
context managers so the file is closed.

WIP part: even with Python 3.7, the tests which call sconsign.py
fail; oddly they do not fail without the patch to compat.py.
sconsign.py does an import using imp module (which is what
generates the errors) so needs to be updated anyway.  It does not
quite fit the "simple usage" pattern - can't do a simple relative
import since sconsign is normally located elsewhere in the tree than
the main scons code body.

With this version of the patch, 700 tests now pass with 3.8, and
Warning messages reduced to 2800 (current master has 200 pass,
9000 warns)

Signed-off-by: Mats Wichmann <mats@linux.com>
@mwichmann mwichmann mentioned this pull request Apr 26, 2019
3 tasks
@mwichmann
Copy link
Collaborator

mwichmann commented May 6, 2019

So as an update, some of the imp work has merged. I'm looking at the changes to Main.py now, and they're not passing the tests (python3 runtest.py test/site_scons) so will need some further thought. The error indicates that the code expects it will have found something, but does not, and so ends up accessing through a None object instead.

*** Error loading site_init file './site_scons/site_init.py':
AttributeError: 'NoneType' object has no attribute 'loader':

imp library has been deprecated since 3.4 and in 3.7 it finally breaks
builds.

Presrving compatibility with python >= 2.7.
@bdbaddog
Copy link
Contributor

obsoleted by PR #3552

@bdbaddog bdbaddog closed this Feb 19, 2020
@mcepl mcepl deleted the replace-imp-with-importlib branch February 19, 2020 16:55
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.

3 participants