diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c0ccb60001a..cae815a3b3b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: nightly + toolchain: stable default: true - run: rustup set default-host ${{ matrix.platform.rust-target }} - name: Build without default features diff --git a/.travis.yml b/.travis.yml index 5ab7b52724e..06b7f48a7ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,23 +16,28 @@ jobs: python: "3.7" - name: Python 3.8 python: "3.8" + # Run clippy and rustfmt + env: RUN_LINT=1 - name: Python 3.9-dev python: "3.9-dev" - - name: Minimum nightly + - name: Nightly python: "3.7" - # Keep this synced up with build.rs and ensure that the nightly version does have clippy available - # https://static.rust-lang.org/dist/YYYY-MM-DD/clippy-nightly-x86_64-unknown-linux-gnu.tar.gz exists - env: TRAVIS_RUST_VERSION=nightly-2020-01-21 + env: TRAVIS_RUST_VERSION=nightly FEATURES="nightly" + - name: Minimum Stable + python: "3.7" + env: TRAVIS_RUST_VERSION=1.39.0 - name: PyPy3.5 7.0 # Tested via anaconda PyPy (since travis's PyPy version is too old) python: "3.7" env: FEATURES="pypy" PATH="$PATH:/opt/anaconda/envs/pypy3/bin" allow_failures: + - name: Nightly - python: 3.9-dev env: global: - - TRAVIS_RUST_VERSION=nightly + - TRAVIS_RUST_VERSION=stable - RUST_BACKTRACE=1 + - RUN_LINT=0 before_install: - source ./ci/travis/setup.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fe74cb09ac..74f496eb4b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added -- Add FFI definition `PyObject_AsFileDescriptor` [#938](https://github.com/PyO3/pyo3/pull/938) +- Support stable versions of Rust (>=1.39). [#969](https://github.com/PyO3/pyo3/pull/969) +- Add FFI definition `PyObject_AsFileDescriptor`. [#938](https://github.com/PyO3/pyo3/pull/938) - Add `PyByteArray::data`, `PyByteArray::as_bytes`, and `PyByteArray::as_bytes_mut`. [#967](https://github.com/PyO3/pyo3/pull/967) - Add `Py::borrow`, `Py::borrow_mut`, `Py::try_borrow`, and `Py::try_borrow_mut` for accessing `#[pyclass]` values. [#976](https://github.com/PyO3/pyo3/pull/976) diff --git a/Cargo.toml b/Cargo.toml index d18cafd6ee4..3073775e2ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,13 +33,13 @@ unindent = { version = "0.1.4", optional = true } [dev-dependencies] assert_approx_eq = "1.1.0" trybuild = "1.0.23" - -[build-dependencies] -version_check = "0.9.1" +rustversion = "1.0" [features] default = ["macros"] macros = ["ctor", "indoc", "inventory", "paste", "pyo3cls", "unindent"] +# Optimizes PyObject to Vec conversion and so on. +nightly = [] # this is no longer needed internally, but setuptools-rust assumes this feature python3 = [] diff --git a/Makefile b/Makefile index 897208f9d9b..5bfb5755650 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ fmt: clippy: @touch src/lib.rs # Touching file to ensure that cargo clippy will re-check the project - cargo clippy --all-features --all-targets -- \ + cargo clippy --features="default num-bigint num-complex" --tests -- \ $(addprefix -D ,${CLIPPY_LINTS_TO_DENY}) for example in examples/*; do (cd $$example/; cargo clippy) || exit 1; done diff --git a/README.md b/README.md index 37e1b79e3d5..8c3e60b81d5 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![Actions Status](https://github.com/PyO3/pyo3/workflows/Test/badge.svg)](https://github.com/PyO3/pyo3/actions) [![codecov](https://codecov.io/gh/PyO3/pyo3/branch/master/graph/badge.svg)](https://codecov.io/gh/PyO3/pyo3) [![crates.io](http://meritbadge.herokuapp.com/pyo3)](https://crates.io/crates/pyo3) +[![minimum rustc 1.39](https://img.shields.io/badge/rustc-1.39+-blue.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) [![Join the dev chat](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/PyO3/Lobby) [Rust](http://www.rust-lang.org/) bindings for [Python](https://www.python.org/). This includes running and interacting with Python code from a Rust binary, as well as writing native Python modules. @@ -16,11 +17,7 @@ A comparison with rust-cpython can be found [in the guide](https://pyo3.rs/maste ## Usage -PyO3 supports Python 3.5 and up. The minimum required Rust version is 1.42.0-nightly 2020-01-21. - -If you have never used nightly Rust, the official guide has -[a great section](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html#rustup-and-the-role-of-rust-nightly) -about installing it. +PyO3 supports Python 3.5 and up. The minimum required Rust version is 1.39.0. PyPy is also supported (via cpyext) for Python 3.5 only, targeted PyPy version is 7.0.0. Please refer to the [pypy section in the guide](https://pyo3.rs/master/pypy.html). diff --git a/build.rs b/build.rs index 4459041d557..a091e3e3169 100644 --- a/build.rs +++ b/build.rs @@ -8,13 +8,6 @@ use std::{ process::{Command, Stdio}, str::FromStr, }; -use version_check::{Channel, Date, Version}; - -/// Specifies the minimum nightly version needed to compile pyo3. -/// Keep this synced up with the travis ci config, -/// But note that this is the rustc version which can be lower than the nightly version -const MIN_DATE: &str = "2020-01-20"; -const MIN_VERSION: &str = "1.42.0-nightly"; const PY3_MIN_MINOR: u8 = 5; const CFG_KEY: &str = "py_sys_config"; @@ -76,8 +69,8 @@ impl FromStr for PythonInterpreterKind { type Err = Box; fn from_str(s: &str) -> Result { match s { - "CPython" => Ok(Self::CPython), - "PyPy" => Ok(Self::PyPy), + "CPython" => Ok(PythonInterpreterKind::CPython), + "PyPy" => Ok(PythonInterpreterKind::PyPy), _ => Err(format!("Invalid interpreter: {}", s).into()), } } @@ -302,7 +295,7 @@ fn run_python_script(interpreter: &Path, script: &str) -> Result { ); } } - Ok(ok) if !ok.status.success() => bail!("Python script failed: {}"), + Ok(ref ok) if !ok.status.success() => bail!("Python script failed: {}"), Ok(ok) => Ok(String::from_utf8(ok.stdout)?), } } @@ -567,34 +560,7 @@ fn check_target_architecture(interpreter_config: &InterpreterConfig) -> Result<( Ok(()) } -fn check_rustc_version() -> Result<()> { - let channel = Channel::read().ok_or("Failed to determine rustc channel")?; - if !channel.supports_features() { - bail!("PyO3 requires a nightly or dev version of Rust."); - } - - let actual_version = Version::read().ok_or("Failed to determine the rustc version")?; - if !actual_version.at_least(MIN_VERSION) { - bail!( - "PyO3 requires at least rustc {}, while the current version is {}", - MIN_VERSION, - actual_version - ) - } - - let actual_date = Date::read().ok_or("Failed to determine the rustc date")?; - if !actual_date.at_least(MIN_DATE) { - bail!( - "PyO3 requires at least rustc {}, while the current rustc date is {}", - MIN_DATE, - actual_date - ) - } - Ok(()) -} - fn main() -> Result<()> { - check_rustc_version()?; // 1. Setup cfg variables so we can do conditional compilation in this library based on the // python interpeter's compilation flags. This is necessary for e.g. matching the right unicode // and threading interfaces. First check if we're cross compiling, if so, we cannot run the diff --git a/ci/travis/guide.sh b/ci/travis/guide.sh index 898f0889ec5..158497c900e 100755 --- a/ci/travis/guide.sh +++ b/ci/travis/guide.sh @@ -21,7 +21,7 @@ mdbook build -d ../target/guide guide # Build the doc # This builds the book in target/doc -cargo doc --all-features --no-deps +cargo doc --features="default num-bigint num-complex" --no-deps echo "" > target/doc/index.html # Get the lastest tag across all branches diff --git a/ci/travis/setup.sh b/ci/travis/setup.sh index d9c6f66e35f..c436691afc4 100755 --- a/ci/travis/setup.sh +++ b/ci/travis/setup.sh @@ -7,7 +7,7 @@ set -e # Use profile=minimal here to skip installing clippy curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=$TRAVIS_RUST_VERSION --profile=minimal -y export PATH=$PATH:$HOME/.cargo/bin -if [ "$TRAVIS_JOB_NAME" = "Minimum nightly" ]; then +if [[ $RUN_LINT == 1 ]]; then rustup component add clippy rustup component add rustfmt fi diff --git a/ci/travis/test.sh b/ci/travis/test.sh index 975e05bc42a..eb85e926aca 100755 --- a/ci/travis/test.sh +++ b/ci/travis/test.sh @@ -10,7 +10,7 @@ else PYTHON_SYS_EXECUTABLE="/opt/anaconda/envs/pypy3/bin/pypy3" cargo build; fi -if [ "$TRAVIS_JOB_NAME" = "Minimum nightly" ]; then +if [[ $RUN_LINT == 1 ]]; then pip install --pre black==19.3b0 make lint fi diff --git a/guide/src/advanced.md b/guide/src/advanced.md index 7507771d351..dc04d5d019a 100644 --- a/guide/src/advanced.md +++ b/guide/src/advanced.md @@ -28,3 +28,9 @@ version = "0.8.1" extension-module = ["pyo3/extension-module"] default = ["extension-module"] ``` + +## The `nightly` feature + +The `pyo3/nightly` feature needs the nightly Rust compiler. This allows PyO3 to use Rust's unstable specialization feature to apply the following optimizations: +- `FromPyObject` for `Vec` and `[T;N]` can perform a `memcpy` when the object is a `PyBuffer` +- `ToBorrowedObject` can skip a reference count increase when the provided object is a Python native type. diff --git a/guide/src/class.md b/guide/src/class.md index 391e6e40e4d..47976993959 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -888,12 +888,12 @@ documentation](https://docs.python.org/3/library/stdtypes.html#iterator-types). Users should be able to define a `#[pyclass]` with or without `#[pymethods]`, while PyO3 needs a trait with a function that returns all methods. Since it's impossible to make the code generation in pyclass dependent on whether there is an impl block, we'd need to implement the trait on -`#[pyclass]` and override the implementation in `#[pymethods]`, which is to the best of my knowledge -only possible with the specialization feature, which can't be used on stable. - -To escape this we use [inventory](https://github.com/dtolnay/inventory), +`#[pyclass]` and override the implementation in `#[pymethods]`. +To enable this, we use a static registry type provided by [inventory](https://github.com/dtolnay/inventory), which allows us to collect `impl`s from arbitrary source code by exploiting some binary trick. See [inventory: how it works](https://github.com/dtolnay/inventory#how-it-works) and `pyo3_derive_backend::py_class` for more details. +Also for `#[pyproto]`, we use a similar, but more task-specific registry and +initialize it using the [ctor](https://github.com/mmastrac/rust-ctor) crate. Specifically, the following implementation is generated: diff --git a/guide/src/rust_cpython.md b/guide/src/rust_cpython.md index e5912465014..3017b9571d4 100644 --- a/guide/src/rust_cpython.md +++ b/guide/src/rust_cpython.md @@ -6,7 +6,7 @@ This chapter is based on the discussion in [PyO3/pyo3#55](https://github.com/PyO ## Macros -While rust-cpython has a macro based dsl for declaring modules and classes, PyO3 uses proc macros and specialization. PyO3 also doesn't change your struct and functions so you can still use them as normal Rust functions. The disadvantage is that specialization currently only works on nightly. +While rust-cpython has a `macro_rules!` based dsl for declaring modules and classes, PyO3 uses proc macros. PyO3 also doesn't change your struct and functions so you can still use them as normal Rust functions. **rust-cpython** diff --git a/guide/src/trait_bounds.md b/guide/src/trait_bounds.md index 08e212b2219..6b4ef9d675e 100644 --- a/guide/src/trait_bounds.md +++ b/guide/src/trait_bounds.md @@ -153,6 +153,7 @@ Now we add the PyO3 annotations to the trait implementation: impl Model for UserModel { // the previous trait implementation } +``` However, the previous code will not compile. The compilation error is the following one: `error: #[pymethods] cannot be used on trait impl blocks` diff --git a/guide/src/types.md b/guide/src/types.md index cb36c31aad5..cff7da22bba 100644 --- a/guide/src/types.md +++ b/guide/src/types.md @@ -46,7 +46,7 @@ references is done at runtime using `PyCell`, a scheme very similar to ## Object types -### [`PyAny`] +### [`PyAny`][PyAny] **Represents:** a Python object of unspecified type, restricted to a GIL lifetime. Currently, `PyAny` can only ever occur as a reference, `&PyAny`. @@ -95,7 +95,7 @@ These types all implement `Deref`, so they all expose the same methods which can be found on `PyAny`. To see all Python types exposed by `PyO3` you should consult the -[`pyo3::types`] module. +[`pyo3::types`][pyo3::types] module. **Conversions:** diff --git a/pyo3-derive-backend/src/pyfunction.rs b/pyo3-derive-backend/src/pyfunction.rs index 689cd45f516..96ef584a9ca 100644 --- a/pyo3-derive-backend/src/pyfunction.rs +++ b/pyo3-derive-backend/src/pyfunction.rs @@ -179,22 +179,25 @@ pub fn parse_name_attribute(attrs: &mut Vec) -> syn::Result true, }); - match &*name_attrs { - [] => Ok(None), - [(syn::Lit::Str(s), span)] => { + if 1 < name_attrs.len() { + return Err(syn::Error::new( + name_attrs[0].1, + "#[name] can not be specified multiple times", + )); + } + + match name_attrs.get(0) { + Some((syn::Lit::Str(s), span)) => { let mut ident: syn::Ident = s.parse()?; // This span is the whole attribute span, which is nicer for reporting errors. ident.set_span(*span); Ok(Some(ident)) } - [(_, span)] => Err(syn::Error::new( + Some((_, span)) => Err(syn::Error::new( *span, "Expected string literal for #[name] argument", )), - [(_, span), ..] => Err(syn::Error::new( - *span, - "#[name] can not be specified multiple times", - )), + None => Ok(None), } } diff --git a/pyo3-derive-backend/src/pymethod.rs b/pyo3-derive-backend/src/pymethod.rs index d1f35df0241..a4ef8c497ac 100644 --- a/pyo3-derive-backend/src/pymethod.rs +++ b/pyo3-derive-backend/src/pymethod.rs @@ -512,10 +512,11 @@ fn impl_arg_param( }; if let syn::Type::Reference(tref) = ty { let (tref, mut_) = tref_preprocess(tref); - let as_deref = if mut_.is_some() { - quote! { as_deref_mut } + // To support Rustc 1.39.0, we don't use as_deref here... + let tmp_as_deref = if mut_.is_some() { + quote! { _tmp.as_mut().map(std::ops::DerefMut::deref_mut) } } else { - quote! { as_deref } + quote! { _tmp.as_ref().map(std::ops::Deref::deref) } }; // Get Option<&T> from Option> quote! { @@ -525,7 +526,7 @@ fn impl_arg_param( }, None => #default, }; - let #arg_name = _tmp.#as_deref(); + let #arg_name = #tmp_as_deref; } } else { quote! { @@ -731,8 +732,9 @@ pub(crate) fn impl_py_getter_def( /// Split an argument of pyo3::Python from the front of the arg list, if present fn split_off_python_arg<'a>(args: &'a [FnArg<'a>]) -> (Option<&FnArg>, &[FnArg]) { - match args { - [py, rest @ ..] if utils::if_type_is_python(&py.ty) => (Some(py), rest), - rest => (None, rest), + if args.get(0).map(|py| utils::if_type_is_python(&py.ty)) == Some(true) { + (Some(&args[0]), &args[1..]) + } else { + (None, args) } } diff --git a/src/conversion.rs b/src/conversion.rs index 52e684394ad..8ec196f8bbc 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -98,6 +98,7 @@ pub trait ToBorrowedObject: ToPyObject { F: FnOnce(*mut ffi::PyObject) -> R; } +#[cfg(feature = "nightly")] impl ToBorrowedObject for T where T: ToPyObject, @@ -115,6 +116,25 @@ where } } +#[cfg(not(feature = "nightly"))] +impl ToBorrowedObject for T +where + T: ToPyObject, +{ + fn with_borrowed_ptr(&self, py: Python, f: F) -> R + where + F: FnOnce(*mut ffi::PyObject) -> R, + { + let ptr = self.to_object(py).into_ptr(); + let result = f(ptr); + unsafe { + ffi::Py_XDECREF(ptr); + } + result + } +} + +#[cfg(feature = "nightly")] impl ToBorrowedObject for T where T: ToPyObject + AsPyPointer, diff --git a/src/lib.rs b/src/lib.rs index 92204145722..f55b1a07102 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(specialization)] +#![cfg_attr(feature = "nightly", feature(specialization))] #![allow(clippy::missing_safety_doc)] // FIXME (#698) //! Rust bindings to the Python interpreter. diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 33a84a923ba..b8aedb3cad8 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -1,5 +1,6 @@ // Copyright (c) 2017-present PyO3 Project and Contributors +#[cfg(feature = "nightly")] use crate::buffer; use crate::err::{self, PyDowncastError, PyErr, PyResult}; use crate::exceptions; @@ -261,6 +262,7 @@ impl PySequence { macro_rules! array_impls { ($($N:expr),+) => { $( + #[cfg(feature = "nightly")] impl<'a, T> FromPyObject<'a> for [T; $N] where T: Copy + Default + FromPyObject<'a>, @@ -272,6 +274,19 @@ macro_rules! array_impls { } } + #[cfg(not(feature = "nightly"))] + impl<'a, T> FromPyObject<'a> for [T; $N] + where + T: Copy + Default + FromPyObject<'a>, + { + fn extract(obj: &'a PyAny) -> PyResult { + let mut array = [T::default(); $N]; + extract_sequence_into_slice(obj, &mut array)?; + Ok(array) + } + } + + #[cfg(feature = "nightly")] impl<'source, T> FromPyObject<'source> for [T; $N] where for<'a> T: Copy + Default + FromPyObject<'a> + buffer::Element, @@ -300,6 +315,17 @@ array_impls!( 26, 27, 28, 29, 30, 31, 32 ); +#[cfg(not(feature = "nightly"))] +impl<'a, T> FromPyObject<'a> for Vec +where + T: FromPyObject<'a>, +{ + fn extract(obj: &'a PyAny) -> PyResult { + extract_sequence(obj) + } +} + +#[cfg(feature = "nightly")] impl<'a, T> FromPyObject<'a> for Vec where T: FromPyObject<'a>, @@ -309,6 +335,7 @@ where } } +#[cfg(feature = "nightly")] impl<'source, T> FromPyObject<'source> for Vec where for<'a> T: FromPyObject<'a> + buffer::Element + Copy, diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs index a683f5f58f2..465922b59a8 100644 --- a/tests/test_compile_error.rs +++ b/tests/test_compile_error.rs @@ -1,17 +1,22 @@ #[test] fn test_compile_errors() { let t = trybuild::TestCases::new(); - t.compile_fail("tests/ui/invalid_macro_args.rs"); - t.compile_fail("tests/ui/invalid_property_args.rs"); - t.compile_fail("tests/ui/invalid_pyclass_args.rs"); - t.compile_fail("tests/ui/invalid_pymethod_names.rs"); - t.compile_fail("tests/ui/missing_clone.rs"); - t.compile_fail("tests/ui/reject_generics.rs"); - t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs"); - // Since the current minimum nightly(2020-01-20) has a different error message, - // we skip this test. - // TODO(kngwyu): Remove this `if` when we update minimum nightly. - if option_env!("TRAVIS_JOB_NAME") != Some("Minimum nightly") { + testcase_common(&t); + testcase_latest_stable(&t); + + fn testcase_common(t: &trybuild::TestCases) { + t.compile_fail("tests/ui/invalid_macro_args.rs"); + t.compile_fail("tests/ui/invalid_property_args.rs"); + t.compile_fail("tests/ui/invalid_pyclass_args.rs"); + t.compile_fail("tests/ui/missing_clone.rs"); + t.compile_fail("tests/ui/reject_generics.rs"); + t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs"); + t.compile_fail("tests/ui/invalid_pymethod_names.rs"); + } + #[rustversion::since(1.43)] + fn testcase_latest_stable(t: &trybuild::TestCases) { t.compile_fail("tests/ui/static_ref.rs"); } + #[rustversion::before(1.43)] + fn testcase_latest_stable(_t: &trybuild::TestCases) {} } diff --git a/tests/test_datetime.rs b/tests/test_datetime.rs index b5db6d08f5a..f31148385ed 100644 --- a/tests/test_datetime.rs +++ b/tests/test_datetime.rs @@ -1,6 +1,3 @@ -#![feature(concat_idents)] - -use pyo3::ffi::*; use pyo3::prelude::*; use pyo3::types::IntoPyDict; @@ -37,9 +34,9 @@ fn _get_subclasses<'p>( macro_rules! assert_check_exact { ($check_func:ident, $obj: expr) => { unsafe { - use pyo3::AsPyPointer; + use pyo3::{AsPyPointer, ffi::*}; assert!($check_func(($obj).as_ptr()) != 0); - assert!(concat_idents!($check_func, Exact)(($obj).as_ptr()) != 0); + assert!(pyo3::paste::expr!([<$check_func Exact>])(($obj).as_ptr()) != 0); } }; } @@ -47,9 +44,9 @@ macro_rules! assert_check_exact { macro_rules! assert_check_only { ($check_func:ident, $obj: expr) => { unsafe { - use pyo3::AsPyPointer; + use pyo3::{AsPyPointer, ffi::*}; assert!($check_func(($obj).as_ptr()) != 0); - assert!(concat_idents!($check_func, Exact)(($obj).as_ptr()) == 0); + assert!(pyo3::paste::expr!([<$check_func Exact>])(($obj).as_ptr()) == 0); } }; } diff --git a/tests/ui/invalid_pymethod_names.stderr b/tests/ui/invalid_pymethod_names.stderr index fc1911ac191..17f3595b0b1 100644 --- a/tests/ui/invalid_pymethod_names.stderr +++ b/tests/ui/invalid_pymethod_names.stderr @@ -2,16 +2,16 @@ error: name not allowed with this attribute --> $DIR/invalid_pymethod_names.rs:10:5 | 10 | #[name = "num"] - | ^^^^^^^^^^^^^^^ + | ^ error: #[name] can not be specified multiple times --> $DIR/invalid_pymethod_names.rs:17:5 | 17 | #[name = "foo"] - | ^^^^^^^^^^^^^^^ + | ^ error: name not allowed with this attribute --> $DIR/invalid_pymethod_names.rs:24:5 | 24 | #[name = "makenew"] - | ^^^^^^^^^^^^^^^^^^^ + | ^