return None when return Null from cmodule

not raise when cjson parse faild
This commit is contained in:
lyon 2022-11-04 14:23:54 +08:00
parent c5f2d55a1d
commit 4ed6061733
6 changed files with 37 additions and 11 deletions

View File

@ -5,8 +5,8 @@
PikaObj* pika_cjson_Parse(PikaObj* self, char* value) {
cJSON* item = cJSON_Parse(value);
if (NULL == item) {
obj_setErrorCode(self, 3);
__platform_printf("Error: cJSON parse faild.\r\n");
// obj_setErrorCode(self, 3);
__platform_printf("Error: cJSON parse failed.\r\n");
return NULL;
}
PikaObj* cjson_obj = newNormalObj(New_pika_cjson_cJSON);

View File

@ -15,6 +15,7 @@ class Task(PikaStdTask.Task):
def test(a: int, b: object): ...
def test_dict() -> dict: ...
def test64(a: int64, b: int64) -> int64: ...
def test_raise(): ...
class ProxyTest:

View File

@ -59,3 +59,7 @@ void GTestTask_ProxyTest___setattr__(PikaObj* self,
__platform_printf("GTestTask_ProxyTest___setattr__: %s, %s\r\n", __name,
arg_getStr(__value));
}
void GTestTask_test_raise(PikaObj *self){
obj_setErrorCode(self, 1);
}

View File

@ -5,7 +5,7 @@
PikaObj* pika_cjson_Parse(PikaObj* self, char* value) {
cJSON* item = cJSON_Parse(value);
if (NULL == item) {
obj_setErrorCode(self, 3);
// obj_setErrorCode(self, 3);
__platform_printf("Error: cJSON parse failed.\r\n");
return NULL;
}

View File

@ -1196,6 +1196,7 @@ void method_returnPtr(Args* args, void* val) {
void method_returnObj(Args* args, void* val) {
if (NULL == val) {
args_pushArg_name(args, "@rt", arg_newNull());
return;
}
ArgType type;

View File

@ -26,7 +26,7 @@ TEST(except, try1) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(except, trycmodule1) {
TEST(except, def_none) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
@ -34,15 +34,12 @@ TEST(except, trycmodule1) {
/* run */
obj_run(pikaMain,
"import pika_cjson\n"
"try:\n"
" b = pika_cjson.Parse('')\n"
" print('after failed')\n"
"except:\n"
" print('parse failed')\n"
"b = pika_cjson.Parse('')\n"
"if None == b:\n"
" print('None')\n"
"\n");
/* collect */
EXPECT_STREQ("BEGIN\r\n", log_buff[2]);
EXPECT_STREQ("parse failed\r\n", log_buff[0]);
EXPECT_STREQ("None\r\n", log_buff[0]);
/* assert */
/* deinit */
obj_deinit(pikaMain);
@ -141,6 +138,29 @@ TEST(except, len) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(except, trycmodule1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
__platform_printf("BEGIN\r\n");
/* run */
obj_run(pikaMain,
"import GTestTask\n"
"try:\n"
" GTestTask.test_raise()\n"
" print('after failed')\n"
"except:\n"
" print('parse failed')\n"
"\n");
/* collect */
EXPECT_STREQ("BEGIN\r\n", log_buff[1]);
EXPECT_STREQ("parse failed\r\n", log_buff[0]);
/* assert */
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
TEST_END