'Sets the given cookie name/value with the given options.
Additional keyword arguments are set on the Cookie.Morsel
directly.
See http://docs.python.org/library/cookie.html#morsel-objects
for available attributes.'

def set_cookie(self, name, value, domain=None, expires=None, path='/', expires_days=None, **kwargs):

    name = escape.native_str(name)
    value = escape.native_str(value)
    if re.search('[\\x00-\\x20]', (name + value)):
        raise ValueError(('Invalid    cookie    %r:    %r' % (name, value)))
    if (not hasattr(self, '_new_cookie')):
        self._new_cookie = Cookie.SimpleCookie()
    if (name in self._new_cookie):
        del self._new_cookie[name]
    self._new_cookie[name] = value
    morsel = self._new_cookie[name]
    if domain:
        morsel['domain'] = domain
    if ((expires_days is not None) and (not expires)):
        expires = (datetime.datetime.utcnow() + datetime.timedelta(days=expires_days))
    if expires:
        morsel['expires'] = httputil.format_timestamp(expires)
    if path:
        morsel['path'] = path
    for (k, v) in kwargs.items():
        if (k == 'max_age'):
            k = 'max-age'
        morsel[k] = v
