From 7056f1bb01472d88e4687efab54327e6fe2e1f9f Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Wed, 24 Apr 2024 21:12:59 +0800 Subject: [PATCH] socket_dowanload test passed --- examples/builtins/bytes.py | 11 ++++++ port/linux/test/py-test.cpp | 7 ++-- port/linux/test/python/builtins/bytes.py | 11 ++++++ .../test/python/socket/socket_download.py | 34 +++++++++++-------- src/PikaObj.c | 24 ++++++------- src/PikaVersion.h | 2 +- 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/examples/builtins/bytes.py b/examples/builtins/bytes.py index 39a90310d..738b95369 100644 --- a/examples/builtins/bytes.py +++ b/examples/builtins/bytes.py @@ -1,7 +1,18 @@ + +assert b"\r\n" in b"Hello\r\nWorld" + +assert b"\r\n\r\n" in b"Hello\r\n\r\nWorld" + +assert not (b"\r\n\r\n" in b"Hello\r\nWorld") + +assert not (b"\r\n\r\n" in b'\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x63\x6c\x6f\x73\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x39\x34\x31\x32\x0d\x0a\x41\x63\x63\x65\x70\x74\x2d\x52\x61\x6e\x67\x65\x73\x3a\x20\x62\x79\x74\x65\x73\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20') + + def test(): a = bytes(10) a[1] = 1 return a + print(test()) res = test() diff --git a/port/linux/test/py-test.cpp b/port/linux/test/py-test.cpp index f2a349769..2753ede94 100644 --- a/port/linux/test/py-test.cpp +++ b/port/linux/test/py-test.cpp @@ -104,9 +104,10 @@ TEST_RUN_SINGLE_FILE(modbus, rtu_request, "test/python/modbus/rtu_request.py") TEST_RUN_SINGLE_FILE(PikaStdDevice, inhert, "test/python/PikaStdDevice/inhert.py") -TEST_RUN_SINGLE_FILE(socket, - socket_download, - "test/python/socket/socket_download.py") +TEST_RUN_SINGLE_FILE_EXCEPT_OUTPUT(socket, + socket_download, + "test/python/socket/socket_download.py", + "PASS\r\n") #endif diff --git a/port/linux/test/python/builtins/bytes.py b/port/linux/test/python/builtins/bytes.py index 39a90310d..738b95369 100644 --- a/port/linux/test/python/builtins/bytes.py +++ b/port/linux/test/python/builtins/bytes.py @@ -1,7 +1,18 @@ + +assert b"\r\n" in b"Hello\r\nWorld" + +assert b"\r\n\r\n" in b"Hello\r\n\r\nWorld" + +assert not (b"\r\n\r\n" in b"Hello\r\nWorld") + +assert not (b"\r\n\r\n" in b'\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x63\x6c\x6f\x73\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x39\x34\x31\x32\x0d\x0a\x41\x63\x63\x65\x70\x74\x2d\x52\x61\x6e\x67\x65\x73\x3a\x20\x62\x79\x74\x65\x73\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20') + + def test(): a = bytes(10) a[1] = 1 return a + print(test()) res = test() diff --git a/port/linux/test/python/socket/socket_download.py b/port/linux/test/python/socket/socket_download.py index a8bbde408..b89acc665 100644 --- a/port/linux/test/python/socket/socket_download.py +++ b/port/linux/test/python/socket/socket_download.py @@ -31,12 +31,18 @@ def http_download_file(url: str, file_path: str, buff_size=1024): # Open file to write f = open(file_path, 'wb') # Manually open the file data_received = False - # try: + head_received = False while True: - data = sock.recv(buff_size) + try: + data = sock.recv(buff_size) + except: + print('End of data') + break print("[Data received:", len(data), ']') - sz = f.write(data) - print(data.decode()) + if head_received: + sz = f.write(data) + # print("Data written:", sz) + # print(data.decode()) if len(data) == 0: print("Length of data:", len(data)) if not data_received: @@ -48,16 +54,13 @@ def http_download_file(url: str, file_path: str, buff_size=1024): data_received = True # Handle the end of the HTTP header if it's still present - if b'\r\n\r\n' in data: - print(data.split(b'\r\n\r\n', 1)) - header, data = data.split(b'\r\n\r\n') - print("Header:", header.decode()) - print("Data:", data.decode()) - # except: - # print("Error while receiving data") - # f.close() - # sock.close() - # return -1 + if head_received == False: + if b'\r\n\r\n' in data: + # print("Header received", data) + head_received = True + aplited = data.split(b'\r\n\r\n', 1) + if len(aplited) == 2: + sz = f.write(aplited[1]) # Close file and socket manually f.close() @@ -66,4 +69,5 @@ def http_download_file(url: str, file_path: str, buff_size=1024): return 0 -http_download_file("http://pikapython.com", "pikapython.html") +assert http_download_file("http://pikapython.com", "pikapython.html") == 0 +print("PASS") diff --git a/src/PikaObj.c b/src/PikaObj.c index 588717ec4..eb221ef58 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -4334,28 +4334,28 @@ int builtins_bytearray___getitem__(PikaObj* self, int __key) { pika_bool _bytes_contains(Arg* self, Arg* others) { ArgType type = arg_getType(others); if (type == ARG_TYPE_BYTES) { - if (arg_getBytesSize(self) > arg_getBytesSize(others)) { - return pika_false; - } uint8_t* bytes1 = arg_getBytes(self); uint8_t* bytes2 = arg_getBytes(others); size_t size1 = arg_getBytesSize(self); size_t size2 = arg_getBytesSize(others); - size_t i = 0; - size_t j = 0; - while (i < size1 && j < size2) { + + if (size1 > size2) { + return pika_false; + } + + size_t i = 0, j = 0, start_j = 0; + while (j < size2) { if (bytes1[i] == bytes2[j]) { i++; j++; + if (i == size1) { + return pika_true; + } } else { - j++; + j = ++start_j; // Move `j` to the next start position + i = 0; // Reset `i` } } - if (i == size1) { - return pika_true; - } else { - return pika_false; - } } return pika_false; } diff --git a/src/PikaVersion.h b/src/PikaVersion.h index e24b14150..426ef6570 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 13 #define PIKA_VERSION_MICRO 3 -#define PIKA_EDIT_TIME "2024/04/24 20:40:16" +#define PIKA_EDIT_TIME "2024/04/24 21:12:52"