mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
support str() from bytes to string
This commit is contained in:
parent
f0b5be9c92
commit
2afaea6e7c
@ -116,6 +116,10 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
|
|||||||
res = strsFormat(&buffs, 11, "%f", val);
|
res = strsFormat(&buffs, 11, "%f", val);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
if (ARG_TYPE_BYTES == type) {
|
||||||
|
res = (char*)arg_getBytes(arg);
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
if (argType_isObject(type)) {
|
if (argType_isObject(type)) {
|
||||||
res = obj_toStr(arg_getPtr(arg));
|
res = obj_toStr(arg_getPtr(arg));
|
||||||
if (NULL != res) {
|
if (NULL != res) {
|
||||||
|
@ -116,6 +116,10 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
|
|||||||
res = strsFormat(&buffs, 11, "%f", val);
|
res = strsFormat(&buffs, 11, "%f", val);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
if (ARG_TYPE_BYTES == type) {
|
||||||
|
res = (char*)arg_getBytes(arg);
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
if (argType_isObject(type)) {
|
if (argType_isObject(type)) {
|
||||||
res = obj_toStr(arg_getPtr(arg));
|
res = obj_toStr(arg_getPtr(arg));
|
||||||
if (NULL != res) {
|
if (NULL != res) {
|
||||||
|
57
port/linux/test/ctypes-test.cpp
Normal file
57
port/linux/test/ctypes-test.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "test_common.h"
|
||||||
|
extern "C" {
|
||||||
|
#include "PikaMain.h"
|
||||||
|
#include "PikaParser.h"
|
||||||
|
#include "PikaStdLib_MemChecker.h"
|
||||||
|
#include "PikaVM.h"
|
||||||
|
#include "dataArgs.h"
|
||||||
|
#include "dataMemory.h"
|
||||||
|
#include "dataStrs.h"
|
||||||
|
#include "pikaScript.h"
|
||||||
|
#include "pika_config_gtest.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
extern PikaMemInfo pikaMemInfo;
|
||||||
|
/* the log_buff of printf */
|
||||||
|
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
|
||||||
|
|
||||||
|
TEST(ctypes, test1) {
|
||||||
|
/* init */
|
||||||
|
pikaMemInfo.heapUsedMax = 0;
|
||||||
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||||
|
/* run */
|
||||||
|
obj_run(pikaMain,
|
||||||
|
"read_data = ctypes.c_buffer(b'', 16)\n"
|
||||||
|
"print(\"----read size----\")\n"
|
||||||
|
"datalen = len(read_data.raw)\n"
|
||||||
|
"print(datalen)\n"
|
||||||
|
"print(read_data.raw)\n"
|
||||||
|
"print(\"----read data----\")\n"
|
||||||
|
"for i in range(0,datalen):\n"
|
||||||
|
" print(read_data.raw[i])\n"
|
||||||
|
"\n");
|
||||||
|
/* collect */
|
||||||
|
/* assert */
|
||||||
|
EXPECT_STREQ(log_buff[67], "16\r\n");
|
||||||
|
/* deinit */
|
||||||
|
obj_deinit(pikaMain);
|
||||||
|
EXPECT_EQ(pikaMemNow(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ctypes, test2) {
|
||||||
|
/* init */
|
||||||
|
pikaMemInfo.heapUsedMax = 0;
|
||||||
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||||
|
/* run */
|
||||||
|
obj_run(pikaMain,
|
||||||
|
"len(b'test\\x0')\n"
|
||||||
|
"len(str(b'test\\x0'))\n");
|
||||||
|
/* collect */
|
||||||
|
/* assert */
|
||||||
|
EXPECT_STREQ(log_buff[0], "4\r\n");
|
||||||
|
EXPECT_STREQ(log_buff[1], "5\r\n");
|
||||||
|
/* deinit */
|
||||||
|
obj_deinit(pikaMain);
|
||||||
|
EXPECT_EQ(pikaMemNow(), 0);
|
||||||
|
}
|
@ -129,12 +129,13 @@ Arg* arg_setType(Arg* self, ArgType type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) {
|
Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) {
|
||||||
self = arg_newContent(self, size + sizeof(size_t));
|
self = arg_newContent(self, size + sizeof(size_t) + 1);
|
||||||
self = arg_setName(self, name);
|
self = arg_setName(self, name);
|
||||||
self = arg_setType(self, ARG_TYPE_BYTES);
|
self = arg_setType(self, ARG_TYPE_BYTES);
|
||||||
void* dir = arg_getContent(self);
|
void* dir = arg_getContent(self);
|
||||||
/* set content all to 0 */
|
/* set content all to 0 */
|
||||||
__platform_memset(dir, 0, size + sizeof(size_t));
|
__platform_memset(dir, 0, size + sizeof(size_t) + 1);
|
||||||
|
/* setsize */
|
||||||
__platform_memcpy(dir, &size, sizeof(size_t));
|
__platform_memcpy(dir, &size, sizeof(size_t));
|
||||||
|
|
||||||
/* set init value */
|
/* set init value */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user