Rev 4764: Add a bit more tests. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat

John Arbash Meinel john at arbash-meinel.com
Wed Oct 21 05:15:11 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat

------------------------------------------------------------
revno: 4764
revision-id: john at arbash-meinel.com-20091021041454-gofau8rwtplnolaf
parent: john at arbash-meinel.com-20091021035352-aiwgzctxahbflehc
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-st-concat
timestamp: Tue 2009-10-20 23:14:54 -0500
message:
  Add a bit more tests.
  
  Turns out you don't need to implement 'coerce', instead you just need to
  use the Py_TPFLAGS_CHECKTYPES flag in the type definition.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c	2009-10-21 03:53:52 +0000
+++ b/bzrlib/_static_tuple_c.c	2009-10-21 04:14:54 +0000
@@ -560,76 +560,45 @@
     "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;
+    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);
+    return result;
 
 not_imp:
-	Py_XDECREF(v_t);
-	Py_XDECREF(w_t);
-	return Py_NotImplemented;
+    Py_XDECREF(v_t);
+    Py_XDECREF(w_t);
+    return Py_NotImplemented;
 fail:
-	Py_XDECREF(v_t);
-	Py_XDECREF(w_t);
-	return NULL;
+    Py_XDECREF(v_t);
+    Py_XDECREF(w_t);
+    return NULL;
 }
 
 static PyObject *
@@ -699,26 +668,26 @@
 
 
 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 */
+    (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 */
+    0,                              /* nb_coerce */
 };
-	
+    
 
 static PySequenceMethods StaticTuple_as_sequence = {
     (lenfunc)StaticTuple_length,            /* sq_length */
@@ -759,7 +728,10 @@
     0,                                           /* tp_getattro */
     0,                                           /* tp_setattro */
     0,                                           /* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT,                          /* tp_flags*/
+    /* Py_TPFLAGS_CHECKTYPES tells the number operations that they shouldn't
+     * try to 'coerce' but instead stuff like 'add' will check it arguments.
+     */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES,  /* tp_flags*/
     StaticTuple_doc,                             /* tp_doc */
     /* gc.get_referents checks the IS_GC flag before it calls tp_traverse
      * And we don't include this object in the garbage collector because we
@@ -863,18 +835,15 @@
      */
     set_module = PyImport_ImportModule("bzrlib._simple_set_pyx");
     if (set_module == NULL) {
-	// fprintf(stderr, "Failed to import bzrlib._simple_set_pyx\n");
         goto end;
     }
     /* Add the _simple_set_pyx into sys.modules at the appropriate location. */
     sys_module = PyImport_ImportModule("sys");
     if (sys_module == NULL) {
-    	// fprintf(stderr, "Failed to import sys\n");
         goto end;
     }
     modules = PyObject_GetAttrString(sys_module, "modules");
     if (modules == NULL || !PyDict_Check(modules)) {
-    	// fprintf(stderr, "Failed to find sys.modules\n");
         goto end;
     }
     PyDict_SetItemString(modules, "_simple_set_pyx", set_module);
@@ -913,3 +882,5 @@
     setup_empty_tuple(m);
     setup_c_api(m);
 }
+
+// vim: tabstop=4 sw=4 expandtab

=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py	2009-10-21 03:49:03 +0000
+++ b/bzrlib/tests/test__static_tuple.py	2009-10-21 04:14:54 +0000
@@ -141,6 +141,15 @@
             pass
         else:
             self.fail('TypeError not raised')
+
+    def test_concat_with_non_tuple(self):
+        st1 = self.module.StaticTuple('foo')
+        try:
+            st1 + 10
+        except TypeError:
+            pass
+        else:
+            self.fail('TypeError not raised for addition w/ an int')
         
     def test_as_tuple(self):
         k = self.module.StaticTuple('foo')



More information about the bazaar-commits mailing list