@@ -2,7 +2,8 @@ use super::{PyStrRef, PyType, PyTypeRef, PyWeak};
22use crate :: {
33 class:: PyClassImpl ,
44 function:: OptionalArg ,
5- types:: { Constructor , GetAttr , SetAttr } ,
5+ protocol:: { PySequence , PySequenceMethods } ,
6+ types:: { AsSequence , Constructor , GetAttr , SetAttr } ,
67 Context , Py , PyObjectRef , PyPayload , PyRef , PyResult , VirtualMachine ,
78} ;
89
@@ -57,7 +58,7 @@ crate::common::static_cell! {
5758 static WEAK_SUBCLASS : PyTypeRef ;
5859}
5960
60- #[ pyimpl( with( GetAttr , SetAttr , Constructor ) ) ]
61+ #[ pyimpl( with( GetAttr , SetAttr , Constructor , AsSequence ) ) ]
6162impl PyWeakProxy {
6263 #[ pymethod( magic) ]
6364 fn str ( & self , vm : & VirtualMachine ) -> PyResult < PyStrRef > {
@@ -66,6 +67,20 @@ impl PyWeakProxy {
6667 None => Err ( new_reference_error ( vm) ) ,
6768 }
6869 }
70+
71+ fn len ( & self , vm : & VirtualMachine ) -> PyResult < usize > {
72+ match self . weak . upgrade ( ) {
73+ Some ( obj) => obj. length ( vm) ,
74+ None => Err ( new_reference_error ( vm) ) ,
75+ }
76+ }
77+
78+ fn contains ( & self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult < bool > {
79+ match self . weak . upgrade ( ) {
80+ Some ( obj) => PySequence :: contains ( & obj, & needle, vm) ,
81+ None => Err ( new_reference_error ( vm) ) ,
82+ }
83+ }
6984}
7085
7186fn new_reference_error ( vm : & VirtualMachine ) -> PyRef < super :: PyBaseException > {
@@ -100,6 +115,16 @@ impl SetAttr for PyWeakProxy {
100115 }
101116}
102117
118+ impl AsSequence for PyWeakProxy {
119+ const AS_SEQUENCE : PySequenceMethods = PySequenceMethods {
120+ length : Some ( |seq, vm| Self :: sequence_downcast ( seq) . len ( vm) ) ,
121+ contains : Some ( |seq, needle, vm| {
122+ Self :: sequence_downcast ( seq) . contains ( needle. to_owned ( ) , vm)
123+ } ) ,
124+ ..PySequenceMethods :: NOT_IMPLEMENTED
125+ } ;
126+ }
127+
103128pub fn init ( context : & Context ) {
104129 PyWeakProxy :: extend_class ( context, context. types . weakproxy_type ) ;
105130}
0 commit comments