boddle icon indicating copy to clipboard operation
boddle copied to clipboard

throw error if 'environ' in extra arg. remove extra_orig.

Open signalw opened this issue 6 years ago • 0 comments

I see the point of having extra_orig is when the user passes in extra arguments that contain original request attributes, we temporarily store them in it. We set the values passed in by user when entering the context and change it back to original values when exiting the context.

But actually, I looked into the latest stable version of bottle (0.12.13), the properties of bottle.request are all read only, meaning that we can't actually overwrite them. E.g.:

with boddle(user='hi', forms='hey', files='hello'):
  # bottle.request.user == hi
  # bottle.request.forms != 'hey', it's still the default property!
  # bottle.request.files != 'hello', it's still the default property!

In our boddle.py we do this to set extra arguments:

setattr(bottle.request, k, v)

This is how bottle handles extra properties:

def __setattr__(self, name, value):
    if name == 'environ': return object.__setattr__(self, name, value)
    self.environ['bottle.request.ext.%s'%name] = value
def __getattr__(self, name):
    ''' Search in self.environ for additional user defined attributes. '''
    try:
        var = self.environ['bottle.request.ext.%s'%name]
        return var.__get__(self) if hasattr(var, '__get__') else var
    except KeyError:
        raise AttributeError('Attribute %r not defined.' % name)

First, if a user passes in environ='test' as extra argument, it will blow this whole bottle thing up. Then, bottle.request.user works because the extra property is stored as bottle.request.ext.user in the self.environ dictionary and upon retrieving, there's no user property in request, so we go to the dictionary to get it as an extra property. And files and forms are stored as bottle.request.ext.files and bottle.request.ext.forms in there. That's how bottle prevents its own properties from being overwritten.

I don't know if anything else we can do to make it work... this change only fixes the "environ" keyword issue. Not sure what we should do about the other issue. And since original properties can't be overwritten, I removed extra_orig.

signalw avatar Aug 12 '18 04:08 signalw