Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f8aff9

Browse files
unknownunknown
unknown
authored and
unknown
committed
Fix a few more temporary objects to be disposed cleanly.
1 parent 3a65dc1 commit 8f8aff9

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

src/runtime/importhook.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,27 @@ public static IntPtr GetCLRModule(IntPtr? fromList=null) {
108108
if (Runtime.PyTuple_Check(fromList.GetValueOrDefault()))
109109
{
110110
Runtime.Incref(py_mod_dict);
111-
PyDict mod_dict = new PyDict(py_mod_dict);
112-
113-
Runtime.Incref(fromList.GetValueOrDefault());
114-
PyTuple from = new PyTuple(fromList.GetValueOrDefault());
115-
foreach (PyObject item in from) {
116-
if (mod_dict.HasKey(item))
117-
continue;
118-
119-
string s = item.AsManagedObject(typeof(string)) as string;
120-
if (null == s)
121-
continue;
122-
123-
ManagedType attr = root.GetAttribute(s, true);
124-
if (null == attr)
125-
continue;
126-
127-
Runtime.Incref(attr.pyHandle);
128-
mod_dict.SetItem(s, new PyObject(attr.pyHandle));
111+
using(PyDict mod_dict = new PyDict(py_mod_dict)) {
112+
Runtime.Incref(fromList.GetValueOrDefault());
113+
using (PyTuple from = new PyTuple(fromList.GetValueOrDefault())) {
114+
foreach (PyObject item in from) {
115+
if (mod_dict.HasKey(item))
116+
continue;
117+
118+
string s = item.AsManagedObject(typeof(string)) as string;
119+
if (null == s)
120+
continue;
121+
122+
ManagedType attr = root.GetAttribute(s, true);
123+
if (null == attr)
124+
continue;
125+
126+
Runtime.Incref(attr.pyHandle);
127+
using (PyObject obj = new PyObject(attr.pyHandle)) {
128+
mod_dict.SetItem(s, obj);
129+
}
130+
}
131+
}
129132
}
130133
}
131134
}

src/runtime/pydict.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public bool HasKey(PyObject key) {
102102
/// </remarks>
103103

104104
public bool HasKey(string key) {
105-
return HasKey(new PyString(key));
105+
using (PyString str = new PyString(key))
106+
return HasKey(str);
106107
}
107108

108109

src/runtime/pyfloat.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ public PyFloat(double value) : base() {
7676
/// </remarks>
7777

7878
public PyFloat(string value) : base() {
79-
PyString s = new PyString(value);
80-
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
81-
if (obj == IntPtr.Zero) {
82-
throw new PythonException();
79+
using (PyString s = new PyString(value)) {
80+
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
81+
if (obj == IntPtr.Zero) {
82+
throw new PythonException();
83+
}
8384
}
8485
}
8586

src/runtime/pyobject.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ public virtual PyObject GetItem(PyObject key) {
364364
/// </remarks>
365365

366366
public virtual PyObject GetItem(string key) {
367-
return GetItem(new PyString(key));
367+
using (PyString pyKey = new PyString(key))
368+
return GetItem(pyKey);
368369
}
369370

370371

@@ -413,7 +414,8 @@ public virtual void SetItem(PyObject key, PyObject value) {
413414
/// </remarks>
414415

415416
public virtual void SetItem(string key, PyObject value) {
416-
SetItem(new PyString(key), value);
417+
using (PyString pyKey = new PyString(key))
418+
SetItem(pyKey, value);
417419
}
418420

419421

@@ -461,7 +463,8 @@ public virtual void DelItem(PyObject key) {
461463
/// </remarks>
462464

463465
public virtual void DelItem(string key) {
464-
DelItem(new PyString(key));
466+
using (PyString pyKey = new PyString(key))
467+
DelItem(pyKey);
465468
}
466469

467470

src/runtime/pythonexception.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ public PythonException() : base()
3434
Runtime.Incref(_pyTB);
3535
if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
3636
{
37-
string type = new PyObject(_pyType).GetAttr("__name__").ToString();
37+
string type;
38+
using (PyObject pyType = new PyObject(_pyType)) {
39+
type = pyType.GetAttr("__name__").ToString();
40+
}
3841
string message = Runtime.GetManagedString(_pyValue);
3942
_message = type + " : " + message;
4043
}
4144
if (_pyTB != IntPtr.Zero)
4245
{
4346
PyObject tb_module = PythonEngine.ImportModule("traceback");
44-
_tb = tb_module.InvokeMethod("format_tb", new PyObject(_pyTB)).ToString();
47+
using (PyObject pyTB = new PyObject(_pyTB)) {
48+
_tb = tb_module.InvokeMethod("format_tb", pyTB).ToString();
49+
}
4550
}
4651
PythonEngine.ReleaseLock(gs);
4752
}

0 commit comments

Comments
 (0)