From 2f4b66afe976071ffb456235b2efc7099d528bbb Mon Sep 17 00:00:00 2001 From: William Deegan Date: Sun, 23 Jan 2022 00:17:48 +0000 Subject: address a number of ResourceWarning: unclosed file <_io.TextIOWrapper name='progress.out' mode='w' encoding='UTF-8'> type warnings which are issued when running tests with python 3.9 --- SCons/Tool/gcc.py | 9 ++++---- SCons/Tool/gxx.py | 24 ++++++++++------------ test/Progress/file.py | 17 ++++++++++----- .../sconstruct_ninja_command_line | 4 +++- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/SCons/Tool/gcc.py b/SCons/Tool/gcc.py index 7e38599..9c29c22 100644 --- a/SCons/Tool/gcc.py +++ b/SCons/Tool/gcc.py @@ -74,14 +74,13 @@ def detect_version(env, cc): # GCC versions older than that, we should use --version and a # regular expression. # pipe = SCons.Action._subproc(env, SCons.Util.CLVar(cc) + ['-dumpversion'], - pipe=SCons.Action._subproc(env, SCons.Util.CLVar(cc) + ['--version'], + with SCons.Action._subproc(env, SCons.Util.CLVar(cc) + ['--version'], stdin='devnull', stderr='devnull', - stdout=subprocess.PIPE) - if pipe.wait() != 0: - return version + stdout=subprocess.PIPE) as pipe: + if pipe.wait() != 0: + return version - with pipe.stdout: # -dumpversion variant: # line = pipe.stdout.read().strip() # --version variant: diff --git a/SCons/Tool/gxx.py b/SCons/Tool/gxx.py index 88186cb..edb45e2 100644 --- a/SCons/Tool/gxx.py +++ b/SCons/Tool/gxx.py @@ -1,15 +1,6 @@ -"""SCons.Tool.g++ - -Tool-specific initialization for g++. - -There normally shouldn't be any need to import this module directly. -It will usually be imported through the generic SCons.Tool.Tool() -selection method. - -""" - +# MIT License # -# __COPYRIGHT__ +# Copyright The SCons Foundation # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -29,9 +20,16 @@ selection method. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -# -__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +"""SCons.Tool.g++ + +Tool-specific initialization for g++. + +There normally shouldn't be any need to import this module directly. +It will usually be imported through the generic SCons.Tool.Tool() +selection method. + +""" import SCons.Tool diff --git a/test/Progress/file.py b/test/Progress/file.py index 3184a5b..391f1b4 100644 --- a/test/Progress/file.py +++ b/test/Progress/file.py @@ -1,6 +1,7 @@ #!/usr/bin/env python +# MIT License # -# __COPYRIGHT__ +# Copyright The SCons Foundation # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -20,9 +21,6 @@ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -# - -__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" """ Verify that the file= argument to Progress() allows us to redirect the @@ -36,9 +34,18 @@ import TestSCons test = TestSCons.TestSCons() test.write('SConstruct', """\ +import atexit env = Environment() env['BUILDERS']['C'] = Builder(action=Copy('$TARGET', '$SOURCE')) -Progress('stderr: $TARGET\\n', file=open('progress.out', 'w')) +fo = open('progress.out', 'w') +Progress('stderr: $TARGET\\n', file=fo) + +# close file at exit +def close_it(): + fo.close() + +atexit.register(close_it) + env.C('S1.out', 'S1.in') env.C('S2.out', 'S2.in') env.C('S3.out', 'S3.in') diff --git a/test/ninja/ninja_test_sconscripts/sconstruct_ninja_command_line b/test/ninja/ninja_test_sconscripts/sconstruct_ninja_command_line index 14ae633..4967912 100644 --- a/test/ninja/ninja_test_sconscripts/sconstruct_ninja_command_line +++ b/test/ninja/ninja_test_sconscripts/sconstruct_ninja_command_line @@ -8,5 +8,7 @@ env = Environment(variables=env_vars) env.VariantDir(env['OTHER_VAR'], 'src') env.Tool('ninja') -env.Textfile('$BUILD/bar2.c', open('src/foo.c').read()) +with open('src/foo.c') as foo: + env.Textfile('$BUILD/bar2.c', foo.read()) + env.Program(target = 'foo', source = '$BUILD/bar2.c', CPPPATH='src') -- cgit v0.12 From de52cc83605dd49eb2f0a72dfb74a948735555bc Mon Sep 17 00:00:00 2001 From: William Deegan Date: Sun, 23 Jan 2022 00:19:25 +0000 Subject: update RELEASE.txt and CHANGES.txt --- CHANGES.txt | 2 ++ RELEASE.txt | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index df23c14..c4245ca 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -20,6 +20,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER NOTE: If you build with Python 3.10.0 and then rebuild with 3.10.1 (or higher), you may see unexpected rebuilds. This is due to Python internals changing which changed the signature of a Python Action Function. + - Fix a number of Python ResourceWarnings which are issued when running SCons and/or it's tests + with python 3.9 (or higher) From Daniel Moody: - Add cache-debug messages for push failures. diff --git a/RELEASE.txt b/RELEASE.txt index ff3c82b..c626179 100755 --- a/RELEASE.txt +++ b/RELEASE.txt @@ -33,8 +33,8 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY FIXES ----- -- Fix check for unsupported Python version. It was broken. Also now the error message - will include what is the minimum supported version of Python +- Fix a number of Python ResourceWarnings which are issued when running SCons and/or it's tests + with python 3.9 (or higher) IMPROVEMENTS ------------ -- cgit v0.12 From 8dff0099875bdc8e9f526c8a3ee9baf3b9fc6a5f Mon Sep 17 00:00:00 2001 From: William Deegan Date: Tue, 1 Feb 2022 19:55:53 +0000 Subject: Fixup DummyPopen() ( which is returned from Action._subproc() when subprocess.POpen() errors) so that it can also be used as a context manager. This is part of fixing Python 3.9(+) outputing warnings when file/other handles aren't properly closed. --- CHANGES.txt | 4 ++++ RELEASE.txt | 2 ++ SCons/Action.py | 6 ++++++ test/Variables/Variables.py | 7 +++---- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index c4245ca..005b0a4 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -22,6 +22,10 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER the signature of a Python Action Function. - Fix a number of Python ResourceWarnings which are issued when running SCons and/or it's tests with python 3.9 (or higher) + - Action._subproc() can now be used as a python context manager to ensure that the + POpen object is properly closed. + (Thanks to Mats Wichmann for catching that DummyPopen needed additional logic) + From Daniel Moody: - Add cache-debug messages for push failures. diff --git a/RELEASE.txt b/RELEASE.txt index c626179..6c26a34 100755 --- a/RELEASE.txt +++ b/RELEASE.txt @@ -28,6 +28,8 @@ CHANGED/ENHANCED EXISTING FUNCTIONALITY location for a site_scons. %AllUsersProfile%\Application Data\scons\site_scons will continue to work. There does not seem to be any convention to use an "Application Data" subdirectory here. +- Action._subproc() can now be used as a python context manager to ensure that the + POpen object is properly closed. FIXES diff --git a/SCons/Action.py b/SCons/Action.py index 67eeb1d..cf17181 100644 --- a/SCons/Action.py +++ b/SCons/Action.py @@ -800,6 +800,12 @@ def _subproc(scons_env, cmd, error='ignore', **kw): # return a dummy Popen instance that only returns error class dummyPopen: def __init__(self, e): self.exception = e + # Add the following two to enable using the return value as a context manager + # for example + # with Action._subproc(...) as po: + # logic here which uses po + def __enter__(self): return self + def __exit__(self, *args): pass def communicate(self, input=None): return ('', '') def wait(self): return -self.exception.errno stdin = None diff --git a/test/Variables/Variables.py b/test/Variables/Variables.py index 09a17d5..bb7b237 100644 --- a/test/Variables/Variables.py +++ b/test/Variables/Variables.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -# __COPYRIGHT__ +# MIT License +# +# Copyright The SCons Foundation # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -20,9 +22,6 @@ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -# - -__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" import TestSCons -- cgit v0.12 From 9849b3e48a9036a71a5d1319617043e9f2dabe46 Mon Sep 17 00:00:00 2001 From: William Deegan Date: Tue, 1 Feb 2022 21:33:33 +0000 Subject: Fix sider complaints about single line methods --- SCons/Action.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/SCons/Action.py b/SCons/Action.py index cf17181..e3dfbfd 100644 --- a/SCons/Action.py +++ b/SCons/Action.py @@ -804,10 +804,14 @@ def _subproc(scons_env, cmd, error='ignore', **kw): # for example # with Action._subproc(...) as po: # logic here which uses po - def __enter__(self): return self - def __exit__(self, *args): pass - def communicate(self, input=None): return ('', '') - def wait(self): return -self.exception.errno + def __enter__(self): + return self + def __exit__(self, *args): + pass + def communicate(self, input=None): + return ('', '') + def wait(self): + return -self.exception.errno stdin = None class f: def read(self): return '' -- cgit v0.12 From fb1ad41835af49eadfb4652100e2fc1d5bbe105a Mon Sep 17 00:00:00 2001 From: William Deegan Date: Tue, 1 Feb 2022 21:59:25 +0000 Subject: Fix sider complaints about single line methods --- SCons/Action.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/SCons/Action.py b/SCons/Action.py index e3dfbfd..b7c6bb7 100644 --- a/SCons/Action.py +++ b/SCons/Action.py @@ -799,19 +799,25 @@ def _subproc(scons_env, cmd, error='ignore', **kw): if error == 'raise': raise # return a dummy Popen instance that only returns error class dummyPopen: - def __init__(self, e): self.exception = e + def __init__(self, e): + self.exception = e # Add the following two to enable using the return value as a context manager # for example # with Action._subproc(...) as po: # logic here which uses po + def __enter__(self): return self + def __exit__(self, *args): pass + def communicate(self, input=None): return ('', '') + def wait(self): return -self.exception.errno + stdin = None class f: def read(self): return '' -- cgit v0.12