Modify string constructor to allow no arguments (#226)

* Modify String Constructor

* Add check to restrict arguments
This commit is contained in:
Anurag Bhat 2024-03-12 22:25:54 +05:30 committed by GitHub
parent b0f6b54600
commit 2ab819a724
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -496,7 +496,11 @@ void init_builtins(VM* _vm) {
});
// 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) {
return (i64)_CAST(Str&, _0).hash();

View File

@ -14,6 +14,7 @@ assert repr('\\\n\t\'\r\b\x48') == r"'\\\n\t\'\r\bH'"
a = ''
b = 'test'
c ='test'
assert a == str()
assert len(a) == 0
assert len(b) == 4
assert b == c