@@ -3,8 +3,9 @@ use crate::builtins::PyType;
33use crate :: builtins:: { PyBytes , PyFloat , PyInt , PyNone , PyStr , PyTypeRef } ;
44use crate :: convert:: ToPyObject ;
55use crate :: function:: { Either , OptionalArg } ;
6+ use crate :: protocol:: PyNumberMethods ;
67use crate :: stdlib:: ctypes:: _ctypes:: new_simple_type;
7- use crate :: types:: Constructor ;
8+ use crate :: types:: { AsNumber , Constructor } ;
89use crate :: { AsObject , Py , PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject , VirtualMachine } ;
910use crossbeam_utils:: atomic:: AtomicCell ;
1011use num_traits:: ToPrimitive ;
@@ -158,9 +159,10 @@ pub struct PyCData {
158159impl PyCData { }
159160
160161#[ pyclass( module = "_ctypes" , name = "PyCSimpleType" , base = PyType ) ]
162+ #[ derive( Debug , PyPayload ) ]
161163pub struct PyCSimpleType { }
162164
163- #[ pyclass( flags( BASETYPE ) ) ]
165+ #[ pyclass( flags( BASETYPE ) , with ( AsNumber ) ) ]
164166impl PyCSimpleType {
165167 #[ allow( clippy:: new_ret_no_self) ]
166168 #[ pymethod]
@@ -186,6 +188,33 @@ impl PyCSimpleType {
186188
187189 PyCSimpleType :: from_param ( cls, as_parameter, vm)
188190 }
191+
192+ #[ pymethod]
193+ fn __mul__ ( cls : PyTypeRef , n : isize , vm : & VirtualMachine ) -> PyResult {
194+ PyCSimple :: repeat ( cls, n, vm)
195+ }
196+ }
197+
198+ impl AsNumber for PyCSimpleType {
199+ fn as_number ( ) -> & ' static PyNumberMethods {
200+ static AS_NUMBER : PyNumberMethods = PyNumberMethods {
201+ multiply : Some ( |a, b, vm| {
202+ // a is a PyCSimpleType instance (type object like c_char)
203+ // b is int (array size)
204+ let cls = a
205+ . downcast_ref :: < PyType > ( )
206+ . ok_or_else ( || vm. new_type_error ( "expected type" . to_owned ( ) ) ) ?;
207+ let n = b
208+ . try_index ( vm) ?
209+ . as_bigint ( )
210+ . to_isize ( )
211+ . ok_or_else ( || vm. new_overflow_error ( "array size too large" . to_owned ( ) ) ) ?;
212+ PyCSimple :: repeat ( cls. to_owned ( ) , n, vm)
213+ } ) ,
214+ ..PyNumberMethods :: NOT_IMPLEMENTED
215+ } ;
216+ & AS_NUMBER
217+ }
189218}
190219
191220#[ pyclass(
@@ -276,11 +305,6 @@ impl PyCSimple {
276305 }
277306 . to_pyobject ( vm) )
278307 }
279-
280- #[ pyclassmethod]
281- fn __mul__ ( cls : PyTypeRef , n : isize , vm : & VirtualMachine ) -> PyResult {
282- PyCSimple :: repeat ( cls, n, vm)
283- }
284308}
285309
286310impl PyCSimple {
0 commit comments