Rev 4776: Tweak some of the internals of _chk_map_pyx.pyx in http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple-chk-map
John Arbash Meinel
john at arbash-meinel.com
Wed Oct 21 20:43:24 BST 2009
At http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple-chk-map
------------------------------------------------------------
revno: 4776
revision-id: john at arbash-meinel.com-20091021194305-eso7vfaetsvmhgz6
parent: john at arbash-meinel.com-20091021190730-70kt0cwn91ayepbc
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-static-tuple-chk-map
timestamp: Wed 2009-10-21 14:43:05 -0500
message:
Tweak some of the internals of _chk_map_pyx.pyx
Since we have a fixed type, we can use the StaticTuple_GET_ITEM macro.
-------------- next part --------------
=== modified file 'bzrlib/_chk_map_pyx.pyx'
--- a/bzrlib/_chk_map_pyx.pyx 2009-10-20 22:13:23 +0000
+++ b/bzrlib/_chk_map_pyx.pyx 2009-10-21 19:43:05 +0000
@@ -29,9 +29,8 @@
cdef extern from "Python.h":
ctypedef int Py_ssize_t # Required for older pyrex versions
- struct _PyObject:
+ ctypedef struct PyObject:
pass
- ctypedef _PyObject PyObject
int PyTuple_CheckExact(object p)
Py_ssize_t PyTuple_GET_SIZE(object t)
int PyString_CheckExact(object)
@@ -52,6 +51,18 @@
char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
object PyString_FromStringAndSize(char*, Py_ssize_t)
+# It seems we need to import the definitions so that the pyrex compiler has
+# local names to access them.
+from _static_tuple_c cimport StaticTuple,\
+ import_static_tuple_c, StaticTuple_New, \
+ StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
+
+cdef extern from "_static_tuple_c.h":
+ # Defined explicitly rathert than cimport ing. Using cimport the type for
+ # PyObject is a different class that happens to have the same name...
+ PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
+ Py_ssize_t)
+
cdef extern from "zlib.h":
ctypedef unsigned long uLong
ctypedef unsigned int uInt
@@ -59,11 +70,6 @@
uLong crc32(uLong crc, Bytef *buf, uInt len)
-# It seems we need to import the definitions so that the pyrex compiler has
-# local names to access them.
-from _static_tuple_c cimport StaticTuple,\
- import_static_tuple_c, StaticTuple_New, \
- StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
# This sets up the StaticTuple C_API functionality
@@ -101,7 +107,7 @@
cdef uInt crc_val
cdef Py_ssize_t out_off
cdef char *c_out
- # cdef PyObject *bit
+ cdef PyObject *bit
if not StaticTuple_CheckExact(key):
raise TypeError('key %r is not a StaticTuple' % (key,))
@@ -117,13 +123,11 @@
# We use the _ptr variant, because GET_ITEM returns a borrowed
# reference, and Pyrex assumes that returned 'object' are a new
# reference
- # XXX: This needs to be updated for PySequence_GetItem since both
- # PyTuple and StaticTuple support that api
- bit = key[i]# PyTuple_GET_ITEM_ptr(key, i)
- if not PyString_CheckExact(bit):
+ bit = StaticTuple_GET_ITEM_ptr(key, i)
+ if not PyString_CheckExact_ptr(bit):
raise TypeError('Bit %d of %r is not a string' % (i, key))
- c_bit = <Bytef *>PyString_AS_STRING(bit)
- c_len = PyString_GET_SIZE(bit)
+ c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
+ c_len = PyString_GET_SIZE_ptr(bit)
crc_val = crc32(0, c_bit, c_len)
# Hex(val) order
sprintf(c_out, '%08X', crc_val)
@@ -141,7 +145,7 @@
cdef uInt crc_val
cdef Py_ssize_t out_off
cdef char *c_out
- # cdef PyObject *bit
+ cdef PyObject *bit
if not StaticTuple_CheckExact(key):
raise TypeError('key %r is not a StaticTuple' % (key,))
@@ -154,12 +158,12 @@
if i > 0:
c_out[0] = c'\x00'
c_out = c_out + 1
- bit = key[i] # PyTuple_GET_ITEM_ptr(key, i)
- if not PyString_CheckExact(bit):
- raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
- bit))
- c_bit = <Bytef *>PyString_AS_STRING(bit)
- c_len = PyString_GET_SIZE(bit)
+ bit = StaticTuple_GET_ITEM_ptr(key, i)
+ if not PyString_CheckExact_ptr(bit):
+ raise TypeError('Bit %d of %r is not a string: %r'
+ % (i, key, <object>bit))
+ c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
+ c_len = PyString_GET_SIZE_ptr(bit)
crc_val = crc32(0, c_bit, c_len)
# MSB order
c_out[0] = (crc_val >> 24) & 0xFF
=== modified file 'bzrlib/_static_tuple_c.pxd'
--- a/bzrlib/_static_tuple_c.pxd 2009-10-07 15:57:25 +0000
+++ b/bzrlib/_static_tuple_c.pxd 2009-10-21 19:43:05 +0000
@@ -36,5 +36,9 @@
# Steals a reference and val must be a valid type, no checking is done
void StaticTuple_SET_ITEM(StaticTuple key, Py_ssize_t offset, object val)
- object StaticTuple_GET_ITEM(StaticTuple key, Py_ssize_t offset)
+ # This isn't particularly useful. Namely because Pyrex doesn't think of
+ # this PyObject* to be the same type as a PyObject* defined in another
+ # pyrex file... However, because we don't INCREF, we don't want to define
+ # it as returning a regular 'object'.
+ PyObject * StaticTuple_GET_ITEM(StaticTuple key, Py_ssize_t offset)
int StaticTuple_CheckExact(object)
More information about the bazaar-commits
mailing list