Skip to content

Commit fe6b85d

Browse files
committed
fix OpenSCAD identifiers starting with a digit
1 parent f460842 commit fe6b85d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

solid/solidpython.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,17 @@ def new_openscad_class_str(class_name: str,
739739
def _subbed_keyword(keyword: str) -> str:
740740
"""
741741
Append an underscore to any python reserved word.
742+
Prepend an underscore to any OpenSCAD identifier starting with a digit.
742743
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
743744
"""
744-
new_key = keyword + '_' if keyword in PYTHON_ONLY_RESERVED_WORDS else keyword
745+
new_key = keyword
746+
747+
if keyword in PYTHON_ONLY_RESERVED_WORDS:
748+
new_key = keyword + "_"
749+
750+
if keyword[0].isdigit():
751+
new_key = "_" + keyword
752+
745753
if new_key != keyword:
746754
print(f"\nFound OpenSCAD code that's not compatible with Python. \n"
747755
f"Imported OpenSCAD code using `{keyword}` \n"
@@ -751,10 +759,16 @@ def _subbed_keyword(keyword: str) -> str:
751759
def _unsubbed_keyword(subbed_keyword: str) -> str:
752760
"""
753761
Remove trailing underscore for already-subbed python reserved words.
762+
Remove prepending underscore if remaining identifier starts with a digit.
754763
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
755764
"""
756-
shortened = subbed_keyword[:-1]
757-
return shortened if shortened in PYTHON_ONLY_RESERVED_WORDS else subbed_keyword
765+
if subbed_keyword.endswith("_") and subbed_keyword[:-1] in PYTHON_ONLY_RESERVED_WORDS:
766+
return subbed_keyword[:-1]
767+
768+
if subbed_keyword.startswith("_") and subbed_keyword[1].isdigit():
769+
return subbed_keyword[1:]
770+
771+
return subbed_keyword
758772

759773
# now that we have the base class defined, we can do a circular import
760774
from . import objects

0 commit comments

Comments
 (0)