Compare commits

...

3 Commits

Author SHA1 Message Date
Tanisha Vasudeva
43eb85dd2a
Merge 6cc3d8023350163e72b83d49981ad2c4b08d39b4 into 8c4ed3bc3423f56d907e80ab4dc42dab1267afc3 2026-06-11 05:42:14 +10:00
blueloveTH
8c4ed3bc34 fix a bug of str 2026-06-02 20:42:03 +08:00
tanisha
6cc3d80233 Added readline() 2026-02-28 20:28:04 +05:30
5 changed files with 40 additions and 8 deletions

View File

@ -228,7 +228,8 @@ static bool str__getitem__(int argc, py_Ref argv) {
py_Ref _1 = py_arg(1);
if(_1->type == tp_int) {
int index = py_toint(py_arg(1));
if(!pk__normalize_index(&index, self.size)) return false;
int u8_len = c11_sv__u8_length(self);
if(!pk__normalize_index(&index, u8_len)) return false;
c11_sv res = c11_sv__u8_getitem(self, index);
py_newstrv(py_retval(), res);
return true;

View File

@ -104,8 +104,10 @@ c11_sv c11_sv__slice(c11_sv sv, int start) { return c11_sv__slice2(sv, start, sv
c11_sv c11_sv__slice2(c11_sv sv, int start, int stop) {
if(start < 0) start = 0;
if(stop < start) stop = start;
if(start > sv.size) start = sv.size;
if(stop < 0) stop = 0;
if(stop > sv.size) stop = sv.size;
if(stop < start) stop = start;
return (c11_sv){sv.data + start, stop - start};
}
@ -997,7 +999,6 @@ const static c11_u32_range kLoRanges[] = {
// clang-format on
bool c11__is_unicode_Lo_char(int c) {
if(c == 0x1f955) return true;
const char* data =
c11__search_u32_ranges(c, kLoRanges, sizeof(kLoRanges) / sizeof(c11_u32_range));
return data != NULL;

View File

@ -232,7 +232,31 @@ static bool io_FileIO_flush(int argc, py_Ref argv) {
py_newnone(py_retval());
return true;
}
static bool io_FileIO_readlines(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
io_FileIO* ud = py_touserdata(py_arg(0));
py_newlist(py_retval());
char* buf = NULL;
size_t buf_size = 0;
size_t len = 0;
int c;
while(true) {
len = 0;
while((c = fgetc(ud->file)) != EOF) {
if(len + 1 >= buf_size) {
buf_size = (buf_size == 0) ? 64 : buf_size * 2;
buf = PK_REALLOC(buf, buf_size);
}
buf[len++] = (char)c;
if(c == '\n') break;
}
if(len == 0) break;
py_newstrv(py_getreg(0), (c11_sv){buf, len});
py_list_append(py_retval(), py_getreg(0));
}
if(buf) PK_FREE(buf);
return true;
}
void pk__add_module_io() {
py_Ref mod = py_newmodule("io");
@ -247,6 +271,7 @@ void pk__add_module_io() {
py_bindmethod(FileIO, "tell", io_FileIO_tell);
py_bindmethod(FileIO, "seek", io_FileIO_seek);
py_bindmethod(FileIO, "flush", io_FileIO_flush);
py_bindmethod(FileIO, "readlines", io_FileIO_readlines);
py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);

View File

@ -501,10 +501,10 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
bool py_pickle_loads(const unsigned char* data, int size) {
const unsigned char* p = data;
// \xf0\x9f\xa5\x95
if(size < 4 || p[0] != 240 || p[1] != 159 || p[2] != 165 || p[3] != 149)
// PK
if(size < 2 || p[0] != 'P' || p[1] != 'K')
return ValueError("invalid pickle data");
p += 4;
p += 2;
c11_smallmap_d2d type_mapping;
c11_smallmap_d2d__ctor(&type_mapping);
@ -780,7 +780,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
static bool PickleObject__py_submit(PickleObject* self, py_OutRef out) {
c11_sbuf cleartext;
c11_sbuf__ctor(&cleartext);
c11_sbuf__write_cstr(&cleartext, "\xf0\x9f\xa5\x95");
c11_sbuf__write_cstr(&cleartext, "PK");
// line 1: type mapping
for(py_Type type = 0; type < self->used_types_length; type++) {
if(self->used_types[type]) {

View File

@ -221,6 +221,11 @@ assert chr(0x1f955) == '🥕'
assert ord('') == 27979
assert chr(27979) == ''
assert '测试'[0] == ''
assert '测试'[1] == ''
assert '测试'[-1] == ''
assert '测试'[-2] == ''
# test format()
assert "Hello, {}!".format("World") == "Hello, World!"
assert "{} {} {}".format("I", "love", "Python") == "I love Python"