[PATCH] Fix the Device.id usage.

David Lehman dlehman at redhat.com
Tue Jan 21 20:30:24 UTC 2014


On Tue, 2014-01-21 at 15:22 -0500, David Shea wrote:
> Set the id for subclasses of ObjectID during __new__ so that the id is
> available to subclasses during __init__.
> ---
>  blivet/devices.py | 4 ++--
>  blivet/util.py    | 9 ++++++---
>  2 files changed, 8 insertions(+), 5 deletions(-)

Looks good to me.

> 
> diff --git a/blivet/devices.py b/blivet/devices.py
> index adfc786..e1fde38 100644
> --- a/blivet/devices.py
> +++ b/blivet/devices.py
> @@ -3925,7 +3925,7 @@ class LoopDevice(StorageDevice):
>  
>          if not name:
>              # set up a temporary name until we've activated the loop device
> -            name = "tmploop%d" % Device.id
> +            name = "tmploop%d" % self.id
>  
>          StorageDevice.__init__(self, name, format=format, size=size,
>                                 exists=True, parents=parents)
> @@ -4338,7 +4338,7 @@ class BTRFSDevice(StorageDevice):
>      def __init__(self, *args, **kwargs):
>          """ Passing None or no name means auto-generate one like btrfs.%d """
>          if not args or not args[0]:
> -            args = ("btrfs.%d" % Device.id,)
> +            args = ("btrfs.%d" % self.id,)
>  
>          if kwargs.get("parents") is None:
>              raise ValueError("BTRFSDevice must have at least one parent")
> diff --git a/blivet/util.py b/blivet/util.py
> index 3c236a6..aed4a25 100644
> --- a/blivet/util.py
> +++ b/blivet/util.py
> @@ -345,10 +345,13 @@ class ObjectID(object):
>  
>         The name of the identifier property is id, its type is int.
>  
> -       The constructor always sets object_id to a new value which is unique
> -       for the object type.
> +       The id is set during creation of the class instance to a new value
> +       which is unique for the object type. Subclasses can use self.id during
> +       __init__.
>      """
>      _newid_gen = itertools.count().next
>  
> -    def __init__(self):
> +    def __new__(cls, *args, **kwargs):
> +        self = super(ObjectID, cls).__new__(cls, *args, **kwargs)
>          self.id = self._newid_gen()
> +        return self




More information about the anaconda-patches mailing list