Skip to content


Subversion checkout URL

You can clone with
Download ZIP


fix bug setting options programatically #498

merged 1 commit into from

2 participants


Previously, if you set an option in code:

options.my_option = value

this would overwrite the Options attribute with a non Option value. This would break subsequent retrieval of the option. This code change adds a parallel __setattr to the existing getattr.

I also fixed a bug with the root type - 'str' should have been 'basesting' - since we return a _unicode() converted string - which fails when you try to set the value back again.


Hmm, I was actually surprised to learn that string options are actually type unicode - I guess I've only ever used ascii values in python2 so I never noticed. This looks like a good change.

@bdarnell bdarnell merged commit 625c6e0 into tornadoweb:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 26, 2012
  1. @mckoss
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 5 deletions.
  1. +10 −5 tornado/
15 tornado/
@@ -184,7 +184,7 @@ def print_help(file=sys.stdout):
class _Options(dict):
- """Our global program options, an dictionary with object-like access."""
+ """Our global program options, a dictionary with object-like access."""
def instance(cls):
if not hasattr(cls, "_instance"):
@@ -196,9 +196,14 @@ def __getattr__(self, name):
return self[name].value()
raise AttributeError("Unrecognized option %r" % name)
+ def __setattr__(self, name, value):
+ if isinstance(self.get(name), _Option):
+ return self[name].set(value)
+ raise AttributeError("Unrecognized option %r" % name)
class _Option(object):
- def __init__(self, name, default=None, type=str, help=None, metavar=None,
+ def __init__(self, name, default=None, type=basestring, help=None, metavar=None,
multiple=False, file_name=None, group_name=None):
if default is None and multiple:
default = []
@@ -220,7 +225,7 @@ def parse(self, value):
datetime.datetime: self._parse_datetime,
datetime.timedelta: self._parse_timedelta,
bool: self._parse_bool,
- str: self._parse_string,
+ basestring: self._parse_string,
}.get(self.type, self.type)
if self.multiple:
if self._value is None:
@@ -249,8 +254,8 @@ def set(self, value):
(, self.type.__name__))
if value != None and not isinstance(value, self.type):
- raise Error("Option %r is required to be a %s" %
- (, self.type.__name__))
+ raise Error("Option %r is required to be a %s (%s given)" %
+ (, self.type.__name__, type(value)))
self._value = value
# Supported date/time formats in our options
Something went wrong with that request. Please try again.