@@ -41,6 +41,52 @@ QuadPrecision_raw_new(QuadBackendType backend)
4141 return new ;
4242}
4343
44+ static QuadPrecisionObject *
45+ quad_from_py_int (PyObject * py_int , QuadBackendType backend , QuadPrecisionObject * self_to_cleanup )
46+ {
47+ int overflow = 0 ;
48+ long long lval = PyLong_AsLongLongAndOverflow (py_int , & overflow );
49+
50+ if (overflow != 0 ) {
51+ // Integer is too large, convert to string and recursively call QuadPrecision_from_object
52+ PyObject * str_obj = PyObject_Str (py_int );
53+ if (str_obj == NULL ) {
54+ if (self_to_cleanup ) {
55+ Py_DECREF (self_to_cleanup );
56+ }
57+ return NULL ;
58+ }
59+
60+ QuadPrecisionObject * result = QuadPrecision_from_object (str_obj , backend );
61+ Py_DECREF (str_obj );
62+ if (self_to_cleanup ) {
63+ Py_DECREF (self_to_cleanup ); // discard the default one
64+ }
65+ return result ;
66+ }
67+ else if (lval == -1 && PyErr_Occurred ()) {
68+ if (self_to_cleanup ) {
69+ Py_DECREF (self_to_cleanup );
70+ }
71+ return NULL ;
72+ }
73+
74+ // No overflow, use the integer value directly
75+ QuadPrecisionObject * self = self_to_cleanup ? self_to_cleanup : QuadPrecision_raw_new (backend );
76+ if (!self ) {
77+ return NULL ;
78+ }
79+
80+ if (backend == BACKEND_SLEEF ) {
81+ self -> value .sleef_value = Sleef_cast_from_int64q1 (lval );
82+ }
83+ else {
84+ self -> value .longdouble_value = (long double )lval ;
85+ }
86+ return self ;
87+
88+ }
89+
4490QuadPrecisionObject *
4591QuadPrecision_from_object (PyObject * value , QuadBackendType backend )
4692{
@@ -77,40 +123,9 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
77123 return NULL ;
78124 }
79125
80- int overflow = 0 ;
81- long long lval = PyLong_AsLongLongAndOverflow (py_int , & overflow );
82-
83- if (overflow != 0 )
84- {
85- // Integer is too large, convert to string and recursively call this function
86- PyObject * str_obj = PyObject_Str (py_int );
87- Py_DECREF (py_int );
88- if (str_obj == NULL ) {
89- Py_DECREF (self );
90- return NULL ;
91- }
92-
93- // Recursively convert from string
94- QuadPrecisionObject * result = QuadPrecision_from_object (str_obj , backend );
95- Py_DECREF (str_obj );
96- Py_DECREF (self ); // discard the default one
97- return result ;
98- }
99- else if (lval == -1 && PyErr_Occurred ()) {
100- Py_DECREF (py_int );
101- Py_DECREF (self );
102- return NULL ;
103- }
104- else {
105- Py_DECREF (py_int );
106- if (backend == BACKEND_SLEEF ) {
107- self -> value .sleef_value = Sleef_cast_from_int64q1 (lval );
108- }
109- else {
110- self -> value .longdouble_value = (long double )lval ;
111- }
112- }
113- return self ;
126+ QuadPrecisionObject * result = quad_from_py_int (py_int , backend , self );
127+ Py_DECREF (py_int );
128+ return result ;
114129 }
115130 // Try as boolean
116131 else if (PyArray_IsScalar (value , Bool )) {
@@ -193,36 +208,7 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
193208 }
194209 }
195210 else if (PyLong_Check (value )) {
196- int overflow = 0 ;
197- long long val = PyLong_AsLongLongAndOverflow (value , & overflow );
198-
199- if (overflow != 0 ) {
200- // Integer is too large, convert to string and recursively call this function
201- PyObject * str_obj = PyObject_Str (value );
202- if (str_obj == NULL ) {
203- Py_DECREF (self );
204- return NULL ;
205- }
206-
207- // Recursively convert from string
208- QuadPrecisionObject * result = QuadPrecision_from_object (str_obj , backend );
209- Py_DECREF (str_obj );
210- Py_DECREF (self ); // discard the default one
211- return result ;
212- }
213- else if (val == -1 && PyErr_Occurred ()) {
214- Py_DECREF (self );
215- return NULL ;
216- }
217- else {
218- // No overflow, use the integer value directly
219- if (backend == BACKEND_SLEEF ) {
220- self -> value .sleef_value = Sleef_cast_from_int64q1 (val );
221- }
222- else {
223- self -> value .longdouble_value = (long double )val ;
224- }
225- }
211+ return quad_from_py_int (value , backend , self );
226212 }
227213 else if (Py_TYPE (value ) == & QuadPrecision_Type ) {
228214 Py_DECREF (self ); // discard the default one
0 commit comments