##// END OF EJS Templates
rust-cpython: require GIL to borrow immutable reference from PySharedRefCell...
Yuya Nishihara -
r43580:f8c114f2 default
parent child Browse files
Show More
@@ -1,129 +1,129 b''
1 1 // dirs_multiset.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings for the `hg::dirstate::dirs_multiset` file provided by the
9 9 //! `hg-core` package.
10 10
11 11 use std::cell::RefCell;
12 12 use std::convert::TryInto;
13 13
14 14 use cpython::{
15 15 exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult,
16 16 Python,
17 17 };
18 18
19 19 use crate::dirstate::extract_dirstate;
20 20 use crate::ref_sharing::{PyLeakedRef, PySharedRefCell};
21 21 use hg::{
22 22 utils::hg_path::{HgPath, HgPathBuf},
23 23 DirsMultiset, DirsMultisetIter, DirstateMapError, DirstateParseError,
24 24 EntryState,
25 25 };
26 26
27 27 py_class!(pub class Dirs |py| {
28 28 data inner: PySharedRefCell<DirsMultiset>;
29 29
30 30 // `map` is either a `dict` or a flat iterator (usually a `set`, sometimes
31 31 // a `list`)
32 32 def __new__(
33 33 _cls,
34 34 map: PyObject,
35 35 skip: Option<PyObject> = None
36 36 ) -> PyResult<Self> {
37 37 let mut skip_state: Option<EntryState> = None;
38 38 if let Some(skip) = skip {
39 39 skip_state = Some(
40 40 skip.extract::<PyBytes>(py)?.data(py)[0]
41 41 .try_into()
42 42 .map_err(|e: DirstateParseError| {
43 43 PyErr::new::<exc::ValueError, _>(py, e.to_string())
44 44 })?,
45 45 );
46 46 }
47 47 let inner = if let Ok(map) = map.cast_as::<PyDict>(py) {
48 48 let dirstate = extract_dirstate(py, &map)?;
49 49 DirsMultiset::from_dirstate(&dirstate, skip_state)
50 50 } else {
51 51 let map: Result<Vec<HgPathBuf>, PyErr> = map
52 52 .iter(py)?
53 53 .map(|o| {
54 54 Ok(HgPathBuf::from_bytes(
55 55 o?.extract::<PyBytes>(py)?.data(py),
56 56 ))
57 57 })
58 58 .collect();
59 59 DirsMultiset::from_manifest(&map?)
60 60 };
61 61
62 62 Self::create_instance(
63 63 py,
64 64 PySharedRefCell::new(inner),
65 65 )
66 66 }
67 67
68 68 def addpath(&self, path: PyObject) -> PyResult<PyObject> {
69 69 self.inner_shared(py).borrow_mut()?.add_path(
70 70 HgPath::new(path.extract::<PyBytes>(py)?.data(py)),
71 71 );
72 72 Ok(py.None())
73 73 }
74 74
75 75 def delpath(&self, path: PyObject) -> PyResult<PyObject> {
76 76 self.inner_shared(py).borrow_mut()?.delete_path(
77 77 HgPath::new(path.extract::<PyBytes>(py)?.data(py)),
78 78 )
79 79 .and(Ok(py.None()))
80 80 .or_else(|e| {
81 81 match e {
82 82 DirstateMapError::PathNotFound(_p) => {
83 83 Err(PyErr::new::<exc::ValueError, _>(
84 84 py,
85 85 "expected a value, found none".to_string(),
86 86 ))
87 87 }
88 88 DirstateMapError::EmptyPath => {
89 89 Ok(py.None())
90 90 }
91 91 }
92 92 })
93 93 }
94 94 def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> {
95 95 let leaked_ref = self.inner_shared(py).leak_immutable()?;
96 96 DirsMultisetKeysIterator::from_inner(
97 97 py,
98 98 unsafe { leaked_ref.map(py, |o| o.iter()) },
99 99 )
100 100 }
101 101
102 102 def __contains__(&self, item: PyObject) -> PyResult<bool> {
103 Ok(self.inner(py).borrow().contains(HgPath::new(
103 Ok(self.inner_shared(py).borrow().contains(HgPath::new(
104 104 item.extract::<PyBytes>(py)?.data(py).as_ref(),
105 105 )))
106 106 }
107 107 });
108 108
109 109 py_shared_ref!(Dirs, DirsMultiset, inner, inner_shared);
110 110
111 111 impl Dirs {
112 112 pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
113 113 Self::create_instance(py, PySharedRefCell::new(d))
114 114 }
115 115
116 116 fn translate_key(
117 117 py: Python,
118 118 res: &HgPathBuf,
119 119 ) -> PyResult<Option<PyBytes>> {
120 120 Ok(Some(PyBytes::new(py, res.as_ref())))
121 121 }
122 122 }
123 123
124 124 py_shared_iterator!(
125 125 DirsMultisetKeysIterator,
126 126 PyLeakedRef<DirsMultisetIter<'static>>,
127 127 Dirs::translate_key,
128 128 Option<PyBytes>
129 129 );
@@ -1,504 +1,504 b''
1 1 // dirstate_map.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
9 9 //! `hg-core` package.
10 10
11 11 use std::cell::{Ref, RefCell};
12 12 use std::convert::TryInto;
13 13 use std::time::Duration;
14 14
15 15 use cpython::{
16 16 exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject,
17 17 PyResult, PyTuple, Python, PythonObject, ToPyObject,
18 18 };
19 19
20 20 use crate::{
21 21 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
22 22 dirstate::{dirs_multiset::Dirs, make_dirstate_tuple},
23 23 ref_sharing::{PyLeakedRef, PySharedRefCell},
24 24 };
25 25 use hg::{
26 26 utils::hg_path::{HgPath, HgPathBuf},
27 27 DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
28 28 DirstateParents, DirstateParseError, EntryState, StateMapIter,
29 29 PARENT_SIZE,
30 30 };
31 31
32 32 // TODO
33 33 // This object needs to share references to multiple members of its Rust
34 34 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
35 35 // Right now `CopyMap` is done, but it needs to have an explicit reference
36 36 // to `RustDirstateMap` which itself needs to have an encapsulation for
37 37 // every method in `CopyMap` (copymapcopy, etc.).
38 38 // This is ugly and hard to maintain.
39 39 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
40 40 // `py_class!` is already implemented and does not mention
41 41 // `RustDirstateMap`, rightfully so.
42 42 // All attributes also have to have a separate refcount data attribute for
43 43 // leaks, with all methods that go along for reference sharing.
44 44 py_class!(pub class DirstateMap |py| {
45 45 data inner: PySharedRefCell<RustDirstateMap>;
46 46
47 47 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
48 48 let inner = RustDirstateMap::default();
49 49 Self::create_instance(
50 50 py,
51 51 PySharedRefCell::new(inner),
52 52 )
53 53 }
54 54
55 55 def clear(&self) -> PyResult<PyObject> {
56 56 self.inner_shared(py).borrow_mut()?.clear();
57 57 Ok(py.None())
58 58 }
59 59
60 60 def get(
61 61 &self,
62 62 key: PyObject,
63 63 default: Option<PyObject> = None
64 64 ) -> PyResult<Option<PyObject>> {
65 65 let key = key.extract::<PyBytes>(py)?;
66 match self.inner(py).borrow().get(HgPath::new(key.data(py))) {
66 match self.inner_shared(py).borrow().get(HgPath::new(key.data(py))) {
67 67 Some(entry) => {
68 68 Ok(Some(make_dirstate_tuple(py, entry)?))
69 69 },
70 70 None => Ok(default)
71 71 }
72 72 }
73 73
74 74 def addfile(
75 75 &self,
76 76 f: PyObject,
77 77 oldstate: PyObject,
78 78 state: PyObject,
79 79 mode: PyObject,
80 80 size: PyObject,
81 81 mtime: PyObject
82 82 ) -> PyResult<PyObject> {
83 83 self.inner_shared(py).borrow_mut()?.add_file(
84 84 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
85 85 oldstate.extract::<PyBytes>(py)?.data(py)[0]
86 86 .try_into()
87 87 .map_err(|e: DirstateParseError| {
88 88 PyErr::new::<exc::ValueError, _>(py, e.to_string())
89 89 })?,
90 90 DirstateEntry {
91 91 state: state.extract::<PyBytes>(py)?.data(py)[0]
92 92 .try_into()
93 93 .map_err(|e: DirstateParseError| {
94 94 PyErr::new::<exc::ValueError, _>(py, e.to_string())
95 95 })?,
96 96 mode: mode.extract(py)?,
97 97 size: size.extract(py)?,
98 98 mtime: mtime.extract(py)?,
99 99 },
100 100 );
101 101 Ok(py.None())
102 102 }
103 103
104 104 def removefile(
105 105 &self,
106 106 f: PyObject,
107 107 oldstate: PyObject,
108 108 size: PyObject
109 109 ) -> PyResult<PyObject> {
110 110 self.inner_shared(py).borrow_mut()?
111 111 .remove_file(
112 112 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
113 113 oldstate.extract::<PyBytes>(py)?.data(py)[0]
114 114 .try_into()
115 115 .map_err(|e: DirstateParseError| {
116 116 PyErr::new::<exc::ValueError, _>(py, e.to_string())
117 117 })?,
118 118 size.extract(py)?,
119 119 )
120 120 .or_else(|_| {
121 121 Err(PyErr::new::<exc::OSError, _>(
122 122 py,
123 123 "Dirstate error".to_string(),
124 124 ))
125 125 })?;
126 126 Ok(py.None())
127 127 }
128 128
129 129 def dropfile(
130 130 &self,
131 131 f: PyObject,
132 132 oldstate: PyObject
133 133 ) -> PyResult<PyBool> {
134 134 self.inner_shared(py).borrow_mut()?
135 135 .drop_file(
136 136 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
137 137 oldstate.extract::<PyBytes>(py)?.data(py)[0]
138 138 .try_into()
139 139 .map_err(|e: DirstateParseError| {
140 140 PyErr::new::<exc::ValueError, _>(py, e.to_string())
141 141 })?,
142 142 )
143 143 .and_then(|b| Ok(b.to_py_object(py)))
144 144 .or_else(|_| {
145 145 Err(PyErr::new::<exc::OSError, _>(
146 146 py,
147 147 "Dirstate error".to_string(),
148 148 ))
149 149 })
150 150 }
151 151
152 152 def clearambiguoustimes(
153 153 &self,
154 154 files: PyObject,
155 155 now: PyObject
156 156 ) -> PyResult<PyObject> {
157 157 let files: PyResult<Vec<HgPathBuf>> = files
158 158 .iter(py)?
159 159 .map(|filename| {
160 160 Ok(HgPathBuf::from_bytes(
161 161 filename?.extract::<PyBytes>(py)?.data(py),
162 162 ))
163 163 })
164 164 .collect();
165 165 self.inner_shared(py).borrow_mut()?
166 166 .clear_ambiguous_times(files?, now.extract(py)?);
167 167 Ok(py.None())
168 168 }
169 169
170 170 // TODO share the reference
171 171 def nonnormalentries(&self) -> PyResult<PyObject> {
172 172 let (non_normal, other_parent) =
173 self.inner(py).borrow().non_normal_other_parent_entries();
173 self.inner_shared(py).borrow().non_normal_other_parent_entries();
174 174
175 175 let locals = PyDict::new(py);
176 176 locals.set_item(
177 177 py,
178 178 "non_normal",
179 179 non_normal
180 180 .iter()
181 181 .map(|v| PyBytes::new(py, v.as_ref()))
182 182 .collect::<Vec<PyBytes>>()
183 183 .to_py_object(py),
184 184 )?;
185 185 locals.set_item(
186 186 py,
187 187 "other_parent",
188 188 other_parent
189 189 .iter()
190 190 .map(|v| PyBytes::new(py, v.as_ref()))
191 191 .collect::<Vec<PyBytes>>()
192 192 .to_py_object(py),
193 193 )?;
194 194
195 195 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
196 196 }
197 197
198 198 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
199 199 let d = d.extract::<PyBytes>(py)?;
200 200 Ok(self.inner_shared(py).borrow_mut()?
201 201 .has_tracked_dir(HgPath::new(d.data(py)))
202 202 .to_py_object(py))
203 203 }
204 204
205 205 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
206 206 let d = d.extract::<PyBytes>(py)?;
207 207 Ok(self.inner_shared(py).borrow_mut()?
208 208 .has_dir(HgPath::new(d.data(py)))
209 209 .to_py_object(py))
210 210 }
211 211
212 212 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
213 213 self.inner_shared(py).borrow_mut()?
214 214 .parents(st.extract::<PyBytes>(py)?.data(py))
215 215 .and_then(|d| {
216 216 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
217 217 .to_py_object(py))
218 218 })
219 219 .or_else(|_| {
220 220 Err(PyErr::new::<exc::OSError, _>(
221 221 py,
222 222 "Dirstate error".to_string(),
223 223 ))
224 224 })
225 225 }
226 226
227 227 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
228 228 let p1 = extract_node_id(py, &p1)?;
229 229 let p2 = extract_node_id(py, &p2)?;
230 230
231 231 self.inner_shared(py).borrow_mut()?
232 232 .set_parents(&DirstateParents { p1, p2 });
233 233 Ok(py.None())
234 234 }
235 235
236 236 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
237 237 match self.inner_shared(py).borrow_mut()?
238 238 .read(st.extract::<PyBytes>(py)?.data(py))
239 239 {
240 240 Ok(Some(parents)) => Ok(Some(
241 241 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
242 242 .to_py_object(py)
243 243 .into_object(),
244 244 )),
245 245 Ok(None) => Ok(Some(py.None())),
246 246 Err(_) => Err(PyErr::new::<exc::OSError, _>(
247 247 py,
248 248 "Dirstate error".to_string(),
249 249 )),
250 250 }
251 251 }
252 252 def write(
253 253 &self,
254 254 p1: PyObject,
255 255 p2: PyObject,
256 256 now: PyObject
257 257 ) -> PyResult<PyBytes> {
258 258 let now = Duration::new(now.extract(py)?, 0);
259 259 let parents = DirstateParents {
260 260 p1: extract_node_id(py, &p1)?,
261 261 p2: extract_node_id(py, &p2)?,
262 262 };
263 263
264 264 match self.inner_shared(py).borrow_mut()?.pack(parents, now) {
265 265 Ok(packed) => Ok(PyBytes::new(py, &packed)),
266 266 Err(_) => Err(PyErr::new::<exc::OSError, _>(
267 267 py,
268 268 "Dirstate error".to_string(),
269 269 )),
270 270 }
271 271 }
272 272
273 273 def filefoldmapasdict(&self) -> PyResult<PyDict> {
274 274 let dict = PyDict::new(py);
275 275 for (key, value) in
276 276 self.inner_shared(py).borrow_mut()?.build_file_fold_map().iter()
277 277 {
278 278 dict.set_item(py, key.as_ref().to_vec(), value.as_ref().to_vec())?;
279 279 }
280 280 Ok(dict)
281 281 }
282 282
283 283 def __len__(&self) -> PyResult<usize> {
284 Ok(self.inner(py).borrow().len())
284 Ok(self.inner_shared(py).borrow().len())
285 285 }
286 286
287 287 def __contains__(&self, key: PyObject) -> PyResult<bool> {
288 288 let key = key.extract::<PyBytes>(py)?;
289 Ok(self.inner(py).borrow().contains_key(HgPath::new(key.data(py))))
289 Ok(self.inner_shared(py).borrow().contains_key(HgPath::new(key.data(py))))
290 290 }
291 291
292 292 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
293 293 let key = key.extract::<PyBytes>(py)?;
294 294 let key = HgPath::new(key.data(py));
295 match self.inner(py).borrow().get(key) {
295 match self.inner_shared(py).borrow().get(key) {
296 296 Some(entry) => {
297 297 Ok(make_dirstate_tuple(py, entry)?)
298 298 },
299 299 None => Err(PyErr::new::<exc::KeyError, _>(
300 300 py,
301 301 String::from_utf8_lossy(key.as_bytes()),
302 302 )),
303 303 }
304 304 }
305 305
306 306 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
307 307 let leaked_ref = self.inner_shared(py).leak_immutable()?;
308 308 DirstateMapKeysIterator::from_inner(
309 309 py,
310 310 unsafe { leaked_ref.map(py, |o| o.iter()) },
311 311 )
312 312 }
313 313
314 314 def items(&self) -> PyResult<DirstateMapItemsIterator> {
315 315 let leaked_ref = self.inner_shared(py).leak_immutable()?;
316 316 DirstateMapItemsIterator::from_inner(
317 317 py,
318 318 unsafe { leaked_ref.map(py, |o| o.iter()) },
319 319 )
320 320 }
321 321
322 322 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
323 323 let leaked_ref = self.inner_shared(py).leak_immutable()?;
324 324 DirstateMapKeysIterator::from_inner(
325 325 py,
326 326 unsafe { leaked_ref.map(py, |o| o.iter()) },
327 327 )
328 328 }
329 329
330 330 def getdirs(&self) -> PyResult<Dirs> {
331 331 // TODO don't copy, share the reference
332 332 self.inner_shared(py).borrow_mut()?.set_dirs();
333 333 Dirs::from_inner(
334 334 py,
335 335 DirsMultiset::from_dirstate(
336 &self.inner(py).borrow(),
336 &self.inner_shared(py).borrow(),
337 337 Some(EntryState::Removed),
338 338 ),
339 339 )
340 340 }
341 341 def getalldirs(&self) -> PyResult<Dirs> {
342 342 // TODO don't copy, share the reference
343 343 self.inner_shared(py).borrow_mut()?.set_all_dirs();
344 344 Dirs::from_inner(
345 345 py,
346 346 DirsMultiset::from_dirstate(
347 &self.inner(py).borrow(),
347 &self.inner_shared(py).borrow(),
348 348 None,
349 349 ),
350 350 )
351 351 }
352 352
353 353 // TODO all copymap* methods, see docstring above
354 354 def copymapcopy(&self) -> PyResult<PyDict> {
355 355 let dict = PyDict::new(py);
356 for (key, value) in self.inner(py).borrow().copy_map.iter() {
356 for (key, value) in self.inner_shared(py).borrow().copy_map.iter() {
357 357 dict.set_item(
358 358 py,
359 359 PyBytes::new(py, key.as_ref()),
360 360 PyBytes::new(py, value.as_ref()),
361 361 )?;
362 362 }
363 363 Ok(dict)
364 364 }
365 365
366 366 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
367 367 let key = key.extract::<PyBytes>(py)?;
368 match self.inner(py).borrow().copy_map.get(HgPath::new(key.data(py))) {
368 match self.inner_shared(py).borrow().copy_map.get(HgPath::new(key.data(py))) {
369 369 Some(copy) => Ok(PyBytes::new(py, copy.as_ref())),
370 370 None => Err(PyErr::new::<exc::KeyError, _>(
371 371 py,
372 372 String::from_utf8_lossy(key.data(py)),
373 373 )),
374 374 }
375 375 }
376 376 def copymap(&self) -> PyResult<CopyMap> {
377 377 CopyMap::from_inner(py, self.clone_ref(py))
378 378 }
379 379
380 380 def copymaplen(&self) -> PyResult<usize> {
381 Ok(self.inner(py).borrow().copy_map.len())
381 Ok(self.inner_shared(py).borrow().copy_map.len())
382 382 }
383 383 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
384 384 let key = key.extract::<PyBytes>(py)?;
385 385 Ok(self
386 .inner(py)
386 .inner_shared(py)
387 387 .borrow()
388 388 .copy_map
389 389 .contains_key(HgPath::new(key.data(py))))
390 390 }
391 391 def copymapget(
392 392 &self,
393 393 key: PyObject,
394 394 default: Option<PyObject>
395 395 ) -> PyResult<Option<PyObject>> {
396 396 let key = key.extract::<PyBytes>(py)?;
397 397 match self
398 .inner(py)
398 .inner_shared(py)
399 399 .borrow()
400 400 .copy_map
401 401 .get(HgPath::new(key.data(py)))
402 402 {
403 403 Some(copy) => Ok(Some(
404 404 PyBytes::new(py, copy.as_ref()).into_object(),
405 405 )),
406 406 None => Ok(default),
407 407 }
408 408 }
409 409 def copymapsetitem(
410 410 &self,
411 411 key: PyObject,
412 412 value: PyObject
413 413 ) -> PyResult<PyObject> {
414 414 let key = key.extract::<PyBytes>(py)?;
415 415 let value = value.extract::<PyBytes>(py)?;
416 416 self.inner_shared(py).borrow_mut()?.copy_map.insert(
417 417 HgPathBuf::from_bytes(key.data(py)),
418 418 HgPathBuf::from_bytes(value.data(py)),
419 419 );
420 420 Ok(py.None())
421 421 }
422 422 def copymappop(
423 423 &self,
424 424 key: PyObject,
425 425 default: Option<PyObject>
426 426 ) -> PyResult<Option<PyObject>> {
427 427 let key = key.extract::<PyBytes>(py)?;
428 428 match self
429 429 .inner_shared(py)
430 430 .borrow_mut()?
431 431 .copy_map
432 432 .remove(HgPath::new(key.data(py)))
433 433 {
434 434 Some(_) => Ok(None),
435 435 None => Ok(default),
436 436 }
437 437 }
438 438
439 439 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
440 440 let leaked_ref = self.inner_shared(py).leak_immutable()?;
441 441 CopyMapKeysIterator::from_inner(
442 442 py,
443 443 unsafe { leaked_ref.map(py, |o| o.copy_map.iter()) },
444 444 )
445 445 }
446 446
447 447 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
448 448 let leaked_ref = self.inner_shared(py).leak_immutable()?;
449 449 CopyMapItemsIterator::from_inner(
450 450 py,
451 451 unsafe { leaked_ref.map(py, |o| o.copy_map.iter()) },
452 452 )
453 453 }
454 454
455 455 });
456 456
457 457 impl DirstateMap {
458 458 pub fn get_inner<'a>(
459 459 &'a self,
460 460 py: Python<'a>,
461 461 ) -> Ref<'a, RustDirstateMap> {
462 462 self.inner_shared(py).borrow()
463 463 }
464 464 fn translate_key(
465 465 py: Python,
466 466 res: (&HgPathBuf, &DirstateEntry),
467 467 ) -> PyResult<Option<PyBytes>> {
468 468 Ok(Some(PyBytes::new(py, res.0.as_ref())))
469 469 }
470 470 fn translate_key_value(
471 471 py: Python,
472 472 res: (&HgPathBuf, &DirstateEntry),
473 473 ) -> PyResult<Option<(PyBytes, PyObject)>> {
474 474 let (f, entry) = res;
475 475 Ok(Some((
476 476 PyBytes::new(py, f.as_ref()),
477 477 make_dirstate_tuple(py, entry)?,
478 478 )))
479 479 }
480 480 }
481 481
482 482 py_shared_ref!(DirstateMap, RustDirstateMap, inner, inner_shared);
483 483
484 484 py_shared_iterator!(
485 485 DirstateMapKeysIterator,
486 486 PyLeakedRef<StateMapIter<'static>>,
487 487 DirstateMap::translate_key,
488 488 Option<PyBytes>
489 489 );
490 490
491 491 py_shared_iterator!(
492 492 DirstateMapItemsIterator,
493 493 PyLeakedRef<StateMapIter<'static>>,
494 494 DirstateMap::translate_key_value,
495 495 Option<(PyBytes, PyObject)>
496 496 );
497 497
498 498 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
499 499 let bytes = obj.extract::<PyBytes>(py)?;
500 500 match bytes.data(py).try_into() {
501 501 Ok(s) => Ok(s),
502 502 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
503 503 }
504 504 }
@@ -1,500 +1,500 b''
1 1 // ref_sharing.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 6 // of this software and associated documentation files (the "Software"), to
7 7 // deal in the Software without restriction, including without limitation the
8 8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 9 // sell copies of the Software, and to permit persons to whom the Software is
10 10 // furnished to do so, subject to the following conditions:
11 11 //
12 12 // The above copyright notice and this permission notice shall be included in
13 13 // all copies or substantial portions of the Software.
14 14 //
15 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 21 // IN THE SOFTWARE.
22 22
23 23 //! Macros for use in the `hg-cpython` bridge library.
24 24
25 25 use crate::exceptions::AlreadyBorrowed;
26 26 use cpython::{PyClone, PyObject, PyResult, Python};
27 27 use std::cell::{Cell, Ref, RefCell, RefMut};
28 28
29 29 /// Manages the shared state between Python and Rust
30 30 #[derive(Debug, Default)]
31 31 pub struct PySharedState {
32 32 leak_count: Cell<usize>,
33 33 mutably_borrowed: Cell<bool>,
34 34 }
35 35
36 36 // &PySharedState can be Send because any access to inner cells is
37 37 // synchronized by the GIL.
38 38 unsafe impl Sync for PySharedState {}
39 39
40 40 impl PySharedState {
41 41 pub fn borrow_mut<'a, T>(
42 42 &'a self,
43 43 py: Python<'a>,
44 44 pyrefmut: RefMut<'a, T>,
45 45 ) -> PyResult<PyRefMut<'a, T>> {
46 46 if self.mutably_borrowed.get() {
47 47 return Err(AlreadyBorrowed::new(
48 48 py,
49 49 "Cannot borrow mutably while there exists another \
50 50 mutable reference in a Python object",
51 51 ));
52 52 }
53 53 match self.leak_count.get() {
54 54 0 => {
55 55 self.mutably_borrowed.replace(true);
56 56 Ok(PyRefMut::new(py, pyrefmut, self))
57 57 }
58 58 // TODO
59 59 // For now, this works differently than Python references
60 60 // in the case of iterators.
61 61 // Python does not complain when the data an iterator
62 62 // points to is modified if the iterator is never used
63 63 // afterwards.
64 64 // Here, we are stricter than this by refusing to give a
65 65 // mutable reference if it is already borrowed.
66 66 // While the additional safety might be argued for, it
67 67 // breaks valid programming patterns in Python and we need
68 68 // to fix this issue down the line.
69 69 _ => Err(AlreadyBorrowed::new(
70 70 py,
71 71 "Cannot borrow mutably while there are \
72 72 immutable references in Python objects",
73 73 )),
74 74 }
75 75 }
76 76
77 77 /// Return a reference to the wrapped data and its state with an
78 78 /// artificial static lifetime.
79 79 /// We need to be protected by the GIL for thread-safety.
80 80 ///
81 81 /// # Safety
82 82 ///
83 83 /// This is highly unsafe since the lifetime of the given data can be
84 84 /// extended. Do not call this function directly.
85 85 pub unsafe fn leak_immutable<T>(
86 86 &self,
87 87 py: Python,
88 88 data: &PySharedRefCell<T>,
89 89 ) -> PyResult<(&'static T, &'static PySharedState)> {
90 90 if self.mutably_borrowed.get() {
91 91 return Err(AlreadyBorrowed::new(
92 92 py,
93 93 "Cannot borrow immutably while there is a \
94 94 mutable reference in Python objects",
95 95 ));
96 96 }
97 97 // TODO: it's weird that self is data.py_shared_state. Maybe we
98 98 // can move stuff to PySharedRefCell?
99 99 let ptr = data.as_ptr();
100 100 let state_ptr: *const PySharedState = &data.py_shared_state;
101 101 self.leak_count.replace(self.leak_count.get() + 1);
102 102 Ok((&*ptr, &*state_ptr))
103 103 }
104 104
105 105 /// # Safety
106 106 ///
107 107 /// It's unsafe to update the reference count without knowing the
108 108 /// reference is deleted. Do not call this function directly.
109 109 pub unsafe fn decrease_leak_count(&self, _py: Python, mutable: bool) {
110 110 if mutable {
111 111 assert_eq!(self.leak_count.get(), 0);
112 112 assert!(self.mutably_borrowed.get());
113 113 self.mutably_borrowed.replace(false);
114 114 } else {
115 115 let count = self.leak_count.get();
116 116 assert!(count > 0);
117 117 self.leak_count.replace(count - 1);
118 118 }
119 119 }
120 120 }
121 121
122 122 /// `RefCell` wrapper to be safely used in conjunction with `PySharedState`.
123 123 ///
124 124 /// Only immutable operation is allowed through this interface.
125 125 #[derive(Debug)]
126 126 pub struct PySharedRefCell<T> {
127 127 inner: RefCell<T>,
128 128 py_shared_state: PySharedState,
129 129 }
130 130
131 131 impl<T> PySharedRefCell<T> {
132 132 pub fn new(value: T) -> PySharedRefCell<T> {
133 133 Self {
134 134 inner: RefCell::new(value),
135 135 py_shared_state: PySharedState::default(),
136 136 }
137 137 }
138 138
139 pub fn borrow(&self) -> Ref<T> {
139 pub fn borrow<'a>(&'a self, _py: Python<'a>) -> Ref<'a, T> {
140 140 // py_shared_state isn't involved since
141 141 // - inner.borrow() would fail if self is mutably borrowed,
142 142 // - and inner.borrow_mut() would fail while self is borrowed.
143 143 self.inner.borrow()
144 144 }
145 145
146 146 pub fn as_ptr(&self) -> *mut T {
147 147 self.inner.as_ptr()
148 148 }
149 149
150 150 // TODO: maybe this should be named as try_borrow_mut(), and use
151 151 // inner.try_borrow_mut(). The current implementation panics if
152 152 // self.inner has been borrowed, but returns error if py_shared_state
153 153 // refuses to borrow.
154 154 pub fn borrow_mut<'a>(
155 155 &'a self,
156 156 py: Python<'a>,
157 157 ) -> PyResult<PyRefMut<'a, T>> {
158 158 self.py_shared_state.borrow_mut(py, self.inner.borrow_mut())
159 159 }
160 160 }
161 161
162 162 /// Sharable data member of type `T` borrowed from the `PyObject`.
163 163 pub struct PySharedRef<'a, T> {
164 164 py: Python<'a>,
165 165 owner: &'a PyObject,
166 166 data: &'a PySharedRefCell<T>,
167 167 }
168 168
169 169 impl<'a, T> PySharedRef<'a, T> {
170 170 /// # Safety
171 171 ///
172 172 /// The `data` must be owned by the `owner`. Otherwise, the leak count
173 173 /// would get wrong.
174 174 pub unsafe fn new(
175 175 py: Python<'a>,
176 176 owner: &'a PyObject,
177 177 data: &'a PySharedRefCell<T>,
178 178 ) -> Self {
179 179 Self { py, owner, data }
180 180 }
181 181
182 182 pub fn borrow(&self) -> Ref<'a, T> {
183 self.data.borrow()
183 self.data.borrow(self.py)
184 184 }
185 185
186 186 pub fn borrow_mut(&self) -> PyResult<PyRefMut<'a, T>> {
187 187 self.data.borrow_mut(self.py)
188 188 }
189 189
190 190 /// Returns a leaked reference.
191 191 pub fn leak_immutable(&self) -> PyResult<PyLeakedRef<&'static T>> {
192 192 let state = &self.data.py_shared_state;
193 193 unsafe {
194 194 let (static_ref, static_state_ref) =
195 195 state.leak_immutable(self.py, self.data)?;
196 196 Ok(PyLeakedRef::new(
197 197 self.py,
198 198 self.owner,
199 199 static_ref,
200 200 static_state_ref,
201 201 ))
202 202 }
203 203 }
204 204 }
205 205
206 206 /// Holds a mutable reference to data shared between Python and Rust.
207 207 pub struct PyRefMut<'a, T> {
208 208 inner: RefMut<'a, T>,
209 209 py_shared_state: &'a PySharedState,
210 210 }
211 211
212 212 impl<'a, T> PyRefMut<'a, T> {
213 213 // Must be constructed by PySharedState after checking its leak_count.
214 214 // Otherwise, drop() would incorrectly update the state.
215 215 fn new(
216 216 _py: Python<'a>,
217 217 inner: RefMut<'a, T>,
218 218 py_shared_state: &'a PySharedState,
219 219 ) -> Self {
220 220 Self {
221 221 inner,
222 222 py_shared_state,
223 223 }
224 224 }
225 225 }
226 226
227 227 impl<'a, T> std::ops::Deref for PyRefMut<'a, T> {
228 228 type Target = RefMut<'a, T>;
229 229
230 230 fn deref(&self) -> &Self::Target {
231 231 &self.inner
232 232 }
233 233 }
234 234 impl<'a, T> std::ops::DerefMut for PyRefMut<'a, T> {
235 235 fn deref_mut(&mut self) -> &mut Self::Target {
236 236 &mut self.inner
237 237 }
238 238 }
239 239
240 240 impl<'a, T> Drop for PyRefMut<'a, T> {
241 241 fn drop(&mut self) {
242 242 let gil = Python::acquire_gil();
243 243 let py = gil.python();
244 244 unsafe {
245 245 self.py_shared_state.decrease_leak_count(py, true);
246 246 }
247 247 }
248 248 }
249 249
250 250 /// Allows a `py_class!` generated struct to share references to one of its
251 251 /// data members with Python.
252 252 ///
253 253 /// # Warning
254 254 ///
255 255 /// TODO allow Python container types: for now, integration with the garbage
256 256 /// collector does not extend to Rust structs holding references to Python
257 257 /// objects. Should the need surface, `__traverse__` and `__clear__` will
258 258 /// need to be written as per the `rust-cpython` docs on GC integration.
259 259 ///
260 260 /// # Parameters
261 261 ///
262 262 /// * `$name` is the same identifier used in for `py_class!` macro call.
263 263 /// * `$inner_struct` is the identifier of the underlying Rust struct
264 264 /// * `$data_member` is the identifier of the data member of `$inner_struct`
265 265 /// that will be shared.
266 266 /// * `$shared_accessor` is the function name to be generated, which allows
267 267 /// safe access to the data member.
268 268 ///
269 269 /// # Safety
270 270 ///
271 271 /// `$data_member` must persist while the `$name` object is alive. In other
272 272 /// words, it must be an accessor to a data field of the Python object.
273 273 ///
274 274 /// # Example
275 275 ///
276 276 /// ```
277 277 /// struct MyStruct {
278 278 /// inner: Vec<u32>;
279 279 /// }
280 280 ///
281 281 /// py_class!(pub class MyType |py| {
282 282 /// data inner: PySharedRefCell<MyStruct>;
283 283 /// });
284 284 ///
285 285 /// py_shared_ref!(MyType, MyStruct, inner, inner_shared);
286 286 /// ```
287 287 macro_rules! py_shared_ref {
288 288 (
289 289 $name: ident,
290 290 $inner_struct: ident,
291 291 $data_member: ident,
292 292 $shared_accessor: ident
293 293 ) => {
294 294 impl $name {
295 295 /// Returns a safe reference to the shared `$data_member`.
296 296 ///
297 297 /// This function guarantees that `PySharedRef` is created with
298 298 /// the valid `self` and `self.$data_member(py)` pair.
299 299 fn $shared_accessor<'a>(
300 300 &'a self,
301 301 py: Python<'a>,
302 302 ) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> {
303 303 use cpython::PythonObject;
304 304 use $crate::ref_sharing::PySharedRef;
305 305 let owner = self.as_object();
306 306 let data = self.$data_member(py);
307 307 unsafe { PySharedRef::new(py, owner, data) }
308 308 }
309 309 }
310 310 };
311 311 }
312 312
313 313 /// Manage immutable references to `PyObject` leaked into Python iterators.
314 314 pub struct PyLeakedRef<T> {
315 315 inner: PyObject,
316 316 data: Option<T>,
317 317 py_shared_state: &'static PySharedState,
318 318 }
319 319
320 320 // DO NOT implement Deref for PyLeakedRef<T>! Dereferencing PyLeakedRef
321 321 // without taking Python GIL wouldn't be safe.
322 322
323 323 impl<T> PyLeakedRef<T> {
324 324 /// # Safety
325 325 ///
326 326 /// The `py_shared_state` must be owned by the `inner` Python object.
327 327 // Marked as unsafe so client code wouldn't construct PyLeakedRef
328 328 // struct by mistake. Its drop() is unsafe.
329 329 pub unsafe fn new(
330 330 py: Python,
331 331 inner: &PyObject,
332 332 data: T,
333 333 py_shared_state: &'static PySharedState,
334 334 ) -> Self {
335 335 Self {
336 336 inner: inner.clone_ref(py),
337 337 data: Some(data),
338 338 py_shared_state,
339 339 }
340 340 }
341 341
342 342 /// Returns an immutable reference to the inner value.
343 343 pub fn get_ref<'a>(&'a self, _py: Python<'a>) -> &'a T {
344 344 self.data.as_ref().unwrap()
345 345 }
346 346
347 347 /// Returns a mutable reference to the inner value.
348 348 ///
349 349 /// Typically `T` is an iterator. If `T` is an immutable reference,
350 350 /// `get_mut()` is useless since the inner value can't be mutated.
351 351 pub fn get_mut<'a>(&'a mut self, _py: Python<'a>) -> &'a mut T {
352 352 self.data.as_mut().unwrap()
353 353 }
354 354
355 355 /// Converts the inner value by the given function.
356 356 ///
357 357 /// Typically `T` is a static reference to a container, and `U` is an
358 358 /// iterator of that container.
359 359 ///
360 360 /// # Safety
361 361 ///
362 362 /// The lifetime of the object passed in to the function `f` is cheated.
363 363 /// It's typically a static reference, but is valid only while the
364 364 /// corresponding `PyLeakedRef` is alive. Do not copy it out of the
365 365 /// function call.
366 366 pub unsafe fn map<U>(
367 367 mut self,
368 368 py: Python,
369 369 f: impl FnOnce(T) -> U,
370 370 ) -> PyLeakedRef<U> {
371 371 // f() could make the self.data outlive. That's why map() is unsafe.
372 372 // In order to make this function safe, maybe we'll need a way to
373 373 // temporarily restrict the lifetime of self.data and translate the
374 374 // returned object back to Something<'static>.
375 375 let new_data = f(self.data.take().unwrap());
376 376 PyLeakedRef {
377 377 inner: self.inner.clone_ref(py),
378 378 data: Some(new_data),
379 379 py_shared_state: self.py_shared_state,
380 380 }
381 381 }
382 382 }
383 383
384 384 impl<T> Drop for PyLeakedRef<T> {
385 385 fn drop(&mut self) {
386 386 // py_shared_state should be alive since we do have
387 387 // a Python reference to the owner object. Taking GIL makes
388 388 // sure that the state is only accessed by this thread.
389 389 let gil = Python::acquire_gil();
390 390 let py = gil.python();
391 391 if self.data.is_none() {
392 392 return; // moved to another PyLeakedRef
393 393 }
394 394 unsafe {
395 395 self.py_shared_state.decrease_leak_count(py, false);
396 396 }
397 397 }
398 398 }
399 399
400 400 /// Defines a `py_class!` that acts as a Python iterator over a Rust iterator.
401 401 ///
402 402 /// TODO: this is a bit awkward to use, and a better (more complicated)
403 403 /// procedural macro would simplify the interface a lot.
404 404 ///
405 405 /// # Parameters
406 406 ///
407 407 /// * `$name` is the identifier to give to the resulting Rust struct.
408 408 /// * `$leaked` corresponds to `$leaked` in the matching `py_shared_ref!` call.
409 409 /// * `$iterator_type` is the type of the Rust iterator.
410 410 /// * `$success_func` is a function for processing the Rust `(key, value)`
411 411 /// tuple on iteration success, turning it into something Python understands.
412 412 /// * `$success_func` is the return type of `$success_func`
413 413 ///
414 414 /// # Example
415 415 ///
416 416 /// ```
417 417 /// struct MyStruct {
418 418 /// inner: HashMap<Vec<u8>, Vec<u8>>;
419 419 /// }
420 420 ///
421 421 /// py_class!(pub class MyType |py| {
422 422 /// data inner: PySharedRefCell<MyStruct>;
423 423 ///
424 424 /// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
425 425 /// let leaked_ref = self.inner_shared(py).leak_immutable()?;
426 426 /// MyTypeItemsIterator::from_inner(
427 427 /// py,
428 428 /// unsafe { leaked_ref.map(py, |o| o.iter()) },
429 429 /// )
430 430 /// }
431 431 /// });
432 432 ///
433 433 /// impl MyType {
434 434 /// fn translate_key_value(
435 435 /// py: Python,
436 436 /// res: (&Vec<u8>, &Vec<u8>),
437 437 /// ) -> PyResult<Option<(PyBytes, PyBytes)>> {
438 438 /// let (f, entry) = res;
439 439 /// Ok(Some((
440 440 /// PyBytes::new(py, f),
441 441 /// PyBytes::new(py, entry),
442 442 /// )))
443 443 /// }
444 444 /// }
445 445 ///
446 446 /// py_shared_ref!(MyType, MyStruct, inner, MyTypeLeakedRef);
447 447 ///
448 448 /// py_shared_iterator!(
449 449 /// MyTypeItemsIterator,
450 450 /// PyLeakedRef<HashMap<'static, Vec<u8>, Vec<u8>>>,
451 451 /// MyType::translate_key_value,
452 452 /// Option<(PyBytes, PyBytes)>
453 453 /// );
454 454 /// ```
455 455 macro_rules! py_shared_iterator {
456 456 (
457 457 $name: ident,
458 458 $leaked: ty,
459 459 $success_func: expr,
460 460 $success_type: ty
461 461 ) => {
462 462 py_class!(pub class $name |py| {
463 463 data inner: RefCell<Option<$leaked>>;
464 464
465 465 def __next__(&self) -> PyResult<$success_type> {
466 466 let mut inner_opt = self.inner(py).borrow_mut();
467 467 if let Some(leaked) = inner_opt.as_mut() {
468 468 match leaked.get_mut(py).next() {
469 469 None => {
470 470 // replace Some(inner) by None, drop $leaked
471 471 inner_opt.take();
472 472 Ok(None)
473 473 }
474 474 Some(res) => {
475 475 $success_func(py, res)
476 476 }
477 477 }
478 478 } else {
479 479 Ok(None)
480 480 }
481 481 }
482 482
483 483 def __iter__(&self) -> PyResult<Self> {
484 484 Ok(self.clone_ref(py))
485 485 }
486 486 });
487 487
488 488 impl $name {
489 489 pub fn from_inner(
490 490 py: Python,
491 491 leaked: $leaked,
492 492 ) -> PyResult<Self> {
493 493 Self::create_instance(
494 494 py,
495 495 RefCell::new(Some(leaked)),
496 496 )
497 497 }
498 498 }
499 499 };
500 500 }
General Comments 0
You need to be logged in to leave comments. Login now