Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 3 extra_tests/snippets/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ def __bytes__(self):
assert id(a) == id(a)
assert id(a) != id(a * -1)
assert id(a) != id(a * 0)
assert id(a) == id(a * 1) # only case when `id` stays the same
assert id(a) == id(a * 1) # only cases
assert id(a) == id(1 * a) # when `id` stays the same
assert id(a) != id(a * 2)


Expand Down
29 changes: 27 additions & 2 deletions 29 extra_tests/snippets/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def test_removeprefix():
assert s_uc.removeprefix('😱') == s_ref_uc[1:]
assert s_uc.removeprefix('😱fo') == s_ref_uc[3:]
assert s_uc.removeprefix('😱foo') == s_ref_uc[4:]

assert s_uc.removeprefix('🖖') == s_ref_uc
assert s_uc.removeprefix('foo') == s_ref_uc
assert s_uc.removeprefix(' ') == s_ref_uc
Expand Down Expand Up @@ -676,7 +676,7 @@ def test_removesuffix():
assert s_uc.removesuffix('🖖') == s_ref_uc[:-1]
assert s_uc.removesuffix('oo🖖') == s_ref_uc[:-3]
assert s_uc.removesuffix('foo🖖') == s_ref_uc[:-4]

assert s_uc.removesuffix('😱') == s_ref_uc
assert s_uc.removesuffix('foo') == s_ref_uc
assert s_uc.removesuffix(' ') == s_ref_uc
Expand All @@ -702,3 +702,28 @@ def test_removesuffix_types():
skip_if_unsupported(3,9,test_removeprefix_types)
skip_if_unsupported(3,9,test_removesuffix)
skip_if_unsupported(3,9,test_removesuffix_types)


# Regression to
# https://github.com/RustPython/RustPython/issues/2840

a = 'abc123()'

assert id(a) == id(a)
assert id(a) != id(a * -1)
assert id(a) != id(a * 0)
assert id(a) == id(a * 1) # only cases
assert id(a) == id(1 * a) # when `id` stays the same
assert id(a) != id(a * 2)


class MyString(str):
pass

b = MyString('0123abc*&')
assert id(b) == id(b)
assert id(b) != id(b * -1)
assert id(b) != id(b * 0)
assert id(b) != id(b * 1)
assert id(b) != id(1 * b)
assert id(b) != id(b * 2)
11 changes: 9 additions & 2 deletions 11 vm/src/builtins/pystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,15 @@ impl PyStr {

#[pymethod(name = "__rmul__")]
#[pymethod(magic)]
fn mul(&self, value: isize) -> String {
self.value.repeat(value.to_usize().unwrap_or(0))
fn mul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyRef<Self> {
if value == 1 && zelf.class().is(&vm.ctx.types.str_type) {
// Special case: when some `str` is multiplied by `1`,
// nothing really happens, we need to return an object itself
// with the same `id()` to be compatible with CPython.
// This only works for `str` itself, not its subclasses.
return zelf;
}
Self::from(zelf.value.repeat(value.to_usize().unwrap_or(0))).into_ref(vm)
}

#[pymethod(magic)]
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.