Skip to content

Commit

Permalink
python: Drop use of sys.maxint.
Browse files Browse the repository at this point in the history
sys.maxint does not exist in Python 3, as an int does not have a max
value anymore (except as limited by implementation details and system
resources).

sys.maxsize works as a reasonable substitute as it's the same as
sys.maxint.  The Python 3.0 release notes have this to say:

  The sys.maxint constant was removed, since there is no longer a limit
  to the value of integers. However, sys.maxsize can be used as an
  integer larger than any practical list or string index. It conforms to
  the implementation’s “natural” integer size and is typically the same
  as sys.maxint in previous releases on the same platform (assuming the
  same build options).

sys.maxsize is documented as:

  An integer giving the maximum value a variable of type Py_ssize_t can
  take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a
  64-bit platform.

Signed-off-by: Russell Bryant <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
russellb committed Feb 2, 2016
1 parent 981e956 commit d36bbd3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions python/ovs/db/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def column_set_from_json(json, columns):


class TableSchema(object):
def __init__(self, name, columns, mutable=True, max_rows=sys.maxint,
def __init__(self, name, columns, mutable=True, max_rows=sys.maxsize,
is_root=True, indexes=[]):
self.name = name
self.columns = columns
Expand All @@ -185,7 +185,7 @@ def from_json(json, name):
parser.finish()

if max_rows is None:
max_rows = sys.maxint
max_rows = sys.maxsize
elif max_rows <= 0:
raise error.Error("maxRows must be at least 1", json)

Expand Down Expand Up @@ -236,7 +236,7 @@ def to_json(self, default_is_root=False):
if not column.name.startswith("_"):
columns[column.name] = column.to_json()

if self.max_rows != sys.maxint:
if self.max_rows != sys.maxsize:
json["maxRows"] = self.max_rows

if self.indexes:
Expand Down
26 changes: 13 additions & 13 deletions python/ovs/db/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,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_name=None):
min_length=0, max_length=sys.maxsize, ref_table_name=None):
assert isinstance(type_, AtomicType)
self.type = type_
self.enum = enum
Expand Down Expand Up @@ -194,7 +194,7 @@ def from_json(json):
elif base.type == StringType:
base.min_length = BaseType.__parse_uint(parser, "minLength", 0)
base.max_length = BaseType.__parse_uint(parser, "maxLength",
sys.maxint)
sys.maxsize)
if base.min_length > base.max_length:
raise error.Error("minLength exceeds maxLength", json)
elif base.type == UuidType:
Expand Down Expand Up @@ -232,7 +232,7 @@ def to_json(self):
elif self.type == StringType:
if self.min_length != 0:
json['minLength'] = self.min_length
if self.max_length != sys.maxint:
if self.max_length != sys.maxsize:
json['maxLength'] = self.max_length
elif self.type == UuidType:
if self.ref_table_name:
Expand Down Expand Up @@ -260,7 +260,7 @@ def is_valid(self):
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 != sys.maxsize or
self.ref_table_name is not None)

def without_constraints(self):
Expand All @@ -270,7 +270,7 @@ def without_constraints(self):
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, sys.maxsize)

def is_ref(self):
return self.type == UuidType and self.ref_table_name is not None
Expand Down Expand Up @@ -322,7 +322,7 @@ def constraintsToEnglish(self, escapeLiteral=returnUnchanged,
english = 'at most %s' % escapeNumber(commafy(self.max))
else:
english = 'at most %s' % escapeNumber("%g" % self.max)
elif self.min_length != 0 and self.max_length != sys.maxint:
elif self.min_length != 0 and self.max_length != sys.maxsize:
if self.min_length == self.max_length:
english = ('exactly %s characters long'
% commafy(self.min_length))
Expand All @@ -332,7 +332,7 @@ def constraintsToEnglish(self, escapeLiteral=returnUnchanged,
commafy(self.max_length)))
elif self.min_length != 0:
return 'at least %s characters long' % commafy(self.min_length)
elif self.max_length != sys.maxint:
elif self.max_length != sys.maxsize:
english = 'at most %s characters long' % commafy(self.max_length)
else:
english = ''
Expand Down Expand Up @@ -407,7 +407,7 @@ def cInitBaseType(self, indent, var):
if self.min_length is not None:
stmts.append('%s.u.string.minLen = %d;'
% (var, self.min_length))
if self.max_length != sys.maxint:
if self.max_length != sys.maxsize:
stmts.append('%s.u.string.maxLen = %d;'
% (var, self.max_length))
elif self.type == UuidType:
Expand Down Expand Up @@ -482,7 +482,7 @@ def is_optional_pointer(self):
def __n_from_json(json, default):
if json is None:
return default
elif type(json) == int and 0 <= json <= sys.maxint:
elif isinstance(json, int) and 0 <= json <= sys.maxsize:
return json
else:
raise error.Error("bad min or max value", json)
Expand Down Expand Up @@ -512,7 +512,7 @@ def from_json(json):
n_min = Type.__n_from_json(min_json, Type.DEFAULT_MIN)

if max_json == 'unlimited':
n_max = sys.maxint
n_max = sys.maxsize
else:
n_max = Type.__n_from_json(max_json, Type.DEFAULT_MAX)

Expand All @@ -530,7 +530,7 @@ def to_json(self):
json["value"] = self.value.to_json()
if self.n_min != Type.DEFAULT_MIN:
json["min"] = self.n_min
if self.n_max == sys.maxint:
if self.n_max == sys.maxsize:
json["max"] = "unlimited"
elif self.n_max != Type.DEFAULT_MAX:
json["max"] = self.n_max
Expand All @@ -549,7 +549,7 @@ def toEnglish(self, escapeLiteral=returnUnchanged):
else:
return "optional %s" % keyName
else:
if self.n_max == sys.maxint:
if self.n_max == sys.maxsize:
if self.n_min:
quantity = "%s or more " % commafy(self.n_min)
else:
Expand Down Expand Up @@ -602,7 +602,7 @@ def cInitType(self, indent, var):
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 == sys.maxsize:
n_max = "UINT_MAX"
else:
n_max = self.n_max
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def __lex_finish_number(self):
if m:
sign, integer, fraction, exp = m.groups()
if (exp is not None and
(int(exp) > sys.maxint or int(exp) < -sys.maxint - 1)):
(int(exp) > sys.maxsize or int(exp) < -sys.maxsize - 1)):
self.__error("exponent outside valid range")
return

Expand Down
2 changes: 1 addition & 1 deletion python/ovs/vlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def init(log_file=None):

ovs.unixctl.command_register("vlog/reopen", "", 0, 0,
Vlog._unixctl_vlog_reopen, None)
ovs.unixctl.command_register("vlog/set", "spec", 1, sys.maxint,
ovs.unixctl.command_register("vlog/set", "spec", 1, sys.maxsize,
Vlog._unixctl_vlog_set, None)
ovs.unixctl.command_register("vlog/list", "", 0, 0,
Vlog._unixctl_vlog_list, None)
Expand Down

0 comments on commit d36bbd3

Please sign in to comment.