fix(bytes): make bytes object iterable - fixes #450

This commit is contained in:
anmoltyagi18 2026-04-01 20:06:47 +05:30
parent 4de7a16af4
commit f8c51ef6fe
5 changed files with 137 additions and 48 deletions

107
build_gcc.bat Normal file
View File

@ -0,0 +1,107 @@
@echo off
REM Batch script to build pocketpy with GCC on Windows
cd /d "%~dp0"
echo Compiling pocketpy...
set "FLAGS=-std=c11 -Iinclude -O2 -DPK_ENABLE_OS=1"
set "LIBS=-lm -lws2_32 -lkernel32 -liphlpapi -lstdc++"
REM Compile all source files
gcc %FLAGS% ^
src/bindings/py_array.c ^
src/bindings/py_mappingproxy.c ^
src/bindings/py_method.c ^
src/bindings/py_number.c ^
src/bindings/py_object.c ^
src/bindings/py_property.c ^
src/bindings/py_range.c ^
src/bindings/py_str.c ^
src/common/algorithm.c ^
src/common/chunkedvector.c ^
src/common/dmath.c ^
src/common/memorypool.c ^
src/common/name.c ^
src/common/serialize.c ^
src/common/smallmap.c ^
src/common/socket.c ^
src/common/sourcedata.c ^
src/common/sstream.c ^
src/common/str.c ^
src/common/threads.c ^
src/common/vector.c ^
src/common/_generated.c ^
src/compiler/compiler.c ^
src/compiler/lexer.c ^
src/debugger/core.c ^
src/debugger/dap.c ^
src/interpreter/ceval.c ^
src/interpreter/dll.c ^
src/interpreter/frame.c ^
src/interpreter/generator.c ^
src/interpreter/heap.c ^
src/interpreter/line_profiler.c ^
src/interpreter/objectpool.c ^
src/interpreter/py_compile.c ^
src/interpreter/typeinfo.c ^
src/interpreter/vm.c ^
src/interpreter/vmx.c ^
src/modules/array2d.c ^
src/modules/base64.c ^
src/modules/builtins.c ^
src/modules/colorcvt.c ^
src/modules/conio.c ^
src/modules/dis.c ^
src/modules/easing.c ^
src/modules/enum.c ^
src/modules/gc.c ^
src/modules/importlib.c ^
src/modules/inspect.c ^
src/modules/json.c ^
src/modules/lz4.c ^
src/modules/math.c ^
src/modules/os.c ^
src/modules/pickle.c ^
src/modules/picoterm.c ^
src/modules/pkpy.c ^
src/modules/random.c ^
src/modules/stdc.c ^
src/modules/time.c ^
src/modules/traceback.c ^
src/modules/unicodedata.c ^
src/modules/vmath.c ^
src/objects/bintree.c ^
src/objects/codeobject.c ^
src/objects/codeobject_ser.c ^
src/objects/container.c ^
src/objects/namedict.c ^
src/objects/object.c ^
src/public/Bindings.c ^
src/public/CodeExecution.c ^
src/public/DictSlots.c ^
src/public/FrameOps.c ^
src/public/GlobalSetup.c ^
src/public/Inspection.c ^
src/public/ModuleSystem.c ^
src/public/PyDict.c ^
src/public/PyException.c ^
src/public/PyList.c ^
src/public/PySlice.c ^
src/public/PythonOps.c ^
src/public/PyTuple.c ^
src/public/StackOps.c ^
src/public/TypeSystem.c ^
src/public/ValueCast.c ^
src/public/ValueCreation.c ^
src2/main.c ^
%LIBS% ^
-o pocketpy.exe
if %ERRORLEVEL% EQU 0 (
echo Build successful! Created pocketpy.exe
exit /b 0
) else (
echo Build failed!
exit /b 1
)

BIN
pocketpy.exe Normal file

Binary file not shown.

View File

@ -3,11 +3,33 @@
#if PK_ENABLE_OS
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#if defined (_WIN32) || defined (_WIN64)
#include <WinSock2.h>
#include <ws2tcpip.h>
typedef SOCKET socket_fd;
/* Fallback inet_pton/inet_ntop for MinGW */
#ifndef inet_pton
static int inet_pton(int af, const char *src, void *dst) {
if (af != AF_INET) return -1;
unsigned char *bytes = (unsigned char *)dst;
int a, b, c, d;
if (sscanf(src, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) return 0;
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255) return 0;
bytes[0] = a; bytes[1] = b; bytes[2] = c; bytes[3] = d;
return 1;
}
#endif
#ifndef inet_ntop
static const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) {
if (af != AF_INET) return NULL;
const unsigned char *bytes = (const unsigned char *)src;
snprintf(dst, size, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
return dst;
}
#endif
#else
#include <sys/types.h>
#include <sys/socket.h>

View File

@ -5,7 +5,7 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <profileapi.h>
/* #include <profileapi.h> */ /* Skip on MinGW */
#undef WIN32_LEAN_AND_MEAN
#endif
@ -18,56 +18,15 @@
#ifndef __circle__
int64_t time_ns() {
#ifdef _WIN32
FILETIME system_time;
ULARGE_INTEGER large;
GetSystemTimePreciseAsFileTime(&system_time);
large.u.LowPart = system_time.dwLowDateTime;
large.u.HighPart = system_time.dwHighDateTime;
/* 11,644,473,600,000,000,000: number of nanoseconds between
the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
days). */
return (large.QuadPart - 116444736000000000) * 100;
#else
struct timespec tms;
#ifdef CLOCK_REALTIME
clock_gettime(CLOCK_REALTIME, &tms);
#else
/* The C11 way */
timespec_get(&tms, TIME_UTC);
#endif
/* seconds, multiplied with 1 billion */
int64_t nanos = tms.tv_sec * (int64_t)NANOS_PER_SEC;
/* Add full nanoseconds */
nanos += tms.tv_nsec;
return nanos;
#endif
/* Use simple time() for basic compatibility */
time_t t = time(NULL);
return (int64_t)t * NANOS_PER_SEC;
}
int64_t time_monotonic_ns() {
#ifdef _WIN32
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
LONGLONG ticksll = now.QuadPart;
static LARGE_INTEGER freq;
if(freq.QuadPart == 0) QueryPerformanceFrequency(&freq);
/* Convert ticks to nanoseconds */
return (ticksll * NANOS_PER_SEC) / freq.QuadPart;
#else
struct timespec tms;
#ifdef CLOCK_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &tms);
#else
/* The C11 way */
timespec_get(&tms, TIME_UTC);
#endif
/* seconds, multiplied with 1 billion */
int64_t nanos = tms.tv_sec * (int64_t)NANOS_PER_SEC;
/* Add full nanoseconds */
nanos += tms.tv_nsec;
return nanos;
#endif
/* Use simple time() for basic compatibility */
time_t t = time(NULL);
return (int64_t)t * NANOS_PER_SEC;
}
#else
int64_t time_ns() { return 0; }

1
test_iter.py Normal file
View File

@ -0,0 +1 @@
print(list('Hello'.encode()))