File tree 3 files changed +30
-14
lines changed
3 files changed +30
-14
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,19 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
33
33
return Py_NewRef (obj );
34
34
}
35
35
36
+ static inline int _PyWeakref_IS_DEAD (PyObject * ref_obj ) {
37
+ assert (PyWeakref_Check (ref_obj ));
38
+ PyWeakReference * ref = _Py_CAST (PyWeakReference * , ref_obj );
39
+ PyObject * obj = ref -> wr_object ;
40
+ if (obj == Py_None ) {
41
+ // clear_weakref() was called
42
+ return 1 ;
43
+ }
44
+
45
+ // See _PyWeakref_GET_REF() for the rationale of this test
46
+ return (Py_REFCNT (obj ) == 0 );
47
+ }
48
+
36
49
#ifdef __cplusplus
37
50
}
38
51
#endif
Original file line number Diff line number Diff line change 21
21
* 3. This notice may not be removed or altered from any source distribution.
22
22
*/
23
23
24
+ #ifndef Py_BUILD_CORE_BUILTIN
25
+ # define Py_BUILD_CORE_MODULE 1
26
+ #endif
27
+
24
28
#include "module.h"
25
29
#include "structmember.h" // PyMemberDef
26
30
#include "connection.h"
29
33
#include "blob.h"
30
34
#include "prepare_protocol.h"
31
35
#include "util.h"
36
+ #include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
32
37
33
38
#include <stdbool.h>
34
39
@@ -969,29 +974,26 @@ final_callback(sqlite3_context *context)
969
974
970
975
static void _pysqlite_drop_unused_cursor_references (pysqlite_Connection * self )
971
976
{
972
- PyObject * new_list ;
973
- PyObject * weakref ;
974
- int i ;
975
-
976
977
/* we only need to do this once in a while */
977
978
if (self -> created_cursors ++ < 200 ) {
978
979
return ;
979
980
}
980
981
981
982
self -> created_cursors = 0 ;
982
983
983
- new_list = PyList_New (0 );
984
+ PyObject * new_list = PyList_New (0 );
984
985
if (!new_list ) {
985
986
return ;
986
987
}
987
988
988
- for (i = 0 ; i < PyList_Size (self -> cursors ); i ++ ) {
989
- weakref = PyList_GetItem (self -> cursors , i );
990
- if (PyWeakref_GetObject (weakref ) != Py_None ) {
991
- if (PyList_Append (new_list , weakref ) != 0 ) {
992
- Py_DECREF (new_list );
993
- return ;
994
- }
989
+ for (Py_ssize_t i = 0 ; i < PyList_Size (self -> cursors ); i ++ ) {
990
+ PyObject * weakref = PyList_GetItem (self -> cursors , i );
991
+ if (_PyWeakref_IS_DEAD (weakref )) {
992
+ continue ;
993
+ }
994
+ if (PyList_Append (new_list , weakref ) != 0 ) {
995
+ Py_DECREF (new_list );
996
+ return ;
995
997
}
996
998
}
997
999
Original file line number Diff line number Diff line change 1
1
#include "Python.h"
2
- #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
2
+ #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
3
+ #include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
3
4
4
5
5
6
#define GET_WEAKREFS_LISTPTR (o ) \
@@ -43,7 +44,7 @@ is_dead_weakref(PyObject *value)
43
44
PyErr_SetString (PyExc_TypeError , "not a weakref" );
44
45
return -1 ;
45
46
}
46
- return PyWeakref_GET_OBJECT (value ) == Py_None ;
47
+ return _PyWeakref_IS_DEAD (value );
47
48
}
48
49
49
50
/*[clinic input]
You can’t perform that action at this time.
0 commit comments