Skip to content

Commit 39c6c22

Browse files
committed
fix duplicate option handling in config parser for Python 3.13 compatibility
- Updated the `_handle_option` method to accommodate changes in Python 3.13, ensuring proper handling of duplicate options in configuration files. - Enhanced logging to provide clearer error messages when duplicate options are detected, improving troubleshooting capabilities.
1 parent c634802 commit 39c6c22

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

modules/core.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,23 @@ def __init__(self, *args, **kwargs):
5151
super().__init__(*args, **kwargs)
5252

5353
def _handle_option(self, st, line, fpname):
54-
if st.optname in st.options:
55-
logging.getLogger(__name__).error(
56-
"Duplicate option in config file: section [%s], option '%s' (file %s, line %s). Using last value.",
57-
st.sectname, st.optname, fpname, getattr(st, 'lineno', '?'),
58-
)
59-
st.options[st.optname] = st.optvalue
54+
# Python 3.13+ uses st.cursect; older internals used st.options (both mean current section dict).
55+
sect = getattr(st, 'cursect', None) or getattr(st, 'options', None)
56+
if sect is not None:
57+
# Duplicate check: in 3.13 optname is set inside base _handle_option, so parse line here for the check.
58+
line_str = getattr(line, 'clean', None)
59+
if line_str is None:
60+
line_str = line if isinstance(line, str) else str(line)
61+
mo = self._optcre.match(line_str) if line_str else None
62+
if mo:
63+
optname = self.optionxform(mo.group('option').rstrip())
64+
if optname in sect:
65+
logging.getLogger(__name__).error(
66+
"Duplicate option in config file: section [%s], option '%s' (file %s, line %s). Using last value.",
67+
getattr(st, 'sectname', '?'), optname, fpname, getattr(st, 'lineno', '?'),
68+
)
69+
# Delegate to base to parse line and set option (required on Python 3.13+).
70+
super()._handle_option(st, line, fpname)
6071

6172

6273
class MeshCoreBot:

0 commit comments

Comments
 (0)