forked from isagalaev/ijson
-
Notifications
You must be signed in to change notification settings - Fork 54
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
feat: iterate over more than one prefix (#109) #127
Open
wodny
wants to merge
6
commits into
ICRAR:master
Choose a base branch
from
wodny:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b67055
move items_basecoro send implementation into a common unit
wodny 2417743
add vim swap files to .gitignore
wodny 0f07f17
add prefixed_items
wodny e48ac48
deduplicate python implementation of (prefixed_)items_basecoro
wodny 78688c8
use set of strings instead of a single string for items prefix
wodny c8b6b78
revert using a set for prefix in items
wodny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ dist | |
.settings | ||
.cproject | ||
.autotools | ||
.*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,11 +49,13 @@ def _default_backend(): | |
parse_coro = backend.parse_coro | ||
items = backend.items | ||
items_coro = backend.items_coro | ||
prefixed_items = backend.prefixed_items | ||
prefixed_items_coro = backend.prefixed_items_coro | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the async variant is missing |
||
kvitems = backend.kvitems | ||
kvitems_coro = backend.kvitems_coro | ||
basic_parse_async = backend.basic_parse_async | ||
parse_async = backend.parse_async | ||
items_async = backend.items_async | ||
kvitems_async = backend.kvitems_async | ||
backend_name = backend.backend_name | ||
backend = backend.backend | ||
backend = backend.backend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ static int items_basecoro_init(ItemsBasecoro *self, PyObject *args, PyObject *kw | |
self->target_send = NULL; | ||
self->prefix = NULL; | ||
self->object_depth = 0; | ||
self->pending_builder_reset = 0; | ||
M1_N(self->module_state = get_state_from_imported_module()); | ||
builder_create(&self->builder); | ||
|
||
|
@@ -42,35 +43,13 @@ static void items_basecoro_dealloc(ItemsBasecoro *self) | |
PyObject* items_basecoro_send_impl(PyObject *self, PyObject *path, PyObject *event, PyObject *value) | ||
{ | ||
ItemsBasecoro *coro = (ItemsBasecoro *)self; | ||
enames_t enames = coro->module_state->enames; | ||
|
||
if (builder_isactive(&coro->builder)) { | ||
coro->object_depth += (event == enames.start_map_ename || event == enames.start_array_ename); | ||
coro->object_depth -= (event == enames.end_map_ename || event == enames.end_array_ename); | ||
if (coro->object_depth > 0) { | ||
N_M1( builder_event(&coro->builder, enames, event, value) ); | ||
} | ||
else { | ||
PyObject *retval = builder_value(&coro->builder); | ||
CORO_SEND(coro->target_send, retval); | ||
Py_DECREF(retval); | ||
N_M1(builder_reset(&coro->builder)); | ||
} | ||
} | ||
else { | ||
int cmp = PyObject_RichCompareBool(path, coro->prefix, Py_EQ); | ||
N_M1(cmp); | ||
if (cmp) { | ||
if (event == enames.start_map_ename || event == enames.start_array_ename) { | ||
coro->object_depth = 1; | ||
N_M1(builder_event(&coro->builder, enames, event, value)); | ||
} | ||
else { | ||
CORO_SEND(coro->target_send, value); | ||
} | ||
} | ||
} | ||
|
||
PyObject *retval; | ||
N_N(retval = items_common_send_top(self, path, event, value)); | ||
if (retval == Py_None) | ||
Py_RETURN_NONE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is incrementing a reference to |
||
CORO_SEND(coro->target_send, retval); | ||
items_common_send_bottom(self, retval); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#ifndef ITEMS_COMMON_H | ||
#define ITEMS_COMMON_H | ||
|
||
#include "builder.h" | ||
#include "module_state.h" | ||
|
||
/** | ||
* common items/prefixed_items_basecoro coroutine object structure | ||
*/ | ||
typedef struct { | ||
PyObject_HEAD | ||
builder_t builder; | ||
int pending_builder_reset; | ||
PyObject *target_send; | ||
PyObject *prefix; | ||
int object_depth; | ||
yajl2_state *module_state; | ||
} ItemsCommonBasecoro; | ||
|
||
static inline | ||
PyObject* items_common_send_top(PyObject *self, PyObject *path, PyObject *event, PyObject *value) | ||
{ | ||
ItemsCommonBasecoro *coro = (ItemsCommonBasecoro *)self; | ||
enames_t enames = coro->module_state->enames; | ||
|
||
if (builder_isactive(&coro->builder)) { | ||
coro->object_depth += (event == enames.start_map_ename || event == enames.start_array_ename); | ||
coro->object_depth -= (event == enames.end_map_ename || event == enames.end_array_ename); | ||
if (coro->object_depth > 0) { | ||
N_M1( builder_event(&coro->builder, enames, event, value) ); | ||
} | ||
else { | ||
PyObject *retval = builder_value(&coro->builder); | ||
coro->pending_builder_reset = 1; | ||
return retval; | ||
} | ||
} | ||
else { | ||
int cmp = SPECIFIC_ITEMS_CMP(coro->prefix, path); | ||
N_M1(cmp); | ||
if (cmp) { | ||
if (event == enames.start_map_ename || event == enames.start_array_ename) { | ||
coro->object_depth = 1; | ||
N_M1(builder_event(&coro->builder, enames, event, value)); | ||
} | ||
else { | ||
Py_INCREF(value); | ||
return value; | ||
} | ||
} | ||
} | ||
|
||
Py_RETURN_NONE; | ||
} | ||
|
||
static inline | ||
void items_common_send_bottom(PyObject *self, PyObject *retval) | ||
{ | ||
ItemsCommonBasecoro *coro = (ItemsCommonBasecoro *)self; | ||
|
||
Py_DECREF(retval); | ||
if (coro->pending_builder_reset) { | ||
builder_reset(&coro->builder); | ||
coro->pending_builder_reset = 0; | ||
} | ||
} | ||
|
||
#endif // ITEMS_COMMON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* items generator implementation for ijson's C backend | ||
* | ||
* Contributed by Rodrigo Tobar <[email protected]> | ||
* | ||
* ICRAR - International Centre for Radio Astronomy Research | ||
* (c) UWA - The University of Western Australia, 2020 | ||
* Copyright by UWA (in the framework of the ICRAR) | ||
*/ | ||
|
||
#include "common.h" | ||
#include "prefixed_items.h" | ||
#include "prefixed_items_basecoro.h" | ||
#include "parse_basecoro.h" | ||
#include "basic_parse_basecoro.h" | ||
|
||
/* | ||
* __init__, destructor, __iter__ and __next__ | ||
*/ | ||
static int prefixed_itemsgen_init(PrefixedItemsGen *self, PyObject *args, PyObject *kwargs) | ||
{ | ||
PyObject *reading_args = PySequence_GetSlice(args, 0, 2); | ||
PyObject *items_args = PySequence_GetSlice(args, 2, 4); | ||
pipeline_node coro_pipeline[] = { | ||
{&PrefixedItemsBasecoro_Type, items_args, NULL}, | ||
{&ParseBasecoro_Type, NULL, NULL}, | ||
{&BasicParseBasecoro_Type, NULL, kwargs}, | ||
{NULL} | ||
}; | ||
int res = reading_generator_init(&self->reading_gen, reading_args, coro_pipeline); | ||
Py_DECREF(items_args); | ||
Py_DECREF(reading_args); | ||
return res; | ||
} | ||
|
||
static void prefixed_itemsgen_dealloc(PrefixedItemsGen *self) | ||
{ | ||
reading_generator_dealloc(&self->reading_gen); | ||
Py_TYPE(self)->tp_free((PyObject*)self); | ||
} | ||
|
||
static PyObject* prefixed_itemsgen_iternext(PyObject *self) | ||
{ | ||
PrefixedItemsGen *gen = (PrefixedItemsGen *)self; | ||
return reading_generator_next(&gen->reading_gen); | ||
} | ||
|
||
/* | ||
* items generator object type | ||
*/ | ||
PyTypeObject PrefixedItemsGen_Type = { | ||
PyVarObject_HEAD_INIT(NULL, 0) | ||
.tp_basicsize = sizeof(PrefixedItemsGen), | ||
.tp_name = "_yajl2.prefixed_items", | ||
.tp_doc = "Generates items", | ||
.tp_init = (initproc)prefixed_itemsgen_init, | ||
.tp_dealloc = (destructor)prefixed_itemsgen_dealloc, | ||
.tp_flags = Py_TPFLAGS_DEFAULT, | ||
.tp_iter = ijson_return_self, | ||
.tp_iternext = prefixed_itemsgen_iternext | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* items generator for ijson's C backend | ||
* | ||
* Contributed by Rodrigo Tobar <[email protected]> | ||
* | ||
* ICRAR - International Centre for Radio Astronomy Research | ||
* (c) UWA - The University of Western Australia, 2020 | ||
* Copyright by UWA (in the framework of the ICRAR) | ||
*/ | ||
|
||
#ifndef PREFIXED_ITEMS_H | ||
#define PREFIXED_ITEMS_H | ||
|
||
#include "reading_generator.h" | ||
|
||
/** | ||
* items generator object structure | ||
*/ | ||
typedef struct { | ||
PyObject_HEAD | ||
reading_generator_t reading_gen; | ||
} PrefixedItemsGen; | ||
|
||
|
||
/** | ||
* items generator object type | ||
*/ | ||
extern PyTypeObject PrefixedItemsGen_Type; | ||
|
||
#endif /* PREFIXED_ITEMS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* items_async asynchronous iterable implementation for ijson's C backend | ||
* | ||
* Contributed by Rodrigo Tobar <[email protected]> | ||
* | ||
* ICRAR - International Centre for Radio Astronomy Research | ||
* (c) UWA - The University of Western Australia, 2020 | ||
* Copyright by UWA (in the framework of the ICRAR) | ||
*/ | ||
|
||
|
||
#include "async_reading_generator.h" | ||
#include "basic_parse_basecoro.h" | ||
#include "parse_basecoro.h" | ||
#include "prefixed_items_basecoro.h" | ||
#include "common.h" | ||
#include "coro_utils.h" | ||
|
||
/** | ||
* items_async asynchronous iterable object structure | ||
*/ | ||
typedef struct { | ||
PyObject_HEAD | ||
async_reading_generator *reading_generator; | ||
} PrefixedItemsAsync; | ||
|
||
/* | ||
* __init__, destructor and __anext__ | ||
*/ | ||
static int prefixed_itemsasync_init(PrefixedItemsAsync *self, PyObject *args, PyObject *kwargs) | ||
{ | ||
PyObject *reading_args = PySequence_GetSlice(args, 0, 2); | ||
PyObject *items_args = PySequence_GetSlice(args, 2, 4); | ||
pipeline_node coro_pipeline[] = { | ||
{&PrefixedItemsBasecoro_Type, items_args, NULL}, | ||
{&ParseBasecoro_Type, NULL, NULL}, | ||
{&BasicParseBasecoro_Type, NULL, kwargs}, | ||
{NULL} | ||
}; | ||
M1_N(self->reading_generator = (async_reading_generator *)PyObject_CallObject((PyObject *)&AsyncReadingGeneratorType, reading_args)); | ||
int ret = async_reading_generator_add_coro(self->reading_generator, coro_pipeline); | ||
Py_DECREF(items_args); | ||
Py_DECREF(reading_args); | ||
return ret; | ||
} | ||
|
||
static void prefixed_itemsasync_dealloc(PrefixedItemsAsync *self) { | ||
Py_XDECREF(self->reading_generator); | ||
Py_TYPE(self)->tp_free((PyObject*)self); | ||
} | ||
|
||
static PyObject *prefixed_itemsasync_anext(PyObject *self) | ||
{ | ||
PrefixedItemsAsync *gen = (PrefixedItemsAsync *)self; | ||
Py_INCREF(gen->reading_generator); | ||
return (PyObject *)gen->reading_generator; | ||
} | ||
|
||
static PyAsyncMethods prefixed_itemsasync_methods = { | ||
.am_await = ijson_return_self, | ||
.am_aiter = ijson_return_self, | ||
.am_anext = prefixed_itemsasync_anext | ||
}; | ||
|
||
PyTypeObject PrefixedItemsAsync_Type = { | ||
PyVarObject_HEAD_INIT(NULL, 0) | ||
.tp_basicsize = sizeof(PrefixedItemsAsync), | ||
.tp_name = "_yajl2._prefixed_items_async", | ||
.tp_doc = "Asynchronous iterable yielding fully-built items", | ||
.tp_init = (initproc)prefixed_itemsasync_init, | ||
.tp_dealloc = (destructor)prefixed_itemsasync_dealloc, | ||
.tp_flags = Py_TPFLAGS_DEFAULT, | ||
.tp_as_async = &prefixed_itemsasync_methods | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good place as any to discuss the name. I'm not convinced by "prefixed items" doesn't convey the fact that now you can specify multiple prefixes. I originally thought of "multi_items" or something along those lines, to make it clearer that there was no restriction on the number of prefixes.