Rev 4741: Move some of the information into the pxd header file. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple

John Arbash Meinel john at arbash-meinel.com
Fri Oct 2 20:15:44 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple

------------------------------------------------------------
revno: 4741
revision-id: john at arbash-meinel.com-20091002191539-8hhe3na35zc10ff0
parent: john at arbash-meinel.com-20091002190523-6cvyhl238q5hs8v1
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-static-tuple
timestamp: Fri 2009-10-02 14:15:39 -0500
message:
  Move some of the information into the pxd header file.
  
  This will make it easier to directly use attributes of the class in other modules.
  (More of a case for StaticTuple than StaticTupleInterner, but I figured I'd get
  practice using it.
-------------- next part --------------
=== added file 'bzrlib/_static_tuple_interned_pyx.pxd'
--- a/bzrlib/_static_tuple_interned_pyx.pxd	1970-01-01 00:00:00 +0000
+++ b/bzrlib/_static_tuple_interned_pyx.pxd	2009-10-02 19:15:39 +0000
@@ -0,0 +1,38 @@
+# Copyright (C) 2009 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""Interface definition of a class to intern StaticTuple objects."""
+
+cdef extern from "python-compat.h":
+    ctypedef long Py_ssize_t
+
+cdef extern from "Python.h":
+    ctypedef struct PyObject:
+        pass
+
+cdef public api class StaticTupleInterner [object StaticTupleInternerObject,
+                                           type StaticTupleInterner_type]:
+
+    cdef readonly Py_ssize_t used    # active
+    cdef readonly Py_ssize_t fill    # active + dummy
+    cdef readonly Py_ssize_t mask    # Table contains (mask+1) slots, a power
+                                     # of 2
+    cdef PyObject **table   # Pyrex/Cython doesn't support arrays to 'object'
+                            # so we manage it manually
+
+    cdef PyObject *_get(self, object key) except? NULL
+    cpdef object add(self, key)
+    cpdef int discard(self, key) except -1

=== modified file 'bzrlib/_static_tuple_interned_pyx.pyx'
--- a/bzrlib/_static_tuple_interned_pyx.pyx	2009-10-02 19:05:23 +0000
+++ b/bzrlib/_static_tuple_interned_pyx.pyx	2009-10-02 19:15:39 +0000
@@ -18,9 +18,6 @@
 
 cdef extern from "Python.h":
     ctypedef unsigned long size_t
-    ctypedef struct PyTypeObject
-    ctypedef struct PyObject:
-        PyTypeObject *ob_type
     ctypedef long (*hashfunc)(PyObject*)
     ctypedef PyObject *(*richcmpfunc)(PyObject *, PyObject *, int)
     int Py_EQ
@@ -31,12 +28,13 @@
     ctypedef struct PyTypeObject:
         hashfunc tp_hash
         richcmpfunc tp_richcompare
+
+    PyTypeObject *Py_TYPE(PyObject *)
         
     void *PyMem_Malloc(size_t nbytes)
     void PyMem_Free(void *)
     void memset(void *, int, size_t)
 
-
 cdef object _dummy_obj
 cdef PyObject *_dummy
 _dummy_obj = object()
@@ -49,10 +47,10 @@
 
     if this == other:
         return 1
-    other_hash = other.ob_type.tp_hash(other)
+    other_hash = Py_TYPE(other).tp_hash(other)
     if other_hash != this_hash:
         return 0
-    res = this.ob_type.tp_richcompare(this, other, Py_EQ)
+    res = Py_TYPE(this).tp_richcompare(this, other, Py_EQ)
     if res == Py_True:
         Py_DECREF(res)
         return 1
@@ -62,7 +60,7 @@
     # required, and Py_NotImplemented => not equal
     if res == Py_NotImplemented:
         Py_DECREF(res)
-        res = other.ob_type.tp_richcompare(other, this, Py_EQ)
+        res = Py_TYPE(other).tp_richcompare(other, this, Py_EQ)
     if res == Py_True:
         Py_DECREF(res)
         return 1
@@ -85,14 +83,7 @@
     As such, it uses 1/3rd the amount of memory to store a pointer to the
     interned object.
     """
-
-    cdef readonly Py_ssize_t used    # active
-    cdef readonly Py_ssize_t fill    # active + dummy
-    cdef readonly Py_ssize_t mask    # Table contains (mask+1) slots, a power
-                                     # of 2
-    cdef PyObject **table   # Pyrex/Cython doesn't support arrays to 'object'
-                            # so we manage it manually
-
+    # Attributes are defined in the .pxd file
     DEF DEFAULT_SIZE=1024
     DEF PERTURB_SHIFT=5
 
@@ -141,7 +132,7 @@
             return False
         return True
 
-    cdef PyObject *_get(self, object key):
+    cdef PyObject *_get(self, object key) except? NULL:
         """Return the object (or nothing) define at the given location."""
         cdef PyObject **slot
 



More information about the bazaar-commits mailing list