25
25
PyObject *Py_NotImplemented
26
26
void Py_INCREF(PyObject *)
27
27
void Py_DECREF(PyObject *)
28
ctypedef struct PyVarObject:
28
30
ctypedef struct PyTypeObject:
30
32
richcmpfunc tp_richcompare
32
34
PyTypeObject *Py_TYPE(PyObject *)
35
PyVarObject * _PyObject_NewVar(PyTypeObject *, Py_ssize_t) except NULL
34
37
void *PyMem_Malloc(size_t nbytes)
35
38
void PyMem_Free(void *)
546
548
key[0] = table[i]
552
cdef StaticTuple _empty_tuple
553
def _get_empty_tuple():
554
"""Return the 'empty tuple'
556
This is the singleton StaticTuple that has no content.
561
cdef api StaticTuple StaticTuple_New(Py_ssize_t size):
562
"""Create a new StaticTuple object with the number of slots specified."""
564
cdef StaticTuple stuple
567
raise ValueError('size must be > 0')
569
if (size == 0 and _empty_tuple is not None):
571
# Note that we use PyObject_NewVar because we want to allocate a variable
572
# width entry. However we *aren't* truly a PyVarObject because we don't
573
# use a long for ob_size. Instead we use a plain 'size' that is an int,
574
# and will be overloaded with flags in the future.
575
# As such we do the alloc, and then have to clean up anything it does
576
# incorrectly. Consider switching to PyObject_MALLOC directly
577
tmp = <PyObject *>_PyObject_NewVar(<PyTypeObject*>StaticTuple, size)
578
stuple = <StaticTuple>tmp
579
Py_DECREF(tmp) # The cast to <StaticTuple> causes an INCREF
585
memset(stuple.items, 0, sizeof(PyObject *) * size)
588
cdef api int StaticTuple_CheckExact(object s):
589
return isinstance(s, StaticTuple)
592
cdef api StaticTupleInterner _interned_tuples
593
def _get_interned_tuples():
594
"""Get a copy of the _interned_tuples object.
596
Note that this object should *never* be mutated. Doing so could cause
599
return _interned_tuples
602
cdef api StaticTuple StaticTuple_Intern(StaticTuple self):
603
if _interned_tuples is None:
605
if self.flags & STATIC_TUPLE_INTERNED_FLAG:
607
# StaticTupleInterner_Add returns whatever object is present at self
608
# or the new object if it needs to add it.
610
unique_key = _interned_tuples.add(self)
611
if unique_key is not self:
612
# There was already a key here, just return it
614
# This is now interned, mark it as such, and adjust the refcount
615
self.flags |= STATIC_TUPLE_INTERNED_FLAG
616
Py_DECREF(<PyObject *>self)
620
_interned_tuples = StaticTupleInterner()
621
_empty_tuple = _interned_tuples.add(StaticTuple())
622
_empty_tuple.flags |= STATIC_TUPLE_ALL_STRING