Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.read() doesn't advance offset #13

Closed
lopisan opened this issue May 4, 2021 · 4 comments
Closed

.read() doesn't advance offset #13

lopisan opened this issue May 4, 2021 · 4 comments

Comments

@lopisan
Copy link

lopisan commented May 4, 2021

Hi,
I've find a possible bug in AsyncPath lib:
f.read(count) doesn't advacne offset, so two consecutive reads read just the beggining of the file.
I've created tests showing the bug:

from pathlib import Path

import pytest
from tempfile import TemporaryDirectory
from aiopath import AsyncPath


@pytest.mark.asyncio
async def test_async_path():
    with TemporaryDirectory(prefix='read_test') as temp_dir:
        file = AsyncPath(temp_dir) / 'test.bin'
        await file.write_bytes(b'0123456789abcdefghijklmnopqrstuvwxyz')
        async with file.open('rb') as f:
            chunk = await f.read(10)
            assert chunk == b'0123456789'
            chunk = await f.read(10)
            assert chunk == b'abcdefghij' # THIS LINE FAILS - eventhough it shouldn't (in pathlib this works)


def test_path():
    with TemporaryDirectory(prefix='read_test') as temp_dir:
        file = Path(temp_dir) / 'test.bin'
        file.write_bytes(b'0123456789abcdefghijklmnopqrstuvwxyz')
        with file.open('rb') as f:
            chunk = f.read(10)
            assert chunk == b'0123456789'
            chunk = f.read(10)
            assert chunk == b'abcdefghij'
@alexdelorenzo
Copy link
Owner

alexdelorenzo commented May 6, 2021

Hi @lopisan, thanks for opening an issue and including test cases.

When you use the AsyncPath.open() context manager, it yields an AIOFile object from the aiofile project. From what I can tell, this behavior of the AIOFile.read() method is deliberate, as the README for aiofile indicates that it wants you to use the seek() method between reads if you want to advance the offset. If you want a quick fix for your own project, check out the documentation for aiofile.

However, aiopath subclasses AIOFile as IterableAIOFIle in order to mimic the behavior of the file-like object pathlib.Path.open() yields.

I agree that the behavior of read() should increment the offset between calls, so I'll modify the read() method so that it behaves the same way it does with pathlib.

@alexdelorenzo
Copy link
Owner

v0.5.6 fixes this issue on Python 3.7 - 3.9. For 3.10, it's available as v0.6.5 on PyPI.

Thanks again for the test cases, @lopisan.

@lopisan
Copy link
Author

lopisan commented May 6, 2021

Thanks for a quick fix! It works like a charm now.

@lopisan lopisan closed this as completed May 6, 2021
@alexdelorenzo
Copy link
Owner

Glad to hear it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants