darvid / python-hyperscan

🐍 A CPython extension for the Hyperscan regular expression matching library.
https://python-hyperscan.readthedocs.io/en/latest/
MIT License
165 stars 28 forks source link

dumpb/loadb results in non-working database #153

Open piotr-piatkowski opened 2 months ago

piotr-piatkowski commented 2 months ago

I was trying to serialize and then restore database, all I get when trying to call scan on restored database is hyperscan.InvalidError: error code -1. I'm using python module version 0.7.7 and python 3.11.9.

Here's sample code:

    db = hyperscan.Database(mode=hyperscan.HS_MODE_BLOCK)
    db.compile(
        expressions=[b'bar']
    )

    def callback(id, start, end, flags, context):
        print(f"Matched {id} from {start} to {end}")

    db.scan(b'foo bar baz', callback)

    data = hyperscan.dumpb(db)
    db2 = hyperscan.loadb(data, hyperscan.HS_MODE_BLOCK)
    db2.scan(b'foo bar baz', callback)

And results:

        data = hyperscan.dumpb(db)
        db2 = hyperscan.loadb(data, hyperscan.HS_MODE_BLOCK)
>       db2.scan(b'foo bar baz', callback)
E       hyperscan.InvalidError: error code -1

I also noticed in debugger that restored db has no scratch atribute set (it's None), but I'm not sure if this is the reason.

betterlch commented 2 months ago
sc = hyperscan.Scratch(db2)
db2.scan(b'foo bar baz', callback, scratch=sc)