Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Simple implementation of circular buffer.

Project Description

  • License : MIT


Simple circular buffer written in python extension.

CircularBuffer will allocate requested size + 2 bytes for two sections of null terminated strings.

May temporary allocate another half of the allocated bytes if you used buffer protocol, I mostly used them for regex.


Simply pip install pycircularbuffer, compilation from downloaded source is probably needed.


from circularbuffer import CircularBuffer

buf = CircularBuffer(1024)

buf.write(b'some text')
while len(buf) > 0:

from re import match


# python2
with buf:
    match_found = match(br'^ha', buf)

# python3
match_found = match(br'^ha', buf)

# use `result` immediately because regex didn't make memory copy of the
# internal buffer, or run another `match()` on a memory copy, for example:

match_found_str =
independent_match_found = match(br'^ha', match_found_str)


Don’t to this in python 3: ‘a’ in buf, instead: b’a’ in buf.


Regular methods:

  • clear()
  • read()
  • resize()
  • write()
  • write_available()
  • make_contiguous()

String methods:

  • count()
  • startswith()
  • find()
  • index()

Sequence methods:

  • __contains__()
  • __getitem__()
  • __len__()
  • __setitem__()

Magic methods:

  • __repr__()

  • __str__()

    Note: while string representation makes thing easier it always creates memory copy.

  • __enter__()

  • __exit__()

Buffer protocol:

Note: for python version < 3, you need to use context manager, the with statement, to let CircularBuffer know when you are releasing the buffer.

Using buffer protocol will throw ReservedError exception for

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(10.1 kB) Copy SHA256 Hash SHA256
Source None Jan 8, 2017

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers