From d1b491621af66784fe63f66436b337405f432970 Mon Sep 17 00:00:00 2001 From: dbroemmel Date: Tue, 4 Jul 2023 16:56:41 +0200 Subject: [PATCH 1/2] Ugly fix for #153 This is a sort of brute-force workaround to avoid #153. Likely not up to the standard of `fprettify` but 'works-for-me'. Test amended to cover the corner case, I think. --- fprettify/__init__.py | 26 ++++++++++++++++++++++++-- fprettify/tests/__init__.py | 6 +++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/fprettify/__init__.py b/fprettify/__init__.py index d6450a3..6a07edf 100644 --- a/fprettify/__init__.py +++ b/fprettify/__init__.py @@ -899,6 +899,8 @@ def replace_relational_single_fline(f_line, cstyle): .ne. <--> /= """ + # keep track of whether the line may be affected + line_affected = False new_line = f_line # only act on lines that do contain a relation @@ -942,8 +944,25 @@ def replace_relational_single_fline(f_line, cstyle): line_parts[pos] = part new_line = ''.join(line_parts) + line_affected = True - return new_line + return new_line, line_affected + + +def replace_relational_lines(lines, cstyle): + """ + format a list of lines - replaces scalar relational + operators in logical expressions to either Fortran or C-style + by calling original function for single line. + """ + + new_lines = [] + + for f_line in lines: + new_line, changed = replace_relational_single_fline(f_line, cstyle) + new_lines.append(new_line) + + return new_lines def replace_keywords_single_fline(f_line, case_dict): @@ -1559,7 +1578,10 @@ def reformat_ffile_combined(infile, outfile, impose_indent=True, indent_size=3, f_line = f_line.strip(' ') if impose_replacements: - f_line = replace_relational_single_fline(f_line, cstyle) + f_line, relation_found = replace_relational_single_fline(f_line, cstyle) + if relation_found: + updated_lines = replace_relational_lines(lines, cstyle) + linebreak_pos = get_linebreak_pos(updated_lines, filter_fypp=not indent_fypp) if impose_case: f_line = replace_keywords_single_fline(f_line, case_dict) diff --git a/fprettify/tests/__init__.py b/fprettify/tests/__init__.py index 5980930..6195cbb 100644 --- a/fprettify/tests/__init__.py +++ b/fprettify/tests/__init__.py @@ -381,7 +381,7 @@ def test_relation_replacement(self): "'==== heading", "if (vtk%my_rank .eq. 0) write (vtk%filehandle_par, '(\"\",", - "if (abc(1) .lt. -bca .or. &\n qwe .gt. ewq) then"] + "if (abc(1)<=-bca .or. &\n qwe .gt. ewq) then"] f_outstring = ["if (min .lt. max .and. min .lt. thres)", "if (min .gt. max .and. min .gt. thres)", "if (min .eq. max .and. min .eq. thres)", @@ -391,7 +391,7 @@ def test_relation_replacement(self): "'==== heading", "if (vtk%my_rank .eq. 0) write (vtk%filehandle_par, '(\"\",", - "if (abc(1) .lt. -bca .or. &\n qwe .gt. ewq) then"] + "if (abc(1) .le. -bca .or. &\n qwe .gt. ewq) then"] c_outstring = ["if (min < max .and. min < thres)", "if (min > max .and. min > thres)", "if (min == max .and. min == thres)", @@ -401,7 +401,7 @@ def test_relation_replacement(self): "'==== heading", "if (vtk%my_rank == 0) write (vtk%filehandle_par, '(\"\",", - "if (abc(1) < -bca .or. &\n qwe > ewq) then"] + "if (abc(1) <= -bca .or. &\n qwe > ewq) then"] for i in range(0, len(instring)): self.assert_fprettify_result(['--enable-replacements', '--c-relations'], instring[i], c_outstring[i]) self.assert_fprettify_result(['--enable-replacements'], instring[i], f_outstring[i]) From 727f27d528e3b486d5beb7627e84d5708774d425 Mon Sep 17 00:00:00 2001 From: dbroemmel Date: Tue, 2 Sep 2025 15:42:36 +0200 Subject: [PATCH 2/2] Implement nicer fix Implement nicer fix from #168 and switch to previous test framework for older Python versions. --- .github/workflows/release.yml | 4 ++-- .github/workflows/test.yml | 20 ++++++++++++-------- fprettify/__init__.py | 31 +++++-------------------------- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8c8214b..90cfe2c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,10 +10,10 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: "3.x" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 212432e..46ce119 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,11 +12,11 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Create resource cache id: cache - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ./fortran_tests/before/*/ key: resources-${{ github.event_name }} @@ -39,19 +39,19 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - python: ["3.7", "3.8", "3.9", "3.10", "3.11-dev"] + python: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Load resources - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ./fortran_tests/before/*/ key: resources-${{ github.event_name }} - - uses: actions/setup-python@v4 + - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} @@ -60,7 +60,11 @@ jobs: - name: Run tests run: | - coverage run --source=fprettify setup.py test + if [ "$python" == "3.8" ] || [ "$python" == "3.9" ] || [ "$python" == "3.10" ] ; then + coverage run --source=fprettify setup.py test + else + coverage run --source=fprettify -m unittest ./run_tests.py + fi - name: Coverage upload run: coveralls --service=github @@ -75,7 +79,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/setup-python@v4 + - uses: actions/setup-python@v5 with: python-version: "3.x" diff --git a/fprettify/__init__.py b/fprettify/__init__.py index 6a07edf..90bf0f7 100644 --- a/fprettify/__init__.py +++ b/fprettify/__init__.py @@ -899,8 +899,6 @@ def replace_relational_single_fline(f_line, cstyle): .ne. <--> /= """ - # keep track of whether the line may be affected - line_affected = False new_line = f_line # only act on lines that do contain a relation @@ -944,25 +942,8 @@ def replace_relational_single_fline(f_line, cstyle): line_parts[pos] = part new_line = ''.join(line_parts) - line_affected = True - return new_line, line_affected - - -def replace_relational_lines(lines, cstyle): - """ - format a list of lines - replaces scalar relational - operators in logical expressions to either Fortran or C-style - by calling original function for single line. - """ - - new_lines = [] - - for f_line in lines: - new_line, changed = replace_relational_single_fline(f_line, cstyle) - new_lines.append(new_line) - - return new_lines + return new_line def replace_keywords_single_fline(f_line, case_dict): @@ -1573,19 +1554,17 @@ def reformat_ffile_combined(infile, outfile, impose_indent=True, indent_size=3, lines, pre_ampersand, ampersand_sep = remove_pre_ampersands( lines, is_special, orig_filename, stream.line_nr) - linebreak_pos = get_linebreak_pos(lines, filter_fypp=not indent_fypp) - f_line = f_line.strip(' ') if impose_replacements: - f_line, relation_found = replace_relational_single_fline(f_line, cstyle) - if relation_found: - updated_lines = replace_relational_lines(lines, cstyle) - linebreak_pos = get_linebreak_pos(updated_lines, filter_fypp=not indent_fypp) + f_line = replace_relational_single_fline(f_line, cstyle) + lines = [replace_relational_single_fline(l, cstyle) for l in lines] if impose_case: f_line = replace_keywords_single_fline(f_line, case_dict) + linebreak_pos = get_linebreak_pos(lines, filter_fypp=not indent_fypp) + if impose_whitespace: lines = format_single_fline( f_line, whitespace, whitespace_dict, linebreak_pos, ampersand_sep,