]> granicus.if.org Git - python/commitdiff
Issue #17576: Deprecation warning emitted now when __int__() or __index__()
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 11 Dec 2013 19:26:36 +0000 (21:26 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 11 Dec 2013 19:26:36 +0000 (21:26 +0200)
return not int instance.  Introduced _PyLong_FromNbInt() and refactored
PyLong_As*() functions.

1  2 
Include/longobject.h
Lib/test/test_getargs2.py
Lib/test/test_int.py
Misc/NEWS
Objects/abstract.c
Objects/longobject.c

Simple merge
Simple merge
Simple merge
diff --cc Misc/NEWS
index edafe29b0fee37b58a3e01765d967a4b8c0176b8,0acfa7242397eb87fa894bb675f14a4c53696ada..ad3cad97d3f50d7e4c8a116602252cf223a6563c
+++ b/Misc/NEWS
@@@ -10,11 -10,11 +10,14 @@@ Release date: 2014-01-0
  Core and Builtins
  -----------------
  
+ - Issue #17576: Deprecation warning emitted now when __int__() or __index__()
+   return not int instance.
  - Issue #19932: Fix typo in import.h, missing whitespaces in function prototypes.
  
 +- Issue #19736: Add module-level statvfs constants defined for GNU/glibc
 +  based systems.
 +
  - Issue #19729: In str.format(), fix recursive expansion in format spec.
  
  - Issue #19638: Fix possible crash / undefined behaviour from huge (more than 2
Simple merge
index 68a667e7714c1abbd858eb7c7eec370368dd63a3,29040e60a9f467215d92ba42d343d620aadb6042..9411216bcbebc10bbba20eef8adf60be890c2902
@@@ -634,9 -667,8 +673,8 @@@ _PyLong_AsUnsignedLongMask(PyObject *vv
  }
  
  unsigned long
 -PyLong_AsUnsignedLongMask(register PyObject *op)
 +PyLong_AsUnsignedLongMask(PyObject *op)
  {
-     PyNumberMethods *nb;
      PyLongObject *lo;
      unsigned long val;
  
@@@ -1166,41 -1195,43 +1194,42 @@@ PyLong_AsLongLong(PyObject *vv
  {
      PyLongObject *v;
      PY_LONG_LONG bytes;
 -    int one = 1;
      int res;
+     int do_decref = 0; /* if nb_int was called */
  
      if (vv == NULL) {
          PyErr_BadInternalCall();
          return -1;
      }
-     if (!PyLong_Check(vv)) {
-         PyNumberMethods *nb;
-         PyObject *io;
-         if ((nb = vv->ob_type->tp_as_number) == NULL ||
-             nb->nb_int == NULL) {
-             PyErr_SetString(PyExc_TypeError, "an integer is required");
-             return -1;
-         }
-         io = (*nb->nb_int) (vv);
-         if (io == NULL)
+     if (PyLong_Check(vv)) {
+         v = (PyLongObject *)vv;
+     }
+     else {
+         v = _PyLong_FromNbInt(vv);
+         if (v == NULL)
              return -1;
-         if (PyLong_Check(io)) {
-             bytes = PyLong_AsLongLong(io);
-             Py_DECREF(io);
-             return bytes;
-         }
-         Py_DECREF(io);
-         PyErr_SetString(PyExc_TypeError, "integer conversion failed");
-         return -1;
+         do_decref = 1;
      }
  
-     v = (PyLongObject*)vv;
+     res = 0;
      switch(Py_SIZE(v)) {
-     case -1: return -(sdigit)v->ob_digit[0];
-     case 0: return 0;
-     case 1: return v->ob_digit[0];
+     case -1:
+         bytes = -(sdigit)v->ob_digit[0];
+         break;
+     case 0:
+         bytes = 0;
+         break;
+     case 1:
+         bytes = v->ob_digit[0];
+         break;
+     default:
+         res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes,
 -                                  SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
++                                  SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1);
+     }
+     if (do_decref) {
+         Py_DECREF(v);
      }
-     res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
-                               SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1);
  
      /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
      if (res < 0)
@@@ -1278,39 -1310,29 +1307,28 @@@ _PyLong_AsUnsignedLongLongMask(PyObjec
  }
  
  unsigned PY_LONG_LONG
 -PyLong_AsUnsignedLongLongMask(register PyObject *op)
 +PyLong_AsUnsignedLongLongMask(PyObject *op)
  {
-     PyNumberMethods *nb;
      PyLongObject *lo;
      unsigned PY_LONG_LONG val;
  
-     if (op && PyLong_Check(op))
-         return _PyLong_AsUnsignedLongLongMask(op);
+     if (op == NULL) {
+         PyErr_BadInternalCall();
+         return (unsigned long)-1;
+     }
  
-     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
-         nb->nb_int == NULL) {
-         PyErr_SetString(PyExc_TypeError, "an integer is required");
-         return (unsigned PY_LONG_LONG)-1;
+     if (PyLong_Check(op)) {
+         return _PyLong_AsUnsignedLongLongMask(op);
      }
  
-     lo = (PyLongObject*) (*nb->nb_int) (op);
+     lo = _PyLong_FromNbInt(op);
      if (lo == NULL)
          return (unsigned PY_LONG_LONG)-1;
-     if (PyLong_Check(lo)) {
-         val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
-         Py_DECREF(lo);
-         if (PyErr_Occurred())
-             return (unsigned PY_LONG_LONG)-1;
-         return val;
-     }
-     else
-     {
-         Py_DECREF(lo);
-         PyErr_SetString(PyExc_TypeError,
-                         "nb_int should return int object");
-         return (unsigned PY_LONG_LONG)-1;
-     }
+     val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
+     Py_DECREF(lo);
+     return val;
  }
 -#undef IS_LITTLE_ENDIAN
  
  /* Get a C long long int from an int object or any object that has an
     __int__ method.