Skip to content

Commit

Permalink
python: expose jail iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Jan 2, 2019
1 parent 91ede01 commit 5d6e2b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bindings/python/jail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from ._jail import RunningJail, StoppedJail
from ._jail import RunningJail, StoppedJail, Jls

__all__ = ['RunningJail', 'StoppedJail']
__all__ = ['RunningJail', 'StoppedJail', 'Jls']
38 changes: 38 additions & 0 deletions bindings/python/src/jls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use pyo3::prelude::*;
use pyo3::{PyIterProtocol, PyObjectWithToken};

use jail as native;
use running::RunningJail;

#[pyclass]
pub struct Jls {
token: PyToken,
it: native::RunningJailIter,
}

#[pymethods]
impl Jls {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|token| Jls {
it: native::RunningJail::all(),
token,
})
}
}

#[pyproto]
impl PyIterProtocol for Jls {
fn __iter__(&mut self) -> PyResult<PyObject> {
self.it = native::RunningJail::all();
Ok(self.into())
}

fn __next__(&mut self) -> PyResult<Option<Py<RunningJail>>> {
match self.it.next() {
None => Ok(None),
Some(next) => Ok(Some(self.py().init(|token| RunningJail::create(token, next))?)
)
}
}
}
4 changes: 4 additions & 0 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ mod error;
mod param;
mod running;
mod stopped;
mod jls;

use child::Child;
use running::RunningJail;
use stopped::StoppedJail;
use jls::Jls;

#[pyclass]
struct JailError {
Expand All @@ -33,6 +36,7 @@ fn jail_modinit(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<RunningJail>()?;
m.add_class::<StoppedJail>()?;
m.add_class::<Child>()?;
m.add_class::<Jls>()?;

Ok(())
}

0 comments on commit 5d6e2b5

Please sign in to comment.