Skip to content

Commit

Permalink
fix core dump when key wasn’t string. (#23)
Browse files Browse the repository at this point in the history
* fix core dump when key wasn’t string.

* add test code
  • Loading branch information
netyum authored Dec 26, 2023
1 parent 23d143f commit 5177330
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bridge/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static void dict2array(PyObject *pv, zval *zv) {
py2php_fn(value, &item);

ssize_t len;
const char *key = phpy::python::string2utf8(next, &len);
const char *key = phpy::python::string2utf8(PyObject_Str(next), &len);
add_assoc_zval_ex(zv, key, len, &item);
}
Py_DECREF(iter);
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/DictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ public function testIterInt()
}
$this->assertEquals($keys, array_keys($map));
}

public function testKeyNotString()
{
$pycode = <<<CODE
key1 = ("sum", "数量")
key2 = ("mean", "数量")
dict_data = {key1: 1, key2: 2}
CODE;
$json = json_encode(PyCore::scalar(PyCore::eval($pycode)->dict_data));
$result = "{\"('sum', '\u6570\u91cf')\":1,\"('mean', '\u6570\u91cf')\":2}";
$this->assertEquals($json, $result);
}
}

0 comments on commit 5177330

Please sign in to comment.