diff --git a/bindings/python/ns__init__.py b/bindings/python/ns__init__.py index f0f3c5d20..6738d0d58 100644 --- a/bindings/python/ns__init__.py +++ b/bindings/python/ns__init__.py @@ -155,7 +155,7 @@ def extract_linked_libraries(library_name: str, prefix: str) -> tuple: linked_libs = [] # First discover which 3rd-party libraries are used by the current module try: - with open(os.path.abspath(library_path), "rb") as f: + with open(os.path.abspath(library_path), "rb", encoding="utf-8") as f: linked_libs = re.findall(b"\x00(lib.*?.%b)" % LIBRARY_EXTENSION.encode("utf-8"), f.read()) except Exception as e: print("Failed to extract libraries used by {library} with exception:{exception}" diff --git a/doc/contributing/pickle-to-xml.py b/doc/contributing/pickle-to-xml.py index 1f557370f..bf055d996 100755 --- a/doc/contributing/pickle-to-xml.py +++ b/doc/contributing/pickle-to-xml.py @@ -12,7 +12,7 @@ import os import codecs def dump_pickles(out, dirname, filename, path): - f = open(os.path.join(dirname, filename), 'r') + f = open(os.path.join(dirname, filename), 'r', encoding='utf-8') data = pickle.load(f) fragment_file = codecs.open(data['current_page_name'] + '.frag', mode='w', encoding='utf-8') fragment_file.write(data['body']) diff --git a/doc/tutorial/pickle-to-xml.py b/doc/tutorial/pickle-to-xml.py index 1f557370f..bf055d996 100755 --- a/doc/tutorial/pickle-to-xml.py +++ b/doc/tutorial/pickle-to-xml.py @@ -12,7 +12,7 @@ import os import codecs def dump_pickles(out, dirname, filename, path): - f = open(os.path.join(dirname, filename), 'r') + f = open(os.path.join(dirname, filename), 'r', encoding='utf-8') data = pickle.load(f) fragment_file = codecs.open(data['current_page_name'] + '.frag', mode='w', encoding='utf-8') fragment_file.write(data['body']) diff --git a/src/flow-monitor/examples/flowmon-parse-results.py b/src/flow-monitor/examples/flowmon-parse-results.py index bbc3c9f8b..eebc032fe 100644 --- a/src/flow-monitor/examples/flowmon-parse-results.py +++ b/src/flow-monitor/examples/flowmon-parse-results.py @@ -179,7 +179,7 @@ class Simulation(object): def main(argv): - file_obj = open(argv[1]) + file_obj = open(argv[1], encoding="utf-8") print("Reading XML file ", end=" ") sys.stdout.flush() diff --git a/src/spectrum/utils/two-ray-to-three-gpp-ch-calibration.py b/src/spectrum/utils/two-ray-to-three-gpp-ch-calibration.py index a48f02c81..55c60d002 100644 --- a/src/spectrum/utils/two-ray-to-three-gpp-ch-calibration.py +++ b/src/spectrum/utils/two-ray-to-three-gpp-ch-calibration.py @@ -438,7 +438,7 @@ def print_cplusplus_map_from_fit_results(fit: pd.DataFrame, out_fname: str): out_str = out_str[0:-2] out_str += '}\n' - f = open(out_fname, "w") + f = open(out_fname, "w", encoding="utf-8") f.write(out_str) f.close() @@ -499,7 +499,7 @@ if __name__ == '__main__': res = joblib.Parallel(n_jobs=10)( joblib.delayed(fit_ftr_to_reference)(df, params_comb, num_search_grid_params, num_refinements) for params_comb in product(scenarios, is_los, frequencies)) - f = open(fit_out_fname, "w") + f = open(fit_out_fname, "w", encoding="utf-8") f.write("scen\tcond\tfc\tsigma\tk\tdelta\tm\n") for line in res: f.write(line) diff --git a/src/wifi/examples/reference/bianchi11ax.py b/src/wifi/examples/reference/bianchi11ax.py index 43dbff0de..bd758f06b 100644 --- a/src/wifi/examples/reference/bianchi11ax.py +++ b/src/wifi/examples/reference/bianchi11ax.py @@ -129,7 +129,7 @@ ack_rates_160MHz = [6e6, 12e6, 12e6, 24e6, 24e6, 24e6, 24e6, 24e6, 24e6, 24e6, 2 k = 1 difs = 1 -fo = open("bianchi_11ax_difs.txt", "w") +fo = open("bianchi_11ax_difs.txt", "w", encoding="utf-8") for i in range(len(data_rates_20MHz)): bianchi_result = bianchi_ax(data_rates_20MHz[i], ack_rates_20MHz[i], k, difs) str_s = str_result(bianchi_result, i, 20) @@ -149,7 +149,7 @@ for i in range(len(data_rates_160MHz)): fo.close() difs = 0 -fo = open("bianchi_11ax_eifs.txt", "w") +fo = open("bianchi_11ax_eifs.txt", "w", encoding="utf-8") for i in range(len(data_rates_20MHz)): bianchi_result = bianchi_ax(data_rates_20MHz[i], ack_rates_20MHz[i], k, difs) str_s = str_result(bianchi_result, i, 20) diff --git a/test.py b/test.py index 7ba7097df..793dd1b73 100755 --- a/test.py +++ b/test.py @@ -277,7 +277,7 @@ def translate_to_text(results_file, text_file): import xml.etree.ElementTree as ET et = ET.parse(results_file) - with open(text_file, 'w') as f: + with open(text_file, 'w', encoding='utf-8') as f: for test in et.findall('Test'): node_to_text(test, f) @@ -303,7 +303,7 @@ def translate_to_html(results_file, html_file): html_file += '.html' print('Writing results to html file %s...' % html_file, end='') - with open(html_file, 'w') as f: + with open(html_file, 'w', encoding='utf-8') as f: f.write("\n") f.write("\n") f.write("

