[Python-checkins] cpython (3.2): In the _hashlib module, only initialize the static data for OpenSSL's

gregory.p.smith python-checkins at python.org
Sat Feb 2 02:10:28 CET 2013


http://hg.python.org/cpython/rev/b6792067aafa
changeset:   81898:b6792067aafa
branch:      3.2
parent:      81894:11047da3e5f6
user:        Gregory P. Smith <greg at krypto.org>
date:        Fri Feb 01 17:05:29 2013 -0800
summary:
  In the _hashlib module, only initialize the static data for OpenSSL's
constructors once, to avoid memory leaks when finalizing and re-initializing
the Python interpreter.

files:
  Modules/_hashopenssl.c |  15 +++++++++------
  1 files changed, 9 insertions(+), 6 deletions(-)


diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
--- a/Modules/_hashopenssl.c
+++ b/Modules/_hashopenssl.c
@@ -70,7 +70,7 @@
 
 
 #define DEFINE_CONSTS_FOR_NEW(Name)  \
-    static PyObject *CONST_ ## Name ## _name_obj; \
+    static PyObject *CONST_ ## Name ## _name_obj = NULL; \
     static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
     static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
 
@@ -587,12 +587,15 @@
                   " hash object; optionally initialized with a string") \
     }
 
-/* used in the init function to setup a constructor */
+/* used in the init function to setup a constructor: initialize OpenSSL
+   constructor constants if they haven't been initialized already.  */
 #define INIT_CONSTRUCTOR_CONSTANTS(NAME)  do { \
-    CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
-    if (EVP_get_digestbyname(#NAME)) { \
-        CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
-        EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
+    if (CONST_ ## NAME ## _name_obj == NULL) { \
+        CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
+        if (EVP_get_digestbyname(#NAME)) { \
+            CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
+            EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
+        } \
     } \
 } while (0);
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list