Skip to content

Commit 55e2b55

Browse files
committed
don't allow . in signal/system names (state names are OK)
1 parent b8ba051 commit 55e2b55

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

control/namedio.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def _name_or_default(self, name=None, prefix_suffix_name=None):
6060
if name is None:
6161
name = "sys[{}]".format(NamedIOSystem._idCounter)
6262
NamedIOSystem._idCounter += 1
63+
elif re.match(r".*\..*", name):
64+
raise ValueError(f"invalid system name '{name}' ('.' not allowed)")
65+
6366
prefix = "" if prefix_suffix_name is None else config.defaults[
6467
'namedio.' + prefix_suffix_name + '_system_name_prefix']
6568
suffix = "" if prefix_suffix_name is None else config.defaults[
@@ -187,7 +190,6 @@ def copy(self, name=None, use_prefix_suffix=True):
187190
return newsys
188191

189192
def set_inputs(self, inputs, prefix='u'):
190-
191193
"""Set the number/names of the system inputs.
192194
193195
Parameters
@@ -271,7 +273,7 @@ def set_states(self, states, prefix='x'):
271273
272274
"""
273275
self.nstates, self.state_index = \
274-
_process_signal_list(states, prefix=prefix)
276+
_process_signal_list(states, prefix=prefix, allow_dot=True)
275277

276278
def find_state(self, name):
277279
"""Find the index for a state given its name (`None` if not found)"""
@@ -626,7 +628,7 @@ def _process_dt_keyword(keywords, defaults={}, static=False):
626628

627629

628630
# Utility function to parse a list of signals
629-
def _process_signal_list(signals, prefix='s'):
631+
def _process_signal_list(signals, prefix='s', allow_dot=False):
630632
if signals is None:
631633
# No information provided; try and make it up later
632634
return None, {}
@@ -637,10 +639,17 @@ def _process_signal_list(signals, prefix='s'):
637639

638640
elif isinstance(signals, str):
639641
# Single string given => single signal with given name
642+
if not allow_dot and re.match(r".*\..*", signals):
643+
raise ValueError(
644+
f"invalid signal name '{signals}' ('.' not allowed)")
640645
return 1, {signals: 0}
641646

642647
elif all(isinstance(s, str) for s in signals):
643648
# Use the list of strings as the signal names
649+
for signal in signals:
650+
if not allow_dot and re.match(r".*\..*", signal):
651+
raise ValueError(
652+
f"invalid signal name '{signal}' ('.' not allowed)")
644653
return len(signals), {signals[i]: i for i in range(len(signals))}
645654

646655
else:

control/tests/namedio_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,12 @@ def test_find_signals():
326326
assert sys.find_outputs(['y', 'z']) == [0, 1, 2, 3]
327327
assert sys.find_outputs(['y[1:]', 'z']) == [1, 2, 3]
328328
assert sys.find_outputs(['y', 'z[:1]']) == [0, 1, 2, 3]
329+
330+
331+
# Invalid signal names
332+
def test_invalid_signal_names():
333+
with pytest.raises(ValueError, match="invalid signal name"):
334+
sys = ct.rss(4, inputs="input.signal", outputs=1)
335+
336+
with pytest.raises(ValueError, match="invalid system name"):
337+
sys = ct.rss(4, inputs=1, outputs=1, name="system.subsys")

0 commit comments

Comments
 (0)