@@ -23,28 +23,17 @@ internal static void InitializeModuleDef()
2323 module_def = ModuleDefOffset . AllocModuleDef ( "clr" ) ;
2424 }
2525 }
26- #endif
2726
28- /// <summary>
29- /// Get a <i>New reference</i> to the builtins module.
30- /// </summary>
31- static IntPtr GetNewRefToBuiltins ( )
27+ internal static void ReleaseModuleDef ( )
3228 {
33- if ( Runtime . IsPython3 )
34- {
35- return Runtime . PyImport_ImportModule ( "builtins" ) ;
36- }
37- else
29+ if ( module_def == IntPtr . Zero )
3830 {
39- // dict is a borrowed ref, no need to decref
40- IntPtr dict = Runtime . PyImport_GetModuleDict ( ) ;
41-
42- // GetItemString is a borrowed ref; incref to get a new ref
43- IntPtr builtins = Runtime . PyDict_GetItemString ( dict , "__builtin__" ) ;
44- Runtime . XIncref ( builtins ) ;
45- return builtins ;
31+ return ;
4632 }
33+ ModuleDefOffset . FreeModuleDef ( module_def ) ;
34+ module_def = IntPtr . Zero ;
4735 }
36+ #endif
4837
4938 /// <summary>
5039 /// Initialize just the __import__ hook itself.
@@ -54,11 +43,14 @@ static void InitImport()
5443 // We replace the built-in Python __import__ with our own: first
5544 // look in CLR modules, then if we don't find any call the default
5645 // Python __import__.
57- IntPtr builtins = GetNewRefToBuiltins ( ) ;
46+ IntPtr builtins = Runtime . GetBuiltins ( ) ;
5847 py_import = Runtime . PyObject_GetAttrString ( builtins , "__import__" ) ;
48+ PythonException . ThrowIfIsNull ( py_import ) ;
49+
5950 hook = new MethodWrapper ( typeof ( ImportHook ) , "__import__" , "TernaryFunc" ) ;
60- Runtime . PyObject_SetAttrString ( builtins , "__import__" , hook . ptr ) ;
61- Runtime . XDecref ( hook . ptr ) ;
51+ int res = Runtime . PyObject_SetAttrString ( builtins , "__import__" , hook . ptr ) ;
52+ PythonException . ThrowIfIsNotZero ( res ) ;
53+
6254 Runtime . XDecref ( builtins ) ;
6355 }
6456
@@ -67,12 +59,16 @@ static void InitImport()
6759 /// </summary>
6860 static void RestoreImport ( )
6961 {
70- IntPtr builtins = GetNewRefToBuiltins ( ) ;
62+ IntPtr builtins = Runtime . GetBuiltins ( ) ;
7163
72- Runtime . PyObject_SetAttrString ( builtins , "__import__" , py_import ) ;
64+ int res = Runtime . PyObject_SetAttrString ( builtins , "__import__" , py_import ) ;
65+ PythonException . ThrowIfIsNotZero ( res ) ;
7366 Runtime . XDecref ( py_import ) ;
7467 py_import = IntPtr . Zero ;
7568
69+ hook . Release ( ) ;
70+ hook = null ;
71+
7672 Runtime . XDecref ( builtins ) ;
7773 }
7874
@@ -112,13 +108,26 @@ internal static void Initialize()
112108 /// </summary>
113109 internal static void Shutdown ( )
114110 {
115- if ( Runtime . Py_IsInitialized ( ) ! = 0 )
111+ if ( Runtime . Py_IsInitialized ( ) = = 0 )
116112 {
117- RestoreImport ( ) ;
113+ return ;
114+ }
115+
116+ RestoreImport ( ) ;
118117
119- Runtime . XDecref ( py_clr_module ) ;
120- Runtime . XDecref ( root . pyHandle ) ;
118+ bool shouldFreeDef = Runtime . Refcount ( py_clr_module ) == 1 ;
119+ Runtime . XDecref ( py_clr_module ) ;
120+ py_clr_module = IntPtr . Zero ;
121+ #if PYTHON3
122+ if ( shouldFreeDef )
123+ {
124+ ReleaseModuleDef ( ) ;
121125 }
126+ #endif
127+
128+ Runtime . XDecref ( root . pyHandle ) ;
129+ root = null ;
130+ CLRModule . Reset ( ) ;
122131 }
123132
124133 /// <summary>
0 commit comments