Codebase list sugar-calculate-activity / 2005518
Block redefinition of default variables Block any redefinition of default variables, such as pi and e, and show an error message instead. The error is "Cannot redefine a constant". Fixes SL#2698 https://bugs.sugarlabs.org/ticket/2698 Signed-off-by: James Cameron <quozl@laptop.org> Ezequiel Pereira Lopez authored 9 years ago James Cameron committed 7 years ago
2 changed file(s) with 16 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
313313 if name.startswith('_') or isinstance(item, types.ModuleType):
314314 continue
315315
316 self.set_var(name, item)
316 self.set_var(name, item, True)
317317 if type(item) in (types.FunctionType, types.ClassType):
318318 if item.__doc__ is not None:
319319 self._helper.add_help(name, item.__doc__)
348348 '''Set variable <name> to <value>, which could be a function too.'''
349349 name = unicode(name)
350350 if name in self._immutable_vars:
351 raise Exception, "Cannot redefine a constant"
351352 return False
352353 self._namespace[unicode(name)] = value
353354 if immutable:
3535 from shareable_activity import ShareableActivity
3636 from layout import CalcLayout
3737 from mathlib import MathLib
38 from astparser import AstParser, ParserError, RuntimeError
38 from astparser import AstParser, ParserError, ParseError, RuntimeError
3939 from svgimage import SVGImage
4040
4141 from decimal import Decimal
470470 prepend=not prepend)
471471 self.last_eqn_textview = None
472472
473 if eq.label is not None and len(eq.label) > 0:
474 w = self.create_var_textview(eq.label, eq.result)
475 if w is not None:
476 self.layout.add_variable(eq.label, w)
477
478 if tree is None:
479 tree = self.parser.parse(eq.equation)
480 try: self.parser.set_var(eq.label, tree)
481 except Exception, e:
482 eq.result = ParseError(e.message, 0, "")
483 self.set_error_equation(eq)
484 return
485
473486 own = (eq.owner == self.get_owner_id())
474487 w = eq.create_history_object()
475488 w.connect('button-press-event', lambda w,
484497 self.last_eqn_textview = w
485498 else:
486499 self.layout.add_equation(w, own, prepend=not prepend)
487
488 if eq.label is not None and len(eq.label) > 0:
489 w = self.create_var_textview(eq.label, eq.result)
490 if w is not None:
491 self.layout.add_variable(eq.label, w)
492
493 if tree is None:
494 tree = self.parser.parse(eq.equation)
495 self.parser.set_var(eq.label, tree)
496500
497501 # FIXME: to be implemented
498502 def process_async(self, eqn):