Skip to content

Commit bc51076

Browse files
authored
Merge pull request #29415 from charris/backport-29369
BUG: fix casting issue in center, ljust, rjust, and zfill (#29369)
2 parents db022e2 + 45a361c commit bc51076

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

numpy/_core/src/umath/stringdtype_ufuncs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ center_ljust_rjust_strided_loop(PyArrayMethod_Context *context,
17361736
size_t num_codepoints = inbuf.num_codepoints();
17371737
npy_intp width = (npy_intp)*(npy_int64*)in2;
17381738

1739-
if (num_codepoints > (size_t)width) {
1739+
if ((npy_intp)num_codepoints > width) {
17401740
width = num_codepoints;
17411741
}
17421742

@@ -1866,8 +1866,8 @@ zfill_strided_loop(PyArrayMethod_Context *context,
18661866
{
18671867
Buffer<ENCODING::UTF8> inbuf((char *)is.buf, is.size);
18681868
size_t in_codepoints = inbuf.num_codepoints();
1869-
size_t width = (size_t)*(npy_int64 *)in2;
1870-
if (in_codepoints > width) {
1869+
npy_intp width = (npy_intp)*(npy_int64*)in2;
1870+
if ((npy_intp)in_codepoints > width) {
18711871
width = in_codepoints;
18721872
}
18731873
// number of leading one-byte characters plus the size of the

numpy/_core/tests/test_strings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ def test_rjust_raises_multiple_character_fill(self, dt):
846846
('abc', 6, ' ', ' abc '),
847847
('abc', 3, ' ', 'abc'),
848848
('abc', 2, ' ', 'abc'),
849+
('abc', -2, ' ', 'abc'),
849850
('abc', 10, '*', '***abc****'),
850851
])
851852
def test_center(self, buf, width, fillchar, res, dt):
@@ -859,6 +860,7 @@ def test_center(self, buf, width, fillchar, res, dt):
859860
('abc', 6, ' ', 'abc '),
860861
('abc', 3, ' ', 'abc'),
861862
('abc', 2, ' ', 'abc'),
863+
('abc', -2, ' ', 'abc'),
862864
('abc', 10, '*', 'abc*******'),
863865
])
864866
def test_ljust(self, buf, width, fillchar, res, dt):
@@ -872,6 +874,7 @@ def test_ljust(self, buf, width, fillchar, res, dt):
872874
('abc', 6, ' ', ' abc'),
873875
('abc', 3, ' ', 'abc'),
874876
('abc', 2, ' ', 'abc'),
877+
('abc', -2, ' ', 'abc'),
875878
('abc', 10, '*', '*******abc'),
876879
])
877880
def test_rjust(self, buf, width, fillchar, res, dt):
@@ -893,6 +896,7 @@ def test_rjust(self, buf, width, fillchar, res, dt):
893896
('-0123', 5, '-0123'),
894897
('000', 3, '000'),
895898
('34', 1, '34'),
899+
('34', -1, '34'),
896900
('0034', 4, '0034'),
897901
])
898902
def test_zfill(self, buf, width, res, dt):

0 commit comments

Comments
 (0)