@@ -38,10 +38,7 @@ def _init(self, value: list[Any] | None) -> None:
38
38
39
39
def _set (self , index : int , value : Any ) -> None :
40
40
with self .doc .transaction () as txn :
41
- if isinstance (txn , ReadTransaction ):
42
- raise RuntimeError (
43
- "Read-only transaction cannot be used to modify document structure"
44
- )
41
+ self ._forbid_read_transaction (txn )
45
42
if isinstance (value , BaseDoc ):
46
43
# subdoc
47
44
self .integrated .insert_doc (txn ._txn , index , value ._doc )
@@ -76,16 +73,16 @@ def insert(self, index, object) -> None:
76
73
77
74
def pop (self , index : int = - 1 ) -> Any :
78
75
with self .doc .transaction ():
76
+ index = self ._check_index (index )
79
77
res = self [index ]
80
78
del self [index ]
81
79
return res
82
80
83
81
def move (self , source_index : int , destination_index : int ) -> None :
84
82
with self .doc .transaction () as txn :
85
- if isinstance (txn , ReadTransaction ):
86
- raise RuntimeError (
87
- "Read-only transaction cannot be used to modify document structure"
88
- )
83
+ self ._forbid_read_transaction (txn )
84
+ source_index = self ._check_index (source_index )
85
+ destination_index = self ._check_index (destination_index )
89
86
self .integrated .move_to (txn ._txn , source_index , destination_index )
90
87
91
88
def __add__ (self , value : list [Any ]) -> Array :
@@ -102,11 +99,7 @@ def __radd__(self, value: list[Any]) -> Array:
102
99
def __setitem__ (self , key : int | slice , value : Any | list [Any ]) -> None :
103
100
with self .doc .transaction ():
104
101
if isinstance (key , int ):
105
- length = len (self )
106
- if length == 0 :
107
- raise IndexError ("Array index out of range" )
108
- if key < 0 :
109
- key += length
102
+ key = self ._check_index (key )
110
103
del self [key ]
111
104
self [key :key ] = [value ]
112
105
elif isinstance (key , slice ):
@@ -121,18 +114,21 @@ def __setitem__(self, key: int | slice, value: Any | list[Any]) -> None:
121
114
else :
122
115
raise RuntimeError (f"Index not supported: { key } " )
123
116
117
+ def _check_index (self , idx : int ) -> int :
118
+ if not isinstance (idx , int ):
119
+ raise RuntimeError ("Index must be of type int" )
120
+ length = len (self )
121
+ if idx < 0 :
122
+ idx += length
123
+ if idx < 0 or idx >= length :
124
+ raise IndexError ("Array index out of range" )
125
+ return idx
126
+
124
127
def __delitem__ (self , key : int | slice ) -> None :
125
128
with self .doc .transaction () as txn :
126
- if isinstance (txn , ReadTransaction ):
127
- raise RuntimeError (
128
- "Read-only transaction cannot be used to modify document structure"
129
- )
129
+ self ._forbid_read_transaction (txn )
130
130
if isinstance (key , int ):
131
- length = len (self )
132
- if length == 0 :
133
- raise IndexError ("Array index out of range" )
134
- if key < 0 :
135
- key += length
131
+ key = self ._check_index (key )
136
132
self .integrated .remove_range (txn ._txn , key , 1 )
137
133
elif isinstance (key , slice ):
138
134
if key .step is not None :
@@ -156,11 +152,7 @@ def __delitem__(self, key: int | slice) -> None:
156
152
def __getitem__ (self , key : int ) -> BaseType :
157
153
with self .doc .transaction () as txn :
158
154
if isinstance (key , int ):
159
- length = len (self )
160
- if length == 0 :
161
- raise IndexError ("Array index out of range" )
162
- if key < 0 :
163
- key += length
155
+ key = self ._check_index (key )
164
156
return self ._maybe_as_type_or_doc (self .integrated .get (txn ._txn , key ))
165
157
elif isinstance (key , slice ):
166
158
i0 = 0 if key .start is None else key .start
0 commit comments