Skip to content

Commit

Permalink
item.set() filters reserved keywords (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
smathot committed Oct 11, 2012
1 parent 73051ca commit 8c4829e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions libopensesame/item.py
Expand Up @@ -51,7 +51,14 @@ def __init__(self, name, experiment, string=None):
self.experiment = experiment
self.debug = debug.enabled
self.count = 0
self.reserved_words = "run", "prepare", "get", "set", "has"

# A number of keywords are reserved, which means that they cannot be used
# as variable names
self.reserved_words = ['experiment', 'variables', 'comments', 'item_type']
for attr in dir(item):
if hasattr(getattr(item, attr), '__call__'):
self.reserved_words.append(attr)

self._get_lock = None

if not hasattr(self, "item_type"):
Expand Down Expand Up @@ -338,12 +345,18 @@ def set(self, var, val):

# Make sure the variable name and the value are of the correct types
var = self.unistr(var)
val = self.auto_type(val)
val = self.auto_type(val)
# Check whether the variable name is valid
if regexp.sanitize_var_name.sub('_', var) != var:
raise exceptions.runtime_error( \
'"%s" is not a valid variable name. Variable names must consist of alphanumeric characters and underscores, and may not start with a digit.' \
% var)
# Check whether the variable name is not protected
if var in self.reserved_words:
raise exceptions.runtime_error( \
'"%s" is a reserved keyword (i.e. it has a special meaning for OpenSesame), and therefore cannot be used as a variable name. Sorry!' \
% var)

# Register the variables
setattr(self, var, val)
self.variables[var] = val
Expand Down

0 comments on commit 8c4829e

Please sign in to comment.