@@ -644,6 +644,30 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
644644}
645645
646646
647+ static PyObject *
648+ test_import (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (args ))
649+ {
650+ PyObject * mod = PyImport_ImportModule ("sys" );
651+ if (mod == NULL ) {
652+ return NULL ;
653+ }
654+ Py_ssize_t refcnt = Py_REFCNT (mod );
655+
656+ // test PyImport_AddModuleRef()
657+ PyObject * mod2 = PyImport_AddModuleRef ("sys" );
658+ if (mod2 == NULL ) {
659+ return NULL ;
660+ }
661+ assert (PyModule_Check (mod2 ));
662+ assert (Py_REFCNT (mod ) == (refcnt + 1 ));
663+
664+ Py_DECREF (mod2 );
665+ Py_DECREF (mod );
666+
667+ Py_RETURN_NONE ;
668+ }
669+
670+
647671static struct PyMethodDef methods [] = {
648672 {"test_object" , test_object , METH_NOARGS , _Py_NULL },
649673 {"test_py_is" , test_py_is , METH_NOARGS , _Py_NULL },
@@ -662,6 +686,7 @@ static struct PyMethodDef methods[] = {
662686 {"test_code" , test_code , METH_NOARGS , _Py_NULL },
663687#endif
664688 {"test_api_casts" , test_api_casts , METH_NOARGS , _Py_NULL },
689+ {"test_import" , test_import , METH_NOARGS , _Py_NULL },
665690 {_Py_NULL , _Py_NULL , 0 , _Py_NULL }
666691};
667692
0 commit comments