Skip to content

Commit 9a68259

Browse files
committed
Merge branch 'openscad_identifiers_starting_with_digits' into fix_173
2 parents 010c815 + fe6b85d commit 9a68259

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
@@ -712,9 +712,17 @@ def new_openscad_class_str(class_name: str,
712712
def _subbed_keyword(keyword: str) -> str:
713713
"""
714714
Append an underscore to any python reserved word.
715+
Prepend an underscore to any OpenSCAD identifier starting with a digit.
715716
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
716717
"""
717-
new_key = keyword + '_' if keyword in PYTHON_ONLY_RESERVED_WORDS else keyword
718+
new_key = keyword
719+
720+
if keyword in PYTHON_ONLY_RESERVED_WORDS:
721+
new_key = keyword + "_"
722+
723+
if keyword[0].isdigit():
724+
new_key = "_" + keyword
725+
718726
if new_key != keyword:
719727
print(f"\nFound OpenSCAD code that's not compatible with Python. \n"
720728
f"Imported OpenSCAD code using `{keyword}` \n"
@@ -724,10 +732,16 @@ def _subbed_keyword(keyword: str) -> str:
724732
def _unsubbed_keyword(subbed_keyword: str) -> str:
725733
"""
726734
Remove trailing underscore for already-subbed python reserved words.
735+
Remove prepending underscore if remaining identifier starts with a digit.
727736
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
728737
"""
729-
shortened = subbed_keyword[:-1]
730-
return shortened if shortened in PYTHON_ONLY_RESERVED_WORDS else subbed_keyword
738+
if subbed_keyword.endswith("_") and subbed_keyword[:-1] in PYTHON_ONLY_RESERVED_WORDS:
739+
return subbed_keyword[:-1]
740+
741+
if subbed_keyword.startswith("_") and subbed_keyword[1].isdigit():
742+
return subbed_keyword[1:]
743+
744+
return subbed_keyword
731745

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

0 commit comments

Comments
 (0)