ns-3 Test Results

\n") @@ -596,7 +596,7 @@ def read_ns3_config(): f = None try: # sys.platform reports linux2 for python2 and linux for python3 - f = open(lock_filename, "rt") + f = open(lock_filename, "rt", encoding="utf-8") except FileNotFoundError: print('The .lock-ns3 file was not found. You must configure before running test.py.', file=sys.stderr) sys.exit(2) @@ -616,7 +616,7 @@ def read_ns3_config(): global NS3_BUILDDIR NS3_BUILDDIR = out_dir - with open(lock_filename) as f: + with open(lock_filename, encoding='utf-8') as f: for line in f.readlines(): for item in interesting_config_items: if line.startswith(item): @@ -1296,7 +1296,7 @@ def run_tests(): # do this since the tests will just append individual results to this file. # xml_results_file = os.path.join(testpy_output_dir, "results.xml") - with open(xml_results_file, 'w') as f: + with open(xml_results_file, 'w', encoding='utf-8') as f: f.write('\n') f.write('\n') @@ -1768,7 +1768,7 @@ def run_tests(): # XXX We could add some timing information to the examples, i.e. run # them through time and print the results here. # - with open(xml_results_file, 'a') as f: + with open(xml_results_file, 'a', encoding='utf-8') as f: f.write('\n') example_name = " %s\n" % job.display_name f.write(example_name) @@ -1833,7 +1833,7 @@ def run_tests(): # followed by a VALGR failing test suite of the same name. # if job.is_skip: - with open(xml_results_file, 'a') as f: + with open(xml_results_file, 'a', encoding='utf-8') as f: f.write("\n") f.write(" %s\n" % job.display_name) f.write(' SKIP\n') @@ -1841,10 +1841,10 @@ def run_tests(): f.write("\n") else: if job.returncode == 0 or job.returncode == 1 or job.returncode == 2: - with open(xml_results_file, 'a') as f_to, open(job.tmp_file_name) as f_from: + with open(xml_results_file, 'a', encoding='utf-8') as f_to, open(job.tmp_file_name, encoding='utf-8') as f_from: f_to.write(f_from.read()) else: - with open(xml_results_file, 'a') as f: + with open(xml_results_file, 'a', encoding='utf-8') as f: f.write("\n") f.write(" %s\n" % job.display_name) f.write(' CRASH\n') @@ -1864,7 +1864,7 @@ def run_tests(): # individual pieces. So, we need to finish off and close out the XML # document # - with open(xml_results_file, 'a') as f: + with open(xml_results_file, 'a', encoding='utf-8') as f: f.write('\n') # diff --git a/utils.py b/utils.py index 134bb234c..2be96c1d3 100644 --- a/utils.py +++ b/utils.py @@ -20,7 +20,7 @@ def get_list_from_file(file_path, list_name): # Read in the file if it exists. if os.path.exists(file_path): - with open(file_path, "r") as file_in: + with open(file_path, "r", encoding="utf-8") as file_in: # Look for the list. list_string = "" @@ -58,7 +58,7 @@ def get_bool_from_file(file_path, bool_name, value_if_missing): # Read in the file if it exists. if os.path.exists(file_path): - with open(file_path, "r") as file_in: + with open(file_path, "r", encoding="utf-8") as file_in: # Look for the boolean variable. bool_found = False diff --git a/utils/create-module.py b/utils/create-module.py index a3881d4cd..8aff7f1be 100755 --- a/utils/create-module.py +++ b/utils/create-module.py @@ -336,7 +336,7 @@ def create_file(path, template, **kwargs): artifact_path = Path(path) #open file for (w)rite and in (t)ext mode - with artifact_path.open("wt") as f: + with artifact_path.open("wt", encoding="utf-8") as f: f.write(template.format(**kwargs)) diff --git a/utils/grid.py b/utils/grid.py index 3303b69cc..e8293af8c 100644 --- a/utils/grid.py +++ b/utils/grid.py @@ -1608,7 +1608,7 @@ def read_data(filename): m3 = re.compile('event-int ([^ ]+) ([^ ]+) ([0-9]+) ([0-9]+)') m4 = re.compile('color ([^ ]+) #([a-fA-F0-9]{2,2})([a-fA-F0-9]{2,2})([a-fA-F0-9]{2,2})') - with open(filename) as fh: + with open(filename, encoding='utf-8') as fh: for line in fh.readlines(): m = m1.match(line) if m: diff --git a/utils/tests/TestBase.py b/utils/tests/TestBase.py index 58cfa4877..b23aaddbc 100644 --- a/utils/tests/TestBase.py +++ b/utils/tests/TestBase.py @@ -121,9 +121,9 @@ class TestBaseClass: passed = 0 progress = 0.0 failed_cases = [] - with open(self.options.out_file, 'w') as out: + with open(self.options.out_file, 'w', encoding='utf-8') as out: outstream = out - with open(os.devnull, 'w') as sink: + with open(os.devnull, 'w', encoding='utf-8') as sink: if self.options.mute: outstream = sink for cmd in cmds: diff --git a/utils/tests/test-ns3.py b/utils/tests/test-ns3.py index 34c55528d..27880a240 100755 --- a/utils/tests/test-ns3.py +++ b/utils/tests/test-ns3.py @@ -126,7 +126,7 @@ def get_programs_list(): @return list of programs. """ values = {} - with open(ns3_lock_filename) as f: + with open(ns3_lock_filename, encoding="utf-8") as f: exec(f.read(), globals(), values) programs_list = values["ns3_runnable_programs"] @@ -163,7 +163,7 @@ def read_lock_entry(entry): @return value of the requested entry. """ values = {} - with open(ns3_lock_filename) as f: + with open(ns3_lock_filename, encoding="utf-8") as f: exec(f.read(), globals(), values) return values.get(entry, None) @@ -206,7 +206,7 @@ class DockerContainerManager: currentTestCase.skipTest("python-on-whales was not found") # Import rootless docker settings from .bashrc - with open(os.path.expanduser("~/.bashrc"), "r") as f: + with open(os.path.expanduser("~/.bashrc"), "r", encoding="utf-8") as f: docker_settings = re.findall("(DOCKER_.*=.*)", f.read()) for setting in docker_settings: key, value = setting.split("=") @@ -285,7 +285,7 @@ class NS3UnusedSourcesTestCase(unittest.TestCase): continue # Open the examples CMakeLists.txt and read it - with open(os.path.join(example_directory, "CMakeLists.txt"), "r") as f: + with open(os.path.join(example_directory, "CMakeLists.txt"), "r", encoding="utf-8") as f: cmake_contents = f.read() # For each file, check if it is in the CMake contents @@ -319,7 +319,7 @@ class NS3UnusedSourcesTestCase(unittest.TestCase): cmake_path = os.path.join(parent_directory, os.path.basename(cmake_path)) # Open the module CMakeLists.txt and read it - with open(cmake_path, "r") as f: + with open(cmake_path, "r", encoding="utf-8") as f: cmake_contents = f.read() # For each file, check if it is in the CMake contents @@ -358,7 +358,7 @@ class NS3UnusedSourcesTestCase(unittest.TestCase): cmake_path = os.path.join(parent_directory, os.path.basename(cmake_path)) # Open the module CMakeLists.txt and read it - with open(cmake_path, "r") as f: + with open(cmake_path, "r", encoding="utf-8") as f: cmake_contents = f.read() # For each file, check if it is in the CMake contents @@ -389,7 +389,7 @@ class NS3DependenciesTestCase(unittest.TestCase): for path in module_paths: # Open the module CMakeLists.txt and read it cmake_path = os.path.join(path, "CMakeLists.txt") - with open(cmake_path, "r") as f: + with open(cmake_path, "r", encoding="utf-8") as f: cmake_contents = f.readlines() module_name = os.path.relpath(path, ns3_path) @@ -957,7 +957,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): ns3rc_template = ns3rc_str(ns3rc_type) # Now we repeat the command line tests but with the ns3rc file. - with open(ns3rc_script, "w") as f: + with open(ns3rc_script, "w", encoding="utf-8") as f: f.write(ns3rc_template.format(modules="'lte'", examples="False", tests="True")) # Reconfigure. @@ -972,7 +972,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): self.assertLessEqual(len(get_programs_list()), len(self.ns3_executables)) # Replace the ns3rc file with the wifi module, enabling examples and disabling tests - with open(ns3rc_script, "w") as f: + with open(ns3rc_script, "w", encoding="utf-8") as f: f.write(ns3rc_template.format(modules="'wifi'", examples="True", tests="False")) # Reconfigure @@ -987,7 +987,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): self.assertGreater(len(get_programs_list()), len(self.ns3_executables)) # Replace the ns3rc file with multiple modules - with open(ns3rc_script, "w") as f: + with open(ns3rc_script, "w", encoding="utf-8") as f: f.write(ns3rc_template.format(modules="'core','network'", examples="True", tests="False")) # Reconfigure @@ -1004,7 +1004,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # Replace the ns3rc file with multiple modules, # in various different ways and with comments - with open(ns3rc_script, "w") as f: + with open(ns3rc_script, "w", encoding="utf-8") as f: if ns3rc_type == "python": f.write(ns3rc_template.format(modules="""'core', #comment 'lte', @@ -1126,7 +1126,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # Cause a sigsegv sigsegv_example = os.path.join(ns3_path, "scratch", "sigsegv.cc") - with open(sigsegv_example, "w") as f: + with open(sigsegv_example, "w", encoding="utf-8") as f: f.write(""" int main (int argc, char *argv[]) { @@ -1144,7 +1144,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # Cause an abort abort_example = os.path.join(ns3_path, "scratch", "abort.cc") - with open(abort_example, "w") as f: + with open(abort_example, "w", encoding="utf-8") as f: f.write(""" #include "ns3/core-module.h" @@ -1217,7 +1217,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): for path in test_files + backup_files: filepath = os.path.join(ns3_path, path) os.makedirs(os.path.dirname(filepath), exist_ok=True) - with open(filepath, "w") as f: + with open(filepath, "w", encoding="utf-8") as f: if "main" in path: f.write("int main (int argc, char *argv[]){}") else: @@ -1358,10 +1358,10 @@ class NS3ConfigureTestCase(NS3BaseTestCase): os.makedirs("contrib/borked/examples", exist_ok=True) # Test if configuration succeeds and building the module library fails - with open("contrib/borked/examples/CMakeLists.txt", "w") as f: + with open("contrib/borked/examples/CMakeLists.txt", "w", encoding="utf-8") as f: f.write("") for invalid_or_nonexistent_library in ["", "gsd", "lib", "libfi", "calibre"]: - with open("contrib/borked/CMakeLists.txt", "w") as f: + with open("contrib/borked/CMakeLists.txt", "w", encoding="utf-8") as f: f.write(""" build_lib( LIBNAME borked @@ -1400,7 +1400,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # - no additional library (should work) # - invalid library names (should fail to configure) # - valid library names but nonexistent libraries (should not create a target) - with open("contrib/borked/CMakeLists.txt", "w") as f: + with open("contrib/borked/CMakeLists.txt", "w", encoding="utf-8") as f: f.write(""" build_lib( LIBNAME borked @@ -1409,7 +1409,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): ) """) for invalid_or_nonexistent_library in ["", "gsd", "lib", "libfi", "calibre"]: - with open("contrib/borked/examples/CMakeLists.txt", "w") as f: + with open("contrib/borked/examples/CMakeLists.txt", "w", encoding="utf-8") as f: f.write(""" build_lib_example( NAME borked-example @@ -1452,9 +1452,9 @@ class NS3ConfigureTestCase(NS3BaseTestCase): os.makedirs("contrib/calibre/examples", exist_ok=True) # Now test if we can have a library with "lib" in it - with open("contrib/calibre/examples/CMakeLists.txt", "w") as f: + with open("contrib/calibre/examples/CMakeLists.txt", "w", encoding="utf-8") as f: f.write("") - with open("contrib/calibre/CMakeLists.txt", "w") as f: + with open("contrib/calibre/CMakeLists.txt", "w", encoding="utf-8") as f: f.write(""" build_lib( LIBNAME calibre @@ -1539,7 +1539,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): "VERSION_TAG_DISTANCE = '0'\n" "VERSION_BUILD_PROFILE = 'debug'\n" ) - with open(version_cache_file, "w") as version: + with open(version_cache_file, "w", encoding="utf-8") as version: version.write(version_cache_contents) # Configuration should now succeed @@ -1549,7 +1549,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja"))) # And contents of version cache should be unchanged - with open(version_cache_file, "r") as version: + with open(version_cache_file, "r", encoding="utf-8") as version: self.assertEqual(version.read(), version_cache_contents) # Third case: we rename the .git directory temporarily and reconfigure @@ -1571,7 +1571,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): container.execute("./ns3 configure -G Ninja --enable-build-version") container.execute("./ns3 build core") self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja"))) - with open(version_cache_file, "r") as version: + with open(version_cache_file, "r", encoding="utf-8") as version: self.assertNotEqual(version.read(), version_cache_contents) # Remove version cache file if it exists @@ -1638,7 +1638,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # Check if configuration properly detected lld self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja"))) - with open(os.path.join(ns3_path, "cmake-cache", "build.ninja"), "r") as f: + with open(os.path.join(ns3_path, "cmake-cache", "build.ninja"), "r", encoding="utf-8") as f: self.assertIn("-fuse-ld=lld", f.read()) # Try to build using the lld linker @@ -1659,7 +1659,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # Check if configuration properly detected mold self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja"))) - with open(os.path.join(ns3_path, "cmake-cache", "build.ninja"), "r") as f: + with open(os.path.join(ns3_path, "cmake-cache", "build.ninja"), "r", encoding="utf-8") as f: self.assertIn("-fuse-ld=mold", f.read()) # Try to build using the lld linker @@ -1676,7 +1676,7 @@ class NS3ConfigureTestCase(NS3BaseTestCase): # Check if configuration properly disabled lld/mold usage self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja"))) - with open(os.path.join(ns3_path, "cmake-cache", "build.ninja"), "r") as f: + with open(os.path.join(ns3_path, "cmake-cache", "build.ninja"), "r", encoding="utf-8") as f: self.assertNotIn("-fuse-ld=mold", f.read()) def test_21_ClangTimeTrace(self): @@ -1958,7 +1958,7 @@ class NS3BuildBaseTestCase(NS3BaseTestCase): self.ns3_libraries = get_libraries_list() version_file = os.sep.join([ns3_path, "VERSION"]) - with open(version_file, "w") as f: + with open(version_file, "w", encoding="utf-8") as f: f.write("3-00\n") # Reconfigure. @@ -1990,7 +1990,7 @@ class NS3BuildBaseTestCase(NS3BaseTestCase): self.assertTrue(os.path.exists(library)) # Restore version file. - with open(version_file, "w") as f: + with open(version_file, "w", encoding="utf-8") as f: f.write("3-dev\n") def test_07_OutputDirectory(self): @@ -2077,7 +2077,7 @@ class NS3BuildBaseTestCase(NS3BaseTestCase): # 3-dev version format is not supported by CMake, so we use 3.01. version_file = os.sep.join([ns3_path, "VERSION"]) - with open(version_file, "w") as f: + with open(version_file, "w", encoding="utf-8") as f: f.write("3-01\n") # Reconfigure setting the installation folder to ns-3-dev/build/install. @@ -2113,7 +2113,7 @@ class NS3BuildBaseTestCase(NS3BaseTestCase): # Now create a test CMake project and try to find_package ns-3. test_main_file = os.sep.join([install_prefix, "main.cpp"]) - with open(test_main_file, "w") as f: + with open(test_main_file, "w", encoding="utf-8") as f: f.write(""" #include using namespace ns3; @@ -2163,7 +2163,7 @@ class NS3BuildBaseTestCase(NS3BaseTestCase): """ + import_method test_cmake_project_file = os.sep.join([install_prefix, "CMakeLists.txt"]) - with open(test_cmake_project_file, "w") as f: + with open(test_cmake_project_file, "w", encoding="utf-8") as f: f.write(test_cmake_project) # Configure the test project @@ -2215,7 +2215,7 @@ class NS3BuildBaseTestCase(NS3BaseTestCase): # Restore 3-dev version file os.remove(version_file) - with open(version_file, "w") as f: + with open(version_file, "w", encoding="utf-8") as f: f.write("3-dev\n") def test_09_Scratches(self): @@ -2999,7 +2999,7 @@ class NS3QualityControlTestCase(unittest.TestCase): continue try: - with open(filepath, "r") as f: + with open(filepath, "r", encoding="utf-8") as f: matches = regex.findall(f.read()) # Get first group for each match (containing the URL)