Rev 4760: Implement number.add and coerce to allow concatenation with tuples. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat
John Arbash Meinel
john at arbash-meinel.com
Wed Oct 21 04:41:54 BST 2009
At http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat
------------------------------------------------------------
revno: 4760
revision-id: john at arbash-meinel.com-20091021034136-kj285v0bd7g92wb6
parent: pqm at pqm.ubuntu.com-20091020114259-xxou482wvh20lhl6
author: Andrew Bennetts <andrew.bennetts at canonical.com>
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-st-concat
timestamp: Tue 2009-10-20 22:41:36 -0500
message:
Implement number.add and coerce to allow concatenation with tuples.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c 2009-10-17 00:34:28 +0000
+++ b/bzrlib/_static_tuple_c.c 2009-10-21 03:41:36 +0000
@@ -559,6 +559,78 @@
"Check to see if this tuple has been interned.\n";
+int
+StaticTuple_coerce(PyObject **v, PyObject **w)
+{
+ StaticTuple *st;
+ if (PyTuple_Check(*v)) {
+ st = (StaticTuple*) StaticTuple_new_constructor(
+ &StaticTuple_Type, *v, NULL);
+ if (!st)
+ return -1;
+ Py_INCREF(st);
+ *v = (PyObject*)st;
+ } else if (StaticTuple_CheckExact(*v))
+ Py_INCREF(*v);
+ else
+ return 1;
+
+ if (PyTuple_Check(*w)) {
+ st = (StaticTuple*) StaticTuple_new_constructor(
+ &StaticTuple_Type, *w, NULL);
+ if (!st)
+ return -1;
+ Py_INCREF(st);
+ *w = (PyObject*)st;
+ } else if (StaticTuple_CheckExact(*w))
+ Py_INCREF(*w);
+ else
+ return 1;
+ return 0;
+}
+
+static PyObject *
+StaticTuple_add(PyObject *v, PyObject *w)
+{
+ PyObject *v_t = NULL, *w_t = NULL;
+ PyObject *tmp_tuple, *result;
+ /* StaticTuples and plain tuples may be added (concatenated) to
+ * StaticTuples.
+ */
+ if (StaticTuple_CheckExact(v)) {
+ v_t = StaticTuple_as_tuple((StaticTuple*)v);
+ if (!v_t)
+ goto fail;
+ } else if (PyTuple_Check(v))
+ v_t = v;
+ else
+ goto not_imp;
+
+ if (StaticTuple_CheckExact(w)) {
+ w_t = StaticTuple_as_tuple((StaticTuple*)w);
+ if (!w_t)
+ goto fail;
+ } else if (PyTuple_Check(w))
+ w_t = w;
+ else
+ goto not_imp;
+
+ tmp_tuple = PySequence_Concat(v_t, w_t);
+ result = StaticTuple_new_constructor(&StaticTuple_Type, tmp_tuple, NULL);
+ Py_DECREF(tmp_tuple);
+ Py_INCREF(result);
+ return result;
+
+not_imp:
+ Py_XDECREF(v_t);
+ Py_XDECREF(w_t);
+ return Py_NotImplemented;
+fail:
+ Py_XDECREF(v_t);
+ Py_XDECREF(w_t);
+ return NULL;
+}
+
static PyObject *
StaticTuple_item(StaticTuple *self, Py_ssize_t offset)
{
@@ -624,6 +696,29 @@
{NULL, NULL} /* sentinel */
};
+
+static PyNumberMethods StaticTuple_as_number = {
+ (binaryfunc) StaticTuple_add, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_divide */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ 0, /* nb_nonzero */
+ 0, /* nb_invert */
+ 0, /* nb_lshift */
+ 0, /* nb_rshift */
+ 0, /* nb_and */
+ 0, /* nb_xor */
+ 0, /* nb_or */
+ StaticTuple_coerce, /* nb_coerce */
+};
+
+
static PySequenceMethods StaticTuple_as_sequence = {
(lenfunc)StaticTuple_length, /* sq_length */
0, /* sq_concat */
@@ -654,7 +749,7 @@
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)StaticTuple_repr, /* tp_repr */
- 0, /* tp_as_number */
+ &StaticTuple_as_number, /* tp_as_number */
&StaticTuple_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)StaticTuple_hash, /* tp_hash */
More information about the bazaar-commits
mailing list