Permalink
Browse files

Backwards incompatible change to CounterField

  • Loading branch information...
schmichael committed Sep 24, 2011
1 parent 5528038 commit fcb912555749449d396338e8e0bfc8106d6c1007
Showing with 37 additions and 9 deletions.
  1. +32 −4 mmstats.py
  2. +5 −5 tests/test_types.py
View
@@ -197,6 +197,38 @@ def _init(self, state, mm, offset):
return offset + ctypes.sizeof(state._StructCls)
class _Counter(object):
"""Internal counter class used by CounterFields"""
def __init__(self, state):
self._struct = state._struct
@property
def value(self):
return self._struct.buffers[self._struct.write_buffer ^ 1]
def inc(self, n=1):
# Set the write buffer
self._struct.buffers[self._struct.write_buffer] = self.value + n
# Swap the write buffer
self._struct.write_buffer ^= 1
class CounterField(DoubleBufferedField):
"""Counter field supporting an inc() method and value attribute"""
buffer_type = ctypes.c_uint64
type_signature = 'L'
def _init(self, state, mm, offset):
offset = super(CounterField, self)._init(state, mm, offset)
state.counter = _Counter(state)
return offset
def __get__(self, inst, owner):
if inst is None:
return self
return inst._fields[self.key].counter
class BufferedDescriptorField(DoubleBufferedField, BufferedDescriptorMixin):
"""Base class for double buffered descriptor fields"""
@@ -207,10 +239,6 @@ class UInt64Field(BufferedDescriptorField):
type_signature = 'L'
# Alias UInt64Field to CounterField for simplicity
CounterField = UInt64Field
class UIntField(BufferedDescriptorField):
"""32bit Double Buffered Unsigned Integer field"""
buffer_type = ctypes.c_uint32
View
@@ -124,9 +124,9 @@ class SimpleCounter(mmstats.MmStats):
counter = mmstats.CounterField()
s = SimpleCounter(filename='mmstats-test_counter')
self.assertEqual(s.counter, 0)
s.counter += 1
self.assertEqual(s.counter, 1)
s.counter -= 2
self.assertNotEqual(s.counter, -1)
self.assertEqual(s.counter.value, 0)
s.counter.inc()
self.assertEqual(s.counter.value, 1)
s.counter.inc(-2)
self.assertNotEqual(s.counter.value, -1)

0 comments on commit fcb9125

Please sign in to comment.