Check symbol names
[linpy.git] / linpy / linexprs.py
index cf2a980..834c3b4 100644 (file)
@@ -456,8 +456,13 @@ class Symbol(LinExpr):
         """
         if not isinstance(name, str):
             raise TypeError('name must be a string')
         """
         if not isinstance(name, str):
             raise TypeError('name must be a string')
+        node = ast.parse(name)
+        try:
+            name = node.body[0].value.id
+        except (AttributeError, SyntaxError):
+            raise SyntaxError('invalid syntax')
         self = object().__new__(cls)
         self = object().__new__(cls)
-        self._name = name.strip()
+        self._name = name
         self._coefficients = {self: Fraction(1)}
         self._constant = Fraction(0)
         self._symbols = (self,)
         self._coefficients = {self: Fraction(1)}
         self._constant = Fraction(0)
         self._symbols = (self,)