mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
Modify string constructor to allow no arguments (#226)
* Modify String Constructor * Add check to restrict arguments
This commit is contained in:
parent
b0f6b54600
commit
2ab819a724
@ -496,7 +496,11 @@ void init_builtins(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// tp_str
|
// tp_str
|
||||||
_vm->bind_constructor<2>(_vm->_t(VM::tp_str), PK_LAMBDA(vm->py_str(args[1])));
|
_vm->bind_constructor<-1>(_vm->_t(VM::tp_str), [](VM* vm, ArgsView args) {
|
||||||
|
if(args.size() == 1) return VAR(Str());
|
||||||
|
if(args.size() > 2) vm->TypeError("str() takes at most 1 argument");
|
||||||
|
return vm->py_str(args[1]);
|
||||||
|
});
|
||||||
|
|
||||||
_vm->bind__hash__(VM::tp_str, [](VM* vm, PyObject* _0) {
|
_vm->bind__hash__(VM::tp_str, [](VM* vm, PyObject* _0) {
|
||||||
return (i64)_CAST(Str&, _0).hash();
|
return (i64)_CAST(Str&, _0).hash();
|
||||||
|
@ -14,6 +14,7 @@ assert repr('\\\n\t\'\r\b\x48') == r"'\\\n\t\'\r\bH'"
|
|||||||
a = ''
|
a = ''
|
||||||
b = 'test'
|
b = 'test'
|
||||||
c ='test'
|
c ='test'
|
||||||
|
assert a == str()
|
||||||
assert len(a) == 0
|
assert len(a) == 0
|
||||||
assert len(b) == 4
|
assert len(b) == 4
|
||||||
assert b == c
|
assert b == c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user