PyModule_AddObject steals a reference. Fix all of the double calls to it by adding a Py_INCREF. Also, some other assorted hacks which probably shouldn't be necessary but are at the moment.
diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h
index b5e6b65..346e04d 100644
--- a/src/crypto/crypto.h
+++ b/src/crypto/crypto.h
@@ -100,7 +100,7 @@
PyObject *crypto_dict, *crypto_api_object; \
crypto_dict = PyModule_GetDict(crypto_module); \
crypto_api_object = PyDict_GetItemString(crypto_dict, "_C_API"); \
- if (PyCObject_Check(crypto_api_object)) { \
+ if (crypto_api_object && PyCObject_Check(crypto_api_object)) { \
crypto_API = (void **)PyCObject_AsVoidPtr(crypto_api_object); \
} \
} \
diff --git a/src/crypto/netscape_spki.c b/src/crypto/netscape_spki.c
index 4fa9d8d..425ea22 100644
--- a/src/crypto/netscape_spki.c
+++ b/src/crypto/netscape_spki.c
@@ -295,6 +295,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference
+ */
+ Py_INCREF((PyObject *)&crypto_NetscapeSPKI_Type);
if (PyModule_AddObject(module, "NetscapeSPKI", (PyObject *)&crypto_NetscapeSPKI_Type) != 0) {
return 0;
}
diff --git a/src/crypto/pkcs12.c b/src/crypto/pkcs12.c
index 2302242..1a65e8b 100644
--- a/src/crypto/pkcs12.c
+++ b/src/crypto/pkcs12.c
@@ -565,6 +565,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&crypto_PKCS12_Type);
if (PyModule_AddObject(module, "PKCS12", (PyObject *)&crypto_PKCS12_Type) != 0) {
return 0;
}
diff --git a/src/crypto/pkey.c b/src/crypto/pkey.c
index 583a2a1..ab8363a 100644
--- a/src/crypto/pkey.c
+++ b/src/crypto/pkey.c
@@ -266,6 +266,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&crypto_PKey_Type);
if (PyModule_AddObject(module, "PKey", (PyObject *)&crypto_PKey_Type) != 0) {
return 0;
}
diff --git a/src/crypto/x509.c b/src/crypto/x509.c
index e089d40..b230c3d 100644
--- a/src/crypto/x509.c
+++ b/src/crypto/x509.c
@@ -852,6 +852,11 @@
return 0;
}
+ /* PyModule_AddObject steals a reference. We need crypto_X509_Type to
+ * still be ours at least until the second PyModule_AddObject call
+ * below.
+ */
+ Py_INCREF((PyObject *)&crypto_X509_Type);
if (PyModule_AddObject(module, "X509", (PyObject *)&crypto_X509_Type) != 0) {
return 0;
}
diff --git a/src/crypto/x509ext.c b/src/crypto/x509ext.c
index 90ef543..426741c 100644
--- a/src/crypto/x509ext.c
+++ b/src/crypto/x509ext.c
@@ -308,6 +308,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&crypto_X509Extension_Type);
if (PyModule_AddObject(module, "X509Extension",
(PyObject *)&crypto_X509Extension_Type) != 0) {
return 0;
diff --git a/src/crypto/x509name.c b/src/crypto/x509name.c
index 39fdcf8..a203bbd 100644
--- a/src/crypto/x509name.c
+++ b/src/crypto/x509name.c
@@ -475,6 +475,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&crypto_X509Name_Type);
if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
return 0;
}
diff --git a/src/crypto/x509req.c b/src/crypto/x509req.c
index 07bd44b..c56692b 100644
--- a/src/crypto/x509req.c
+++ b/src/crypto/x509req.c
@@ -425,6 +425,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&crypto_X509Req_Type);
if (PyModule_AddObject(module, "X509Req", (PyObject *)&crypto_X509Req_Type) != 0) {
return 0;
}
diff --git a/src/ssl/connection.c b/src/ssl/connection.c
index 6ee484d..ec15a3c 100755
--- a/src/ssl/connection.c
+++ b/src/ssl/connection.c
@@ -1413,6 +1413,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&ssl_Connection_Type);
if (PyModule_AddObject(module, "Connection", (PyObject *)&ssl_Connection_Type) != 0) {
return 0;
}
diff --git a/src/ssl/context.c b/src/ssl/context.c
index ebaf164..ce23f95 100644
--- a/src/ssl/context.c
+++ b/src/ssl/context.c
@@ -1299,6 +1299,9 @@
return 0;
}
+ /* PyModule_AddObject steals a reference.
+ */
+ Py_INCREF((PyObject *)&ssl_Context_Type);
if (PyModule_AddObject(module, "Context", (PyObject *)&ssl_Context_Type) < 0) {
return 0;
}
diff --git a/src/ssl/ssl.c b/src/ssl/ssl.c
index 94d4844..1f12d67 100644
--- a/src/ssl/ssl.c
+++ b/src/ssl/ssl.c
@@ -92,8 +92,12 @@
} while (0)
ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL);
- if (ssl_Error == NULL)
+ if (ssl_Error == NULL) {
goto error;
+ }
+ /* XXX PyPy hack */
+ Py_INCREF(ssl_Error);
+
if (PyModule_AddObject(module, "Error", ssl_Error) != 0)
goto error;