Author: Philip Jenvey <pjen...@underboss.org> Branch: py3k Changeset: r73929:83c6e78c4f7d Date: 2014-10-13 14:51 -0700 http://bitbucket.org/pypy/pypy/changeset/83c6e78c4f7d/
Log: issue1884: kill PyInt_* diff --git a/pypy/module/cpyext/intobject.py b/pypy/module/cpyext/intobject.py deleted file mode 100644 --- a/pypy/module/cpyext/intobject.py +++ /dev/null @@ -1,162 +0,0 @@ - -from rpython.rtyper.lltypesystem import rffi, lltype -from pypy.interpreter.error import OperationError -from pypy.module.cpyext.api import ( - cpython_api, cpython_struct, build_type_checkers, bootstrap_function, - PyObject, PyObjectFields, CONST_STRING, CANNOT_FAIL, Py_ssize_t) -from pypy.module.cpyext.pyobject import ( - make_typedescr, track_reference, RefcountState, from_ref) -from rpython.rlib.rarithmetic import r_uint, intmask, LONG_TEST, r_ulonglong -from pypy.objspace.std.intobject import W_IntObject -import sys - -PyIntObjectStruct = lltype.ForwardReference() -PyIntObject = lltype.Ptr(PyIntObjectStruct) -PyIntObjectFields = PyObjectFields + \ - (("ob_ival", rffi.LONG),) -cpython_struct("PyIntObject", PyIntObjectFields, PyIntObjectStruct) - -@bootstrap_function -def init_intobject(space): - "Type description of PyIntObject" - make_typedescr(space.w_int.instancetypedef, - basestruct=PyIntObject.TO, - attach=int_attach, - realize=int_realize) - -def int_attach(space, py_obj, w_obj): - """ - Fills a newly allocated PyIntObject with the given int object. The - value must not be modified. - """ - py_int = rffi.cast(PyIntObject, py_obj) - py_int.c_ob_ival = space.int_w(w_obj) - -def int_realize(space, obj): - intval = rffi.cast(lltype.Signed, rffi.cast(PyIntObject, obj).c_ob_ival) - w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type)) - w_obj = space.allocate_instance(W_IntObject, w_type) - w_obj.__init__(intval) - track_reference(space, obj, w_obj) - state = space.fromcache(RefcountState) - state.set_lifeline(w_obj, obj) - return w_obj - -PyInt_Check, PyInt_CheckExact = build_type_checkers("Int") - -@cpython_api([], lltype.Signed, error=CANNOT_FAIL) -def PyInt_GetMax(space): - """Return the system's idea of the largest integer it can handle (LONG_MAX, - as defined in the system header files).""" - return sys.maxint - -@cpython_api([lltype.Signed], PyObject) -def PyInt_FromLong(space, ival): - """Create a new integer object with a value of ival. - - """ - return space.wrap(ival) - -@cpython_api([PyObject], lltype.Signed, error=-1) -def PyInt_AsLong(space, w_obj): - """Will first attempt to cast the object to a PyIntObject, if it is not - already one, and then return its value. If there is an error, -1 is - returned, and the caller should check PyErr_Occurred() to find out whether - there was an error, or whether the value just happened to be -1.""" - if w_obj is None: - raise OperationError(space.w_TypeError, - space.wrap("an integer is required, got NULL")) - return space.int_w(space.int(w_obj)) - -@cpython_api([PyObject], lltype.Unsigned, error=-1) -def PyInt_AsUnsignedLong(space, w_obj): - """Return a C unsigned long representation of the contents of pylong. - If pylong is greater than ULONG_MAX, an OverflowError is - raised.""" - if w_obj is None: - raise OperationError(space.w_TypeError, - space.wrap("an integer is required, got NULL")) - return space.uint_w(space.int(w_obj)) - - -@cpython_api([PyObject], rffi.ULONG, error=-1) -def PyInt_AsUnsignedLongMask(space, w_obj): - """Will first attempt to cast the object to a PyIntObject or - PyLongObject, if it is not already one, and then return its value as - unsigned long. This function does not check for overflow. - """ - w_int = space.int(w_obj) - num = space.bigint_w(w_int) - return num.uintmask() - - -@cpython_api([PyObject], rffi.ULONGLONG, error=-1) -def PyInt_AsUnsignedLongLongMask(space, w_obj): - """Will first attempt to cast the object to a PyIntObject or - PyLongObject, if it is not already one, and then return its value as - unsigned long long, without checking for overflow. - """ - w_int = space.int(w_obj) - if space.isinstance_w(w_int, space.w_int): - num = space.int_w(w_int) - return r_ulonglong(num) - else: - num = space.bigint_w(w_int) - return num.ulonglongmask() - -@cpython_api([PyObject], lltype.Signed, error=CANNOT_FAIL) -def PyInt_AS_LONG(space, w_int): - """Return the value of the object w_int. No error checking is performed.""" - return space.int_w(w_int) - -@cpython_api([PyObject], Py_ssize_t, error=-1) -def PyInt_AsSsize_t(space, w_obj): - """Will first attempt to cast the object to a PyIntObject or - PyLongObject, if it is not already one, and then return its value as - Py_ssize_t. - """ - if w_obj is None: - raise OperationError(space.w_TypeError, - space.wrap("an integer is required, got NULL")) - return space.int_w(w_obj) # XXX this is wrong on win64 - -LONG_MAX = int(LONG_TEST - 1) - -@cpython_api([rffi.SIZE_T], PyObject) -def PyInt_FromSize_t(space, ival): - """Create a new integer object with a value of ival. If the value exceeds - LONG_MAX, a long integer object is returned. - """ - if ival <= LONG_MAX: - return space.wrap(intmask(ival)) - return space.wrap(ival) - -@cpython_api([Py_ssize_t], PyObject) -def PyInt_FromSsize_t(space, ival): - """Create a new integer object with a value of ival. If the value is larger - than LONG_MAX or smaller than LONG_MIN, a long integer object is - returned. - """ - return space.wrap(ival) - -@cpython_api([CONST_STRING, rffi.CCHARPP, rffi.INT_real], PyObject) -def PyInt_FromString(space, str, pend, base): - """Return a new PyIntObject or PyLongObject based on the string - value in str, which is interpreted according to the radix in base. If - pend is non-NULL, *pend will point to the first character in str which - follows the representation of the number. If base is 0, the radix will be - determined based on the leading characters of str: if str starts with - '0x' or '0X', radix 16 will be used; if str starts with '0', radix - 8 will be used; otherwise radix 10 will be used. If base is not 0, it - must be between 2 and 36, inclusive. Leading spaces are ignored. If - there are no digits, ValueError will be raised. If the string represents - a number too large to be contained within the machine's long int type - and overflow warnings are being suppressed, a PyLongObject will be - returned. If overflow warnings are not being suppressed, NULL will be - returned in this case.""" - s = rffi.charp2str(str) - w_str = space.wrap(s) - w_base = space.wrap(rffi.cast(lltype.Signed, base)) - if pend: - pend[0] = rffi.ptradd(str, len(s)) - return space.call_function(space.w_int, w_str, w_base) diff --git a/pypy/module/cpyext/test/test_intobject.py b/pypy/module/cpyext/test/test_intobject.py deleted file mode 100644 --- a/pypy/module/cpyext/test/test_intobject.py +++ /dev/null @@ -1,195 +0,0 @@ -import py.test -from pypy.module.cpyext.test.test_api import BaseApiTest -from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase -import sys - -py.test.skip("PyInt_ APIs were removed from py3k") - -class TestIntObject(BaseApiTest): - def test_intobject(self, space, api): - assert api.PyInt_Check(space.wrap(3)) - assert api.PyInt_Check(space.w_True) - assert not api.PyInt_Check(space.wrap((1, 2, 3))) - for i in [3, -5, -1, -sys.maxint, sys.maxint - 1]: - x = api.PyInt_AsLong(space.wrap(i)) - y = api.PyInt_AS_LONG(space.wrap(i)) - assert x == i - assert y == i - w_x = api.PyInt_FromLong(x + 1) - assert space.type(w_x) is space.w_int - assert space.eq_w(w_x, space.wrap(i + 1)) - - assert api.PyInt_AsLong(space.w_None) == -1 - assert api.PyErr_Occurred() is space.w_TypeError - api.PyErr_Clear() - - assert api.PyInt_AsLong(None) == -1 - assert api.PyErr_Occurred() is space.w_TypeError - api.PyErr_Clear() - - assert api.PyInt_AsUnsignedLong(space.wrap(sys.maxint)) == sys.maxint - assert api.PyInt_AsUnsignedLong(space.wrap(-5)) == sys.maxint * 2 + 1 - assert api.PyErr_Occurred() is space.w_ValueError - api.PyErr_Clear() - - assert (api.PyInt_AsUnsignedLongMask(space.wrap(sys.maxint)) - == sys.maxint) - assert (api.PyInt_AsUnsignedLongMask(space.wrap(10**30)) - == 10**30 % ((sys.maxint + 1) * 2)) - - assert (api.PyInt_AsUnsignedLongLongMask(space.wrap(sys.maxint)) - == sys.maxint) - assert (api.PyInt_AsUnsignedLongLongMask(space.wrap(10**30)) - == 10**30 % (2**64)) - - def test_coerce(self, space, api): - w_obj = space.appexec([], """(): - class Coerce(object): - def __int__(self): - return 42 - return Coerce()""") - assert api.PyInt_AsLong(w_obj) == 42 - -class AppTestIntObject(AppTestCpythonExtensionBase): - def test_fromstring(self): - module = self.import_extension('foo', [ - ("from_string", "METH_NOARGS", - """ - return PyInt_FromString("1234", NULL, 16); - """), - ]) - assert module.from_string() == 0x1234 - assert type(module.from_string()) is int - - def test_size_t(self): - module = self.import_extension('foo', [ - ("values", "METH_NOARGS", - """ - return Py_BuildValue("NNNN", - PyInt_FromSize_t(123), - PyInt_FromSize_t((size_t)-1), - PyInt_FromSsize_t(123), - PyInt_FromSsize_t((size_t)-1)); - """), - ]) - values = module.values() - types = [type(x) for x in values] - assert types == [int, int, int, int] - - def test_int_subtype(self): - module = self.import_extension( - 'foo', [ - ("newEnum", "METH_VARARGS", - """ - EnumObject *enumObj; - long intval; - PyObject *name; - - if (!PyArg_ParseTuple(args, "Oi", &name, &intval)) - return NULL; - - PyType_Ready(&Enum_Type); - enumObj = PyObject_New(EnumObject, &Enum_Type); - if (!enumObj) { - return NULL; - } - - enumObj->ob_ival = intval; - Py_INCREF(name); - enumObj->ob_name = name; - - return (PyObject *)enumObj; - """), - ], - prologue=""" - typedef struct - { - PyObject_HEAD - long ob_ival; - PyObject* ob_name; - } EnumObject; - - static void - enum_dealloc(PyObject *op) - { - Py_DECREF(((EnumObject *)op)->ob_name); - Py_TYPE(op)->tp_free(op); - } - - static PyMemberDef enum_members[] = { - {"name", T_OBJECT, offsetof(EnumObject, ob_name), 0, NULL}, - {NULL} /* Sentinel */ - }; - - PyTypeObject Enum_Type = { - PyObject_HEAD_INIT(0) - /*ob_size*/ 0, - /*tp_name*/ "Enum", - /*tp_basicsize*/ sizeof(EnumObject), - /*tp_itemsize*/ 0, - /*tp_dealloc*/ enum_dealloc, - /*tp_print*/ 0, - /*tp_getattr*/ 0, - /*tp_setattr*/ 0, - /*tp_compare*/ 0, - /*tp_repr*/ 0, - /*tp_as_number*/ 0, - /*tp_as_sequence*/ 0, - /*tp_as_mapping*/ 0, - /*tp_hash*/ 0, - /*tp_call*/ 0, - /*tp_str*/ 0, - /*tp_getattro*/ 0, - /*tp_setattro*/ 0, - /*tp_as_buffer*/ 0, - /*tp_flags*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, - /*tp_doc*/ 0, - /*tp_traverse*/ 0, - /*tp_clear*/ 0, - /*tp_richcompare*/ 0, - /*tp_weaklistoffset*/ 0, - /*tp_iter*/ 0, - /*tp_iternext*/ 0, - /*tp_methods*/ 0, - /*tp_members*/ enum_members, - /*tp_getset*/ 0, - /*tp_base*/ 0, /* set to &PyInt_Type in init function for MSVC */ - /*tp_dict*/ 0, - /*tp_descr_get*/ 0, - /*tp_descr_set*/ 0, - /*tp_dictoffset*/ 0, - /*tp_init*/ 0, - /*tp_alloc*/ 0, - /*tp_new*/ 0 - }; - """, more_init = ''' - Enum_Type.tp_base = &PyInt_Type; - ''') - - a = module.newEnum("ULTIMATE_ANSWER", 42) - assert type(a).__name__ == "Enum" - assert isinstance(a, int) - assert a == int(a) == 42 - assert a.name == "ULTIMATE_ANSWER" - - def test_int_cast(self): - mod = self.import_extension('foo', [ - #prove it works for ints - ("test_int", "METH_NOARGS", - """ - PyObject * obj = PyInt_FromLong(42); - PyObject * val; - if (!PyInt_Check(obj)) { - Py_DECREF(obj); - PyErr_SetNone(PyExc_ValueError); - return NULL; - } - val = PyInt_FromLong(((PyIntObject *)obj)->ob_ival); - Py_DECREF(obj); - return val; - """ - ), - ]) - i = mod.test_int() - assert isinstance(i, int) - assert i == 42 diff --git a/pypy/module/cpyext/test/test_longobject.py b/pypy/module/cpyext/test/test_longobject.py --- a/pypy/module/cpyext/test/test_longobject.py +++ b/pypy/module/cpyext/test/test_longobject.py @@ -1,6 +1,5 @@ import sys, py from rpython.rtyper.lltypesystem import rffi, lltype -from pypy.objspace.std.intobject import W_IntObject from pypy.objspace.std.longobject import W_LongObject from pypy.module.cpyext.test.test_api import BaseApiTest from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit