Skip to content

Simple expression not matching 'from' 'to' indices #196

@tegonzalez

Description

@tegonzalez

.. or I'm using it improperly/misunderstanding the documentation. 'from' index appears to be 0 when it should indicate the start of the matched sequence when scanning for a substring. This makes it difficult to collect all matching expressions in a stream. I added the following pytest:

@pytest.fixture(scope="module")                                                                                                                                            
def database_stream2() -> hyperscan.Database:                                                                                                                              
    patterns = (                                                                                                                                                           
        # expression,  id, flags                                                                                                                                           
        (br'XY',      0,  0),                                                                                                                                              
    )                                                                                                                                                                      
    expressions2, ids2, flags2 = zip(*patterns)                                                                                                                            
    db = hyperscan.Database(                                                                                                                                               
        mode=(hyperscan.HS_MODE_STREAM | hyperscan.HS_MODE_SOM_HORIZON_LARGE)                                                                                              
    )                                                                                                                                                                      
    db.compile(                                                                                                                                                            
        expressions=expressions2,                                                                                                                                          
        ids=ids2,                                                                                                                                                          
        elements=len(expressions2),                                                                                                                                        
        flags=flags2,                                                                                                                                                      
    )                                                                                                                                                                      
    return db                                                                                                                                                              
                                                                                                                                                                           
def test_stream_scan2(database_stream2, mocker):                                                                                                                           
    callback = mocker.Mock(return_value=None)                                                                                                                              
                                                                                                                                                                           
    with database_stream2.stream(match_event_handler=callback) as stream:                                                                                                  
        stream.scan(b"zzzXY")                                                                                                                                              
    callback.assert_has_calls(                                                                                                                                             
        [                                                                                                                                                                  
            mocker.call(0, 3, 2, 0, None),                                                                                                                                 
        ],                                                                                                                                                                 
        any_order=True,                                                                                                                                                    
    )  

and here's the output:

database_stream2 = <hyperscan.Database object at 0x101c9f390>, mocker = <pytest_mock.plugin.MockerFixture object at 0x101c80cd0>

    def test_stream_scan2(database_stream2, mocker):
        callback = mocker.Mock(return_value=None)
    
        with database_stream2.stream(match_event_handler=callback) as stream:
            stream.scan(b"zzzXY")
>       callback.assert_has_calls(
            [
                mocker.call(0, 3, 2, 0, None),
            ],
            any_order=True,
        )
E       AssertionError: 'mock' does not contain all of (call(0, 3, 2, 0, None),) in its call list, found [call(0, 0, 5, 0, None)] instead

test_hyperscan.py:125: AssertionError
========================================================================= short test summary info =========================================================================
FAILED test_hyperscan.py::test_stream_scan2 - AssertionError: 'mock' does not contain all of (call(0, 3, 2, 0, None),) in its call list, found [call(0, 0, 5, 0, None)] instead
====================================================================== 1 failed, 24 passed in 0.07s =======================================================================

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