diff options
author | Christian Heimes <christian@cheimes.de> | 2008-01-25 11:23:10 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-01-25 11:23:10 (GMT) |
commit | 969fe57baa0eb80332990f9cda936a33e13fabef (patch) | |
tree | 050188e2c28aa859b072b1a2e860f268a2534a42 /Modules | |
parent | bad17e7b176334a3b1dc92a4e496e1f8f4ff34a7 (diff) | |
download | cpython-969fe57baa0eb80332990f9cda936a33e13fabef.zip cpython-969fe57baa0eb80332990f9cda936a33e13fabef.tar.gz cpython-969fe57baa0eb80332990f9cda936a33e13fabef.tar.bz2 |
Merged revisions 60245-60277 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60246 | guido.van.rossum | 2008-01-24 18:58:05 +0100 (Thu, 24 Jan 2008) | 2 lines
Fix test67.py from issue #1303614.
........
r60248 | raymond.hettinger | 2008-01-24 19:05:54 +0100 (Thu, 24 Jan 2008) | 1 line
Clean-up and speed-up code by accessing numerator/denominator directly. There's no reason to enforce readonliness
........
r60249 | raymond.hettinger | 2008-01-24 19:12:23 +0100 (Thu, 24 Jan 2008) | 1 line
Revert 60189 and restore performance.
........
r60250 | guido.van.rossum | 2008-01-24 19:21:02 +0100 (Thu, 24 Jan 2008) | 5 lines
News about recently fixed crashers:
- A few crashers fixed: weakref_in_del.py (issue #1377858);
loosing_dict_ref.py (issue #1303614, test67.py);
borrowed_ref_[34].py (not in tracker).
........
r60252 | thomas.heller | 2008-01-24 19:36:27 +0100 (Thu, 24 Jan 2008) | 7 lines
Use a PyDictObject again for the array type cache; retrieving items
from the WeakValueDictionary was slower by nearly a factor of 3.
To avoid leaks, weakref proxies for the array types are put into the
cache dict, with weakref callbacks that removes the entries when the
type goes away.
........
r60253 | thomas.heller | 2008-01-24 19:54:12 +0100 (Thu, 24 Jan 2008) | 2 lines
Replace Py_BuildValue with PyTuple_Pack because it is faster.
Also add a missing DECREF.
........
r60254 | raymond.hettinger | 2008-01-24 20:05:29 +0100 (Thu, 24 Jan 2008) | 1 line
Add support for trunc().
........
r60255 | thomas.heller | 2008-01-24 20:15:02 +0100 (Thu, 24 Jan 2008) | 5 lines
Invert the checks in get_[u]long and get_[u]longlong. The intent was
to not accept float types; the result was that integer-like objects
were not accepted.
Ported from release25-maint.
........
r60256 | raymond.hettinger | 2008-01-24 20:30:19 +0100 (Thu, 24 Jan 2008) | 1 line
Add support for int(r) just like the other numeric classes.
........
r60263 | raymond.hettinger | 2008-01-24 22:23:58 +0100 (Thu, 24 Jan 2008) | 1 line
Expand tests to include nested graph structures.
........
r60264 | raymond.hettinger | 2008-01-24 22:47:56 +0100 (Thu, 24 Jan 2008) | 1 line
Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
........
r60265 | amaury.forgeotdarc | 2008-01-24 23:51:18 +0100 (Thu, 24 Jan 2008) | 14 lines
#1920: when considering a block starting by "while 0", the compiler optimized the
whole construct away, even when an 'else' clause is present::
while 0:
print("no")
else:
print("yes")
did not generate any code at all.
Now the compiler emits the 'else' block, like it already does for 'if' statements.
Will backport.
........
r60266 | amaury.forgeotdarc | 2008-01-24 23:59:25 +0100 (Thu, 24 Jan 2008) | 2 lines
News entry for r60265 (Issue 1920).
........
r60269 | raymond.hettinger | 2008-01-25 00:50:26 +0100 (Fri, 25 Jan 2008) | 1 line
More code cleanup. Remove unnecessary indirection to useless class methods.
........
r60270 | raymond.hettinger | 2008-01-25 01:21:54 +0100 (Fri, 25 Jan 2008) | 1 line
Add support for copy, deepcopy, and pickle.
........
r60271 | raymond.hettinger | 2008-01-25 01:33:45 +0100 (Fri, 25 Jan 2008) | 1 line
Mark todos and review comments.
........
r60272 | raymond.hettinger | 2008-01-25 02:13:12 +0100 (Fri, 25 Jan 2008) | 1 line
Add one other review comment.
........
r60273 | raymond.hettinger | 2008-01-25 02:23:38 +0100 (Fri, 25 Jan 2008) | 1 line
Fix-up signature for approximation.
........
r60274 | raymond.hettinger | 2008-01-25 02:46:33 +0100 (Fri, 25 Jan 2008) | 1 line
More design notes
........
r60276 | neal.norwitz | 2008-01-25 07:37:23 +0100 (Fri, 25 Jan 2008) | 6 lines
Make the test more robust by trying to reconnect up to 3 times
in case there were transient failures. This will hopefully silence
the buildbots for this test. As we find other tests that have a problem,
we can fix with a similar strategy assuming it is successful. It worked
on my box in a loop for 10+ runs where it would have an exception otherwise.
........
r60277 | neal.norwitz | 2008-01-25 09:04:16 +0100 (Fri, 25 Jan 2008) | 4 lines
Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
and eliminate a compiler warning in floatobject.c. There might be
a better way to go about this, but it should be good enough for now.
........
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 166 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 36 | ||||
-rw-r--r-- | Modules/mathmodule.c | 5 |
3 files changed, 169 insertions, 38 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index c6098b9..49f55c8 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -122,12 +122,134 @@ bytes(cdata) PyObject *PyExc_ArgError; static PyTypeObject Simple_Type; -PyObject *array_types_cache; char *conversion_mode_encoding = NULL; char *conversion_mode_errors = NULL; +/****************************************************************/ + +typedef struct { + PyObject_HEAD + PyObject *key; + PyObject *dict; +} DictRemoverObject; + +static void +_DictRemover_dealloc(PyObject *_self) +{ + DictRemoverObject *self = (DictRemoverObject *)_self; + Py_XDECREF(self->key); + Py_XDECREF(self->dict); + Py_TYPE(self)->tp_free(_self); +} + +static PyObject * +_DictRemover_call(PyObject *_self, PyObject *args, PyObject *kw) +{ + DictRemoverObject *self = (DictRemoverObject *)_self; + if (self->key && self->dict) { + if (-1 == PyDict_DelItem(self->dict, self->key)) + /* XXX Error context */ + PyErr_WriteUnraisable(Py_None); + Py_DECREF(self->key); + self->key = NULL; + Py_DECREF(self->dict); + self->dict = NULL; + } + Py_INCREF(Py_None); + return Py_None; +} + +static PyTypeObject DictRemover_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_ctypes.DictRemover", /* tp_name */ + sizeof(DictRemoverObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + _DictRemover_dealloc, /* tp_dealloc */ + 0, /* 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 */ + _DictRemover_call, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ +/* XXX should participate in GC? */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + "deletes a key from a dictionary", /* 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 */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ + 0, /* tp_free */ +}; + +int +PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item) +{ + PyObject *obj; + DictRemoverObject *remover; + PyObject *proxy; + int result; + + obj = PyObject_CallObject((PyObject *)&DictRemover_Type, NULL); + if (obj == NULL) + return -1; + + remover = (DictRemoverObject *)obj; + assert(remover->key == NULL); + assert(remover->dict == NULL); + Py_INCREF(key); + remover->key = key; + Py_INCREF(dict); + remover->dict = dict; + + proxy = PyWeakref_NewProxy(item, obj); + Py_DECREF(obj); + if (proxy == NULL) + return -1; + + result = PyDict_SetItem(dict, key, proxy); + Py_DECREF(proxy); + return result; +} + +PyObject * +PyDict_GetItemProxy(PyObject *dict, PyObject *key) +{ + PyObject *result; + PyObject *item = PyDict_GetItem(dict, key); + + if (item == NULL) + return NULL; + if (!PyWeakref_CheckProxy(item)) + return item; + result = PyWeakref_GET_OBJECT(item); + if (result == Py_None) + return NULL; + return result; +} + /******************************************************************/ /* StructType_Type - a meta type/class. Creating a new class using this one as @@ -2381,7 +2503,7 @@ _CData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, only it's object list. So we create a tuple, containing b_objects list PLUS the array itself, and return that! */ - return Py_BuildValue("(OO)", keep, value); + return PyTuple_Pack(2, keep, value); } PyErr_Format(PyExc_TypeError, "incompatible types, %s instance instead of %s instance", @@ -3993,19 +4115,30 @@ PyTypeObject Array_Type = { PyObject * CreateArrayType(PyObject *itemtype, Py_ssize_t length) { + static PyObject *cache; PyObject *key; PyObject *result; char name[256]; + PyObject *len; - key = Py_BuildValue("(On)", itemtype, length); + if (cache == NULL) { + cache = PyDict_New(); + if (cache == NULL) + return NULL; + } + len = PyLong_FromSsize_t(length); + if (len == NULL) + return NULL; + key = PyTuple_Pack(2, itemtype, len); + Py_DECREF(len); if (!key) return NULL; - result = PyObject_GetItem(array_types_cache, key); + result = PyDict_GetItemProxy(cache, key); if (result) { + Py_INCREF(result); Py_DECREF(key); return result; - } else - PyErr_Clear(); + } if (!PyType_Check(itemtype)) { PyErr_SetString(PyExc_TypeError, @@ -4029,9 +4162,11 @@ CreateArrayType(PyObject *itemtype, Py_ssize_t length) "_type_", itemtype ); - if (!result) + if (result == NULL) { + Py_DECREF(key); return NULL; - if (-1 == PyObject_SetItem(array_types_cache, key, result)) { + } + if (-1 == PyDict_SetItemProxy(cache, key, result)) { Py_DECREF(key); Py_DECREF(result); return NULL; @@ -4792,7 +4927,6 @@ PyMODINIT_FUNC init_ctypes(void) { PyObject *m; - PyObject *weakref; /* Note: ob_type is the metatype (the 'type'), defaults to PyType_Type, @@ -4805,16 +4939,6 @@ init_ctypes(void) if (!m) return; - weakref = PyImport_ImportModule("weakref"); - if (weakref == NULL) - return; - array_types_cache = PyObject_CallMethod(weakref, - "WeakValueDictionary", - NULL); - if (array_types_cache == NULL) - return; - Py_DECREF(weakref); - if (PyType_Ready(&PyCArg_Type) < 0) return; @@ -4910,6 +5034,10 @@ init_ctypes(void) * Other stuff */ + DictRemover_Type.tp_new = PyType_GenericNew; + if (PyType_Ready(&DictRemover_Type) < 0) + return; + #ifdef MS_WIN32 if (create_comerror() < 0) return; diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index f4056bf..f7c78f3 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -334,10 +334,10 @@ static int get_long(PyObject *v, long *p) { long x; - if (!PyLong_Check(v)) { - PyErr_Format(PyExc_TypeError, - "int expected instead of %s instance", - v->ob_type->tp_name); + + if (PyFloat_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "int expected instead of float"); return -1; } x = PyLong_AsUnsignedLongMask(v); @@ -353,10 +353,10 @@ static int get_ulong(PyObject *v, unsigned long *p) { unsigned long x; - if (!PyLong_Check(v)) { - PyErr_Format(PyExc_TypeError, - "int expected instead of %s instance", - v->ob_type->tp_name); + + if (PyFloat_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "int expected instead of float"); return -1; } x = PyLong_AsUnsignedLongMask(v); @@ -374,11 +374,10 @@ static int get_longlong(PyObject *v, PY_LONG_LONG *p) { PY_LONG_LONG x; - if (!PyLong_Check(v)) { - PyErr_Format(PyExc_TypeError, - "int expected instead of %s instance", - v->ob_type->tp_name); - return -1; + if (PyFloat_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "int expected instead of float"); + return -1; } x = PyLong_AsUnsignedLongLongMask(v); if (x == -1 && PyErr_Occurred()) @@ -393,12 +392,11 @@ static int get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) { unsigned PY_LONG_LONG x; - if (!PyLong_Check(v)) { - PyErr_Format(PyExc_TypeError, - "int expected instead of %s instance", - v->ob_type->tp_name); - return -1; - } + if (PyFloat_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "int expected instead of float"); + return -1; + } x = PyLong_AsUnsignedLongLongMask(v); if (x == -1 && PyErr_Occurred()) return -1; diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index b773f70..4730680 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -12,6 +12,11 @@ extern double modf (double, double *); #endif /* __STDC__ */ #endif /* _MSC_VER */ +#ifdef _OSF_SOURCE +/* OSF1 5.1 doesn't make this available with XOPEN_SOURCE_EXTENDED defined */ +extern double copysign(double, double); +#endif + /* Call is_error when errno != 0, and where x is the result libm * returned. is_error will usually set up an exception and return * true (1), but may return false (0) without setting up an exception. |