@@ -9698,11 +9698,11 @@ split(PyObject *self,
9698
9698
PyObject * out ;
9699
9699
len1 = PyUnicode_GET_LENGTH (self );
9700
9700
kind1 = PyUnicode_KIND (self );
9701
- if (maxcount < 0 ) {
9702
- maxcount = len1 ;
9703
- }
9704
9701
9705
- if (substring == NULL )
9702
+ if (substring == NULL ) {
9703
+ if (maxcount < 0 ) {
9704
+ maxcount = (len1 - 1 ) / 2 + 1 ;
9705
+ }
9706
9706
switch (kind1 ) {
9707
9707
case PyUnicode_1BYTE_KIND :
9708
9708
if (PyUnicode_IS_ASCII (self ))
@@ -9728,9 +9728,16 @@ split(PyObject *self,
9728
9728
default :
9729
9729
Py_UNREACHABLE ();
9730
9730
}
9731
+ }
9731
9732
9732
9733
kind2 = PyUnicode_KIND (substring );
9733
9734
len2 = PyUnicode_GET_LENGTH (substring );
9735
+ if (maxcount < 0 ) {
9736
+ // if len2 == 0, it will raise ValueError.
9737
+ maxcount = len2 == 0 ? 0 : (len1 / len2 ) + 1 ;
9738
+ // handle expected overflow case: (Py_SSIZE_T_MAX / 1) + 1
9739
+ maxcount = maxcount < 0 ? len1 : maxcount ;
9740
+ }
9734
9741
if (kind1 < kind2 || len1 < len2 ) {
9735
9742
out = PyList_New (1 );
9736
9743
if (out == NULL )
@@ -9785,11 +9792,11 @@ rsplit(PyObject *self,
9785
9792
9786
9793
len1 = PyUnicode_GET_LENGTH (self );
9787
9794
kind1 = PyUnicode_KIND (self );
9788
- if (maxcount < 0 ) {
9789
- maxcount = len1 ;
9790
- }
9791
9795
9792
- if (substring == NULL )
9796
+ if (substring == NULL ) {
9797
+ if (maxcount < 0 ) {
9798
+ maxcount = (len1 - 1 ) / 2 + 1 ;
9799
+ }
9793
9800
switch (kind1 ) {
9794
9801
case PyUnicode_1BYTE_KIND :
9795
9802
if (PyUnicode_IS_ASCII (self ))
@@ -9815,9 +9822,15 @@ rsplit(PyObject *self,
9815
9822
default :
9816
9823
Py_UNREACHABLE ();
9817
9824
}
9818
-
9825
+ }
9819
9826
kind2 = PyUnicode_KIND (substring );
9820
9827
len2 = PyUnicode_GET_LENGTH (substring );
9828
+ if (maxcount < 0 ) {
9829
+ // if len2 == 0, it will raise ValueError.
9830
+ maxcount = len2 == 0 ? 0 : (len1 / len2 ) + 1 ;
9831
+ // handle expected overflow case: (Py_SSIZE_T_MAX / 1) + 1
9832
+ maxcount = maxcount < 0 ? len1 : maxcount ;
9833
+ }
9821
9834
if (kind1 < kind2 || len1 < len2 ) {
9822
9835
out = PyList_New (1 );
9823
9836
if (out == NULL )
0 commit comments