From 75031ccb574c697574d743af0b9019196cd47385 Mon Sep 17 00:00:00 2001
From: Markus Scheidgen <markus.scheidgen@gmail.com>
Date: Mon, 27 Mar 2023 12:54:36 +0200
Subject: [PATCH] Fixed processing with reuse_parser set to False.

There was an issue with recreating parser instances, which only
appeared when the reuser_parser process setting was set to
false. The fix now allows to correctly recreate parser instances.

Changelog: Fixed
---
 nomad/parsing/parser.py       |  5 +++++
 nomad/processing/data.py      |  2 +-
 tests/processing/test_data.py | 14 ++++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/nomad/parsing/parser.py b/nomad/parsing/parser.py
index 49c230b9aa..09a876b494 100644
--- a/nomad/parsing/parser.py
+++ b/nomad/parsing/parser.py
@@ -392,6 +392,11 @@ class MatchingParserInterface(MatchingParser):
         self._parser_class_name = parser_class_name
         self._mainfile_parser = None
 
+    def new_parser_instance(self):
+        ''' Forgets the existing parser instance and forces the creation of a new one. '''
+        self._mainfile_parser = None
+        return self._mainfile_parser
+
     @property
     def mainfile_parser(self):
         if self._mainfile_parser is None:
diff --git a/nomad/processing/data.py b/nomad/processing/data.py
index 194a768334..415ea57472 100644
--- a/nomad/processing/data.py
+++ b/nomad/processing/data.py
@@ -1140,7 +1140,7 @@ class Entry(Proc):
             if not config.process.reuse_parser:
                 if isinstance(parser, parsing.MatchingParserInterface):
                     try:
-                        parser = parser.__class__()
+                        parser.new_parser_instance()
                     except Exception as e:
                         raise ProcessFailure(
                             'could not re-create parser instance',
diff --git a/tests/processing/test_data.py b/tests/processing/test_data.py
index 17ba7eb4b1..7bd62adbbc 100644
--- a/tests/processing/test_data.py
+++ b/tests/processing/test_data.py
@@ -536,6 +536,20 @@ def test_re_process_match(non_empty_processed, published, monkeypatch, no_warn):
         assert not upload.with_embargo
 
 
+@pytest.mark.parametrize('reuse_parser', [False, True])
+def test_reuse_parser(monkeypatch, tmp, test_user, proc_infra, reuse_parser, no_warn):
+    upload_path = os.path.join(tmp, 'example_upload.zip')
+    with zipfile.ZipFile(upload_path, 'w') as zf:
+        zf.write('tests/data/parsers/vasp/vasp.xml', 'one/run.vasp.xml')
+        zf.write('tests/data/parsers/vasp/vasp.xml', 'two/run.vasp.xml')
+
+    monkeypatch.setattr('nomad.config.process.reuse_parser', reuse_parser)
+    upload = run_processing(('example_upload', upload_path,), test_user)
+
+    assert upload.total_entries_count == 2
+    assert upload.process_status == 'SUCCESS'
+
+
 @pytest.mark.parametrize('args', [
     pytest.param(
         dict(
-- 
GitLab