File tree 2 files changed +41
-22
lines changed 2 files changed +41
-22
lines changed Original file line number Diff line number Diff line change @@ -21,42 +21,57 @@ public class PyIter : PyObject, IEnumerator<object>
21
21
private PyObject _current = null ;
22
22
23
23
/// <summary>
24
- /// PyIter Constructor
25
- /// </summary>
26
- ///
27
- /// <remarks>
28
- /// Creates a new PyIter from an existing iterator reference. Note
29
- /// that the instance assumes ownership of the object reference.
30
- /// The object reference is not checked for type-correctness.
31
- /// </remarks>
24
+ /// PyIter Constructor
25
+ /// </summary>
26
+ ///
27
+ /// <remarks>
28
+ /// Creates a new PyIter from an existing iterator reference. Note
29
+ /// that the instance assumes ownership of the object reference.
30
+ /// The object reference is not checked for type-correctness.
31
+ /// </remarks>
32
32
33
- public PyIter ( IntPtr ptr ) : base ( ptr ) { }
33
+ public PyIter ( IntPtr ptr ) : base ( ptr ) { }
34
34
35
35
/// <summary>
36
- /// PyIter Constructor
37
- /// </summary>
38
- ///
39
- /// <remarks>
40
- /// Creates a Python iterator from an iterable. Like doing "iter(iterable)" in python.
41
- /// </remarks>
36
+ /// PyIter Constructor
37
+ /// </summary>
38
+ ///
39
+ /// <remarks>
40
+ /// Creates a Python iterator from an iterable. Like doing "iter(iterable)" in python.
41
+ /// </remarks>
42
42
43
- public PyIter ( PyObject iterable ) : base ( )
43
+ public PyIter ( PyObject iterable ) : base ( )
44
44
{
45
45
obj = Runtime . PyObject_GetIter ( iterable . obj ) ;
46
46
if ( obj == IntPtr . Zero )
47
47
throw new PythonException ( ) ;
48
48
}
49
49
50
+ protected override void Dispose ( bool disposing )
51
+ {
52
+ if ( null != _current )
53
+ {
54
+ _current . Dispose ( ) ;
55
+ _current = null ;
56
+ }
57
+ base . Dispose ( disposing ) ;
58
+ }
59
+
50
60
#region IEnumerator Members
51
61
52
62
public bool MoveNext ( )
53
63
{
64
+ // dispose of the previous object, if there was one
65
+ if ( null != _current )
66
+ {
67
+ _current . Dispose ( ) ;
68
+ _current = null ;
69
+ }
70
+
54
71
IntPtr next = Runtime . PyIter_Next ( obj ) ;
55
72
if ( next == IntPtr . Zero )
56
- {
57
- _current = null ; //release reference
58
73
return false ;
59
- }
74
+
60
75
_current = new PyObject ( next ) ;
61
76
return true ;
62
77
}
Original file line number Diff line number Diff line change @@ -118,19 +118,23 @@ public object AsManagedObject(Type t) {
118
118
/// collection occurs.
119
119
/// </remarks>
120
120
121
- public void Dispose ( ) {
121
+ protected virtual void Dispose ( bool disposing ) {
122
122
if ( ! disposed ) {
123
123
if ( Runtime . Py_IsInitialized ( ) > 0 ) {
124
124
IntPtr gs = PythonEngine . AcquireLock ( ) ;
125
125
Runtime . Decref ( obj ) ;
126
- obj = IntPtr . Zero ;
126
+ obj = IntPtr . Zero ;
127
127
PythonEngine . ReleaseLock ( gs ) ;
128
128
}
129
- GC . SuppressFinalize ( this ) ;
130
129
disposed = true ;
131
130
}
132
131
}
133
132
133
+ public void Dispose ( ) {
134
+ Dispose ( true ) ;
135
+ GC . SuppressFinalize ( this ) ;
136
+ }
137
+
134
138
135
139
/// <summary>
136
140
/// GetPythonType Method
You can’t perform that action at this time.
0 commit comments