@@ -32,19 +32,20 @@ namespace IronPython.Modules {
3232 public static partial class PythonWeakRef {
3333 public const string __doc__ = "Provides support for creating weak references and proxies to objects" ;
3434
35- internal static IWeakReferenceable ConvertToWeakReferenceable ( object obj ) {
36- IWeakReferenceable iwr = obj as IWeakReferenceable ;
37- if ( iwr != null ) return iwr ;
35+ /// <summary>
36+ /// Wrapper provided for backwards compatibility.
37+ /// </summary>
38+ internal static IWeakReferenceable ConvertToWeakReferenceable ( PythonContext context , object obj ) {
39+ return context . ConvertToWeakReferenceable ( obj ) ;
3840
39- throw PythonOps . TypeError ( "cannot create weak reference to '{0}' object" , PythonOps . GetPythonTypeName ( obj ) ) ;
4041 }
4142
42- public static int getweakrefcount ( object @object ) {
43- return @ref . GetWeakRefCount ( @object ) ;
43+ public static int getweakrefcount ( CodeContext context , object @object ) {
44+ return @ref . GetWeakRefCount ( PythonContext . GetContext ( context ) , @object ) ;
4445 }
4546
46- public static List getweakrefs ( object @object ) {
47- return @ref . GetWeakRefs ( @object ) ;
47+ public static List getweakrefs ( CodeContext context , object @object ) {
48+ return @ref . GetWeakRefs ( PythonContext . GetContext ( context ) , @object ) ;
4849 }
4950
5051 public static object proxy ( CodeContext context , object @object ) {
@@ -75,10 +76,11 @@ public class @ref : IStructuralEquatable
7576 private WeakHandle _target ;
7677 private int _hashVal ;
7778 private bool _fHasHash ;
79+ private CodeContext _context ;
7880
7981 #region Python Constructors
8082 public static object __new__ ( CodeContext context , PythonType cls , object @object ) {
81- IWeakReferenceable iwr = ConvertToWeakReferenceable ( @object ) ;
83+ IWeakReferenceable iwr = ConvertToWeakReferenceable ( PythonContext . GetContext ( context ) , @object ) ;
8284
8385 if ( cls == DynamicHelpers . GetPythonTypeFromType ( typeof ( @ref ) ) ) {
8486 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
@@ -90,7 +92,7 @@ public static object __new__(CodeContext context, PythonType cls, object @object
9092 }
9193 }
9294
93- return new @ref ( @object ) ;
95+ return new @ref ( context , @object ) ;
9496 } else {
9597 return cls . CreateInstance ( context , @object ) ;
9698 }
@@ -99,20 +101,21 @@ public static object __new__(CodeContext context, PythonType cls, object @object
99101 public static object __new__ ( CodeContext context , PythonType cls , object @object , object callback ) {
100102 if ( callback == null ) return __new__ ( context , cls , @object ) ;
101103 if ( cls == DynamicHelpers . GetPythonTypeFromType ( typeof ( @ref ) ) ) {
102- return new @ref ( @object , callback ) ;
104+ return new @ref ( context , @object , callback ) ;
103105 } else {
104106 return cls . CreateInstance ( context , @object , callback ) ;
105107 }
106108 }
107109 #endregion
108110
109111 #region Constructors
110- public @ref ( object @object )
111- : this ( @object , null ) {
112+ public @ref ( CodeContext context , object @object )
113+ : this ( context , @object , null ) {
112114 }
113115
114- public @ref ( object @object , object callback ) {
115- WeakRefHelpers . InitializeWeakRef ( this , @object , callback ) ;
116+ public @ref ( CodeContext context , object @object , object callback ) {
117+ this . _context = context ;
118+ WeakRefHelpers . InitializeWeakRef ( this . _context . GetPythonContext ( ) , this , @object , callback ) ;
116119 this . _target = new WeakHandle ( @object , false ) ;
117120 }
118121 #endregion
@@ -122,8 +125,8 @@ public @ref(object @object, object callback) {
122125 // remove our self from the chain...
123126 try {
124127 if ( _target . IsAlive ) {
125- IWeakReferenceable iwr = _target . Target as IWeakReferenceable ;
126- if ( iwr != null ) {
128+ IWeakReferenceable iwr ;
129+ if ( this . _context . GetPythonContext ( ) . TryConvertToWeakReferenceable ( _target . Target , out iwr ) ) {
127130 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
128131 if ( wrt != null ) {
129132 // weak reference being finalized before target object,
@@ -143,20 +146,20 @@ public @ref(object @object, object callback) {
143146
144147 #region Static helpers
145148
146- internal static int GetWeakRefCount ( object o ) {
147- IWeakReferenceable iwr = o as IWeakReferenceable ;
148- if ( iwr != null ) {
149+ internal static int GetWeakRefCount ( PythonContext context , object o ) {
150+ IWeakReferenceable iwr ;
151+ if ( context . TryConvertToWeakReferenceable ( o , out iwr ) ) {
149152 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
150153 if ( wrt != null ) return wrt . HandlerCount ;
151154 }
152155
153156 return 0 ;
154157 }
155158
156- internal static List GetWeakRefs ( object o ) {
159+ internal static List GetWeakRefs ( PythonContext context , object o ) {
157160 List l = new List ( ) ;
158- IWeakReferenceable iwr = o as IWeakReferenceable ;
159- if ( iwr != null ) {
161+ IWeakReferenceable iwr ;
162+ if ( context . TryConvertToWeakReferenceable ( o , out iwr ) ) {
160163 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
161164 if ( wrt != null ) {
162165 for ( int i = 0 ; i < wrt . HandlerCount ; i ++ ) {
@@ -172,14 +175,14 @@ internal static List GetWeakRefs(object o) {
172175 [ SpecialName ]
173176 public object Call ( CodeContext context ) {
174177 if ( ! _target . IsAlive ) {
175- throw PythonOps . ReferenceError ( "weak object has gone away" ) ;
178+ return null ;
176179 }
177180 try {
178181 object res = _target . Target ;
179182 GC . KeepAlive ( this ) ;
180183 return res ;
181184 } catch ( InvalidOperationException ) {
182- throw PythonOps . ReferenceError ( "weak object has gone away" ) ;
185+ return null ;
183186 }
184187 }
185188
@@ -311,7 +314,7 @@ public sealed partial class weakproxy : IPythonObject, ICodeFormattable, IProxyO
311314
312315 #region Python Constructors
313316 internal static object MakeNew ( CodeContext /*!*/ context , object @object , object callback ) {
314- IWeakReferenceable iwr = ConvertToWeakReferenceable ( @object ) ;
317+ IWeakReferenceable iwr = ConvertToWeakReferenceable ( PythonContext . GetContext ( context ) , @object ) ;
315318
316319 if ( callback == null ) {
317320 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
@@ -331,7 +334,7 @@ internal static object MakeNew(CodeContext/*!*/ context, object @object, object
331334 #region Constructors
332335
333336 private weakproxy ( CodeContext /*!*/ context , object target , object callback ) {
334- WeakRefHelpers . InitializeWeakRef ( this , target , callback ) ;
337+ WeakRefHelpers . InitializeWeakRef ( PythonContext . GetContext ( context ) , this , target , callback ) ;
335338 _target = new WeakHandle ( target , false ) ;
336339 _context = context ;
337340 }
@@ -341,8 +344,8 @@ private weakproxy(CodeContext/*!*/ context, object target, object callback) {
341344 ~ weakproxy ( ) {
342345 // remove our self from the chain...
343346 try {
344- IWeakReferenceable iwr = _target . Target as IWeakReferenceable ;
345- if ( iwr != null ) {
347+ IWeakReferenceable iwr ;
348+ if ( this . _context . GetPythonContext ( ) . TryConvertToWeakReferenceable ( _target . Target , out iwr ) ) {
346349 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
347350 wrt . RemoveHandler ( this ) ;
348351 }
@@ -579,7 +582,7 @@ public sealed partial class weakcallableproxy :
579582 #region Python Constructors
580583
581584 internal static object MakeNew ( CodeContext /*!*/ context , object @object , object callback ) {
582- IWeakReferenceable iwr = ConvertToWeakReferenceable ( @object ) ;
585+ IWeakReferenceable iwr = ConvertToWeakReferenceable ( PythonContext . GetContext ( context ) , @object ) ;
583586
584587 if ( callback == null ) {
585588 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
@@ -602,7 +605,7 @@ internal static object MakeNew(CodeContext/*!*/ context, object @object, object
602605 #region Constructors
603606
604607 private weakcallableproxy ( CodeContext context , object target , object callback ) {
605- WeakRefHelpers . InitializeWeakRef ( this , target , callback ) ;
608+ WeakRefHelpers . InitializeWeakRef ( PythonContext . GetContext ( context ) , this , target , callback ) ;
606609 _target = new WeakHandle ( target , false ) ;
607610 _context = context ;
608611 }
@@ -614,8 +617,8 @@ private weakcallableproxy(CodeContext context, object target, object callback) {
614617 ~ weakcallableproxy ( ) {
615618 // remove our self from the chain...
616619 try {
617- IWeakReferenceable iwr = _target . Target as IWeakReferenceable ;
618- if ( iwr != null ) {
620+ IWeakReferenceable iwr ;
621+ if ( this . _context . GetPythonContext ( ) . TryConvertToWeakReferenceable ( _target . Target , out iwr ) ) {
619622 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
620623 wrt . RemoveHandler ( this ) ;
621624 }
@@ -829,8 +832,8 @@ public object __nonzero__() {
829832 }
830833
831834 static class WeakRefHelpers {
832- public static void InitializeWeakRef ( object self , object target , object callback ) {
833- IWeakReferenceable iwr = ConvertToWeakReferenceable ( target ) ;
835+ public static void InitializeWeakRef ( PythonContext context , object self , object target , object callback ) {
836+ IWeakReferenceable iwr = ConvertToWeakReferenceable ( context , target ) ;
834837
835838 WeakRefTracker wrt = iwr . GetWeakRef ( ) ;
836839 if ( wrt == null ) {
0 commit comments