Skip to content

Commit

Permalink
Use ::std to fix #233
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Sep 26, 2018
1 parent af68396 commit c1cb5ca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
2 changes: 2 additions & 0 deletions examples/rustapi_module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#[macro_use]
extern crate pyo3;

pub mod othermod;

use pyo3::prelude::*;
use pyo3::types::{
PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyDict, PyTime, PyTimeAccess,
Expand Down
29 changes: 29 additions & 0 deletions examples/rustapi_module/src/othermod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! https://github.com/PyO3/pyo3/issues/233
//!
//! The code below just tries to use the most important code generation paths

use pyo3::prelude::*;

#[pyclass]
pub struct ModClass {
_somefield: String,
}

#[pymethods]
impl ModClass {
fn noop(&self, x: usize) -> usize {
x
}
}

#[pyfunction]
fn double(x: i32) -> i32 {
x*2
}

#[pymodinit]
fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_function!(double))?;
m.add_class::<ModClass>()?;
Ok(())
}
22 changes: 11 additions & 11 deletions pyo3-derive-backend/src/py_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ fn impl_class(
self.#token.py()
}
}
impl<'a> std::convert::From<&'a mut #cls> for &'a #cls
impl<'a> ::std::convert::From<&'a mut #cls> for &'a #cls
{
fn from(ob: &'a mut #cls) -> Self {
unsafe{std::mem::transmute(ob)}
}
}
impl std::fmt::Debug for #cls {
fn fmt(&self, f : &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
impl ::std::fmt::Debug for #cls {
fn fmt(&self, f : &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
use ::pyo3::ObjectProtocol;
let s = self.repr().map_err(|_| std::fmt::Error)?;
let s = self.repr().map_err(|_| ::std::fmt::Error)?;
f.write_str(&s.to_string_lossy())
}
}
impl std::fmt::Display for #cls {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
impl ::std::fmt::Display for #cls {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
use ::pyo3::ObjectProtocol;
let s = self.str().map_err(|_| std::fmt::Error)?;
let s = self.str().map_err(|_| ::std::fmt::Error)?;
f.write_str(&s.to_string_lossy())
}
}
Expand Down Expand Up @@ -186,14 +186,14 @@ fn impl_class(

const SIZE: usize = {
Self::OFFSET as usize +
std::mem::size_of::<#cls>() + #weakref + #dict
::std::mem::size_of::<#cls>() + #weakref + #dict
};
const OFFSET: isize = {
// round base_size up to next multiple of align
(
(<#base as ::pyo3::typeob::PyTypeInfo>::SIZE +
std::mem::align_of::<#cls>() - 1) /
std::mem::align_of::<#cls>() * std::mem::align_of::<#cls>()
::std::mem::align_of::<#cls>() - 1) /
::std::mem::align_of::<#cls>() * ::std::mem::align_of::<#cls>()
) as isize
};

Expand All @@ -207,7 +207,7 @@ fn impl_class(
impl ::pyo3::typeob::PyTypeObject for #cls {
#[inline]
fn init_type() {
static START: std::sync::Once = std::sync::ONCE_INIT;
static START: ::std::sync::Once = ::std::sync::ONCE_INIT;
START.call_once(|| {
let ty = unsafe{<#cls as ::pyo3::typeob::PyTypeInfo>::type_object()};

Expand Down
6 changes: 3 additions & 3 deletions pyo3-derive-backend/src/py_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn impl_wrap_new(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec) -> Token
_args: *mut ::pyo3::ffi::PyObject,
_kwargs: *mut ::pyo3::ffi::PyObject) -> *mut ::pyo3::ffi::PyObject
{
use pyo3::typeob::PyTypeInfo;
use ::pyo3::typeob::PyTypeInfo;

const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = ::pyo3::GILPool::new();
Expand Down Expand Up @@ -307,7 +307,7 @@ pub fn impl_wrap_static(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec) -> To
pub(crate) fn impl_wrap_getter(cls: &syn::Type, name: &syn::Ident) -> TokenStream {
quote! {
unsafe extern "C" fn __wrap(
_slf: *mut ::pyo3::ffi::PyObject, _: *mut std::os::raw::c_void) -> *mut ::pyo3::ffi::PyObject
_slf: *mut ::pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> *mut ::pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");

Expand Down Expand Up @@ -339,7 +339,7 @@ pub(crate) fn impl_wrap_setter(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec
#[allow(unused_mut)]
unsafe extern "C" fn __wrap(
_slf: *mut ::pyo3::ffi::PyObject,
_value: *mut ::pyo3::ffi::PyObject, _: *mut std::os::raw::c_void) -> ::pyo3::libc::c_int
_value: *mut ::pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> ::pyo3::libc::c_int
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = ::pyo3::GILPool::new();
Expand Down

0 comments on commit c1cb5ca

Please sign in to comment.