Setting an attribute on a shim object without first reading an attribute causes a crash

Bug #662740 reported by Leonard Richardson
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
lazr.restfulclient
Triaged
Low
Unassigned

Bug Description

Code like this:

project = launchpad.projects('foo')
foo.bar = 'baz'

Will yield a traceback like this:

Traceback (most recent call last):
  File "./security-scanner.py", line 341, in setUp
    self._create_objects()
  File "./security-scanner.py", line 374, in _create_objects
    project.project_group = test_project
  File "/usr/lib/python2.6/dist-packages/lazr/restfulclient/resource.py", line 647, in __setattr__
    if not self.lp_has_parameter(name):
  File "/usr/lib/python2.6/dist-packages/lazr/restfulclient/resource.py", line 187, in lp_has_parameter
    return self._get_external_param_name(param_name) is not None
  File "/usr/lib/python2.6/dist-packages/lazr/restfulclient/resource.py", line 323, in _get_external_param_name
    if self._wadl_resource.get_parameter(name):
  File "/usr/lib/pymodules/python2.6/wadllib/application.py", line 422, in get_parameter
    definition = self._find_representation_definition(media_type)
  File "/usr/lib/pymodules/python2.6/wadllib/application.py", line 531, in _find_representation_definition
    "Resource is not bound to any representation, and no media "
NoBoundRepresentationError: Resource is not bound to any representation, and no media media type was specified.

First off, "media media type" is a typo. Second, you should probably be able to set a parameter on a shim object without having to get the current representation. Or if you can't (because you don't know what the ETag is, so you can't send a conditional PATCH), setting a parameter value on a shim object should fetch the representation, just as getting a parameter value does.

Gary Poster (gary)
Changed in lazr.restfulclient:
status: New → Triaged
importance: Undecided → Low
Revision history for this message
Colin Watson (cjwatson) wrote :

I ran into this while changing the default branch for a bunch of projects when debian-installer moved to git. The workaround (thanks to leonardr) was to do '_ = focus.name' before trying to set focus.branch (where focus was a project_series object).

Revision history for this message
Martin Pool (mbp) wrote :

I hit this too trying to update bug privacy, where the search was enough to know that I wanted to change the bug without reading any attributes. Thanks for the workaround.

Revision history for this message
Martin Pool (mbp) wrote :

One thing that's a bit tricky and surprising here is that you can't say

  print bug_task.bug.title
  bug_task.bug.private = False

you need to hold a reference to the actual Bug proxy.

Revision history for this message
Colin Watson (cjwatson) wrote :

There's a similar effect with named operations. For example:

    >>> target = lp.git_repositories.getByPath(path='germinate').target
    >>> target.getSeries(name='trunk')
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/usr/lib/python2.7/dist-packages/lazr/restfulclient/resource.py", line 734, in __getattr__
        return super(Entry, self).__getattr__(name)
      File "/usr/lib/python2.7/dist-packages/lazr/restfulclient/resource.py", line 358, in __getattr__
        % (self, attr))
    AttributeError: https://api.launchpad.net/devel/germinate object has no attribute 'getSeries'
    >>> target._ensure_representation()
    >>> target.getSeries(name='trunk')
    <project_series at https://api.launchpad.net/devel/germinate/trunk>

As suggested by the (inappropriate for production use) workaround in this transcript, I think the fix is probably just to sprinkle self._ensure_representation() around a couple more places in Resource and its subclasses that rely on having the representation available.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.