Skip to content

Commit

Permalink
Support bytes and str in rich entry points
Browse files Browse the repository at this point in the history
Now that we have a rich set of entry points there's no reason to hold
back supporting other types of inputs. The main one is obviously bytes
and strings, which we can easily convert into their corresponding file
objects. New tests ensure this is happening correctly.

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
  • Loading branch information
rtobar committed Jun 25, 2020
1 parent 226ed77 commit 0eb32b0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ijson/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ def is_iterable(x):
return hasattr(x, '__iter__')


def _get_source(source):
if isinstance(source, compat.bytetype):
return compat.BytesIO(source)
elif isinstance(source, compat.texttype):
return compat.StringIO(source)
return source


def _make_basic_parse_gen(backend):
def basic_parse_gen(file_obj, buf_size=64*1024, **config):
return utils.coros2gen(
Expand Down Expand Up @@ -340,6 +348,7 @@ def kvitems_gen(file_obj, prefix, map_type=None, buf_size=64*1024, **config):

def _make_basic_parse(backend):
def basic_parse(source, buf_size=64*1024, **config):
source = _get_source(source)
if is_async_file(source):
return backend['basic_parse_async'](
source, buf_size=buf_size, **config
Expand All @@ -354,6 +363,7 @@ def basic_parse(source, buf_size=64*1024, **config):

def _make_parse(backend):
def parse(source, buf_size=64*1024, **config):
source = _get_source(source)
if is_async_file(source):
return backend['parse_async'](
source, buf_size=buf_size, **config
Expand All @@ -372,6 +382,7 @@ def parse(source, buf_size=64*1024, **config):

def _make_items(backend):
def items(source, prefix, map_type=None, buf_size=64*1024, **config):
source = _get_source(source)
if is_async_file(source):
return backend['items_async'](
source, prefix, map_type=map_type, buf_size=buf_size, **config
Expand All @@ -390,6 +401,7 @@ def items(source, prefix, map_type=None, buf_size=64*1024, **config):

def _make_kvitems(backend):
def kvitems(source, prefix, map_type=None, buf_size=64*1024, **config):
source = _get_source(source)
if is_async_file(source):
return backend['kvitems_async'](
source, prefix, map_type=map_type, buf_size=buf_size, **config
Expand Down
22 changes: 22 additions & 0 deletions test/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ def _assert_invalid_type(self, routine, *args, **kwargs):
with self.assertRaises(ValueError):
routine(lambda _: JSON, *args, **kwargs)

def _assert_bytes(self, expected_results, routine, *args, **kwargs):
results = list(routine(JSON, *args, **kwargs))
self.assertEqual(expected_results, results)

def _assert_str(self, expected_results, routine, *args, **kwargs):
with warning_catcher() as warns:
results = list(routine(compat.b2s(JSON), *args, **kwargs))
self.assertEqual(expected_results, results)
if self.warn_on_string_stream:
self.assertEqual(1, len(warns))

def _assert_unicode(self, expected_results, routine, *args, **kwargs):
if not compat.IS_PY2:
return
with warning_catcher() as warns:
results = list(routine(unicode(JSON, 'utf-8'), *args, **kwargs))
self.assertEqual(expected_results, results)
self.assertEqual(1, len(warns))

def _assert_file(self, expected_results, routine, *args, **kwargs):
results = list(routine(compat.BytesIO(JSON), *args, **kwargs))
self.assertEqual(expected_results, results)
Expand All @@ -47,6 +66,9 @@ def event_yielder():
def _assert_entry_point(self, expected_results, previous_routine, routine,
*args, **kwargs):
self._assert_invalid_type(routine, *args, **kwargs)
self._assert_bytes(expected_results, routine, *args, **kwargs)
self._assert_str(expected_results, routine, *args, **kwargs)
self._assert_unicode(expected_results, routine, *args, **kwargs)
self._assert_file(expected_results, routine, *args, **kwargs)
self._assert_async_file(expected_results, routine, *args, **kwargs)
if previous_routine:
Expand Down

0 comments on commit 0eb32b0

Please sign in to comment.