@@ -1717,7 +1717,9 @@ long_to_decimal_string_internal(PyObject *aa,
1717
1717
digit * pout , * pin , rem , tenpow ;
1718
1718
int negative ;
1719
1719
int d ;
1720
- enum PyUnicode_Kind kind ;
1720
+
1721
+ // writer or bytes_writer can be used, but not both at the same time.
1722
+ assert (writer == NULL || bytes_writer == NULL );
1721
1723
1722
1724
a = (PyLongObject * )aa ;
1723
1725
if (a == NULL || !PyLong_Check (a )) {
@@ -1819,7 +1821,6 @@ long_to_decimal_string_internal(PyObject *aa,
1819
1821
Py_DECREF (scratch );
1820
1822
return -1 ;
1821
1823
}
1822
- kind = writer -> kind ;
1823
1824
}
1824
1825
else if (bytes_writer ) {
1825
1826
* bytes_str = _PyBytesWriter_Prepare (bytes_writer , * bytes_str , strlen );
@@ -1834,7 +1835,6 @@ long_to_decimal_string_internal(PyObject *aa,
1834
1835
Py_DECREF (scratch );
1835
1836
return -1 ;
1836
1837
}
1837
- kind = PyUnicode_KIND (str );
1838
1838
}
1839
1839
1840
1840
#define WRITE_DIGITS (p ) \
@@ -1882,19 +1882,23 @@ long_to_decimal_string_internal(PyObject *aa,
1882
1882
WRITE_DIGITS (p );
1883
1883
assert (p == * bytes_str );
1884
1884
}
1885
- else if (kind == PyUnicode_1BYTE_KIND ) {
1886
- Py_UCS1 * p ;
1887
- WRITE_UNICODE_DIGITS (Py_UCS1 );
1888
- }
1889
- else if (kind == PyUnicode_2BYTE_KIND ) {
1890
- Py_UCS2 * p ;
1891
- WRITE_UNICODE_DIGITS (Py_UCS2 );
1892
- }
1893
1885
else {
1894
- Py_UCS4 * p ;
1895
- assert (kind == PyUnicode_4BYTE_KIND );
1896
- WRITE_UNICODE_DIGITS (Py_UCS4 );
1886
+ enum PyUnicode_Kind kind = writer ? writer -> kind : PyUnicode_KIND (str );
1887
+ if (kind == PyUnicode_1BYTE_KIND ) {
1888
+ Py_UCS1 * p ;
1889
+ WRITE_UNICODE_DIGITS (Py_UCS1 );
1890
+ }
1891
+ else if (kind == PyUnicode_2BYTE_KIND ) {
1892
+ Py_UCS2 * p ;
1893
+ WRITE_UNICODE_DIGITS (Py_UCS2 );
1894
+ }
1895
+ else {
1896
+ assert (kind == PyUnicode_4BYTE_KIND );
1897
+ Py_UCS4 * p ;
1898
+ WRITE_UNICODE_DIGITS (Py_UCS4 );
1899
+ }
1897
1900
}
1901
+
1898
1902
#undef WRITE_DIGITS
1899
1903
#undef WRITE_UNICODE_DIGITS
1900
1904
@@ -1935,11 +1939,12 @@ long_format_binary(PyObject *aa, int base, int alternate,
1935
1939
PyObject * v = NULL ;
1936
1940
Py_ssize_t sz ;
1937
1941
Py_ssize_t size_a ;
1938
- enum PyUnicode_Kind kind ;
1939
1942
int negative ;
1940
1943
int bits ;
1941
1944
1942
1945
assert (base == 2 || base == 8 || base == 16 );
1946
+ // writer or bytes_writer can be used, but not both at the same time.
1947
+ assert (writer == NULL || bytes_writer == NULL );
1943
1948
if (a == NULL || !PyLong_Check (a )) {
1944
1949
PyErr_BadInternalCall ();
1945
1950
return -1 ;
@@ -1987,7 +1992,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
1987
1992
if (writer ) {
1988
1993
if (_PyUnicodeWriter_Prepare (writer , sz , 'x' ) == -1 )
1989
1994
return -1 ;
1990
- kind = writer -> kind ;
1991
1995
}
1992
1996
else if (bytes_writer ) {
1993
1997
* bytes_str = _PyBytesWriter_Prepare (bytes_writer , * bytes_str , sz );
@@ -1998,7 +2002,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
1998
2002
v = PyUnicode_New (sz , 'x' );
1999
2003
if (v == NULL )
2000
2004
return -1 ;
2001
- kind = PyUnicode_KIND (v );
2002
2005
}
2003
2006
2004
2007
#define WRITE_DIGITS (p ) \
@@ -2059,19 +2062,23 @@ long_format_binary(PyObject *aa, int base, int alternate,
2059
2062
WRITE_DIGITS (p );
2060
2063
assert (p == * bytes_str );
2061
2064
}
2062
- else if (kind == PyUnicode_1BYTE_KIND ) {
2063
- Py_UCS1 * p ;
2064
- WRITE_UNICODE_DIGITS (Py_UCS1 );
2065
- }
2066
- else if (kind == PyUnicode_2BYTE_KIND ) {
2067
- Py_UCS2 * p ;
2068
- WRITE_UNICODE_DIGITS (Py_UCS2 );
2069
- }
2070
2065
else {
2071
- Py_UCS4 * p ;
2072
- assert (kind == PyUnicode_4BYTE_KIND );
2073
- WRITE_UNICODE_DIGITS (Py_UCS4 );
2066
+ enum PyUnicode_Kind kind = writer ? writer -> kind : PyUnicode_KIND (v );
2067
+ if (kind == PyUnicode_1BYTE_KIND ) {
2068
+ Py_UCS1 * p ;
2069
+ WRITE_UNICODE_DIGITS (Py_UCS1 );
2070
+ }
2071
+ else if (kind == PyUnicode_2BYTE_KIND ) {
2072
+ Py_UCS2 * p ;
2073
+ WRITE_UNICODE_DIGITS (Py_UCS2 );
2074
+ }
2075
+ else {
2076
+ assert (kind == PyUnicode_4BYTE_KIND );
2077
+ Py_UCS4 * p ;
2078
+ WRITE_UNICODE_DIGITS (Py_UCS4 );
2079
+ }
2074
2080
}
2081
+
2075
2082
#undef WRITE_DIGITS
2076
2083
#undef WRITE_UNICODE_DIGITS
2077
2084
0 commit comments