Skip to content

Commit f875309

Browse files
author
Andrew J Westlake
committed
Fixed runtime variable name, added GitHub Actions workflow config, added githooks for commit/push quality checks
1 parent 4d0983d commit f875309

File tree

5 files changed

+173
-11
lines changed

5 files changed

+173
-11
lines changed

.githooks/pre-commit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
cargo check --all-targets

.githooks/pre-push

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
cargo test

.github/workflows/ci.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
fmt:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions/setup-python@v2
18+
- run: pip install black==19.10b0
19+
- uses: actions-rs/toolchain@v1
20+
with:
21+
toolchain: stable
22+
profile: minimal
23+
components: rustfmt
24+
- name: Check python formatting (black)
25+
run: black --check .
26+
- name: Check rust formatting (rustfmt)
27+
run: cargo fmt --all -- --check
28+
29+
clippy:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v2
33+
- uses: actions-rs/toolchain@v1
34+
with:
35+
toolchain: stable
36+
profile: minimal
37+
components: clippy
38+
- run: make clippy
39+
40+
build:
41+
needs: [fmt] # don't wait for clippy as fails rarely and takes longer
42+
name: python${{ matrix.python-version }}-${{ matrix.platform.python-architecture }} ${{ matrix.platform.os }} ${{ matrix.msrv }}
43+
runs-on: ${{ matrix.platform.os }}
44+
strategy:
45+
fail-fast: false # If one platform fails, allow the rest to keep testing.
46+
matrix:
47+
rust: [stable]
48+
python-version: [3.6, 3.7, 3.8, 3.9, 3.10-dev, pypy-3.6]
49+
platform: [
50+
{ os: "macOS-latest", python-architecture: "x64", rust-target: "x86_64-apple-darwin" },
51+
{ os: "ubuntu-latest", python-architecture: "x64", rust-target: "x86_64-unknown-linux-gnu" },
52+
{ os: "windows-latest", python-architecture: "x64", rust-target: "x86_64-pc-windows-msvc" },
53+
{ os: "windows-latest", python-architecture: "x86", rust-target: "i686-pc-windows-msvc" },
54+
]
55+
exclude:
56+
# There is no 64-bit pypy on windows
57+
- python-version: pypy-3.6
58+
platform: { os: "windows-latest", python-architecture: "x64" }
59+
include:
60+
# Test minimal supported Rust version
61+
- rust: 1.45.0
62+
python-version: 3.9
63+
platform: { os: "ubuntu-latest", python-architecture: "x64", rust-target: "x86_64-unknown-linux-gnu" }
64+
msrv: "MSRV"
65+
66+
steps:
67+
- uses: actions/checkout@v2
68+
69+
- name: Set up Python ${{ matrix.python-version }}
70+
uses: actions/setup-python@v2
71+
with:
72+
python-version: ${{ matrix.python-version }}
73+
architecture: ${{ matrix.platform.python-architecture }}
74+
75+
- name: Install Rust toolchain
76+
uses: actions-rs/toolchain@v1
77+
with:
78+
toolchain: ${{ matrix.rust }}
79+
target: ${{ matrix.platform.rust-target }}
80+
profile: minimal
81+
default: true
82+
83+
- if: matrix.platform.os == 'ubuntu-latest'
84+
name: Prepare LD_LIBRARY_PATH (Ubuntu only)
85+
run: echo LD_LIBRARY_PATH=${pythonLocation}/lib >> $GITHUB_ENV
86+
87+
- name: Build docs
88+
run: cargo doc --no-default-features --verbose --target ${{ matrix.platform.rust-target }}
89+
90+
- name: Build (no features)
91+
run: cargo build --no-default-features --verbose --target ${{ matrix.platform.rust-target }}
92+
93+
- name: Build (all additive features)
94+
run: cargo build --no-default-features --verbose --target ${{ matrix.platform.rust-target }}
95+
96+
# Run tests (except on PyPy, because no embedding API).
97+
- if: matrix.python-version != 'pypy-3.6'
98+
name: Test
99+
run: cargo test --no-default-features --target ${{ matrix.platform.rust-target }}
100+
101+
# Run tests again, but in abi3 mode
102+
- if: matrix.python-version != 'pypy-3.6'
103+
name: Test (abi3)
104+
run: cargo test --no-default-features --target ${{ matrix.platform.rust-target }}
105+
106+
# Run tests again, for abi3-py36 (the minimal Python version)
107+
- if: (matrix.python-version != 'pypy-3.6') && (matrix.python-version != '3.6')
108+
name: Test (abi3-py36)
109+
run: cargo test --no-default-features --target ${{ matrix.platform.rust-target }}
110+
111+
- name: Install python test dependencies
112+
run: |
113+
python -m pip install -U pip setuptools
114+
pip install setuptools-rust pytest pytest-benchmark tox tox-venv
115+
- name: Test example extension modules
116+
shell: bash
117+
run: |
118+
for example_dir in examples/*; do
119+
tox --discover $(which python) -c $example_dir -e py
120+
done
121+
env:
122+
RUST_BACKTRACE: 1
123+
RUSTFLAGS: "-D warnings"
124+
# TODO: this is a hack to workaround compile_error! warnings about auto-initialize on PyPy
125+
# Once cargo's `resolver = "2"` is stable (~ MSRV Rust 1.52), remove this.
126+
PYO3_CI: 1
127+
128+
coverage:
129+
needs: [fmt]
130+
runs-on: ubuntu-latest
131+
steps:
132+
- uses: actions/checkout@v2
133+
- uses: actions-rs/toolchain@v1
134+
with:
135+
toolchain: nightly
136+
override: true
137+
profile: minimal
138+
- uses: actions-rs/cargo@v1
139+
with:
140+
command: test
141+
args:
142+
env:
143+
CARGO_INCREMENTAL: 0
144+
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests"
145+
RUSTDOCFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests"
146+
- uses: actions-rs/[email protected]
147+
id: coverage
148+
- uses: codecov/codecov-action@v1
149+
with:
150+
file: ${{ steps.coverage.outputs.report }}

Contributing.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Git Hooks (Recommended)
2+
3+
Using the project's githooks are recommended to prevent CI from failing for trivial reasons such as build checks or integration tests failing. Enabling the hooks should run `cargo check --all-targets` for every commit and `cargo test` for every push.
4+
5+
```
6+
git config core.hookspath .githooks
7+
```

src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tokio::{
1515
};
1616

1717
lazy_static! {
18-
static ref MULTI_RUNTIME: Runtime = {
18+
static ref CURRENT_THREAD_RUNTIME: Runtime = {
1919
Builder::new_current_thread()
2020
.enable_all()
2121
.build()
@@ -35,7 +35,10 @@ static CREATE_FUTURE: OnceCell<PyObject> = OnceCell::new();
3535
pub fn try_init(py: Python) -> PyResult<()> {
3636
let asyncio = py.import("asyncio")?;
3737
let event_loop = asyncio.call_method0("get_event_loop")?;
38-
let executor = py.import("concurrent.futures.thread")?.getattr("ThreadPoolExecutor")?.call0()?;
38+
let executor = py
39+
.import("concurrent.futures.thread")?
40+
.getattr("ThreadPoolExecutor")?
41+
.call0()?;
3942

4043
event_loop.call_method1("set_default_executor", (executor,))?;
4144

@@ -50,7 +53,7 @@ pub fn try_init(py: Python) -> PyResult<()> {
5053
CREATE_FUTURE.get_or_init(|| create_future.into());
5154

5255
thread::spawn(|| {
53-
MULTI_RUNTIME.block_on(future::pending::<()>());
56+
CURRENT_THREAD_RUNTIME.block_on(future::pending::<()>());
5457
});
5558

5659
Ok(())
@@ -98,13 +101,9 @@ where
98101
pub fn close(py: Python) -> PyResult<()> {
99102
// Shutdown the executor and wait until all threads are cleaned up
100103
EXECUTOR.get().unwrap().call_method0(py, "shutdown")?;
101-
102-
EVENT_LOOP
103-
.get()
104-
.unwrap()
105-
.call_method0(py, "stop")?;
104+
105+
EVENT_LOOP.get().unwrap().call_method0(py, "stop")?;
106106
EVENT_LOOP.get().unwrap().call_method0(py, "close")?;
107-
108107
Ok(())
109108
}
110109

@@ -114,7 +113,7 @@ where
114113
F: Future + Send + 'static,
115114
F::Output: Send + 'static,
116115
{
117-
MULTI_RUNTIME.spawn(fut)
116+
CURRENT_THREAD_RUNTIME.spawn(fut)
118117
}
119118

120119
/// Spawn a blocking task onto the executor
@@ -123,7 +122,7 @@ where
123122
F: FnOnce() -> R + Send + 'static,
124123
R: Send + 'static,
125124
{
126-
MULTI_RUNTIME.spawn_blocking(func)
125+
CURRENT_THREAD_RUNTIME.spawn_blocking(func)
127126
}
128127

129128
#[pyclass]

0 commit comments

Comments
 (0)