blueloveTH 1 неделя назад
Родитель
Сommit
6e3ed84a9e
2 измененных файлов с 12 добавлено и 5 удалено
  1. 1 1
      include/typings/stdc.pyi
  2. 11 4
      src/modules/stdc.c

+ 1 - 1
include/typings/stdc.pyi

@@ -5,7 +5,7 @@ intptr = int
 def malloc(size: int) -> intptr: ...
 def free(ptr: intptr) -> None: ...
 
-def memcpy(dst: intptr, src: intptr, n: int) -> None: ...
+def memcpy(dst: intptr, src: intptr | bytes, n: int) -> None: ...
 def memset(s: intptr, c: int, n: int) -> None: ...
 def memcmp(s1: intptr, s2: intptr, n: int) -> int: ...
 

+ 11 - 4
src/modules/stdc.c

@@ -124,12 +124,19 @@ static bool stdc_free(int argc, py_Ref argv) {
 
 static bool stdc_memcpy(int argc, py_Ref argv) {
     PY_CHECK_ARGC(3);
-    PY_CHECK_ARG_TYPE(0, tp_int);
-    PY_CHECK_ARG_TYPE(1, tp_int);
-    PY_CHECK_ARG_TYPE(2, tp_int);
+    PY_CHECK_ARG_TYPE(0, tp_int);   // dst
     void* dst = (void*)(intptr_t)py_toint(&argv[0]);
-    void* src = (void*)(intptr_t)py_toint(&argv[1]);
+    PY_CHECK_ARG_TYPE(2, tp_int);   // n
     py_i64 n = py_toint(&argv[2]);
+    void* src;
+    if(py_istype(&argv[1], tp_bytes)) {
+        int size;
+        src = py_tobytes(&argv[1], &size);
+        if(size < n) n = size;
+    } else {
+        PY_CHECK_ARG_TYPE(1, tp_int);   // src
+        src = (void*)(intptr_t)py_toint(&argv[1]);
+    }
     memcpy(dst, src, (size_t)n);
     py_newnone(py_retval());
     return true;