14
0
forked from pool/python-ijson
Files
python-ijson/tests_asyncio.py
Tomáš Chvátal 886e95d4a4 Accepting request 792735 from home:mcalabkova:branches:devel:languages:python
- update to 3.0
  * Exposing backend's name under ``<backend>.backend``,
  and default backend's name under ``ijson.backend``.
  * Exposing ``ijson.sendable_list`` to users in case it comes in handy.
  * Improved the protocol for user-facing coroutines,
  where instead of having to send a final, empty bytes string
  to finish the parsing process
  users can simply call ``.close()`` on the coroutine.
  * Including C code in coverage measurements,
  and increased overall code coverage up to 99%.
  * Full re-design of ijson.
  * Initial support for ``asyncio`` in python 3.5+.
  * Exposure of underlying infrastructure implementing the push model.
  * C extension broken down into separate source files
  for easier understanding and maintenance.
  * Fixed a deprecation warning in the C backend
  present in python 3.8 when parsing Decimal values.
  * New `kvitems` method in all backends.
  Like `items`, it takes a prefix,
  and iterates over the key/value pairs of matching objects
  (instead of iterating over objects themselves, like in `items`).
  * When using python 2, all backends now return
  `map_key` values as `unicode` objects, not `str`.
  * Including more files in source distributions (#14).
  * Adjusting python backend to avoid reading off the input stream
  too eagerly (#15).

OBS-URL: https://build.opensuse.org/request/show/792735
OBS-URL: https://build.opensuse.org/package/show/devel:languages:python/python-ijson?expand=0&rev=3
2020-04-12 07:31:19 +00:00

47 lines
1.2 KiB
Python

# -*- coding:utf-8 -*-
import asyncio
import io
from ijson import compat
class AsyncReader(object):
def __init__(self, data):
if type(data) == compat.bytetype:
self.data = io.BytesIO(data)
else:
self.data = io.StringIO(data)
async def read(self, n=-1):
return self.data.read(n)
class Async(object):
'''Test adaptation for async generators'''
suffix = '_async'
def _run(self, f):
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(f)
finally:
loop.close()
def all(self, routine, json_content, *args, **kwargs):
events = []
async def run():
async for event in routine(AsyncReader(json_content), *args, **kwargs):
events.append(event)
self._run(run())
return events
def first(self, routine, json_content, *args, **kwargs):
events = []
async def run():
async for event in routine(AsyncReader(json_content), *args, **kwargs):
events.append(event)
if events:
return
self._run(run())
return events[0]