@@ -72,6 +72,15 @@ impl<'a> PySequence<'a> {
7272 methods : OnceCell :: from ( methods) ,
7373 }
7474 }
75+
76+ pub fn try_protocol ( obj : & ' a PyObject , vm : & VirtualMachine ) -> PyResult < Self > {
77+ let zelf = Self :: from ( obj) ;
78+ if zelf. check ( vm) {
79+ Ok ( zelf)
80+ } else {
81+ Err ( vm. new_type_error ( format ! ( "'{}' is not a sequence" , obj. class( ) ) ) )
82+ }
83+ }
7584}
7685
7786impl PySequence < ' _ > {
@@ -80,18 +89,10 @@ impl PySequence<'_> {
8089 }
8190
8291 // PySequence_Check
83- pub fn has_protocol ( & self , vm : & VirtualMachine ) -> bool {
92+ pub fn check ( & self , vm : & VirtualMachine ) -> bool {
8493 self . methods ( vm) . item . is_some ( )
8594 }
8695
87- pub fn try_protocol ( & self , vm : & VirtualMachine ) -> PyResult < ( ) > {
88- if self . has_protocol ( vm) {
89- Ok ( ( ) )
90- } else {
91- Err ( vm. new_type_error ( format ! ( "'{}' is not a sequence" , self . obj. class( ) . name( ) ) ) )
92- }
93- }
94-
9596 pub fn methods ( & self , vm : & VirtualMachine ) -> & PySequenceMethods {
9697 self . methods_cow ( vm) . borrow ( )
9798 }
@@ -116,7 +117,7 @@ impl PySequence<'_> {
116117 self . length_opt ( vm) . ok_or_else ( || {
117118 vm. new_type_error ( format ! (
118119 "'{}' is not a sequence or has no len()" ,
119- self . obj. class( ) . name ( )
120+ self . obj. class( )
120121 ) )
121122 } ) ?
122123 }
@@ -127,15 +128,15 @@ impl PySequence<'_> {
127128 }
128129
129130 // if both arguments apear to be sequences, try fallback to __add__
130- if self . has_protocol ( vm) && PySequence :: from ( other) . has_protocol ( vm) {
131+ if self . check ( vm) && PySequence :: from ( other) . check ( vm) {
131132 let ret = vm. _add ( self . obj , other) ?;
132133 if let PyArithmeticValue :: Implemented ( ret) = PyArithmeticValue :: from_object ( vm, ret) {
133134 return Ok ( ret) ;
134135 }
135136 }
136137 Err ( vm. new_type_error ( format ! (
137138 "'{}' object can't be concatenated" ,
138- self . obj. class( ) . name ( )
139+ self . obj. class( )
139140 ) ) )
140141 }
141142
@@ -145,15 +146,15 @@ impl PySequence<'_> {
145146 }
146147
147148 // try fallback to __mul__
148- if self . has_protocol ( vm) {
149+ if self . check ( vm) {
149150 let ret = vm. _mul ( self . obj , & n. into_pyobject ( vm) ) ?;
150151 if let PyArithmeticValue :: Implemented ( ret) = PyArithmeticValue :: from_object ( vm, ret) {
151152 return Ok ( ret) ;
152153 }
153154 }
154155 Err ( vm. new_type_error ( format ! (
155156 "'{}' object can't be repeated" ,
156- self . obj. class( ) . name ( )
157+ self . obj. class( )
157158 ) ) )
158159 }
159160
@@ -166,15 +167,15 @@ impl PySequence<'_> {
166167 }
167168
168169 // if both arguments apear to be sequences, try fallback to __iadd__
169- if self . has_protocol ( vm) && PySequence :: from ( other) . has_protocol ( vm) {
170+ if self . check ( vm) && PySequence :: from ( other) . check ( vm) {
170171 let ret = vm. _iadd ( self . obj , other) ?;
171172 if let PyArithmeticValue :: Implemented ( ret) = PyArithmeticValue :: from_object ( vm, ret) {
172173 return Ok ( ret) ;
173174 }
174175 }
175176 Err ( vm. new_type_error ( format ! (
176177 "'{}' object can't be concatenated" ,
177- self . obj. class( ) . name ( )
178+ self . obj. class( )
178179 ) ) )
179180 }
180181
@@ -186,15 +187,15 @@ impl PySequence<'_> {
186187 return f ( self , n, vm) ;
187188 }
188189
189- if self . has_protocol ( vm) {
190+ if self . check ( vm) {
190191 let ret = vm. _imul ( self . obj , & n. into_pyobject ( vm) ) ?;
191192 if let PyArithmeticValue :: Implemented ( ret) = PyArithmeticValue :: from_object ( vm, ret) {
192193 return Ok ( ret) ;
193194 }
194195 }
195196 Err ( vm. new_type_error ( format ! (
196197 "'{}' object can't be repeated" ,
197- self . obj. class( ) . name ( )
198+ self . obj. class( )
198199 ) ) )
199200 }
200201
@@ -204,7 +205,7 @@ impl PySequence<'_> {
204205 }
205206 Err ( vm. new_type_error ( format ! (
206207 "'{}' is not a sequence or does not support indexing" ,
207- self . obj. class( ) . name ( )
208+ self . obj. class( )
208209 ) ) )
209210 }
210211
@@ -214,7 +215,7 @@ impl PySequence<'_> {
214215 }
215216 Err ( vm. new_type_error ( format ! (
216217 "'{}' is not a sequence or doesn't support item {}" ,
217- self . obj. class( ) . name ( ) ,
218+ self . obj. class( ) ,
218219 if value. is_some( ) {
219220 "assignment"
220221 } else {
@@ -242,7 +243,7 @@ impl PySequence<'_> {
242243 } else {
243244 Err ( vm. new_type_error ( format ! (
244245 "'{}' object is unsliceable" ,
245- self . obj. class( ) . name ( )
246+ self . obj. class( )
246247 ) ) )
247248 }
248249 }
@@ -265,7 +266,7 @@ impl PySequence<'_> {
265266 } else {
266267 Err ( vm. new_type_error ( format ! (
267268 "'{}' object doesn't support slice {}" ,
268- self . obj. class( ) . name ( ) ,
269+ self . obj. class( ) ,
269270 if value. is_some( ) {
270271 "assignment"
271272 } else {
0 commit comments