##// END OF EJS Templates
rust-cpython: drop self.leak_immutable() in favor of PySharedRef wrapper
Yuya Nishihara -
r43449:7d6758f2 default
parent child Browse files
Show More
@@ -1,131 +1,132 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.borrow_mut(py)?.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.borrow_mut(py)?.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 let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
95 let (leak_handle, leaked_ref) =
96 unsafe { self.inner_shared(py).leak_immutable()? };
96 97 DirsMultisetKeysIterator::from_inner(
97 98 py,
98 99 leak_handle,
99 100 leaked_ref.iter(),
100 101 )
101 102 }
102 103
103 104 def __contains__(&self, item: PyObject) -> PyResult<bool> {
104 105 Ok(self.inner(py).borrow().contains(HgPath::new(
105 106 item.extract::<PyBytes>(py)?.data(py).as_ref(),
106 107 )))
107 108 }
108 109 });
109 110
110 111 py_shared_ref!(Dirs, DirsMultiset, inner, inner_shared);
111 112
112 113 impl Dirs {
113 114 pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
114 115 Self::create_instance(py, PySharedRefCell::new(d))
115 116 }
116 117
117 118 fn translate_key(
118 119 py: Python,
119 120 res: &HgPathBuf,
120 121 ) -> PyResult<Option<PyBytes>> {
121 122 Ok(Some(PyBytes::new(py, res.as_ref())))
122 123 }
123 124 }
124 125
125 126 py_shared_iterator!(
126 127 DirsMultisetKeysIterator,
127 128 PyLeakedRef,
128 129 DirsMultisetIter<'static>,
129 130 Dirs::translate_key,
130 131 Option<PyBytes>
131 132 );
@@ -1,528 +1,533 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::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 use libc::c_char;
20 20
21 21 use crate::{
22 22 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
23 23 dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs},
24 24 ref_sharing::{PyLeakedRef, PySharedRefCell},
25 25 };
26 26 use hg::{
27 27 utils::hg_path::{HgPath, HgPathBuf},
28 28 DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
29 29 DirstateParents, DirstateParseError, EntryState, StateMapIter,
30 30 PARENT_SIZE,
31 31 };
32 32
33 33 // TODO
34 34 // This object needs to share references to multiple members of its Rust
35 35 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
36 36 // Right now `CopyMap` is done, but it needs to have an explicit reference
37 37 // to `RustDirstateMap` which itself needs to have an encapsulation for
38 38 // every method in `CopyMap` (copymapcopy, etc.).
39 39 // This is ugly and hard to maintain.
40 40 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
41 41 // `py_class!` is already implemented and does not mention
42 42 // `RustDirstateMap`, rightfully so.
43 43 // All attributes also have to have a separate refcount data attribute for
44 44 // leaks, with all methods that go along for reference sharing.
45 45 py_class!(pub class DirstateMap |py| {
46 46 data inner: PySharedRefCell<RustDirstateMap>;
47 47
48 48 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
49 49 let inner = RustDirstateMap::default();
50 50 Self::create_instance(
51 51 py,
52 52 PySharedRefCell::new(inner),
53 53 )
54 54 }
55 55
56 56 def clear(&self) -> PyResult<PyObject> {
57 57 self.borrow_mut(py)?.clear();
58 58 Ok(py.None())
59 59 }
60 60
61 61 def get(
62 62 &self,
63 63 key: PyObject,
64 64 default: Option<PyObject> = None
65 65 ) -> PyResult<Option<PyObject>> {
66 66 let key = key.extract::<PyBytes>(py)?;
67 67 match self.inner(py).borrow().get(HgPath::new(key.data(py))) {
68 68 Some(entry) => {
69 69 // Explicitly go through u8 first, then cast to
70 70 // platform-specific `c_char`.
71 71 let state: u8 = entry.state.into();
72 72 Ok(Some(decapsule_make_dirstate_tuple(py)?(
73 73 state as c_char,
74 74 entry.mode,
75 75 entry.size,
76 76 entry.mtime,
77 77 )))
78 78 },
79 79 None => Ok(default)
80 80 }
81 81 }
82 82
83 83 def addfile(
84 84 &self,
85 85 f: PyObject,
86 86 oldstate: PyObject,
87 87 state: PyObject,
88 88 mode: PyObject,
89 89 size: PyObject,
90 90 mtime: PyObject
91 91 ) -> PyResult<PyObject> {
92 92 self.borrow_mut(py)?.add_file(
93 93 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
94 94 oldstate.extract::<PyBytes>(py)?.data(py)[0]
95 95 .try_into()
96 96 .map_err(|e: DirstateParseError| {
97 97 PyErr::new::<exc::ValueError, _>(py, e.to_string())
98 98 })?,
99 99 DirstateEntry {
100 100 state: state.extract::<PyBytes>(py)?.data(py)[0]
101 101 .try_into()
102 102 .map_err(|e: DirstateParseError| {
103 103 PyErr::new::<exc::ValueError, _>(py, e.to_string())
104 104 })?,
105 105 mode: mode.extract(py)?,
106 106 size: size.extract(py)?,
107 107 mtime: mtime.extract(py)?,
108 108 },
109 109 );
110 110 Ok(py.None())
111 111 }
112 112
113 113 def removefile(
114 114 &self,
115 115 f: PyObject,
116 116 oldstate: PyObject,
117 117 size: PyObject
118 118 ) -> PyResult<PyObject> {
119 119 self.borrow_mut(py)?
120 120 .remove_file(
121 121 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
122 122 oldstate.extract::<PyBytes>(py)?.data(py)[0]
123 123 .try_into()
124 124 .map_err(|e: DirstateParseError| {
125 125 PyErr::new::<exc::ValueError, _>(py, e.to_string())
126 126 })?,
127 127 size.extract(py)?,
128 128 )
129 129 .or_else(|_| {
130 130 Err(PyErr::new::<exc::OSError, _>(
131 131 py,
132 132 "Dirstate error".to_string(),
133 133 ))
134 134 })?;
135 135 Ok(py.None())
136 136 }
137 137
138 138 def dropfile(
139 139 &self,
140 140 f: PyObject,
141 141 oldstate: PyObject
142 142 ) -> PyResult<PyBool> {
143 143 self.borrow_mut(py)?
144 144 .drop_file(
145 145 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
146 146 oldstate.extract::<PyBytes>(py)?.data(py)[0]
147 147 .try_into()
148 148 .map_err(|e: DirstateParseError| {
149 149 PyErr::new::<exc::ValueError, _>(py, e.to_string())
150 150 })?,
151 151 )
152 152 .and_then(|b| Ok(b.to_py_object(py)))
153 153 .or_else(|_| {
154 154 Err(PyErr::new::<exc::OSError, _>(
155 155 py,
156 156 "Dirstate error".to_string(),
157 157 ))
158 158 })
159 159 }
160 160
161 161 def clearambiguoustimes(
162 162 &self,
163 163 files: PyObject,
164 164 now: PyObject
165 165 ) -> PyResult<PyObject> {
166 166 let files: PyResult<Vec<HgPathBuf>> = files
167 167 .iter(py)?
168 168 .map(|filename| {
169 169 Ok(HgPathBuf::from_bytes(
170 170 filename?.extract::<PyBytes>(py)?.data(py),
171 171 ))
172 172 })
173 173 .collect();
174 174 self.borrow_mut(py)?
175 175 .clear_ambiguous_times(files?, now.extract(py)?);
176 176 Ok(py.None())
177 177 }
178 178
179 179 // TODO share the reference
180 180 def nonnormalentries(&self) -> PyResult<PyObject> {
181 181 let (non_normal, other_parent) =
182 182 self.inner(py).borrow().non_normal_other_parent_entries();
183 183
184 184 let locals = PyDict::new(py);
185 185 locals.set_item(
186 186 py,
187 187 "non_normal",
188 188 non_normal
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 locals.set_item(
195 195 py,
196 196 "other_parent",
197 197 other_parent
198 198 .iter()
199 199 .map(|v| PyBytes::new(py, v.as_ref()))
200 200 .collect::<Vec<PyBytes>>()
201 201 .to_py_object(py),
202 202 )?;
203 203
204 204 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
205 205 }
206 206
207 207 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
208 208 let d = d.extract::<PyBytes>(py)?;
209 209 Ok(self.borrow_mut(py)?
210 210 .has_tracked_dir(HgPath::new(d.data(py)))
211 211 .to_py_object(py))
212 212 }
213 213
214 214 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
215 215 let d = d.extract::<PyBytes>(py)?;
216 216 Ok(self.borrow_mut(py)?
217 217 .has_dir(HgPath::new(d.data(py)))
218 218 .to_py_object(py))
219 219 }
220 220
221 221 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
222 222 self.borrow_mut(py)?
223 223 .parents(st.extract::<PyBytes>(py)?.data(py))
224 224 .and_then(|d| {
225 225 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
226 226 .to_py_object(py))
227 227 })
228 228 .or_else(|_| {
229 229 Err(PyErr::new::<exc::OSError, _>(
230 230 py,
231 231 "Dirstate error".to_string(),
232 232 ))
233 233 })
234 234 }
235 235
236 236 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
237 237 let p1 = extract_node_id(py, &p1)?;
238 238 let p2 = extract_node_id(py, &p2)?;
239 239
240 240 self.borrow_mut(py)?
241 241 .set_parents(&DirstateParents { p1, p2 });
242 242 Ok(py.None())
243 243 }
244 244
245 245 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
246 246 match self.borrow_mut(py)?
247 247 .read(st.extract::<PyBytes>(py)?.data(py))
248 248 {
249 249 Ok(Some(parents)) => Ok(Some(
250 250 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
251 251 .to_py_object(py)
252 252 .into_object(),
253 253 )),
254 254 Ok(None) => Ok(Some(py.None())),
255 255 Err(_) => Err(PyErr::new::<exc::OSError, _>(
256 256 py,
257 257 "Dirstate error".to_string(),
258 258 )),
259 259 }
260 260 }
261 261 def write(
262 262 &self,
263 263 p1: PyObject,
264 264 p2: PyObject,
265 265 now: PyObject
266 266 ) -> PyResult<PyBytes> {
267 267 let now = Duration::new(now.extract(py)?, 0);
268 268 let parents = DirstateParents {
269 269 p1: extract_node_id(py, &p1)?,
270 270 p2: extract_node_id(py, &p2)?,
271 271 };
272 272
273 273 match self.borrow_mut(py)?.pack(parents, now) {
274 274 Ok(packed) => Ok(PyBytes::new(py, &packed)),
275 275 Err(_) => Err(PyErr::new::<exc::OSError, _>(
276 276 py,
277 277 "Dirstate error".to_string(),
278 278 )),
279 279 }
280 280 }
281 281
282 282 def filefoldmapasdict(&self) -> PyResult<PyDict> {
283 283 let dict = PyDict::new(py);
284 284 for (key, value) in self.borrow_mut(py)?.build_file_fold_map().iter() {
285 285 dict.set_item(py, key.as_ref().to_vec(), value.as_ref().to_vec())?;
286 286 }
287 287 Ok(dict)
288 288 }
289 289
290 290 def __len__(&self) -> PyResult<usize> {
291 291 Ok(self.inner(py).borrow().len())
292 292 }
293 293
294 294 def __contains__(&self, key: PyObject) -> PyResult<bool> {
295 295 let key = key.extract::<PyBytes>(py)?;
296 296 Ok(self.inner(py).borrow().contains_key(HgPath::new(key.data(py))))
297 297 }
298 298
299 299 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
300 300 let key = key.extract::<PyBytes>(py)?;
301 301 let key = HgPath::new(key.data(py));
302 302 match self.inner(py).borrow().get(key) {
303 303 Some(entry) => {
304 304 // Explicitly go through u8 first, then cast to
305 305 // platform-specific `c_char`.
306 306 let state: u8 = entry.state.into();
307 307 Ok(decapsule_make_dirstate_tuple(py)?(
308 308 state as c_char,
309 309 entry.mode,
310 310 entry.size,
311 311 entry.mtime,
312 312 ))
313 313 },
314 314 None => Err(PyErr::new::<exc::KeyError, _>(
315 315 py,
316 316 String::from_utf8_lossy(key.as_bytes()),
317 317 )),
318 318 }
319 319 }
320 320
321 321 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
322 let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
322 let (leak_handle, leaked_ref) =
323 unsafe { self.inner_shared(py).leak_immutable()? };
323 324 DirstateMapKeysIterator::from_inner(
324 325 py,
325 326 leak_handle,
326 327 leaked_ref.iter(),
327 328 )
328 329 }
329 330
330 331 def items(&self) -> PyResult<DirstateMapItemsIterator> {
331 let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
332 let (leak_handle, leaked_ref) =
333 unsafe { self.inner_shared(py).leak_immutable()? };
332 334 DirstateMapItemsIterator::from_inner(
333 335 py,
334 336 leak_handle,
335 337 leaked_ref.iter(),
336 338 )
337 339 }
338 340
339 341 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
340 let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
342 let (leak_handle, leaked_ref) =
343 unsafe { self.inner_shared(py).leak_immutable()? };
341 344 DirstateMapKeysIterator::from_inner(
342 345 py,
343 346 leak_handle,
344 347 leaked_ref.iter(),
345 348 )
346 349 }
347 350
348 351 def getdirs(&self) -> PyResult<Dirs> {
349 352 // TODO don't copy, share the reference
350 353 self.borrow_mut(py)?.set_dirs();
351 354 Dirs::from_inner(
352 355 py,
353 356 DirsMultiset::from_dirstate(
354 357 &self.inner(py).borrow(),
355 358 Some(EntryState::Removed),
356 359 ),
357 360 )
358 361 }
359 362 def getalldirs(&self) -> PyResult<Dirs> {
360 363 // TODO don't copy, share the reference
361 364 self.borrow_mut(py)?.set_all_dirs();
362 365 Dirs::from_inner(
363 366 py,
364 367 DirsMultiset::from_dirstate(
365 368 &self.inner(py).borrow(),
366 369 None,
367 370 ),
368 371 )
369 372 }
370 373
371 374 // TODO all copymap* methods, see docstring above
372 375 def copymapcopy(&self) -> PyResult<PyDict> {
373 376 let dict = PyDict::new(py);
374 377 for (key, value) in self.inner(py).borrow().copy_map.iter() {
375 378 dict.set_item(
376 379 py,
377 380 PyBytes::new(py, key.as_ref()),
378 381 PyBytes::new(py, value.as_ref()),
379 382 )?;
380 383 }
381 384 Ok(dict)
382 385 }
383 386
384 387 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
385 388 let key = key.extract::<PyBytes>(py)?;
386 389 match self.inner(py).borrow().copy_map.get(HgPath::new(key.data(py))) {
387 390 Some(copy) => Ok(PyBytes::new(py, copy.as_ref())),
388 391 None => Err(PyErr::new::<exc::KeyError, _>(
389 392 py,
390 393 String::from_utf8_lossy(key.data(py)),
391 394 )),
392 395 }
393 396 }
394 397 def copymap(&self) -> PyResult<CopyMap> {
395 398 CopyMap::from_inner(py, self.clone_ref(py))
396 399 }
397 400
398 401 def copymaplen(&self) -> PyResult<usize> {
399 402 Ok(self.inner(py).borrow().copy_map.len())
400 403 }
401 404 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
402 405 let key = key.extract::<PyBytes>(py)?;
403 406 Ok(self
404 407 .inner(py)
405 408 .borrow()
406 409 .copy_map
407 410 .contains_key(HgPath::new(key.data(py))))
408 411 }
409 412 def copymapget(
410 413 &self,
411 414 key: PyObject,
412 415 default: Option<PyObject>
413 416 ) -> PyResult<Option<PyObject>> {
414 417 let key = key.extract::<PyBytes>(py)?;
415 418 match self
416 419 .inner(py)
417 420 .borrow()
418 421 .copy_map
419 422 .get(HgPath::new(key.data(py)))
420 423 {
421 424 Some(copy) => Ok(Some(
422 425 PyBytes::new(py, copy.as_ref()).into_object(),
423 426 )),
424 427 None => Ok(default),
425 428 }
426 429 }
427 430 def copymapsetitem(
428 431 &self,
429 432 key: PyObject,
430 433 value: PyObject
431 434 ) -> PyResult<PyObject> {
432 435 let key = key.extract::<PyBytes>(py)?;
433 436 let value = value.extract::<PyBytes>(py)?;
434 437 self.borrow_mut(py)?.copy_map.insert(
435 438 HgPathBuf::from_bytes(key.data(py)),
436 439 HgPathBuf::from_bytes(value.data(py)),
437 440 );
438 441 Ok(py.None())
439 442 }
440 443 def copymappop(
441 444 &self,
442 445 key: PyObject,
443 446 default: Option<PyObject>
444 447 ) -> PyResult<Option<PyObject>> {
445 448 let key = key.extract::<PyBytes>(py)?;
446 449 match self
447 450 .borrow_mut(py)?
448 451 .copy_map
449 452 .remove(HgPath::new(key.data(py)))
450 453 {
451 454 Some(_) => Ok(None),
452 455 None => Ok(default),
453 456 }
454 457 }
455 458
456 459 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
457 let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
460 let (leak_handle, leaked_ref) =
461 unsafe { self.inner_shared(py).leak_immutable()? };
458 462 CopyMapKeysIterator::from_inner(
459 463 py,
460 464 leak_handle,
461 465 leaked_ref.copy_map.iter(),
462 466 )
463 467 }
464 468
465 469 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
466 let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
470 let (leak_handle, leaked_ref) =
471 unsafe { self.inner_shared(py).leak_immutable()? };
467 472 CopyMapItemsIterator::from_inner(
468 473 py,
469 474 leak_handle,
470 475 leaked_ref.copy_map.iter(),
471 476 )
472 477 }
473 478
474 479 });
475 480
476 481 impl DirstateMap {
477 482 fn translate_key(
478 483 py: Python,
479 484 res: (&HgPathBuf, &DirstateEntry),
480 485 ) -> PyResult<Option<PyBytes>> {
481 486 Ok(Some(PyBytes::new(py, res.0.as_ref())))
482 487 }
483 488 fn translate_key_value(
484 489 py: Python,
485 490 res: (&HgPathBuf, &DirstateEntry),
486 491 ) -> PyResult<Option<(PyBytes, PyObject)>> {
487 492 let (f, entry) = res;
488 493
489 494 // Explicitly go through u8 first, then cast to
490 495 // platform-specific `c_char`.
491 496 let state: u8 = entry.state.into();
492 497 Ok(Some((
493 498 PyBytes::new(py, f.as_ref()),
494 499 decapsule_make_dirstate_tuple(py)?(
495 500 state as c_char,
496 501 entry.mode,
497 502 entry.size,
498 503 entry.mtime,
499 504 ),
500 505 )))
501 506 }
502 507 }
503 508
504 509 py_shared_ref!(DirstateMap, RustDirstateMap, inner, inner_shared);
505 510
506 511 py_shared_iterator!(
507 512 DirstateMapKeysIterator,
508 513 PyLeakedRef,
509 514 StateMapIter<'static>,
510 515 DirstateMap::translate_key,
511 516 Option<PyBytes>
512 517 );
513 518
514 519 py_shared_iterator!(
515 520 DirstateMapItemsIterator,
516 521 PyLeakedRef,
517 522 StateMapIter<'static>,
518 523 DirstateMap::translate_key_value,
519 524 Option<(PyBytes, PyObject)>
520 525 );
521 526
522 527 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
523 528 let bytes = obj.extract::<PyBytes>(py)?;
524 529 match bytes.data(py).try_into() {
525 530 Ok(s) => Ok(s),
526 531 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
527 532 }
528 533 }
@@ -1,480 +1,473 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 139 pub fn borrow(&self) -> Ref<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<T> {
183 183 self.data.borrow()
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 and its management object.
191 191 ///
192 192 /// # Safety
193 193 ///
194 194 /// It's up to you to make sure that the management object lives
195 195 /// longer than the leaked reference. Otherwise, you'll get a
196 196 /// dangling reference.
197 197 pub unsafe fn leak_immutable(
198 198 &self,
199 199 ) -> PyResult<(PyLeakedRef, &'static T)> {
200 200 let (static_ref, static_state_ref) = self
201 201 .data
202 202 .py_shared_state
203 203 .leak_immutable(self.py, self.data)?;
204 204 let leak_handle =
205 205 PyLeakedRef::new(self.py, self.owner, static_state_ref);
206 206 Ok((leak_handle, static_ref))
207 207 }
208 208 }
209 209
210 210 /// Holds a mutable reference to data shared between Python and Rust.
211 211 pub struct PyRefMut<'a, T> {
212 212 inner: RefMut<'a, T>,
213 213 py_shared_state: &'a PySharedState,
214 214 }
215 215
216 216 impl<'a, T> PyRefMut<'a, T> {
217 217 // Must be constructed by PySharedState after checking its leak_count.
218 218 // Otherwise, drop() would incorrectly update the state.
219 219 fn new(
220 220 _py: Python<'a>,
221 221 inner: RefMut<'a, T>,
222 222 py_shared_state: &'a PySharedState,
223 223 ) -> Self {
224 224 Self {
225 225 inner,
226 226 py_shared_state,
227 227 }
228 228 }
229 229 }
230 230
231 231 impl<'a, T> std::ops::Deref for PyRefMut<'a, T> {
232 232 type Target = RefMut<'a, T>;
233 233
234 234 fn deref(&self) -> &Self::Target {
235 235 &self.inner
236 236 }
237 237 }
238 238 impl<'a, T> std::ops::DerefMut for PyRefMut<'a, T> {
239 239 fn deref_mut(&mut self) -> &mut Self::Target {
240 240 &mut self.inner
241 241 }
242 242 }
243 243
244 244 impl<'a, T> Drop for PyRefMut<'a, T> {
245 245 fn drop(&mut self) {
246 246 let gil = Python::acquire_gil();
247 247 let py = gil.python();
248 248 unsafe {
249 249 self.py_shared_state.decrease_leak_count(py, true);
250 250 }
251 251 }
252 252 }
253 253
254 254 /// Allows a `py_class!` generated struct to share references to one of its
255 255 /// data members with Python.
256 256 ///
257 257 /// # Warning
258 258 ///
259 259 /// TODO allow Python container types: for now, integration with the garbage
260 260 /// collector does not extend to Rust structs holding references to Python
261 261 /// objects. Should the need surface, `__traverse__` and `__clear__` will
262 262 /// need to be written as per the `rust-cpython` docs on GC integration.
263 263 ///
264 264 /// # Parameters
265 265 ///
266 266 /// * `$name` is the same identifier used in for `py_class!` macro call.
267 267 /// * `$inner_struct` is the identifier of the underlying Rust struct
268 268 /// * `$data_member` is the identifier of the data member of `$inner_struct`
269 269 /// that will be shared.
270 270 /// * `$shared_accessor` is the function name to be generated, which allows
271 271 /// safe access to the data member.
272 272 ///
273 273 /// # Safety
274 274 ///
275 275 /// `$data_member` must persist while the `$name` object is alive. In other
276 276 /// words, it must be an accessor to a data field of the Python object.
277 277 ///
278 278 /// # Example
279 279 ///
280 280 /// ```
281 281 /// struct MyStruct {
282 282 /// inner: Vec<u32>;
283 283 /// }
284 284 ///
285 285 /// py_class!(pub class MyType |py| {
286 286 /// data inner: PySharedRefCell<MyStruct>;
287 287 /// });
288 288 ///
289 289 /// py_shared_ref!(MyType, MyStruct, inner, inner_shared);
290 290 /// ```
291 291 macro_rules! py_shared_ref {
292 292 (
293 293 $name: ident,
294 294 $inner_struct: ident,
295 295 $data_member: ident,
296 296 $shared_accessor: ident
297 297 ) => {
298 298 impl $name {
299 299 /// Returns a safe reference to the shared `$data_member`.
300 300 ///
301 301 /// This function guarantees that `PySharedRef` is created with
302 302 /// the valid `self` and `self.$data_member(py)` pair.
303 303 fn $shared_accessor<'a>(
304 304 &'a self,
305 305 py: Python<'a>,
306 306 ) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> {
307 307 use cpython::PythonObject;
308 308 use $crate::ref_sharing::PySharedRef;
309 309 let owner = self.as_object();
310 310 let data = self.$data_member(py);
311 311 unsafe { PySharedRef::new(py, owner, data) }
312 312 }
313 313
314 314 // TODO: remove this function in favor of $shared_accessor(py)
315 315 fn borrow_mut<'a>(
316 316 &'a self,
317 317 py: Python<'a>,
318 318 ) -> PyResult<crate::ref_sharing::PyRefMut<'a, $inner_struct>>
319 319 {
320 320 self.$shared_accessor(py).borrow_mut()
321 321 }
322
323 // TODO: remove this function in favor of $shared_accessor(py)
324 unsafe fn leak_immutable<'a>(
325 &'a self,
326 py: Python<'a>,
327 ) -> PyResult<(PyLeakedRef, &'static $inner_struct)> {
328 self.$shared_accessor(py).leak_immutable()
329 }
330 322 }
331 323 };
332 324 }
333 325
334 326 /// Manage immutable references to `PyObject` leaked into Python iterators.
335 327 ///
336 328 /// In truth, this does not represent leaked references themselves;
337 329 /// it is instead useful alongside them to manage them.
338 330 pub struct PyLeakedRef {
339 331 _inner: PyObject,
340 332 py_shared_state: &'static PySharedState,
341 333 }
342 334
343 335 impl PyLeakedRef {
344 336 /// # Safety
345 337 ///
346 338 /// The `py_shared_state` must be owned by the `inner` Python object.
347 339 // Marked as unsafe so client code wouldn't construct PyLeakedRef
348 340 // struct by mistake. Its drop() is unsafe.
349 341 pub unsafe fn new(
350 342 py: Python,
351 343 inner: &PyObject,
352 344 py_shared_state: &'static PySharedState,
353 345 ) -> Self {
354 346 Self {
355 347 _inner: inner.clone_ref(py),
356 348 py_shared_state,
357 349 }
358 350 }
359 351 }
360 352
361 353 impl Drop for PyLeakedRef {
362 354 fn drop(&mut self) {
363 355 // py_shared_state should be alive since we do have
364 356 // a Python reference to the owner object. Taking GIL makes
365 357 // sure that the state is only accessed by this thread.
366 358 let gil = Python::acquire_gil();
367 359 let py = gil.python();
368 360 unsafe {
369 361 self.py_shared_state.decrease_leak_count(py, false);
370 362 }
371 363 }
372 364 }
373 365
374 366 /// Defines a `py_class!` that acts as a Python iterator over a Rust iterator.
375 367 ///
376 368 /// TODO: this is a bit awkward to use, and a better (more complicated)
377 369 /// procedural macro would simplify the interface a lot.
378 370 ///
379 371 /// # Parameters
380 372 ///
381 373 /// * `$name` is the identifier to give to the resulting Rust struct.
382 374 /// * `$leaked` corresponds to `$leaked` in the matching `py_shared_ref!` call.
383 375 /// * `$iterator_type` is the type of the Rust iterator.
384 376 /// * `$success_func` is a function for processing the Rust `(key, value)`
385 377 /// tuple on iteration success, turning it into something Python understands.
386 378 /// * `$success_func` is the return type of `$success_func`
387 379 ///
388 380 /// # Example
389 381 ///
390 382 /// ```
391 383 /// struct MyStruct {
392 384 /// inner: HashMap<Vec<u8>, Vec<u8>>;
393 385 /// }
394 386 ///
395 387 /// py_class!(pub class MyType |py| {
396 388 /// data inner: PySharedRefCell<MyStruct>;
397 389 ///
398 390 /// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
399 /// let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? };
391 /// let (leak_handle, leaked_ref) =
392 /// unsafe { self.inner_shared(py).leak_immutable()? };
400 393 /// MyTypeItemsIterator::from_inner(
401 394 /// py,
402 395 /// leak_handle,
403 396 /// leaked_ref.iter(),
404 397 /// )
405 398 /// }
406 399 /// });
407 400 ///
408 401 /// impl MyType {
409 402 /// fn translate_key_value(
410 403 /// py: Python,
411 404 /// res: (&Vec<u8>, &Vec<u8>),
412 405 /// ) -> PyResult<Option<(PyBytes, PyBytes)>> {
413 406 /// let (f, entry) = res;
414 407 /// Ok(Some((
415 408 /// PyBytes::new(py, f),
416 409 /// PyBytes::new(py, entry),
417 410 /// )))
418 411 /// }
419 412 /// }
420 413 ///
421 414 /// py_shared_ref!(MyType, MyStruct, inner, MyTypeLeakedRef);
422 415 ///
423 416 /// py_shared_iterator!(
424 417 /// MyTypeItemsIterator,
425 418 /// PyLeakedRef,
426 419 /// HashMap<'static, Vec<u8>, Vec<u8>>,
427 420 /// MyType::translate_key_value,
428 421 /// Option<(PyBytes, PyBytes)>
429 422 /// );
430 423 /// ```
431 424 macro_rules! py_shared_iterator {
432 425 (
433 426 $name: ident,
434 427 $leaked: ty,
435 428 $iterator_type: ty,
436 429 $success_func: expr,
437 430 $success_type: ty
438 431 ) => {
439 432 py_class!(pub class $name |py| {
440 433 data inner: RefCell<Option<$leaked>>;
441 434 data it: RefCell<$iterator_type>;
442 435
443 436 def __next__(&self) -> PyResult<$success_type> {
444 437 let mut inner_opt = self.inner(py).borrow_mut();
445 438 if inner_opt.is_some() {
446 439 match self.it(py).borrow_mut().next() {
447 440 None => {
448 441 // replace Some(inner) by None, drop $leaked
449 442 inner_opt.take();
450 443 Ok(None)
451 444 }
452 445 Some(res) => {
453 446 $success_func(py, res)
454 447 }
455 448 }
456 449 } else {
457 450 Ok(None)
458 451 }
459 452 }
460 453
461 454 def __iter__(&self) -> PyResult<Self> {
462 455 Ok(self.clone_ref(py))
463 456 }
464 457 });
465 458
466 459 impl $name {
467 460 pub fn from_inner(
468 461 py: Python,
469 462 leaked: $leaked,
470 463 it: $iterator_type
471 464 ) -> PyResult<Self> {
472 465 Self::create_instance(
473 466 py,
474 467 RefCell::new(Some(leaked)),
475 468 RefCell::new(it)
476 469 )
477 470 }
478 471 }
479 472 };
480 473 }
General Comments 0
You need to be logged in to leave comments. Login now