Rev 4746: Minor change to the StaticTupleInterner_Discard api, and start using in http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple
John Arbash Meinel
john at arbash-meinel.com
Fri Oct 2 22:14:47 BST 2009
At http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple
------------------------------------------------------------
revno: 4746
revision-id: john at arbash-meinel.com-20091002211441-8j4d2d00cjg4oi7p
parent: john at arbash-meinel.com-20091002204625-gt2jd8emij2wbw44
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-static-tuple
timestamp: Fri 2009-10-02 16:14:41 -0500
message:
Minor change to the StaticTupleInterner_Discard api, and start using
the new StaticTupleInterner api in StaticTuple.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c 2009-10-02 15:51:03 +0000
+++ b/bzrlib/_static_tuple_c.c 2009-10-02 21:14:41 +0000
@@ -22,6 +22,7 @@
#include "_static_tuple_c.h"
#include "_export_c_api.h"
+#include "_static_tuple_interned_pyx_api.h"
#include "python-compat.h"
@@ -84,25 +85,23 @@
Py_INCREF(self);
return self;
}
- unique_key = PyDict_GetItem((PyObject *)_interned_tuples, (PyObject *)self);
- if (unique_key) {
- // An entry already existed, return it, instead of self
- Py_INCREF(unique_key);
+ /* StaticTupleInterner_Add returns whatever object is present at self
+ * or the new object if it needs to add it.
+ */
+ unique_key = StaticTupleInterner_Add(_interned_tuples, (PyObject *)self);
+ if (!unique_key) {
+ // Suppress any error and just return the object
+ PyErr_Clear();
+ return self;
+ }
+ if (unique_key != (PyObject *)self) {
+ // There was already a key at that location
return (StaticTuple *)unique_key;
}
- // An entry did not exist, make 'self' the unique item
- if (PyDict_SetItem(_interned_tuples, (PyObject *)self, (PyObject *)self) < 0) {
- // Suppress an error
- PyErr_Clear();
- Py_INCREF(self);
- return self;
- }
- // self was added to the dict, return it.
- Py_INCREF(self);
self->flags |= STATIC_TUPLE_INTERNED_FLAG;
// The two references in the dict do not count, so that the StaticTuple object
// does not become immortal just because it was interned.
- Py_REFCNT(self) -= 2;
+ Py_REFCNT(self) -= 1;
return self;
}
@@ -121,10 +120,9 @@
if (_StaticTuple_is_interned(self)) {
/* revive dead object temporarily for DelItem */
- Py_REFCNT(self) = 3;
- if (PyDict_DelItem(_interned_tuples, (PyObject *)self) != 0) {
+ // Py_REFCNT(self) = 2;
+ if (StaticTupleInterner_Discard(_interned_tuples, (PyObject*)self) != 1)
Py_FatalError("deletion of interned StaticTuple failed");
- }
}
len = self->size;
for (i = 0; i < len; ++i) {
@@ -690,7 +688,7 @@
static void
setup_interned_tuples(PyObject *m)
{
- _interned_tuples = PyDict_New();
+ _interned_tuples = (PyObject *)StaticTupleInterner_New();
if (_interned_tuples != NULL) {
Py_INCREF(_interned_tuples);
PyModule_AddObject(m, "_interned_tuples", _interned_tuples);
@@ -742,8 +740,6 @@
if (PyType_Ready(&StaticTuple_Type) < 0)
return;
- //if (PyType_Ready(&KeyIntern_Type) < 0)
- // return;
m = Py_InitModule3("_static_tuple_c", static_tuple_c_methods,
"C implementation of a StaticTuple structure");
@@ -752,6 +748,7 @@
Py_INCREF(&StaticTuple_Type);
PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
+ import_bzrlib___static_tuple_interned_pyx();
setup_interned_tuples(m);
setup_empty_tuple(m);
setup_c_api(m);
=== modified file 'bzrlib/_static_tuple_interned_pyx.pyx'
--- a/bzrlib/_static_tuple_interned_pyx.pyx 2009-10-02 20:46:25 +0000
+++ b/bzrlib/_static_tuple_interned_pyx.pyx 2009-10-02 21:14:41 +0000
@@ -480,8 +480,7 @@
return key in true_self
-cdef api int StaticTupleInterner_Discard(StaticTupleInterner self,
- object key) except -1:
+cdef api int StaticTupleInterner_Discard(object self, object key) except -1:
"""Remove the object referenced at location 'key'.
:param self: The StaticTupleInterner being modified
=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py 2009-10-02 04:20:30 +0000
+++ b/bzrlib/tests/test__static_tuple.py 2009-10-02 21:14:41 +0000
@@ -338,6 +338,7 @@
self.assertIs(key, key3)
self.assertTrue(key in self.module._interned_tuples)
self.assertEqual(key, self.module._interned_tuples[key])
+ self.assertEqual(3, sys.getrefcount(key))
del key3
# We should not increase the refcount just via 'intern'
self.assertEqual(2, sys.getrefcount(key))
More information about the bazaar-commits
mailing list