Skip to content

Commit

Permalink
Fix parsing with no space after a comma
Browse files Browse the repository at this point in the history
  • Loading branch information
lambacck committed Apr 27, 2020
1 parent a47bf3d commit fd1c958
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/pghstore/_speedups.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int _find_quoted(char **start_pos, char *s_start, char *s_end, char **token_star
case '\n':
break;
case '"':
// start of key
// start of quoted token
only_space = 0;
*token_start = pos + 1;
*token_end = strchr_unescaped(*token_start, s_end-(*token_start), '"');
Expand All @@ -163,7 +163,7 @@ int _find_quoted(char **start_pos, char *s_start, char *s_end, char **token_star

*start_pos = (*token_end) + 1;

debug_print( "found key: pos[%c, %li], ks[%c, %li], ke[%c, %li]\n", **start_pos, *start_pos-s_start, **token_start, *token_start-s_start, **token_end, *token_end-s_start);
debug_print( "found token: pos[%c, %li], ks[%c, %li], ke[%c, %li]\n", **start_pos, *start_pos-s_start, **token_start, *token_start-s_start, **token_end, *token_end-s_start);
return 0;

case 'N':
Expand Down Expand Up @@ -229,7 +229,7 @@ int _find_comma_separator(char **s, char *s_start, char *s_end){
default:
if (retval == 1) {
// a comma was found and this should be the start of a new key.
*s = pos-1;
*s = pos;
return retval;
}
// Non-space found before comma
Expand Down Expand Up @@ -318,10 +318,12 @@ _speedups_loads(PyObject *self, PyObject *args, PyObject *keywds)
switch (_find_quoted(&pos, s_start, s_end, &value_start, &value_end, 1)){
case 2:
// NULL
debug_print( "found null value: pos[%c, %li]\n", *pos, pos-s_start);
null_value = 1;
break;
case 0:
// quoted value
debug_print( "found value: pos[%c, %li], vs[%c, %li], ve[%c, %li]\n", *pos, pos-s_start, *value_start, value_start-s_start, *value_end, value_end-s_start);
null_value = 0;
break;

Expand Down Expand Up @@ -366,11 +368,13 @@ _speedups_loads(PyObject *self, PyObject *args, PyObject *keywds)
switch (_find_comma_separator(&pos, s_start, s_end)) {
case 0:
// only space
debug_print( "found only space: pos[%c, %li]\n", *pos, pos-s_start);
need_one = 0;
break;
case 1:
// comma found
// we expect to need an iteration
debug_print( "found comma: pos[%c, %li]\n", *pos, pos-s_start);
need_one = 1;
break;

Expand Down
6 changes: 6 additions & 0 deletions tests/test_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ def test_roundtrip_with_all_the_escapables(self):
d = {"failing": r"some test \""}
self.assertDictEqual(d, self.pghstore.loads(self.pghstore.dumps(d)))

def test_roundtrip_multiple(self):
src = [("pgsql", "mysql"), ("python", "php"), ("gevent", "nodejs")]
self.assertEqual(
src, self.pghstore.loads(self.pghstore.dumps(src), return_type=list)
)


@pytest.mark.skipif(
_speedups is None, reason="Could not compile C extensions for tests"
Expand Down

0 comments on commit fd1c958

Please sign in to comment.