Commit 994f04db authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #28998: More APIs now support longs as well as ints.

parent 58c2c6eb
...@@ -133,7 +133,7 @@ class async_chat (asyncore.dispatcher): ...@@ -133,7 +133,7 @@ class async_chat (asyncore.dispatcher):
# no terminator, collect it all # no terminator, collect it all
self.collect_incoming_data (self.ac_in_buffer) self.collect_incoming_data (self.ac_in_buffer)
self.ac_in_buffer = '' self.ac_in_buffer = ''
elif isinstance(terminator, int) or isinstance(terminator, long): elif isinstance(terminator, (int, long)):
# numeric terminator # numeric terminator
n = terminator n = terminator
if lb < n: if lb < n:
......
...@@ -581,7 +581,7 @@ def getArgCount(args): ...@@ -581,7 +581,7 @@ def getArgCount(args):
def twobyte(val): def twobyte(val):
"""Convert an int argument into high and low bytes""" """Convert an int argument into high and low bytes"""
assert isinstance(val, int) assert isinstance(val, (int, long))
return divmod(val, 256) return divmod(val, 256)
class LineAddrTable: class LineAddrTable:
......
...@@ -1526,7 +1526,7 @@ for k, v in token.tok_name.items(): ...@@ -1526,7 +1526,7 @@ for k, v in token.tok_name.items():
def debug_tree(tree): def debug_tree(tree):
l = [] l = []
for elt in tree: for elt in tree:
if isinstance(elt, int): if isinstance(elt, (int, long)):
l.append(_names.get(elt, elt)) l.append(_names.get(elt, elt))
elif isinstance(elt, str): elif isinstance(elt, str):
l.append(elt) l.append(elt)
......
...@@ -1370,7 +1370,7 @@ class PseudoInputFile(PseudoFile): ...@@ -1370,7 +1370,7 @@ class PseudoInputFile(PseudoFile):
raise ValueError("read from closed file") raise ValueError("read from closed file")
if size is None: if size is None:
size = -1 size = -1
elif not isinstance(size, int): elif not isinstance(size, (int, long)):
raise TypeError('must be int, not ' + type(size).__name__) raise TypeError('must be int, not ' + type(size).__name__)
result = self._line_buffer result = self._line_buffer
self._line_buffer = '' self._line_buffer = ''
...@@ -1393,7 +1393,7 @@ class PseudoInputFile(PseudoFile): ...@@ -1393,7 +1393,7 @@ class PseudoInputFile(PseudoFile):
raise ValueError("read from closed file") raise ValueError("read from closed file")
if size is None: if size is None:
size = -1 size = -1
elif not isinstance(size, int): elif not isinstance(size, (int, long)):
raise TypeError('must be int, not ' + type(size).__name__) raise TypeError('must be int, not ' + type(size).__name__)
line = self._line_buffer or self.shell.readline() line = self._line_buffer or self.shell.readline()
if size < 0: if size < 0:
......
...@@ -1409,7 +1409,7 @@ def Time2Internaldate(date_time): ...@@ -1409,7 +1409,7 @@ def Time2Internaldate(date_time):
be in the correct format. be in the correct format.
""" """
if isinstance(date_time, (int, float)): if isinstance(date_time, (int, long, float)):
tt = time.localtime(date_time) tt = time.localtime(date_time)
elif isinstance(date_time, (tuple, time.struct_time)): elif isinstance(date_time, (tuple, time.struct_time)):
tt = date_time tt = date_time
......
...@@ -1174,9 +1174,9 @@ class Misc: ...@@ -1174,9 +1174,9 @@ class Misc:
elif isinstance(v, (tuple, list)): elif isinstance(v, (tuple, list)):
nv = [] nv = []
for item in v: for item in v:
if not isinstance(item, (basestring, int)): if not isinstance(item, (basestring, int, long)):
break break
elif isinstance(item, int): elif isinstance(item, (int, long)):
nv.append('%d' % item) nv.append('%d' % item)
else: else:
# format it to proper Tcl code if it contains space # format it to proper Tcl code if it contains space
......
...@@ -276,7 +276,7 @@ class Vec2D(tuple): ...@@ -276,7 +276,7 @@ class Vec2D(tuple):
return self[0]*other[0]+self[1]*other[1] return self[0]*other[0]+self[1]*other[1]
return Vec2D(self[0]*other, self[1]*other) return Vec2D(self[0]*other, self[1]*other)
def __rmul__(self, other): def __rmul__(self, other):
if isinstance(other, int) or isinstance(other, float): if isinstance(other, (int, long, float)):
return Vec2D(self[0]*other, self[1]*other) return Vec2D(self[0]*other, self[1]*other)
def __sub__(self, other): def __sub__(self, other):
return Vec2D(self[0]-other[0], self[1]-other[1]) return Vec2D(self[0]-other[0], self[1]-other[1])
...@@ -2352,7 +2352,7 @@ class TPen(object): ...@@ -2352,7 +2352,7 @@ class TPen(object):
self._resizemode = p["resizemode"] self._resizemode = p["resizemode"]
if "stretchfactor" in p: if "stretchfactor" in p:
sf = p["stretchfactor"] sf = p["stretchfactor"]
if isinstance(sf, (int, float)): if isinstance(sf, (int, long, float)):
sf = (sf, sf) sf = (sf, sf)
self._stretchfactor = sf self._stretchfactor = sf
if "outline" in p: if "outline" in p:
......
...@@ -74,7 +74,7 @@ class ParserGenerator(object): ...@@ -74,7 +74,7 @@ class ParserGenerator(object):
else: else:
# A named token (NAME, NUMBER, STRING) # A named token (NAME, NUMBER, STRING)
itoken = getattr(token, label, None) itoken = getattr(token, label, None)
assert isinstance(itoken, int), label assert isinstance(itoken, (int, long)), label
assert itoken in token.tok_name, label assert itoken in token.tok_name, label
if itoken in c.tokens: if itoken in c.tokens:
return c.tokens[itoken] return c.tokens[itoken]
......
...@@ -1222,7 +1222,7 @@ class Logger(Filterer): ...@@ -1222,7 +1222,7 @@ class Logger(Filterer):
logger.log(level, "We have a %s", "mysterious problem", exc_info=1) logger.log(level, "We have a %s", "mysterious problem", exc_info=1)
""" """
if not isinstance(level, int): if not isinstance(level, (int, long)):
if raiseExceptions: if raiseExceptions:
raise TypeError("level must be an integer") raise TypeError("level must be an integer")
else: else:
......
...@@ -86,7 +86,7 @@ class MaybeEncodingError(Exception): ...@@ -86,7 +86,7 @@ class MaybeEncodingError(Exception):
def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None): def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None):
assert maxtasks is None or (type(maxtasks) == int and maxtasks > 0) assert maxtasks is None or (type(maxtasks) in (int, long) and maxtasks > 0)
put = outqueue.put put = outqueue.put
get = inqueue.get get = inqueue.get
if hasattr(inqueue, '_writer'): if hasattr(inqueue, '_writer'):
......
...@@ -227,7 +227,7 @@ class Process(object): ...@@ -227,7 +227,7 @@ class Process(object):
else: else:
status = 'started' status = 'started'
if type(status) is int: if type(status) in (int, long):
if status == 0: if status == 0:
status = 'stopped' status = 'stopped'
else: else:
...@@ -262,8 +262,8 @@ class Process(object): ...@@ -262,8 +262,8 @@ class Process(object):
except SystemExit, e: except SystemExit, e:
if not e.args: if not e.args:
exitcode = 1 exitcode = 1
elif isinstance(e.args[0], int): elif isinstance(e.args[0], (int, long)):
exitcode = e.args[0] exitcode = int(e.args[0])
else: else:
sys.stderr.write(str(e.args[0]) + '\n') sys.stderr.write(str(e.args[0]) + '\n')
sys.stderr.flush() sys.stderr.flush()
......
...@@ -174,7 +174,7 @@ class Finalize(object): ...@@ -174,7 +174,7 @@ class Finalize(object):
Class which supports object finalization using weakrefs Class which supports object finalization using weakrefs
''' '''
def __init__(self, obj, callback, args=(), kwargs=None, exitpriority=None): def __init__(self, obj, callback, args=(), kwargs=None, exitpriority=None):
assert exitpriority is None or type(exitpriority) is int assert exitpriority is None or type(exitpriority) in (int, long)
if obj is not None: if obj is not None:
self._weakref = weakref.ref(obj, self) self._weakref = weakref.ref(obj, self)
......
...@@ -185,7 +185,7 @@ class ArgumentDescriptor(object): ...@@ -185,7 +185,7 @@ class ArgumentDescriptor(object):
assert isinstance(name, str) assert isinstance(name, str)
self.name = name self.name = name
assert isinstance(n, int) and (n >= 0 or assert isinstance(n, (int, long)) and (n >= 0 or
n in (UP_TO_NEWLINE, n in (UP_TO_NEWLINE,
TAKEN_FROM_ARGUMENT1, TAKEN_FROM_ARGUMENT1,
TAKEN_FROM_ARGUMENT4)) TAKEN_FROM_ARGUMENT4))
...@@ -873,7 +873,7 @@ class OpcodeInfo(object): ...@@ -873,7 +873,7 @@ class OpcodeInfo(object):
assert isinstance(x, StackObject) assert isinstance(x, StackObject)
self.stack_after = stack_after self.stack_after = stack_after
assert isinstance(proto, int) and 0 <= proto <= 2 assert isinstance(proto, (int, long)) and 0 <= proto <= 2
self.proto = proto self.proto = proto
assert isinstance(doc, str) assert isinstance(doc, str)
......
...@@ -507,7 +507,7 @@ class Popen(object): ...@@ -507,7 +507,7 @@ class Popen(object):
p2cread, _ = _subprocess.CreatePipe(None, 0) p2cread, _ = _subprocess.CreatePipe(None, 0)
elif stdin == PIPE: elif stdin == PIPE:
p2cread, p2cwrite = _subprocess.CreatePipe(None, 0) p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
elif isinstance(stdin, int): elif isinstance(stdin, (int, long)):
p2cread = msvcrt.get_osfhandle(stdin) p2cread = msvcrt.get_osfhandle(stdin)
else: else:
# Assuming file-like object # Assuming file-like object
...@@ -524,7 +524,7 @@ class Popen(object): ...@@ -524,7 +524,7 @@ class Popen(object):
_, c2pwrite = _subprocess.CreatePipe(None, 0) _, c2pwrite = _subprocess.CreatePipe(None, 0)
elif stdout == PIPE: elif stdout == PIPE:
c2pread, c2pwrite = _subprocess.CreatePipe(None, 0) c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
elif isinstance(stdout, int): elif isinstance(stdout, (int, long)):
c2pwrite = msvcrt.get_osfhandle(stdout) c2pwrite = msvcrt.get_osfhandle(stdout)
else: else:
# Assuming file-like object # Assuming file-like object
...@@ -543,7 +543,7 @@ class Popen(object): ...@@ -543,7 +543,7 @@ class Popen(object):
errread, errwrite = _subprocess.CreatePipe(None, 0) errread, errwrite = _subprocess.CreatePipe(None, 0)
elif stderr == STDOUT: elif stderr == STDOUT:
errwrite = c2pwrite errwrite = c2pwrite
elif isinstance(stderr, int): elif isinstance(stderr, (int, long)):
errwrite = msvcrt.get_osfhandle(stderr) errwrite = msvcrt.get_osfhandle(stderr)
else: else:
# Assuming file-like object # Assuming file-like object
...@@ -800,7 +800,7 @@ class Popen(object): ...@@ -800,7 +800,7 @@ class Popen(object):
elif stdin == PIPE: elif stdin == PIPE:
p2cread, p2cwrite = self.pipe_cloexec() p2cread, p2cwrite = self.pipe_cloexec()
to_close.update((p2cread, p2cwrite)) to_close.update((p2cread, p2cwrite))
elif isinstance(stdin, int): elif isinstance(stdin, (int, long)):
p2cread = stdin p2cread = stdin
else: else:
# Assuming file-like object # Assuming file-like object
...@@ -811,7 +811,7 @@ class Popen(object): ...@@ -811,7 +811,7 @@ class Popen(object):
elif stdout == PIPE: elif stdout == PIPE:
c2pread, c2pwrite = self.pipe_cloexec() c2pread, c2pwrite = self.pipe_cloexec()
to_close.update((c2pread, c2pwrite)) to_close.update((c2pread, c2pwrite))
elif isinstance(stdout, int): elif isinstance(stdout, (int, long)):
c2pwrite = stdout c2pwrite = stdout
else: else:
# Assuming file-like object # Assuming file-like object
...@@ -827,7 +827,7 @@ class Popen(object): ...@@ -827,7 +827,7 @@ class Popen(object):
errwrite = c2pwrite errwrite = c2pwrite
else: # child's stdout is not set, use parent's stdout else: # child's stdout is not set, use parent's stdout
errwrite = sys.__stdout__.fileno() errwrite = sys.__stdout__.fileno()
elif isinstance(stderr, int): elif isinstance(stderr, (int, long)):
errwrite = stderr errwrite = stderr
else: else:
# Assuming file-like object # Assuming file-like object
......
...@@ -10,7 +10,7 @@ class _InterruptHandler(object): ...@@ -10,7 +10,7 @@ class _InterruptHandler(object):
def __init__(self, default_handler): def __init__(self, default_handler):
self.called = False self.called = False
self.original_handler = default_handler self.original_handler = default_handler
if isinstance(default_handler, int): if isinstance(default_handler, (int, long)):
if default_handler == signal.SIG_DFL: if default_handler == signal.SIG_DFL:
# Pretend it's signal.default_int_handler instead. # Pretend it's signal.default_int_handler instead.
default_handler = signal.default_int_handler default_handler = signal.default_int_handler
......
...@@ -84,10 +84,10 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0, ...@@ -84,10 +84,10 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
"category must be a class" "category must be a class"
assert issubclass(category, Warning), "category must be a Warning subclass" assert issubclass(category, Warning), "category must be a Warning subclass"
assert isinstance(module, basestring), "module must be a string" assert isinstance(module, basestring), "module must be a string"
assert isinstance(lineno, int) and lineno >= 0, \ assert isinstance(lineno, (int, long)) and lineno >= 0, \
"lineno must be an int >= 0" "lineno must be an int >= 0"
item = (action, re.compile(message, re.I), category, item = (action, re.compile(message, re.I), category,
re.compile(module), lineno) re.compile(module), int(lineno))
if append: if append:
filters.append(item) filters.append(item)
else: else:
...@@ -105,9 +105,9 @@ def simplefilter(action, category=Warning, lineno=0, append=0): ...@@ -105,9 +105,9 @@ def simplefilter(action, category=Warning, lineno=0, append=0):
""" """
assert action in ("error", "ignore", "always", "default", "module", assert action in ("error", "ignore", "always", "default", "module",
"once"), "invalid action: %r" % (action,) "once"), "invalid action: %r" % (action,)
assert isinstance(lineno, int) and lineno >= 0, \ assert isinstance(lineno, (int, long)) and lineno >= 0, \
"lineno must be an int >= 0" "lineno must be an int >= 0"
item = (action, None, category, None, lineno) item = (action, None, category, None, int(lineno))
if append: if append:
filters.append(item) filters.append(item)
else: else:
......
...@@ -15,6 +15,8 @@ Core and Builtins ...@@ -15,6 +15,8 @@ Core and Builtins
Library Library
------- -------
- Issue #28998: More APIs now support longs as well as ints.
- Issue 28923: Remove editor artifacts from Tix.py, - Issue 28923: Remove editor artifacts from Tix.py,
including encoding not recognized by codecs.lookup. including encoding not recognized by codecs.lookup.
......
...@@ -220,15 +220,19 @@ _set_bool(const char *name, int *target, PyObject *src, int dflt) ...@@ -220,15 +220,19 @@ _set_bool(const char *name, int *target, PyObject *src, int dflt)
static int static int
_set_int(const char *name, int *target, PyObject *src, int dflt) _set_int(const char *name, int *target, PyObject *src, int dflt)
{ {
int value;
if (src == NULL) if (src == NULL)
*target = dflt; *target = dflt;
else { else {
if (!PyInt_Check(src)) { if (!PyInt_Check(src) && !PyLong_Check(src)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"\"%s\" must be an integer", name); "\"%s\" must be an integer", name);
return -1; return -1;
} }
*target = PyInt_AsLong(src); value = PyInt_AsLong(src);
if (value == -1 && PyErr_Occurred())
return -1;
*target = value;
} }
return 0; return 0;
} }
...@@ -1443,17 +1447,20 @@ static PyObject * ...@@ -1443,17 +1447,20 @@ static PyObject *
csv_field_size_limit(PyObject *module, PyObject *args) csv_field_size_limit(PyObject *module, PyObject *args)
{ {
PyObject *new_limit = NULL; PyObject *new_limit = NULL;
long old_limit = field_limit; long old_limit = field_limit, limit;
if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit)) if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
return NULL; return NULL;
if (new_limit != NULL) { if (new_limit != NULL) {
if (!PyInt_Check(new_limit)) { if (!PyInt_Check(new_limit) && !PyLong_Check(new_limit)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"limit must be an integer"); "limit must be an integer");
return NULL; return NULL;
} }
field_limit = PyInt_AsLong(new_limit); limit = PyInt_AsLong(new_limit);
if (limit == -1 && PyErr_Occurred())
return NULL;
field_limit = limit;
} }
return PyInt_FromLong(old_limit); return PyInt_FromLong(old_limit);
} }
......
...@@ -194,8 +194,10 @@ PyCursesCheckERR(int code, char *fname) ...@@ -194,8 +194,10 @@ PyCursesCheckERR(int code, char *fname)
static int static int
PyCurses_ConvertToChtype(PyObject *obj, chtype *ch) PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
{ {
if (PyInt_Check(obj)) { if (PyInt_Check(obj) || PyLong_Check(obj)) {
*ch = (chtype) PyInt_AsLong(obj); *ch = (chtype) PyInt_AsLong(obj);
if (*ch == (chtype) -1 && PyErr_Occurred())
return 0;
} else if(PyString_Check(obj) } else if(PyString_Check(obj)
&& (PyString_Size(obj) == 1)) { && (PyString_Size(obj) == 1)) {
*ch = (chtype) *PyString_AsString(obj); *ch = (chtype) *PyString_AsString(obj);
...@@ -2576,8 +2578,11 @@ PyCurses_UnCtrl(PyObject *self, PyObject *args) ...@@ -2576,8 +2578,11 @@ PyCurses_UnCtrl(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL; if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
if (PyInt_Check(temp)) if (PyInt_Check(temp) || PyLong_Check(temp)) {
ch = (chtype) PyInt_AsLong(temp); ch = (chtype) PyInt_AsLong(temp);
if (ch == (chtype) -1 && PyErr_Occurred())
return NULL;
}
else if (PyString_Check(temp)) else if (PyString_Check(temp))
ch = (chtype) *PyString_AsString(temp); ch = (chtype) *PyString_AsString(temp);
else { else {
...@@ -2598,8 +2603,11 @@ PyCurses_UngetCh(PyObject *self, PyObject *args) ...@@ -2598,8 +2603,11 @@ PyCurses_UngetCh(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL; if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
if (PyInt_Check(temp)) if (PyInt_Check(temp) || PyLong_Check(temp)) {
ch = (int) PyInt_AsLong(temp); ch = (int) PyInt_AsLong(temp);
if (ch == -1 && PyErr_Occurred())
return NULL;
}
else if (PyString_Check(temp)) else if (PyString_Check(temp))
ch = (int) *PyString_AsString(temp); ch = (int) *PyString_AsString(temp);
else { else {
......
...@@ -107,8 +107,11 @@ dl_call(dlobject *xp, PyObject *args) ...@@ -107,8 +107,11 @@ dl_call(dlobject *xp, PyObject *args)
} }
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
PyObject *v = PyTuple_GetItem(args, i); PyObject *v = PyTuple_GetItem(args, i);
if (PyInt_Check(v)) if (PyInt_Check(v) || PyLong_Check(v)) {
alist[i-1] = PyInt_AsLong(v); alist[i-1] = PyInt_AsLong(v);
if (alist[i-1] == -1 && PyErr_Occurred())
return NULL;
}
else if (PyString_Check(v)) else if (PyString_Check(v))
alist[i-1] = (long)PyString_AsString(v); alist[i-1] = (long)PyString_AsString(v);
else if (v == Py_None) else if (v == Py_None)
......
...@@ -686,7 +686,7 @@ sv_LoadMap(svobject *self, PyObject *args) ...@@ -686,7 +686,7 @@ sv_LoadMap(svobject *self, PyObject *args)
if (!cell) if (!cell)
goto finally; goto finally;
if (!PyInt_Check(cell)) { if (!PyInt_Check(cell) && !PyLong_Check(cell)) {
PyErr_BadArgument(); PyErr_BadArgument();
goto finally; goto finally;
} }
...@@ -757,7 +757,7 @@ doParams(svobject *self, PyObject *args, ...@@ -757,7 +757,7 @@ doParams(svobject *self, PyObject *args,
if (!v) if (!v)
goto finally; goto finally;
if (!PyInt_Check(v)) { if (!PyInt_Check(v) && !PyLong_Check(v)) {
PyErr_BadArgument(); PyErr_BadArgument();
goto finally; goto finally;
} }
......
...@@ -185,8 +185,11 @@ termios_tcsetattr(PyObject *self, PyObject *args) ...@@ -185,8 +185,11 @@ termios_tcsetattr(PyObject *self, PyObject *args)
if (PyString_Check(v) && PyString_Size(v) == 1) if (PyString_Check(v) && PyString_Size(v) == 1)
mode.c_cc[i] = (cc_t) * PyString_AsString(v); mode.c_cc[i] = (cc_t) * PyString_AsString(v);
else if (PyInt_Check(v)) else if (PyInt_Check(v) || PyLong_Check(v)) {
mode.c_cc[i] = (cc_t) PyInt_AsLong(v); mode.c_cc[i] = (cc_t) PyInt_AsLong(v);
if (mode.c_cc[i] == (cc_t) -1 && PyErr_Occurred())
return NULL;
}
else { else {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"tcsetattr: elements of attributes must be characters or integers"); "tcsetattr: elements of attributes must be characters or integers");
......
...@@ -155,6 +155,11 @@ PyInt_AsLong(register PyObject *op) ...@@ -155,6 +155,11 @@ PyInt_AsLong(register PyObject *op)
return -1; return -1;
} }
if (PyLong_CheckExact(op)) {
/* avoid creating temporary int object */
return PyLong_AsLong(op);
}
io = (PyIntObject*) (*nb->nb_int) (op); io = (PyIntObject*) (*nb->nb_int) (op);
if (io == NULL) if (io == NULL)
return -1; return -1;
...@@ -163,8 +168,6 @@ PyInt_AsLong(register PyObject *op) ...@@ -163,8 +168,6 @@ PyInt_AsLong(register PyObject *op)
/* got a long? => retry int conversion */ /* got a long? => retry int conversion */
val = PyLong_AsLong((PyObject *)io); val = PyLong_AsLong((PyObject *)io);
Py_DECREF(io); Py_DECREF(io);
if ((val == -1) && PyErr_Occurred())
return -1;
return val; return val;
} }
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment