[ovs-dev] [PATCH 3/3] python: Convert types.py to use UINT_MAX instead of sys.maxint.

Ethan Jackson ethan at nicira.com
Mon Jan 3 20:43:29 UTC 2011


sys.maxint represents the maximum value for an integer in python.
Not the maximum value for the c type "int".  Thus on 64bit systems
it can be 2^64 - 1 which is too big for the c equivalents defined
in vswitch-idl.h
---
 python/ovs/db/types.py |   46 +++++++++++++++++++++++++++-------------------
 1 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/python/ovs/db/types.py b/python/ovs/db/types.py
index d42ac7f..f8bb71e 100644
--- a/python/ovs/db/types.py
+++ b/python/ovs/db/types.py
@@ -19,6 +19,8 @@ import ovs.db.parser
 import ovs.db.data
 import ovs.ovsuuid
 
+UINT_MAX = 2**32 - 1
+
 class AtomicType(object):
     def __init__(self, name, default):
         self.name = name
@@ -106,7 +108,7 @@ def returnUnchanged(x):
 
 class BaseType(object):
     def __init__(self, type_, enum=None, min=None, max=None,
-                 min_length = 0, max_length=sys.maxint, ref_table=None):
+                 min_length = 0, max_length=UINT_MAX, ref_table=None):
         assert isinstance(type_, AtomicType)
         self.type = type_
         self.enum = enum
@@ -172,7 +174,7 @@ class BaseType(object):
         elif base.type == StringType:
             base.min_length = BaseType.__parse_uint(parser, "minLength", 0)
             base.max_length = BaseType.__parse_uint(parser, "maxLength",
-                                                    sys.maxint)
+                                                    UINT_MAX)
             if base.min_length > base.max_length:
                 raise error.Error("minLength exceeds maxLength", json)
         elif base.type == UuidType:
@@ -209,7 +211,7 @@ class BaseType(object):
         elif self.type == StringType:
             if self.min_length != 0:
                 json['minLength'] = self.min_length
-            if self.max_length != sys.maxint:
+            if self.max_length != UINT_MAX:
                 json['maxLength'] = self.max_length
         elif self.type == UuidType:
             if self.ref_table:
@@ -234,7 +236,7 @@ class BaseType(object):
 
     def has_constraints(self):
         return (self.enum is not None or self.min is not None or self.max is not None or
-                self.min_length != 0 or self.max_length != sys.maxint or
+                self.min_length != 0 or self.max_length != UINT_MAX or
                 self.ref_table is not None)
 
     def without_constraints(self):
@@ -244,8 +246,8 @@ class BaseType(object):
     def get_enum_type(atomic_type):
         """Returns the type of the 'enum' member for a BaseType whose
         'type' is 'atomic_type'."""
-        return Type(BaseType(atomic_type), None, 1, sys.maxint)
-    
+        return Type(BaseType(atomic_type), None, 1, UINT_MAX)
+
     def is_ref(self):
         return self.type == UuidType and self.ref_table is not None
 
@@ -290,14 +292,14 @@ class BaseType(object):
                 return 'at most %s' % commafy(self.max)
             else:
                 return 'at most %g' % self.max
-        elif self.min_length != 0 and self.max_length != sys.maxint:
+        elif self.min_length != 0 and self.max_length != UINT_MAX:
             if self.min_length == self.max_length:
                 return 'exactly %d characters long' % (self.min_length)
             else:
                 return 'between %d and %d characters long' % (self.min_length, self.max_length)
         elif self.min_length != 0:
             return 'at least %d characters long' % self.min_length
-        elif self.max_length != sys.maxint:
+        elif self.max_length != UINT_MAX:
             return 'at most %d characters long' % self.max_length
         else:
             return ''
@@ -336,7 +338,7 @@ class BaseType(object):
                        BooleanType: '%s = false;',
                        StringType: '%s = NULL;'}[self.type]
             return pattern % var
-            
+
     def cInitBaseType(self, indent, var):
         stmts = []
         stmts.append('ovsdb_base_type_init(&%s, OVSDB_TYPE_%s);' % (
@@ -357,9 +359,15 @@ class BaseType(object):
                 stmts.append('%s.u.real.max = %d;' % (var, self.max))
         elif self.type == StringType:
             if self.min_length is not None:
-                stmts.append('%s.u.string.minLen = %d;' % (var, self.min_length))            
+                minLen = self.min_length
+                if minLen == UINT_MAX:
+                    minLen = "UINT_MAX"
+                stmts.append('%s.u.string.minLen = %s;' % (var, minLen))
             if self.max_length is not None:
-                stmts.append('%s.u.string.maxLen = %d;' % (var, self.max_length))
+                maxLen = self.max_length
+                if maxLen == UINT_MAX:
+                    maxLen = "UINT_MAX"
+                stmts.append('%s.u.string.maxLen = %s;' % (var, maxLen))
         elif self.type == UuidType:
             if self.ref_table is not None:
                 stmts.append('%s.u.uuid.refTableName = "%s";' % (var, escapeCString(self.ref_table)))
@@ -422,11 +430,11 @@ class Type(object):
     def __n_from_json(json, default):
         if json is None:
             return default
-        elif type(json) == int and json >= 0 and json <= sys.maxint:
+        elif type(json) == int and json >= 0 and json <= UINT_MAX:
             return json
         else:
             raise error.Error("bad min or max value", json)
-    
+
     @staticmethod
     def from_json(json):
         if type(json) in [str, unicode]:
@@ -448,9 +456,9 @@ class Type(object):
         n_min = Type.__n_from_json(min_json, 1)
 
         if max_json == 'unlimited':
-            n_max = sys.maxint
+            n_max = UINT_MAX
         else:
-            n_max = Type.__n_from_json(max_json, 1)            
+            n_max = Type.__n_from_json(max_json, 1)
 
         type_ = Type(key, value, n_min, n_max)
         if not type_.is_valid():
@@ -466,7 +474,7 @@ class Type(object):
             json["value"] = self.value.to_json()
         if self.n_min != 1:
             json["min"] = self.n_min
-        if self.n_max == sys.maxint:
+        if self.n_max == UINT_MAX:
             json["max"] = "unlimited"
         elif self.n_max != 1:
             json["max"] = self.n_max
@@ -485,7 +493,7 @@ class Type(object):
             else:
                 return "optional %s" % keyName
         else:
-            if self.n_max == sys.maxint:
+            if self.n_max == UINT_MAX:
                 if self.n_min:
                     quantity = "%d or more " % self.n_min
                 else:
@@ -521,7 +529,7 @@ class Type(object):
                 constraints += ['value ' + valueConstraints]
 
         return ', '.join(constraints)
-                
+
     def cDeclComment(self):
         if self.n_min == 1 and self.n_max == 1 and self.key.type == StringType:
             return "\t/* Always nonnull. */"
@@ -536,7 +544,7 @@ class Type(object):
             initValue = ('%sovsdb_base_type_init(&%s.value, '
                          'OVSDB_TYPE_VOID);' % (indent, var))
         initMin = "%s%s.n_min = %s;" % (indent, var, self.n_min)
-        if self.n_max == sys.maxint:
+        if self.n_max == UINT_MAX:
             max = "UINT_MAX"
         else:
             max = self.n_max
-- 
1.7.2





More information about the dev mailing list