Skip to content

Commit

Permalink
chore[rust]: Deprecate Python:acquire_gil and use Python::with_gil (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ghuls authored Aug 18, 2022
1 parent 08c7ceb commit ec6ec1b
Show file tree
Hide file tree
Showing 7 changed files with 906 additions and 922 deletions.
145 changes: 67 additions & 78 deletions py-polars/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,40 +470,39 @@ impl<'s> FromPyObject<'s> for Wrap<AnyValue<'s>> {
} else if let Ok(v) = ob.extract::<&'s str>() {
Ok(AnyValue::Utf8(v).into())
} else if ob.get_type().name()?.contains("datetime") {
let gil = Python::acquire_gil();
let py = gil.python();

// windows
#[cfg(target_arch = "windows")]
{
let kwargs = PyDict::new(py);
kwargs.set_item("tzinfo", py.None())?;
let dt = ob.call_method("replace", (), Some(kwargs))?;

let pytz = PyModule::import(py, "pytz")?;
let tz = pytz.call_method("timezone", ("UTC",), None)?;
let kwargs = PyDict::new(py);
kwargs.set_item("is_dst", py.None())?;
let loc_tz = tz.call_method("localize", (dt,), Some(kwargs))?;
loc_tz.call_method0("timestamp")?;
// s to us
let v = (ts.extract::<f64>()? * 1000_000.0) as i64;
Ok(AnyValue::Datetime(v, TimeUnit::Microseconds, &None).into())
}
// unix
#[cfg(not(target_arch = "windows"))]
{
let datetime = PyModule::import(py, "datetime")?;
let timezone = datetime.getattr("timezone")?;
let kwargs = PyDict::new(py);
kwargs.set_item("tzinfo", timezone.getattr("utc")?)?;
let dt = ob.call_method("replace", (), Some(kwargs))?;
let ts = dt.call_method0("timestamp")?;
// s to us
let v = (ts.extract::<f64>()? * 1_000_000.0) as i64;
// we choose us as that is pythons default unit
Ok(AnyValue::Datetime(v, TimeUnit::Microseconds, &None).into())
}
Python::with_gil(|py| {
// windows
#[cfg(target_arch = "windows")]
{
let kwargs = PyDict::new(py);
kwargs.set_item("tzinfo", py.None())?;
let dt = ob.call_method("replace", (), Some(kwargs))?;

let pytz = PyModule::import(py, "pytz")?;
let tz = pytz.call_method("timezone", ("UTC",), None)?;
let kwargs = PyDict::new(py);
kwargs.set_item("is_dst", py.None())?;
let loc_tz = tz.call_method("localize", (dt,), Some(kwargs))?;
loc_tz.call_method0("timestamp")?;
// s to us
let v = (ts.extract::<f64>()? * 1000_000.0) as i64;
Ok(AnyValue::Datetime(v, TimeUnit::Microseconds, &None).into())
}
// unix
#[cfg(not(target_arch = "windows"))]
{
let datetime = PyModule::import(py, "datetime")?;
let timezone = datetime.getattr("timezone")?;
let kwargs = PyDict::new(py);
kwargs.set_item("tzinfo", timezone.getattr("utc")?)?;
let dt = ob.call_method("replace", (), Some(kwargs))?;
let ts = dt.call_method0("timestamp")?;
// s to us
let v = (ts.extract::<f64>()? * 1_000_000.0) as i64;
// we choose us as that is pythons default unit
Ok(AnyValue::Datetime(v, TimeUnit::Microseconds, &None).into())
}
})
} else if ob.is_none() {
Ok(AnyValue::Null.into())
} else if ob.is_instance_of::<PyDict>()? {
Expand Down Expand Up @@ -532,25 +531,25 @@ impl<'s> FromPyObject<'s> for Wrap<AnyValue<'s>> {
let series = py_pyseries.extract::<PySeries>().unwrap().series;
Ok(Wrap(AnyValue::List(series)))
} else if ob.get_type().name()?.contains("date") {
let gil = Python::acquire_gil();
let py = gil.python();
let date = py_modules::UTILS
.getattr(py, "_date_to_pl_date")
.unwrap()
.call1(py, (ob,))
.unwrap();
let v = date.extract::<i32>(py).unwrap();
Ok(Wrap(AnyValue::Date(v)))
Python::with_gil(|py| {
let date = py_modules::UTILS
.getattr(py, "_date_to_pl_date")
.unwrap()
.call1(py, (ob,))
.unwrap();
let v = date.extract::<i32>(py).unwrap();
Ok(Wrap(AnyValue::Date(v)))
})
} else if ob.get_type().name()?.contains("timedelta") {
let gil = Python::acquire_gil();
let py = gil.python();
let td = py_modules::UTILS
.getattr(py, "_timedelta_to_pl_timedelta")
.unwrap()
.call1(py, (ob, "us"))
.unwrap();
let v = td.extract::<i64>(py).unwrap();
Ok(Wrap(AnyValue::Duration(v, TimeUnit::Microseconds)))
Python::with_gil(|py| {
let td = py_modules::UTILS
.getattr(py, "_timedelta_to_pl_timedelta")
.unwrap()
.call1(py, (ob, "us"))
.unwrap();
let v = td.extract::<i64>(py).unwrap();
Ok(Wrap(AnyValue::Duration(v, TimeUnit::Microseconds)))
})
} else {
Err(PyErr::from(PyPolarsErr::Other(format!(
"row type not supported {:?}",
Expand Down Expand Up @@ -604,13 +603,7 @@ pub struct ObjectValue {

impl Hash for ObjectValue {
fn hash<H: Hasher>(&self, state: &mut H) {
let gil = Python::acquire_gil();
let python = gil.python();
let h = self
.inner
.as_ref(python)
.hash()
.expect("should be hashable");
let h = Python::with_gil(|py| self.inner.as_ref(py).hash().expect("should be hashable"));
state.write_isize(h)
}
}
Expand All @@ -619,16 +612,16 @@ impl Eq for ObjectValue {}

impl PartialEq for ObjectValue {
fn eq(&self, other: &Self) -> bool {
let gil = Python::acquire_gil();
let py = gil.python();
match self
.inner
.as_ref(py)
.rich_compare(other.inner.as_ref(py), CompareOp::Eq)
{
Ok(result) => result.is_true().unwrap(),
Err(_) => false,
}
Python::with_gil(|py| {
match self
.inner
.as_ref(py)
.rich_compare(other.inner.as_ref(py), CompareOp::Eq)
{
Ok(result) => result.is_true().unwrap(),
Err(_) => false,
}
})
}
}

Expand All @@ -652,10 +645,10 @@ impl From<PyObject> for ObjectValue {

impl<'a> FromPyObject<'a> for ObjectValue {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
let gil = Python::acquire_gil();
let python = gil.python();
Ok(ObjectValue {
inner: ob.to_object(python),
Python::with_gil(|py| {
Ok(ObjectValue {
inner: ob.to_object(py),
})
})
}
}
Expand All @@ -677,11 +670,7 @@ impl ToPyObject for ObjectValue {

impl Default for ObjectValue {
fn default() -> Self {
let gil = Python::acquire_gil();
let python = gil.python();
ObjectValue {
inner: python.None(),
}
Python::with_gil(|py| ObjectValue { inner: py.None() })
}
}

Expand Down
Loading

0 comments on commit ec6ec1b

Please sign in to comment.