From 0132b0f75f32aab59c4d3804b88bb6d8fd7f8e7d Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:02:55 +0200 Subject: [PATCH 01/31] exception for undefined new block in mcnp input --- montepy/exceptions.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/montepy/exceptions.py b/montepy/exceptions.py index 1426f3d6..f2b38c2d 100644 --- a/montepy/exceptions.py +++ b/montepy/exceptions.py @@ -9,6 +9,11 @@ class LineOverRunWarning(UserWarning): def __init__(self, message): self.message = message +class UndefinedBlock(UserWarning): + """Raised when additional blocks exist after the default data block.""" + + def __init__(self, message): + self.message = message class MalformedInputError(ValueError): """Raised when there is an error with the MCNP input not related to the parser.""" From f3561a5e55e9838bf45bd7ad04d06a883b43c932 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:19:54 +0200 Subject: [PATCH 02/31] added block length counter to calculate the line of the extra block start. Raising warning only once for the excessive block --- montepy/input_parser/input_syntax_reader.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 307bd8ea..a6869441 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -143,15 +143,25 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False): continue_input = False has_non_comments = False input_raw_lines = [] + block_length = -1 def flush_block(): nonlocal block_counter, block_type - if len(input_raw_lines) > 0: + if len(input_raw_lines) > 0: + # warn once for extra blocks + if block_counter == 3: + warnings.warn( + f"Unexpected input after line {current_file.lineno-block_length}", + UndefinedBlock, + stacklevel=2, + ) + yield from flush_input() block_counter += 1 if block_counter < 3: block_type = BlockType(block_counter) + def flush_input(): nonlocal input_raw_lines start_line = current_file.lineno + 1 - len(input_raw_lines) @@ -230,6 +240,8 @@ def flush_input(): else: continue_input = False has_non_comments = has_non_comments or not line_is_comment + if block_counter==3: + block_length += 1 input_raw_lines.append(line.rstrip()) yield from flush_block() From 6e00363ce915696f3e1499cc14670f128841b03f Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:20:11 +0200 Subject: [PATCH 03/31] testing error raising for the extra input --- .../test_pin_cell_extra_block_warning.imcnp | 36 +++++++++++++++++++ tests/test_syntax_parsing.py | 23 ++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/inputs/test_pin_cell_extra_block_warning.imcnp diff --git a/tests/inputs/test_pin_cell_extra_block_warning.imcnp b/tests/inputs/test_pin_cell_extra_block_warning.imcnp new file mode 100644 index 00000000..c4f244b0 --- /dev/null +++ b/tests/inputs/test_pin_cell_extra_block_warning.imcnp @@ -0,0 +1,36 @@ +Made up PWR pin Cell Problem +c uranium rod +1 1 10.0 -1 101 -102 imp:n=1 +c gas gap +2 0 1 -2 101 -102 imp:n=1 +c cladding +3 10 5.0 2 -3 101 -102 imp:n=1 +c water cell +5 2 1.0 2 101 -102 + 103 -104 + 105 -106 imp:n=1 + +1 CZ 0.39 +2 CZ 0.40 +3 CZ 0.46 +101 PZ -0.63 +102 PZ 0.63 +103 PX -0.63 +104 PX 0.63 +105 PY -0.63 +106 PY 0.63 + +M1 92235.80c 0.04 + 92238.80c 0.96 +M2 1001.80c 0.66 + 8016.80c 0.33 +M10 40090.80c 0.515 + 40091.80c 0.112 + 40092.80c 0.171 + 40094.80c 0.174 + 40096.80c 0.028 +mode n +nps 1e3 + +M101 92235.80c 0.04 + 92238.80c 0.96 \ No newline at end of file diff --git a/tests/test_syntax_parsing.py b/tests/test_syntax_parsing.py index ef697600..aab7a186 100644 --- a/tests/test_syntax_parsing.py +++ b/tests/test_syntax_parsing.py @@ -13,6 +13,7 @@ from montepy.input_parser.shortcuts import Shortcuts from montepy.input_parser import syntax_node from montepy.particle import Particle +from montepy.exceptions import UndefinedBlock import warnings @@ -1471,6 +1472,28 @@ def test_jump_and_a_hop(self): assert "J" == jump.upper() str(jump) repr(jump) + def test_extra_block_warning(self): + # Define constants for better readability and maintainability + INPUT_FILE = "tests/inputs/test_pin_cell_extra_block_warning.imcnp" + EXPECTED_LINE = 34 + EXPECTED_WARNING_TYPE = UndefinedBlock + + # Setup: Use the common pattern for defining the generator + generator = input_syntax_reader.read_input_syntax( + MCNP_InputFile(INPUT_FILE) + ) + + # Check that the expected warning is raised + with pytest.warns(EXPECTED_WARNING_TYPE) as recs: + # Exhaust generator so parser runs and warnings are emitted + for _ in generator: + pass + + # Assertions + assert len(recs) == 1, f"Expected exactly one warning, but got {len(recs)}" + warning = recs[0] + assert issubclass(warning.category, EXPECTED_WARNING_TYPE), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" + assert str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}", f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" class TestClassifierNode: From aab3a8ae86ccfbcee1f1f39d70ea6a76319ce6cf Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:30:21 +0200 Subject: [PATCH 04/31] black formatting --- montepy/exceptions.py | 2 ++ montepy/input_parser/input_syntax_reader.py | 7 +++---- tests/test_syntax_parsing.py | 13 ++++++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/montepy/exceptions.py b/montepy/exceptions.py index f2b38c2d..bbb8fa8d 100644 --- a/montepy/exceptions.py +++ b/montepy/exceptions.py @@ -9,12 +9,14 @@ class LineOverRunWarning(UserWarning): def __init__(self, message): self.message = message + class UndefinedBlock(UserWarning): """Raised when additional blocks exist after the default data block.""" def __init__(self, message): self.message = message + class MalformedInputError(ValueError): """Raised when there is an error with the MCNP input not related to the parser.""" diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index a6869441..0edb648e 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -147,7 +147,7 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False): def flush_block(): nonlocal block_counter, block_type - if len(input_raw_lines) > 0: + if len(input_raw_lines) > 0: # warn once for extra blocks if block_counter == 3: warnings.warn( @@ -155,13 +155,12 @@ def flush_block(): UndefinedBlock, stacklevel=2, ) - + yield from flush_input() block_counter += 1 if block_counter < 3: block_type = BlockType(block_counter) - def flush_input(): nonlocal input_raw_lines start_line = current_file.lineno + 1 - len(input_raw_lines) @@ -240,7 +239,7 @@ def flush_input(): else: continue_input = False has_non_comments = has_non_comments or not line_is_comment - if block_counter==3: + if block_counter == 3: block_length += 1 input_raw_lines.append(line.rstrip()) yield from flush_block() diff --git a/tests/test_syntax_parsing.py b/tests/test_syntax_parsing.py index aab7a186..f165bd03 100644 --- a/tests/test_syntax_parsing.py +++ b/tests/test_syntax_parsing.py @@ -1472,6 +1472,7 @@ def test_jump_and_a_hop(self): assert "J" == jump.upper() str(jump) repr(jump) + def test_extra_block_warning(self): # Define constants for better readability and maintainability INPUT_FILE = "tests/inputs/test_pin_cell_extra_block_warning.imcnp" @@ -1479,9 +1480,7 @@ def test_extra_block_warning(self): EXPECTED_WARNING_TYPE = UndefinedBlock # Setup: Use the common pattern for defining the generator - generator = input_syntax_reader.read_input_syntax( - MCNP_InputFile(INPUT_FILE) - ) + generator = input_syntax_reader.read_input_syntax(MCNP_InputFile(INPUT_FILE)) # Check that the expected warning is raised with pytest.warns(EXPECTED_WARNING_TYPE) as recs: @@ -1492,8 +1491,12 @@ def test_extra_block_warning(self): # Assertions assert len(recs) == 1, f"Expected exactly one warning, but got {len(recs)}" warning = recs[0] - assert issubclass(warning.category, EXPECTED_WARNING_TYPE), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" - assert str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}", f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" + assert issubclass( + warning.category, EXPECTED_WARNING_TYPE + ), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" + assert ( + str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}" + ), f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" class TestClassifierNode: From 13b0bcffac79f96dfc196d8235cea5ba244d925a Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:45:06 +0200 Subject: [PATCH 05/31] changelog feuture addition --- doc/source/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index ce701457..9f552c47 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -27,6 +27,7 @@ MontePy Changelog **Features Added** * Added demonstration jupyter notebooks for working with Pin Cell and PWR assemblies in MontePy. +* Added checking for additional input after the ``data`` block, and raising a warning if it exists (:feuture:`525`). **Bugs Fixed** From 5f74b73d96431712eb15c222b8f9997fa5a571ff Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:48:52 +0200 Subject: [PATCH 06/31] changelog addition --- doc/source/changelog.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index ce701457..6d334458 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -7,6 +7,9 @@ MontePy Changelog 1.1.2 -------------- +**Features Added** + +* Added checking for additional input after the ``data`` block, and raising a warning if it exists. **Code Quality** From e6f8f2f998ccea43b32ffe1a6318f917d668f1de Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:02:55 +0200 Subject: [PATCH 07/31] exception for undefined new block in mcnp input --- montepy/exceptions.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/montepy/exceptions.py b/montepy/exceptions.py index 1426f3d6..f2b38c2d 100644 --- a/montepy/exceptions.py +++ b/montepy/exceptions.py @@ -9,6 +9,11 @@ class LineOverRunWarning(UserWarning): def __init__(self, message): self.message = message +class UndefinedBlock(UserWarning): + """Raised when additional blocks exist after the default data block.""" + + def __init__(self, message): + self.message = message class MalformedInputError(ValueError): """Raised when there is an error with the MCNP input not related to the parser.""" From 6b626422b8cfd496efb6a4db97377852d21b6a27 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:19:54 +0200 Subject: [PATCH 08/31] added block length counter to calculate the line of the extra block start. Raising warning only once for the excessive block --- montepy/input_parser/input_syntax_reader.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 9a3bc025..412ae532 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -143,15 +143,25 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False): continue_input = False has_non_comments = False input_raw_lines = [] + block_length = -1 def flush_block(): nonlocal block_counter, block_type - if len(input_raw_lines) > 0: + if len(input_raw_lines) > 0: + # warn once for extra blocks + if block_counter == 3: + warnings.warn( + f"Unexpected input after line {current_file.lineno-block_length}", + UndefinedBlock, + stacklevel=2, + ) + yield from flush_input() block_counter += 1 if block_counter < 3: block_type = BlockType(block_counter) + def flush_input(): nonlocal input_raw_lines start_line = current_file.lineno + 1 - len(input_raw_lines) @@ -232,6 +242,8 @@ def flush_input(): else: continue_input = False has_non_comments = has_non_comments or not line_is_comment + if block_counter==3: + block_length += 1 input_raw_lines.append(line.rstrip()) yield from flush_block() From 18b6723960d1e249dcc83b1d897368d5c81e7a67 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:20:11 +0200 Subject: [PATCH 09/31] testing error raising for the extra input --- .../test_pin_cell_extra_block_warning.imcnp | 36 +++++++++++++++++++ tests/test_syntax_parsing.py | 23 ++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/inputs/test_pin_cell_extra_block_warning.imcnp diff --git a/tests/inputs/test_pin_cell_extra_block_warning.imcnp b/tests/inputs/test_pin_cell_extra_block_warning.imcnp new file mode 100644 index 00000000..c4f244b0 --- /dev/null +++ b/tests/inputs/test_pin_cell_extra_block_warning.imcnp @@ -0,0 +1,36 @@ +Made up PWR pin Cell Problem +c uranium rod +1 1 10.0 -1 101 -102 imp:n=1 +c gas gap +2 0 1 -2 101 -102 imp:n=1 +c cladding +3 10 5.0 2 -3 101 -102 imp:n=1 +c water cell +5 2 1.0 2 101 -102 + 103 -104 + 105 -106 imp:n=1 + +1 CZ 0.39 +2 CZ 0.40 +3 CZ 0.46 +101 PZ -0.63 +102 PZ 0.63 +103 PX -0.63 +104 PX 0.63 +105 PY -0.63 +106 PY 0.63 + +M1 92235.80c 0.04 + 92238.80c 0.96 +M2 1001.80c 0.66 + 8016.80c 0.33 +M10 40090.80c 0.515 + 40091.80c 0.112 + 40092.80c 0.171 + 40094.80c 0.174 + 40096.80c 0.028 +mode n +nps 1e3 + +M101 92235.80c 0.04 + 92238.80c 0.96 \ No newline at end of file diff --git a/tests/test_syntax_parsing.py b/tests/test_syntax_parsing.py index ef697600..aab7a186 100644 --- a/tests/test_syntax_parsing.py +++ b/tests/test_syntax_parsing.py @@ -13,6 +13,7 @@ from montepy.input_parser.shortcuts import Shortcuts from montepy.input_parser import syntax_node from montepy.particle import Particle +from montepy.exceptions import UndefinedBlock import warnings @@ -1471,6 +1472,28 @@ def test_jump_and_a_hop(self): assert "J" == jump.upper() str(jump) repr(jump) + def test_extra_block_warning(self): + # Define constants for better readability and maintainability + INPUT_FILE = "tests/inputs/test_pin_cell_extra_block_warning.imcnp" + EXPECTED_LINE = 34 + EXPECTED_WARNING_TYPE = UndefinedBlock + + # Setup: Use the common pattern for defining the generator + generator = input_syntax_reader.read_input_syntax( + MCNP_InputFile(INPUT_FILE) + ) + + # Check that the expected warning is raised + with pytest.warns(EXPECTED_WARNING_TYPE) as recs: + # Exhaust generator so parser runs and warnings are emitted + for _ in generator: + pass + + # Assertions + assert len(recs) == 1, f"Expected exactly one warning, but got {len(recs)}" + warning = recs[0] + assert issubclass(warning.category, EXPECTED_WARNING_TYPE), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" + assert str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}", f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" class TestClassifierNode: From 9fe1ba8a568bc78d25790d92bcbd068a77b04356 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:30:21 +0200 Subject: [PATCH 10/31] black formatting --- montepy/exceptions.py | 2 ++ montepy/input_parser/input_syntax_reader.py | 7 +++---- tests/test_syntax_parsing.py | 13 ++++++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/montepy/exceptions.py b/montepy/exceptions.py index f2b38c2d..bbb8fa8d 100644 --- a/montepy/exceptions.py +++ b/montepy/exceptions.py @@ -9,12 +9,14 @@ class LineOverRunWarning(UserWarning): def __init__(self, message): self.message = message + class UndefinedBlock(UserWarning): """Raised when additional blocks exist after the default data block.""" def __init__(self, message): self.message = message + class MalformedInputError(ValueError): """Raised when there is an error with the MCNP input not related to the parser.""" diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 412ae532..ec53c127 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -147,7 +147,7 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False): def flush_block(): nonlocal block_counter, block_type - if len(input_raw_lines) > 0: + if len(input_raw_lines) > 0: # warn once for extra blocks if block_counter == 3: warnings.warn( @@ -155,13 +155,12 @@ def flush_block(): UndefinedBlock, stacklevel=2, ) - + yield from flush_input() block_counter += 1 if block_counter < 3: block_type = BlockType(block_counter) - def flush_input(): nonlocal input_raw_lines start_line = current_file.lineno + 1 - len(input_raw_lines) @@ -242,7 +241,7 @@ def flush_input(): else: continue_input = False has_non_comments = has_non_comments or not line_is_comment - if block_counter==3: + if block_counter == 3: block_length += 1 input_raw_lines.append(line.rstrip()) yield from flush_block() diff --git a/tests/test_syntax_parsing.py b/tests/test_syntax_parsing.py index aab7a186..f165bd03 100644 --- a/tests/test_syntax_parsing.py +++ b/tests/test_syntax_parsing.py @@ -1472,6 +1472,7 @@ def test_jump_and_a_hop(self): assert "J" == jump.upper() str(jump) repr(jump) + def test_extra_block_warning(self): # Define constants for better readability and maintainability INPUT_FILE = "tests/inputs/test_pin_cell_extra_block_warning.imcnp" @@ -1479,9 +1480,7 @@ def test_extra_block_warning(self): EXPECTED_WARNING_TYPE = UndefinedBlock # Setup: Use the common pattern for defining the generator - generator = input_syntax_reader.read_input_syntax( - MCNP_InputFile(INPUT_FILE) - ) + generator = input_syntax_reader.read_input_syntax(MCNP_InputFile(INPUT_FILE)) # Check that the expected warning is raised with pytest.warns(EXPECTED_WARNING_TYPE) as recs: @@ -1492,8 +1491,12 @@ def test_extra_block_warning(self): # Assertions assert len(recs) == 1, f"Expected exactly one warning, but got {len(recs)}" warning = recs[0] - assert issubclass(warning.category, EXPECTED_WARNING_TYPE), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" - assert str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}", f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" + assert issubclass( + warning.category, EXPECTED_WARNING_TYPE + ), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" + assert ( + str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}" + ), f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" class TestClassifierNode: From aa5c1fc28d61779faef374da93776213720a9e65 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:48:52 +0200 Subject: [PATCH 11/31] changelog addition --- doc/source/changelog.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index ee140676..6bc600b8 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -19,6 +19,9 @@ MontePy Changelog 1.1.2 -------------- +**Features Added** + +* Added checking for additional input after the ``data`` block, and raising a warning if it exists. **Code Quality** From 7ada57322ebc0bcb8e21e33a7a7550d6677ac6e6 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 19:45:06 +0200 Subject: [PATCH 12/31] changelog feuture addition --- doc/source/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 6bc600b8..14faa818 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -42,6 +42,7 @@ MontePy Changelog **Features Added** * Added demonstration jupyter notebooks for working with Pin Cell and PWR assemblies in MontePy. +* Added checking for additional input after the ``data`` block, and raising a warning if it exists (:feuture:`525`). **Bugs Fixed** From 3ffdd0e093a79f356b694d1ba7c6509c0a933279 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 20:23:34 +0200 Subject: [PATCH 13/31] Revert "changelog feuture addition" This reverts commit 13b0bcffac79f96dfc196d8235cea5ba244d925a. --- doc/source/changelog.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 14faa818..6bc600b8 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -42,7 +42,6 @@ MontePy Changelog **Features Added** * Added demonstration jupyter notebooks for working with Pin Cell and PWR assemblies in MontePy. -* Added checking for additional input after the ``data`` block, and raising a warning if it exists (:feuture:`525`). **Bugs Fixed** From 7d3c955c721bc1d890eea7f2e2a36f13e97ee99d Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 20:24:24 +0200 Subject: [PATCH 14/31] Revert "changelog addition" This reverts commit 5f74b73d96431712eb15c222b8f9997fa5a571ff. --- doc/source/changelog.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 6bc600b8..ee140676 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -19,9 +19,6 @@ MontePy Changelog 1.1.2 -------------- -**Features Added** - -* Added checking for additional input after the ``data`` block, and raising a warning if it exists. **Code Quality** From d2061cde2725fae09eb7cc29b5b3a13ed036d188 Mon Sep 17 00:00:00 2001 From: kordusas Date: Tue, 7 Oct 2025 20:25:00 +0200 Subject: [PATCH 15/31] correct changelog --- doc/source/changelog.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index ee140676..4bb66498 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -7,6 +7,9 @@ MontePy Changelog #Next Release# -------------- +**Features Added** + +* Added checking for additional input after the ``data`` block, and raising a warning if it exists. **Bugs Fixed** From 86c76a738232c9f41ab9e469308ca249d4bcf59c Mon Sep 17 00:00:00 2001 From: kordusas Date: Fri, 10 Oct 2025 15:28:11 +0200 Subject: [PATCH 16/31] added input to list of "BAD" inputs --- tests/constants.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/constants.py b/tests/constants.py index 87ec8663..eeef7bbf 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -15,6 +15,7 @@ "test_extra_data_param.imcnp", "test_extra_data_imp.imcnp", "number_conflict_pin_cell.imcnp", + "test_pin_cell_extra_block_warning.imcnp", } IGNORE_FILES = { "testReadRec1.imcnp", From eef45f52ae24eddbfd37abee01d50d48fd8a1800 Mon Sep 17 00:00:00 2001 From: kordusas Date: Fri, 10 Oct 2025 15:46:10 +0200 Subject: [PATCH 17/31] bug fix for extra line printing after DATA BLOCK when cell data should be in DATA BLOCK --- montepy/mcnp_problem.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/montepy/mcnp_problem.py b/montepy/mcnp_problem.py index 5dede061..db74eb98 100644 --- a/montepy/mcnp_problem.py +++ b/montepy/mcnp_problem.py @@ -612,14 +612,16 @@ def _write_to_stream(self, inp): warning.handled = True for line in lines: inp.write(line + "\n") - if terminate: + + # writing cell data in DATA BLOCK if needed otherwise adding termination line + if obj.__class__.__module__.startswith("montepy.data_inputs"): + for line in self.cells._run_children_format_for_mcnp( + self.data_inputs, self.mcnp_version + ): + inp.write(line + "\n") + elif terminate: inp.write("\n") - for line in self.cells._run_children_format_for_mcnp( - self.data_inputs, self.mcnp_version - ): - inp.write(line + "\n") - inp.write("\n") self._handle_warnings(warning_catch) def _handle_warnings(self, warning_queue): From b201a1152dbef056bc29a2c723d6598bca89529e Mon Sep 17 00:00:00 2001 From: kordusas Date: Sun, 12 Oct 2025 14:26:38 +0200 Subject: [PATCH 18/31] bug fix comment --- doc/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 4bb66498..7824f727 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -18,7 +18,7 @@ MontePy Changelog * Added descriptive TypeError messages (:issue:`801`) - +* Fixed but with cell data writting in DATA BLOCK, removed redundant line termination 1.1.2 -------------- From deaac503c598a16e2a9b2a1eedb5e27a312e8a6d Mon Sep 17 00:00:00 2001 From: kordusas Date: Sun, 12 Oct 2025 14:54:30 +0200 Subject: [PATCH 19/31] updated the block parsing by using termination and stop parsing after 3 BLOCK is parsed --- montepy/input_parser/input_syntax_reader.py | 25 ++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index ec53c127..67e933c4 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -143,26 +143,27 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False): continue_input = False has_non_comments = False input_raw_lines = [] - block_length = -1 + terminate_reading = False def flush_block(): nonlocal block_counter, block_type - if len(input_raw_lines) > 0: - # warn once for extra blocks - if block_counter == 3: - warnings.warn( - f"Unexpected input after line {current_file.lineno-block_length}", - UndefinedBlock, - stacklevel=2, - ) - + # keep parsing while there is input or termination has not been triggered + if len(input_raw_lines) > 0 and not terminate_reading: yield from flush_input() block_counter += 1 if block_counter < 3: block_type = BlockType(block_counter) def flush_input(): - nonlocal input_raw_lines + nonlocal input_raw_lines, terminate_reading + # IF 3 BLOCKS are parsed, the rest should be ignored with a warning and print 3 lines + if block_counter == 3: + warnings.warn( + f"Unexpected input after line {current_file.lineno-1}\n line content: {'\n'.join(input_raw_lines[0:3])}\n", + UndefinedBlock, + stacklevel=6, + ) + terminate_reading = True start_line = current_file.lineno + 1 - len(input_raw_lines) input = Input( input_raw_lines, @@ -241,8 +242,6 @@ def flush_input(): else: continue_input = False has_non_comments = has_non_comments or not line_is_comment - if block_counter == 3: - block_length += 1 input_raw_lines.append(line.rstrip()) yield from flush_block() From c7feacd5e3cd1c70f34bda38e16f3e3210e4ffbf Mon Sep 17 00:00:00 2001 From: kordusas Date: Sun, 12 Oct 2025 15:05:39 +0200 Subject: [PATCH 20/31] corrected test according to updated error message --- tests/test_syntax_parsing.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_syntax_parsing.py b/tests/test_syntax_parsing.py index f165bd03..84a2bdfe 100644 --- a/tests/test_syntax_parsing.py +++ b/tests/test_syntax_parsing.py @@ -1477,6 +1477,7 @@ def test_extra_block_warning(self): # Define constants for better readability and maintainability INPUT_FILE = "tests/inputs/test_pin_cell_extra_block_warning.imcnp" EXPECTED_LINE = 34 + EXPECTED_CONTENT = "M101 92235.80c 0.04\n 92238.80c 0.96\n" EXPECTED_WARNING_TYPE = UndefinedBlock # Setup: Use the common pattern for defining the generator @@ -1495,8 +1496,9 @@ def test_extra_block_warning(self): warning.category, EXPECTED_WARNING_TYPE ), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}" assert ( - str(warning.message) == f"Unexpected input after line {EXPECTED_LINE}" - ), f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}', but got '{str(warning.message)}'" + str(warning.message) + == f"Unexpected input after line {EXPECTED_LINE}\n line content: {EXPECTED_CONTENT}" + ), f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}\n line content: {EXPECTED_CONTENT}', but got '{str(warning.message)}'" class TestClassifierNode: From 2a8749dfe33f344d392148de905490ea39927492 Mon Sep 17 00:00:00 2001 From: kordusas Date: Sun, 12 Oct 2025 19:58:46 +0200 Subject: [PATCH 21/31] changed to isinstance. Comparing obj (last object read in BLOCK) to DataInputAbstract if so then we can trigger writting CELL data into DATA BLOCK if the flags are set --- montepy/mcnp_problem.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/montepy/mcnp_problem.py b/montepy/mcnp_problem.py index db74eb98..6cd5bda0 100644 --- a/montepy/mcnp_problem.py +++ b/montepy/mcnp_problem.py @@ -14,6 +14,7 @@ from montepy.materials import Material, Materials from montepy.surfaces import surface, surface_builder from montepy.surface_collection import Surfaces +from montepy.data_inputs.data_input import DataInputAbstract # weird way to avoid circular imports from montepy.data_inputs import parse_data @@ -613,8 +614,8 @@ def _write_to_stream(self, inp): for line in lines: inp.write(line + "\n") - # writing cell data in DATA BLOCK if needed otherwise adding termination line - if obj.__class__.__module__.startswith("montepy.data_inputs"): + # writing cell data in DATA BLOCK if the last written object inherits DataInputAbstract and there is cell data to write + if isinstance(obj, (DataInputAbstract)): for line in self.cells._run_children_format_for_mcnp( self.data_inputs, self.mcnp_version ): From 770a797e9f436c76678a61785e286fa884163d9a Mon Sep 17 00:00:00 2001 From: kordusas Date: Sun, 12 Oct 2025 20:07:59 +0200 Subject: [PATCH 22/31] fixed \n error --- montepy/input_parser/input_syntax_reader.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 67e933c4..dac36171 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -158,8 +158,10 @@ def flush_input(): nonlocal input_raw_lines, terminate_reading # IF 3 BLOCKS are parsed, the rest should be ignored with a warning and print 3 lines if block_counter == 3: + joined_lines = "\n".join(input_raw_lines[0:3]) + msg = f"Unexpected input after line {current_file.lineno - 1}\n line content: {joined_lines}\n" warnings.warn( - f"Unexpected input after line {current_file.lineno-1}\n line content: {'\n'.join(input_raw_lines[0:3])}\n", + msg, UndefinedBlock, stacklevel=6, ) From 1811498d3df7a6de2db2263ba132f242b70c6479 Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 15 Oct 2025 20:25:34 +0200 Subject: [PATCH 23/31] updated description --- doc/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 7824f727..f0dec26c 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -18,7 +18,7 @@ MontePy Changelog * Added descriptive TypeError messages (:issue:`801`) -* Fixed but with cell data writting in DATA BLOCK, removed redundant line termination +* Fixed a bug that caused to write an extra termination line between the data block and the cell data section in the MCNP input. (:pull:`819`) 1.1.2 -------------- From 9b3c5c46ecc22686b85c036464fccebc275c77dc Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 15 Oct 2025 20:27:08 +0200 Subject: [PATCH 24/31] updated IF condition --- montepy/mcnp_problem.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/montepy/mcnp_problem.py b/montepy/mcnp_problem.py index 6cd5bda0..cbabcf3c 100644 --- a/montepy/mcnp_problem.py +++ b/montepy/mcnp_problem.py @@ -14,7 +14,6 @@ from montepy.materials import Material, Materials from montepy.surfaces import surface, surface_builder from montepy.surface_collection import Surfaces -from montepy.data_inputs.data_input import DataInputAbstract # weird way to avoid circular imports from montepy.data_inputs import parse_data @@ -615,7 +614,7 @@ def _write_to_stream(self, inp): inp.write(line + "\n") # writing cell data in DATA BLOCK if the last written object inherits DataInputAbstract and there is cell data to write - if isinstance(obj, (DataInputAbstract)): + if objects is self.data_inputs: for line in self.cells._run_children_format_for_mcnp( self.data_inputs, self.mcnp_version ): From 45d607da501f04c53e3267b4ec23a9da1272ed57 Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 22 Oct 2025 00:29:35 +0200 Subject: [PATCH 25/31] using return to get out of the generator instead of using bool flag --- montepy/input_parser/input_syntax_reader.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index dac36171..16ca1c18 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -143,19 +143,18 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False): continue_input = False has_non_comments = False input_raw_lines = [] - terminate_reading = False def flush_block(): nonlocal block_counter, block_type # keep parsing while there is input or termination has not been triggered - if len(input_raw_lines) > 0 and not terminate_reading: + if len(input_raw_lines) > 0: yield from flush_input() block_counter += 1 if block_counter < 3: block_type = BlockType(block_counter) def flush_input(): - nonlocal input_raw_lines, terminate_reading + nonlocal input_raw_lines # IF 3 BLOCKS are parsed, the rest should be ignored with a warning and print 3 lines if block_counter == 3: joined_lines = "\n".join(input_raw_lines[0:3]) @@ -165,7 +164,10 @@ def flush_input(): UndefinedBlock, stacklevel=6, ) - terminate_reading = True + return + + + print(f"Still parsing lines...", input_raw_lines) start_line = current_file.lineno + 1 - len(input_raw_lines) input = Input( input_raw_lines, @@ -203,6 +205,7 @@ def flush_input(): and input_raw_lines ): yield from flush_input() + # die if it is a vertical syntax format start_o_line = line[0:BLANK_SPACE_CONTINUE] # eliminate comments, and inputs that use # for other syntax From caedfbd40c3c8d956e5972c8b2a82c61d186839b Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 22 Oct 2025 00:34:23 +0200 Subject: [PATCH 26/31] fixed issue of getting errors if there are even more blocks than 4 .. --- montepy/input_parser/input_syntax_reader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 16ca1c18..50a70c7a 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -156,7 +156,7 @@ def flush_block(): def flush_input(): nonlocal input_raw_lines # IF 3 BLOCKS are parsed, the rest should be ignored with a warning and print 3 lines - if block_counter == 3: + if block_counter >= 3: joined_lines = "\n".join(input_raw_lines[0:3]) msg = f"Unexpected input after line {current_file.lineno - 1}\n line content: {joined_lines}\n" warnings.warn( From 0c05f4ef20ddbc11783e4373918c7e72abb27996 Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 22 Oct 2025 00:34:37 +0200 Subject: [PATCH 27/31] added comment on issue fix --- doc/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index af6d8e5a..19d9e92f 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -18,7 +18,7 @@ MontePy Changelog * Fixed bug where lines that were the allowed length was raising a ``LineOverRunWarning`` when read by MontePy (:issue:`517`). * Added descriptive TypeError messages (:issue:`801`) -* Fixed a bug that caused to write an extra termination line between the data block and the cell data section in the MCNP input. (:pull:`819`). +* Fixed a bug that caused to write an extra termination line between the data block and the cell data section in the MCNP input. (:pull:`819`) (:issue:`703`). **Documentation** From 784589048382f99a969ac730f466659802ccb724 Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 22 Oct 2025 01:07:42 +0200 Subject: [PATCH 28/31] formatting --- montepy/input_parser/input_syntax_reader.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 50a70c7a..9783d2aa 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -165,8 +165,7 @@ def flush_input(): stacklevel=6, ) return - - + print(f"Still parsing lines...", input_raw_lines) start_line = current_file.lineno + 1 - len(input_raw_lines) input = Input( From 1b9e231eb10584ccc11beb9215b441139badb9a9 Mon Sep 17 00:00:00 2001 From: kordusas Date: Wed, 22 Oct 2025 12:06:23 +0200 Subject: [PATCH 29/31] removed leftover redundant lines during testing --- montepy/input_parser/input_syntax_reader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 9783d2aa..876df4a8 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -166,7 +166,6 @@ def flush_input(): ) return - print(f"Still parsing lines...", input_raw_lines) start_line = current_file.lineno + 1 - len(input_raw_lines) input = Input( input_raw_lines, From b2ff3515be2c028cd91062f58b438e49ea3952f2 Mon Sep 17 00:00:00 2001 From: kordusas Date: Thu, 23 Oct 2025 00:02:36 +0200 Subject: [PATCH 30/31] fixing up changelog --- doc/source/changelog.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index f6dc50e8..88ce6a11 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -11,6 +11,7 @@ MontePy Changelog **Features Added** +* Added checking for additional input after the ``data`` block, and raising a warning if it exists. * Allow multiple universe fills to accept 2D MNCP lattices (:issue:`719`) * Make `LatticeType.RECTANGULAR` and `LatticeType.HEXAHEDRAL` synonymous (:issue:`808`). * Allow setting ``cell.fill.universes`` with a numpy array of universe IDs (:issue:`736`). @@ -40,10 +41,6 @@ MontePy Changelog -------------- **Features Added** -* Added checking for additional input after the ``data`` block, and raising a warning if it exists. - -**Features Added** - * Added Boundary condition type to the representation of a ``montepy.Surface`` (e.g., ``repr(surf)``) (:issue:`682`). **Documentation** From 8429ea360c751ddccf5a774c432fa539bac99a68 Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Thu, 23 Oct 2025 08:56:57 -0500 Subject: [PATCH 31/31] Added issue number to changelog. --- doc/source/changelog.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 88ce6a11..1461a806 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -11,8 +11,8 @@ MontePy Changelog **Features Added** -* Added checking for additional input after the ``data`` block, and raising a warning if it exists. -* Allow multiple universe fills to accept 2D MNCP lattices (:issue:`719`) +* Added checking for additional input after the ``data`` block, and raising a warning if it exists (:issue:`525`). +* Allow multiple universe fills to accept 2D MNCP lattices (:issue:`719`). * Make `LatticeType.RECTANGULAR` and `LatticeType.HEXAHEDRAL` synonymous (:issue:`808`). * Allow setting ``cell.fill.universes`` with a numpy array of universe IDs (:issue:`736`).