mosquito/aiofile

Manual context management example on readme has a bug

aloktripathy opened this issue · 1 comments

The following code as listed on the readme page to manage context manually, has a bug.

import asyncio

from aiofile import async_open
from caio import linux_aio, thread_aio


async def main():
    linux_ctx = linux_aio.Context()
    threads_ctx = thread_aio.Context()

    async with async_open("/tmp/test.txt", "a", context=linux_ctx) as afp:
        await afp.write("Hello")

    async with async_open("/tmp/test.txt", "a", context=threads_ctx) as afp:
        print(await afp.read())


await main()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [94], in <cell line: 18>()
     14     async with async_open("/tmp/test.txt", "a", context=threads_ctx) as afp:
     15         print(await afp.read())
---> 18 await main()

Input In [94], in main()
      8 linux_ctx = linux_aio.Context()
      9 threads_ctx = thread_aio.Context()
---> 11 async with async_open("/tmp/test.txt", "a", context=linux_ctx) as afp:
     12     await afp.write("Hello")
     14 async with async_open("/tmp/test.txt", "a", context=threads_ctx) as afp:

File /usr/local/lib/python3.10/site-packages/aiofile/utils.py:200, in FileIOWrapperBase.__aenter__(self)
    199 async def __aenter__(self) -> "FileIOWrapperBase":
--> 200     await self.file.open()
    201     return self

File /usr/local/lib/python3.10/site-packages/aiofile/aio.py:177, in AIOFile.open(self)
    174     return None
    176 if self.__open_result is None:
--> 177     self.__open_result = self._run_in_thread(
    178         open,
    179         self._fname,
    180         self._open_mode,
    181     )
    182     self._file_obj = await self.__open_result
    183     self.__open_result = None

File /usr/local/lib/python3.10/site-packages/aiofile/aio.py:154, in AIOFile._run_in_thread(self, func, *args, **kwargs)
    151 def _run_in_thread(
    152         self, func: "Callable[..., _T]", *args: Any, **kwargs: Any
    153 ) -> "asyncio.Future[_T]":
--> 154     return self.__context.loop.run_in_executor(
    155         self._executor, partial(func, *args, **kwargs),
    156     )

AttributeError: 'Context' object has no attribute 'loop'

Fixed in 3.8.6