mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
some fix
This commit is contained in:
parent
7feb3047e9
commit
403e3c9f0e
@ -96,6 +96,8 @@ py_Type pk_str__register();
|
||||
py_Type pk_bytes__register();
|
||||
py_Type pk_list__register();
|
||||
|
||||
py_TValue pk_builtins__register();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -293,8 +293,8 @@ bool py_callmethod(py_Ref self, py_Name, int argc, py_Ref argv);
|
||||
/// The stack remains unchanged after the operation.
|
||||
bool py_callmagic(py_Name name, int argc, py_Ref argv);
|
||||
|
||||
#define py_repr(self) py_callmagic(__repr__, 1, self)
|
||||
#define py_str(self) py_callmagic(__str__, 1, self)
|
||||
bool py_str(py_Ref val);
|
||||
bool py_repr(py_Ref val);
|
||||
|
||||
/// The return value of the most recent call.
|
||||
py_GlobalRef py_retval();
|
||||
|
@ -132,7 +132,7 @@ void pk_VM__ctor(pk_VM* self) {
|
||||
#undef validate
|
||||
|
||||
self->StopIteration = *py_tpobject(tp_stop_iteration);
|
||||
self->builtins = *py_newmodule("builtins", NULL);
|
||||
self->builtins = pk_builtins__register();
|
||||
|
||||
/* Setup Public Builtin Types */
|
||||
py_Type public_types[] = {tp_object,
|
||||
|
@ -50,3 +50,16 @@ py_Ref py_newmodule(const char* name, const char* package) {
|
||||
py_poptmp(2);
|
||||
return py_getmodule(name);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
static bool _py_builtins__repr(int argc, py_Ref argv){
|
||||
PY_CHECK_ARGC(1);
|
||||
return py_repr(argv);
|
||||
}
|
||||
|
||||
py_TValue pk_builtins__register(){
|
||||
py_Ref builtins = py_newmodule("builtins", NULL);
|
||||
py_bindnativefunc(builtins, "repr", _py_builtins__repr);
|
||||
return *builtins;
|
||||
}
|
@ -52,8 +52,15 @@ unsigned char* py_tobytes(const py_Ref self, int* size) {
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
|
||||
static bool _py_str__new__(int argc, py_Ref argv) { return true; }
|
||||
static bool _py_str__new__(int argc, py_Ref argv) {
|
||||
assert(argc >= 1);
|
||||
if(argc == 1) {
|
||||
py_newstr(py_retval(), "");
|
||||
return true;
|
||||
}
|
||||
if(argc > 2) return TypeError("str() takes at most 1 argument");
|
||||
return py_str(py_arg(1));
|
||||
}
|
||||
|
||||
static bool _py_str__hash__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
@ -157,7 +164,7 @@ static bool _py_str__getitem__(int argc, py_Ref argv) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#define DEF_STR_CMP_OP(op, f, condition) \
|
||||
#define DEF_STR_CMP_OP(op, __f, __cond) \
|
||||
static bool _py_str##op(int argc, py_Ref argv) { \
|
||||
PY_CHECK_ARGC(2); \
|
||||
c11_string* self = py_touserdata(&argv[0]); \
|
||||
@ -165,8 +172,8 @@ static bool _py_str__getitem__(int argc, py_Ref argv) {
|
||||
py_newnotimplemented(py_retval()); \
|
||||
} else { \
|
||||
c11_string* other = py_touserdata(&argv[1]); \
|
||||
int res = c11_sv__cmp(c11_string__sv(self), c11_string__sv(other)); \
|
||||
py_newbool(py_retval(), condition); \
|
||||
int res = __f(c11_string__sv(self), c11_string__sv(other)); \
|
||||
py_newbool(py_retval(), __cond); \
|
||||
} \
|
||||
return true; \
|
||||
}
|
||||
@ -281,3 +288,11 @@ py_Type pk_bytes__register() {
|
||||
// no need to dtor because the memory is controlled by the object
|
||||
return type;
|
||||
}
|
||||
|
||||
bool py_str(py_Ref val) {
|
||||
py_Ref tmp = py_tpfindmagic(val->type, __str__);
|
||||
if(!tmp) return py_repr(val);
|
||||
return py_call(tmp, 1, val);
|
||||
}
|
||||
|
||||
bool py_repr(py_Ref val) { return py_callmagic(__repr__, 1, val); }
|
108
tests/01_int.py
108
tests/01_int.py
@ -103,71 +103,51 @@ assert ~0 == -1
|
||||
assert str(1) == '1'
|
||||
assert repr(1) == '1'
|
||||
|
||||
try:
|
||||
1 // 0
|
||||
exit(1)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
try:
|
||||
1 % 0
|
||||
exit(1)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
try:
|
||||
2**60 // 0
|
||||
exit(1)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
try:
|
||||
2**60 % 0
|
||||
exit(1)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
try:
|
||||
divmod(1, 0)
|
||||
exit(1)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
try:
|
||||
divmod(2**60, 0)
|
||||
exit(1)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
assert not 1 < 2 > 3
|
||||
assert 1 < 2 < 3
|
||||
assert 4 > 3 >= 3
|
||||
|
||||
try:
|
||||
x = eval("231231312312312312312312312312312312314354657553423345632")
|
||||
print(f"eval should fail, but got {x!r}")
|
||||
exit(1)
|
||||
except SyntaxError:
|
||||
pass
|
||||
exit()
|
||||
|
||||
assert int("-5") == -5
|
||||
assert int("-4") == -4
|
||||
assert int("-3") == -3
|
||||
assert int("-2") == -2
|
||||
assert int("-1") == -1
|
||||
assert int("0") == 0
|
||||
assert int("1") == 1
|
||||
assert int("2") == 2
|
||||
assert int("3") == 3
|
||||
assert int("4") == 4
|
||||
assert int("5") == 5
|
||||
assert int("6") == 6
|
||||
assert int("7") == 7
|
||||
assert int("8") == 8
|
||||
assert int("9") == 9
|
||||
assert int("10") == 10
|
||||
assert int("11") == 11
|
||||
assert int("12") == 12
|
||||
assert int("13") == 13
|
||||
assert int("14") == 14
|
||||
assert int("15") == 15
|
||||
assert int("16") == 16
|
||||
# try:
|
||||
# 1 // 0
|
||||
# exit(1)
|
||||
# except ZeroDivisionError:
|
||||
# pass
|
||||
|
||||
# try:
|
||||
# 1 % 0
|
||||
# exit(1)
|
||||
# except ZeroDivisionError:
|
||||
# pass
|
||||
|
||||
# try:
|
||||
# 2**60 // 0
|
||||
# exit(1)
|
||||
# except ZeroDivisionError:
|
||||
# pass
|
||||
|
||||
# try:
|
||||
# 2**60 % 0
|
||||
# exit(1)
|
||||
# except ZeroDivisionError:
|
||||
# pass
|
||||
|
||||
# try:
|
||||
# divmod(1, 0)
|
||||
# exit(1)
|
||||
# except ZeroDivisionError:
|
||||
# pass
|
||||
|
||||
# try:
|
||||
# divmod(2**60, 0)
|
||||
# exit(1)
|
||||
# except ZeroDivisionError:
|
||||
# pass
|
||||
|
||||
# try:
|
||||
# x = eval("231231312312312312312312312312312312314354657553423345632")
|
||||
# print(f"eval should fail, but got {x!r}")
|
||||
# exit(1)
|
||||
# except SyntaxError:
|
||||
# pass
|
Loading…
x
Reference in New Issue
Block a user