Skip to content

Commit 1d068f7

Browse files
committed
Fixes and more tests
1 parent 55d7086 commit 1d068f7

8 files changed

Lines changed: 52 additions & 3 deletions

File tree

mypyc/lib-rt/vecs/vec_nested.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ VecNested VecNested_ConvertFromNested(VecNestedBufItem item) {
6565
}
6666

6767
VecNested VecNested_New(Py_ssize_t size, Py_ssize_t cap, size_t item_type, size_t depth) {
68+
if (cap < 0) {
69+
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
70+
return vec_error();
71+
}
6872
if (cap < size)
6973
cap = size;
7074
VecNested vec = vec_alloc(cap, item_type, depth);

mypyc/lib-rt/vecs/vec_t.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ VecT VecT_ConvertFromNested(VecNestedBufItem item) {
8181
}
8282

8383
VecT VecT_New(Py_ssize_t size, Py_ssize_t cap, size_t item_type) {
84+
if (cap < 0) {
85+
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
86+
return vec_error();
87+
}
8488
if (cap < size)
8589
cap = size;
8690
VecT vec = vec_alloc(cap, item_type);

mypyc/lib-rt/vecs/vec_template.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ VEC FUNC(ConvertFromNested)(VecNestedBufItem item) {
8484
}
8585

8686
VEC FUNC(New)(Py_ssize_t size, Py_ssize_t cap) {
87+
if (cap < 0) {
88+
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
89+
return vec_error();
90+
}
8791
if (cap < size)
88-
size = cap;
92+
cap = size;
8993
VEC vec = vec_alloc(cap);
9094
if (VEC_IS_ERROR(vec))
9195
return vec;

mypyc/test-data/irbuild-vec-i64.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,3 @@ L0:
872872
r4 = r3 + 8
873873
keep_alive r0
874874
return r0
875-

mypyc/test-data/irbuild-vec-t.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,3 @@ L0:
430430
r8 = r7 + 8
431431
keep_alive r4
432432
return r4
433-

mypyc/test-data/run-vecs-i64.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,16 @@ def test_cap_buffer_reuse() -> None:
512512
v = append(v, 3)
513513
v[0] = 99
514514
assert old[0] == 99 # shared buffer
515+
516+
def test_cap_below_initializer_length() -> None:
517+
v = vec[i64]([10, 20, 30], capacity=1)
518+
assert len(v) == 3
519+
assert v[0] == 10
520+
assert v[1] == 20
521+
assert v[2] == 30
522+
523+
def test_cap_negative() -> None:
524+
with assertRaises(ValueError):
525+
vec[i64](capacity=-1)
526+
with assertRaises(ValueError):
527+
vec[i64]([1], capacity=-1)

mypyc/test-data/run-vecs-nested.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,16 @@ def test_cap_buffer_reuse() -> None:
542542
v = append(v, vec[i64]([3]))
543543
v[0] = vec[i64]([99])
544544
assert old[0] == vec[i64]([99]) # shared buffer
545+
546+
def test_cap_below_initializer_length() -> None:
547+
v = vec[vec[i64]]([vec[i64]([10]), vec[i64]([20]), vec[i64]([30])], capacity=1)
548+
assert len(v) == 3
549+
assert v[0] == vec[i64]([10])
550+
assert v[1] == vec[i64]([20])
551+
assert v[2] == vec[i64]([30])
552+
553+
def test_cap_negative() -> None:
554+
with assertRaises(ValueError):
555+
vec[vec[i64]](capacity=-1)
556+
with assertRaises(ValueError):
557+
vec[vec[i64]]([vec[i64]([1])], capacity=-1)

mypyc/test-data/run-vecs-t.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,16 @@ def test_cap_buffer_reuse() -> None:
472472
v = append(v, 'c')
473473
v[0] = 'z'
474474
assert old[0] == 'z' # shared buffer
475+
476+
def test_cap_below_initializer_length() -> None:
477+
v = vec[str](['a', 'b', 'c'], capacity=1)
478+
assert len(v) == 3
479+
assert v[0] == 'a'
480+
assert v[1] == 'b'
481+
assert v[2] == 'c'
482+
483+
def test_cap_negative() -> None:
484+
with assertRaises(ValueError):
485+
vec[str](capacity=-1)
486+
with assertRaises(ValueError):
487+
vec[str](['x'], capacity=-1)

0 commit comments

Comments
 (0)