Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixes and more tests
  • Loading branch information
JukkaL committed Apr 23, 2026
commit 1d068f761e05f55135520dcbeb6ccfb3bb1444fa
4 changes: 4 additions & 0 deletions mypyc/lib-rt/vecs/vec_nested.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ VecNested VecNested_ConvertFromNested(VecNestedBufItem item) {
}

VecNested VecNested_New(Py_ssize_t size, Py_ssize_t cap, size_t item_type, size_t depth) {
if (cap < 0) {
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
return vec_error();
}
if (cap < size)
cap = size;
VecNested vec = vec_alloc(cap, item_type, depth);
Expand Down
4 changes: 4 additions & 0 deletions mypyc/lib-rt/vecs/vec_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ VecT VecT_ConvertFromNested(VecNestedBufItem item) {
}

VecT VecT_New(Py_ssize_t size, Py_ssize_t cap, size_t item_type) {
if (cap < 0) {
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
return vec_error();
}
if (cap < size)
cap = size;
VecT vec = vec_alloc(cap, item_type);
Expand Down
6 changes: 5 additions & 1 deletion mypyc/lib-rt/vecs/vec_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ VEC FUNC(ConvertFromNested)(VecNestedBufItem item) {
}

VEC FUNC(New)(Py_ssize_t size, Py_ssize_t cap) {
if (cap < 0) {
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
return vec_error();
}
if (cap < size)
size = cap;
cap = size;
VEC vec = vec_alloc(cap);
if (VEC_IS_ERROR(vec))
return vec;
Expand Down
1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-vec-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -872,4 +872,3 @@ L0:
r4 = r3 + 8
keep_alive r0
return r0

1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-vec-t.test
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,3 @@ L0:
r8 = r7 + 8
keep_alive r4
return r4

13 changes: 13 additions & 0 deletions mypyc/test-data/run-vecs-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,16 @@ def test_cap_buffer_reuse() -> None:
v = append(v, 3)
v[0] = 99
assert old[0] == 99 # shared buffer

def test_cap_below_initializer_length() -> None:
v = vec[i64]([10, 20, 30], capacity=1)
assert len(v) == 3
assert v[0] == 10
assert v[1] == 20
assert v[2] == 30

def test_cap_negative() -> None:
with assertRaises(ValueError):
vec[i64](capacity=-1)
with assertRaises(ValueError):
vec[i64]([1], capacity=-1)
13 changes: 13 additions & 0 deletions mypyc/test-data/run-vecs-nested.test
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,16 @@ def test_cap_buffer_reuse() -> None:
v = append(v, vec[i64]([3]))
v[0] = vec[i64]([99])
assert old[0] == vec[i64]([99]) # shared buffer

def test_cap_below_initializer_length() -> None:
v = vec[vec[i64]]([vec[i64]([10]), vec[i64]([20]), vec[i64]([30])], capacity=1)
assert len(v) == 3
assert v[0] == vec[i64]([10])
assert v[1] == vec[i64]([20])
assert v[2] == vec[i64]([30])

def test_cap_negative() -> None:
with assertRaises(ValueError):
vec[vec[i64]](capacity=-1)
with assertRaises(ValueError):
vec[vec[i64]]([vec[i64]([1])], capacity=-1)
13 changes: 13 additions & 0 deletions mypyc/test-data/run-vecs-t.test
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,16 @@ def test_cap_buffer_reuse() -> None:
v = append(v, 'c')
v[0] = 'z'
assert old[0] == 'z' # shared buffer

def test_cap_below_initializer_length() -> None:
v = vec[str](['a', 'b', 'c'], capacity=1)
assert len(v) == 3
assert v[0] == 'a'
assert v[1] == 'b'
assert v[2] == 'c'

def test_cap_negative() -> None:
with assertRaises(ValueError):
vec[str](capacity=-1)
with assertRaises(ValueError):
vec[str](['x'], capacity=-1)