@@ -117,7 +117,7 @@ def read(self, size: int) -> bytes:
117117 def write (self , data : Union [SupportsBytes , bytes ]):
118118 self .arch .write (self .ptr , data )
119119
120- def __getitem__ (self , index ) -> T :
120+ def __getitem__ (self , index : int ) -> T :
121121 ptype = self .type
122122 if ptype is None :
123123 raise TypeError (f"No type associated with pointer" )
@@ -137,6 +137,27 @@ def __getitem__(self, index) -> T:
137137 value = ctype .from_buffer (data )
138138 return ptype (value )
139139
140+ def __setitem__ (self , index : int , value : T ):
141+ ptype = self .type
142+ if ptype is None :
143+ raise TypeError (f"No type associated with pointer" )
144+ if P .is_ptr (ptype ):
145+ ptr = self .ptr + index * self .arch .ptr_size ()
146+ self .arch .write_ptr (ptr , int (value ))
147+ else :
148+ size = Struct .sizeof (ptype , self .arch )
149+ ptr = self .ptr + index * size
150+ if issubclass (ptype , Struct ):
151+ if isinstance (value , ptype ):
152+ raise TypeError (f"Expected { ptype } , got { type (value )} " )
153+ self .arch .write (ptr , value )
154+ else :
155+ # TODO: support bool/enum?
156+ if not isinstance (value , int ):
157+ raise TypeError (f"Expected int, got { type (value )} " )
158+ ctype = Struct .translate_ctype (self .arch .ptr_type (), ptype )
159+ self .arch .write (ptr , ctype (int (value )))
160+
140161 def deref (self ) -> T :
141162 return self [0 ]
142163
@@ -431,4 +452,4 @@ class SAL:
431452 comment : str = ""
432453
433454 def __str__ (self ):
434- return self .annotation
455+ return self .annotation . split ( " " )[ 0 ]
0 commit comments