Skip to content

mmap doesn't work on Python 3 #13

@remram44

Description

@remram44
>>> f = BloomFilter(max_elements=10_000_000, error_rate=0.02, filename=('/tmp/bloom.bin', -1))
>>> f.add('test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "site-packages/bloom_filter/bloom_filter.py", line 560, in add
    self.backend.set(bitno)
  File "site-packages/bloom_filter/bloom_filter.py", line 113, in set
    byte = ord(char)
TypeError: ord() expected string of length 1, but int found

In Python 3, indexing a bytes object gives int, there is no need to call ord(). You can make the code compatible with both Python 2 and 3 by using a slice instead:

array = b'123'
idx = 1
# PY2
a = ord(array[idx])
# PY3
b = array[idx]
# PY2 or PY3
c = ord(array[idx:idx + 1])
assert a == b == c == 50

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions