[openstack-dev] [Nova] Code question on dict copy in compute/volume api

Vishvananda Ishaya vishvananda at gmail.com
Wed Aug 15 19:53:42 UTC 2012


On Aug 14, 2012, at 7:51 PM, unicell <unicell at gmail.com> wrote:

> Hi Folks,
> 
> One thing bothers me a lot when reading following code. Could someone
> helps me out? Thanks a lot!
> 
> In nova/volume/api.py
> 
> Why it has to duplicate a dict copy on return? Or, why not directly
> return the "rv", it's also a dictionary anyway.
> 
>    def get_snapshot(self, context, snapshot_id):
>        check_policy(context, 'get_snapshot')
>        rv = self.db.snapshot_get(context, snapshot_id)
>        return dict(rv.iteritems())

> And, why dicrectly return at following case?
> 
>    def get_all_snapshots(self, context, search_opts=None):
>            ...snip...
>            return self.db.snapshot_get_all_by_project(context,
>                                                       context.project_id)
> 
> I kindof feel it is something related to python scoping rule, but not
> quite sure.

The objects returned from the db layer behave like dictionaries but they
are not dictionaries, so this forces the object to an actual dictionary. Most
likely just returning the actual value was causing a failure somewhere because
of how the object was being used. It isn't necessarily required but it does prevent
bad things from happening if the object is sent over rpc for example.

It may just be left over from old code.

Vish




More information about the OpenStack-dev mailing list