8000 Upgrade pyo3, winreg · RustPython/RustPython@a46a74d · GitHub
[go: up one dir, main page]

Skip to content

Commit a46a74d

Browse files
committed
Upgrade pyo3, winreg
1 parent 4ecf443 commit a46a74d

File tree

6 files changed

+55
-46
lines changed

6 files changed

+55
-46
lines changed

Cargo.lock

Lines changed: 22 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ rustyline = { workspace = true }
4949

5050
[dev-dependencies]
5151
criterion = { version = "0.3.5", features = ["html_reports"] }
52-
pyo3 = { version = "0.20.2", features = ["auto-initialize"] }
52+
pyo3 = { version = "0.22", features = ["auto-initialize"] }
5353

5454
[[bench]]
5555
name = "execution"
@@ -176,6 +176,8 @@ rustyline = "14.0.0"
176176
serde = { version = "1.0.133", default-features = false }
177177
schannel = "0.1.22"
178178
static_assertions = "1.1"
179+
strum = "0.26"
180+
strum_macros = "0.26"
179181
syn = "1.0.109"
180182
thiserror = "1.0"
181183
thread_local = "1.1.4"

benches/execution.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::path::Path;
1313
fn bench_cpython_code(b: &mut Bencher, source: &str) {
1414
pyo3::Python::with_gil(|py| {
1515
b.iter(|| {
16-
let module =
17-
pyo3::types::PyModule::from_code(py, source, "", "").expect("Error running source");
16+
let module = pyo3::types::PyModule::from_code_bound(py, source, "", "")
17+
.expect("Error running source");
1818
black_box(module);
1919
})
2020
})
@@ -53,9 +53,10 @@ pub fn benchmark_file_parsing(group: &mut BenchmarkGroup<WallTime>, name: &str,
5353
b.iter(|| ast::Suite::parse(contents, name).unwrap())
5454
});
5555
group.bench_function(BenchmarkId::new("cpython", name), |b| {
56+
use pyo3::types::PyAnyMethods;
5657
pyo3::Python::with_gil(|py| {
57-
let builtins =
58-
pyo3::types::PyModule::import(py, "builtins").expect("Failed to import builtins");
58+
let builtins = pyo3::types::PyModule::import_bound(py, "builtins")
59+
.expect("Failed to import builtins");
5960
let compile = builtins.getattr("compile").expect("no compile in builtins");
6061
b.iter(|| {
6162
let x = compile

benches/microbenchmarks.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use criterion::{
22
criterion_group, criterion_main, measurement::WallTime, BatchSize, BenchmarkGroup, BenchmarkId,
33
Criterion, Throughput,
44
};
5+
use pyo3::types::PyAnyMethods;
56
use rustpython_compiler::Mode;
67
use rustpython_vm::{AsObject, Interpreter, PyResult, Settings};
78
use std::{
@@ -44,25 +45,28 @@ fn bench_cpython_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchma
4445

4546
// Grab the exec function in advance so we don't have lookups in the hot code
4647
let builtins =
47-
pyo3::types::PyModule::import(py, "builtins").expect("Failed to import builtins");
48+
pyo3::types::PyModule::import_bound(py, "builtins").expect("Failed to import builtins");
4849
let exec = builtins.getattr("exec").expect("no exec in builtins");
4950

50-
let bench_func = |(globals, locals): &mut (&pyo3::types::PyDict, &pyo3::types::PyDict)| {
51-
let res = exec.call((code, &*globals, &*locals), None);
51+
let bench_func = |(globals, locals): &mut (
52+
pyo3::Bound<pyo3::types::PyDict>,
53+
pyo3::Bound<pyo3::types::PyDict>,
54+
)| {
55+
let res = exec.call((&code, &*globals, &*locals), None);
5256
if let Err(e) = res {
5357
e.print(py);
5458
panic!("Error running microbenchmark")
5559
}
5660
};
5761

5862
let bench_setup = |iterations| {
59-
let globals = pyo3::types::PyDict::new(py);
60-
let locals = pyo3::types::PyDict::new(py);
63+
let globals = pyo3::types::PyDict::new_bound(py);
64+
let locals = pyo3::types::PyDict::new_bound(py);
6165
if let Some(idx) = iterations {
6266
globals.set_item("ITERATIONS", idx).unwrap();
6367
}
6468

65-
let res = exec.call((setup_code, &globals, &locals), None);
69+
let res = exec.call((&setup_code, &globals, &locals), None);
6670
if let Err(e) = res {
6771
e.print(py);
6872
panic!("Error running microbenchmark setup code")
@@ -93,9 +97,9 @@ fn cpy_compile_code<'a>(
9397
py: pyo3::Python<'a>,
9498
code: &str,
9599
name: &str,
96-
) -> pyo3::PyResult<&'a pyo3::types::PyCode> {
100+
) -> pyo3::PyResult<pyo3::Bound<'a, pyo3::types::PyCode>> {
97101
let builtins =
98-
pyo3::types::PyModule::import(py, "builtins").expect("Failed to import builtins");
102+
pyo3::types::PyModule::import_bound(py, "builtins").expect("Failed to import builtins");
99103
let compile = builtins.getattr("compile").expect("no compile in builtins");
100104
compile.call1((code, name, "exec"))?.extract()
101105
}

vm/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ unic-ucd-ident = "0.9.0"
9494
rustix = { workspace = true }
9595
exitcode = "1.1.2"
9696
uname = "0.1.1"
97-
strum = "0.24.0"
98-
strum_macros = "0.24.0"
97+
strum = { workspace = true }
98+
strum_macros = { workspace = true }
9999

100100
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
101101
rustyline = { workspace = true }
@@ -108,7 +108,7 @@ num_cpus = "1.13.1"
108108
junction = { workspace = true }
109109
schannel = { workspace = true }
110110
widestring = { workspace = true }
111-
winreg = "0.10.1"
111+
winreg = "0.52"
112112

113113
[target.'cfg(windows)'.dependencies.windows]
114114
version = "0.52.0"
@@ -152,4 +152,4 @@ itertools = { workspace = true }
152152
rustc_version = "0.4.0"
153153

154154
[lints]
155-
workspace = true
155+
workspace = true

vm/src/stdlib/winreg.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod winreg {
3333
TryFromObject, VirtualMachine,
3434
};
3535
use ::winreg::{enums::RegType, RegKey, RegValue};
36+
use std::mem::ManuallyDrop;
3637
use std::{ffi::OsStr, io};
3738
use windows_sys::Win32::Foundation;
3839

@@ -97,7 +98,7 @@ mod winreg {
9798

9899
#[pymethod(magic)]
99100
fn bool(&self) -> bool {
100-
!self.key().raw_handle().is_null()
101+
self.key().raw_handle() != 0
101102
}
102103
#[pymethod(magic)]
103104
fn enter(zelf: PyRef<Self>) -> PyRef<Self> {
@@ -125,10 +126,8 @@ mod winreg {
125126
match self {
126127
Self::PyHkey(py) => f(&py.key()),
127128
Self::Constant(hkey) => {
128-
let k = RegKey::predef(*hkey);
129-
let res = f(&k);
130-
std::mem::forget(k);
131-
res
129+
let k = ManuallyDrop::new(RegKey::predef(*hkey));
130+
f(&k)
132131
}
133132
}
134133
}
@@ -283,9 +282,11 @@ mod winreg {
283282
i.map(|i| vm.ctx.new_int(i).into())
284283
}};
285284
}
286-
let bytes_to_wide = |b: &[u8]| -> Option<&[u16]> {
287-
if b.len() % 2 == 0 {
288-
Some(unsafe { std::slice::from_raw_parts(b.as_ptr().cast(), b.len() / 2) })
285+
let bytes_to_wide = |b| {
286+
if <[u8]>::len(b) % 2 == 0 {
287+
let (pref, wide, suf) = unsafe { <[u8]>::align_to::<u16>(b) };
288+
assert!(pref.is_empty() && suf.is_empty(), "wide slice is unaligned");
289+
Some(wide)
289290
} else {
290291
None
291292
}

0 commit comments

Comments
 (0)
0