Skip to content

Commit 806c81b

Browse files
committed
winreg constants to #[pymodule]
1 parent de3df09 commit 806c81b

File tree

1 file changed

+29
-48
lines changed

1 file changed

+29
-48
lines changed

vm/src/stdlib/winreg.rs

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
#![allow(non_snake_case)]
22

3+
#[pymodule]
4+
mod winreg {
5+
// access rights
6+
#[pyattr]
7+
pub use winapi::um::winnt::{
8+
KEY_ALL_ACCESS, KEY_CREATE_LINK, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_EXECUTE,
9+
KEY_NOTIFY, KEY_QUERY_VALUE, KEY_READ, KEY_SET_VALUE, KEY_WOW64_32KEY, KEY_WOW64_64KEY,
10+
KEY_WRITE,
11+
};
12+
// value types
13+
#[pyattr]
14+
pub use winapi::um::winnt::{
15+
REG_BINARY, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_DWORD_LITTLE_ENDIAN, REG_EXPAND_SZ,
16+
REG_FULL_RESOURCE_DESCRIPTOR, REG_LINK, REG_MULTI_SZ, REG_NONE, REG_QWORD,
17+
REG_QWORD_LITTLE_ENDIAN, REG_RESOURCE_LIST, REG_RESOURCE_REQUIREMENTS_LIST, REG_SZ,
18+
};
19+
}
20+
321
use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
422
use crate::{
523
builtins::PyStrRef, function::IntoPyException, PyClassImpl, PyObjectRef, PyRef, PyResult,
624
PyValue, TryFromObject, VirtualMachine,
725
};
26+
use ::winreg::{enums::RegType, RegKey, RegValue};
827
use std::convert::TryInto;
928
use std::ffi::OsStr;
1029
use std::io;
1130
use winapi::shared::winerror;
12-
use winreg::{enums::RegType, RegKey, RegValue};
1331

1432
#[pyclass(module = "winreg", name = "HKEYType")]
1533
#[derive(Debug, PyValue)]
@@ -39,13 +57,13 @@ impl PyHKEY {
3957

4058
#[pymethod]
4159
fn Close(&self) {
42-
let null_key = RegKey::predef(0 as winreg::HKEY);
60+
let null_key = RegKey::predef(0 as ::winreg::HKEY);
4361
let key = std::mem::replace(&mut *self.key_mut(), null_key);
4462
drop(key);
4563
}
4664
#[pymethod]
4765
fn Detach(&self) -> usize {
48-
let null_key = RegKey::predef(0 as winreg::HKEY);
66+
let null_key = RegKey::predef(0 as ::winreg::HKEY);
4967
let key = std::mem::replace(&mut *self.key_mut(), null_key);
5068
let handle = key.raw_handle();
5169
std::mem::forget(key);
@@ -68,13 +86,13 @@ impl PyHKEY {
6886

6987
enum Hkey {
7088
PyHKEY(PyHKEYRef),
71-
Constant(winreg::HKEY),
89+
Constant(::winreg::HKEY),
7290
}
7391
impl TryFromObject for Hkey {
7492
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
7593
obj.downcast()
7694
.map(Self::PyHKEY)
77-
.or_else(|o| usize::try_from_object(vm, o).map(|i| Self::Constant(i as winreg::HKEY)))
95+
.or_else(|o| usize::try_from_object(vm, o).map(|i| Self::Constant(i as ::winreg::HKEY)))
7896
}
7997
}
8098
impl Hkey {
@@ -104,7 +122,7 @@ struct OpenKeyArgs {
104122
sub_key: Option<PyStrRef>,
105123
#[pyarg(any, default = "0")]
106124
reserved: i32,
107-
#[pyarg(any, default = "winreg::enums::KEY_READ")]
125+
#[pyarg(any, default = "::winreg::enums::KEY_READ")]
108126
access: u32,
109127
}
110128

@@ -204,7 +222,7 @@ fn winreg_SetValue(
204222
value: PyStrRef,
205223
vm: &VirtualMachine,
206224
) -> PyResult<()> {
207-
if typ != winreg::enums::REG_SZ as u32 {
225+
if typ != winreg::REG_SZ as u32 {
208226
return Err(vm.new_type_error("type must be winreg.REG_SZ".to_owned()));
209227
}
210228
let subkey = subkey.as_ref().map_or("", |s| s.as_str());
@@ -284,9 +302,10 @@ fn reg_to_py(value: RegValue, vm: &VirtualMachine) -> PyResult {
284302
}
285303

286304
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
305+
let module = winreg::make_module(vm);
287306
let ctx = &vm.ctx;
288307
let hkey_type = PyHKEY::make_class(ctx);
289-
let module = py_module!(vm, "winreg", {
308+
extend_module!(vm, module, {
290309
"HKEYType" => hkey_type,
291310
"OpenKey" => named_function!(ctx, winreg, OpenKey),
292311
"OpenKeyEx" => named_function!(ctx, winreg, OpenKey),
@@ -301,20 +320,14 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
301320
});
302321

303322
macro_rules! add_constants {
304-
(hkey, $($name:ident),*$(,)?) => {
305-
extend_module!(vm, module, {
306-
$((stringify!($name)) => vm.new_pyobj(winreg::enums::$name as usize)),*
307-
})
308-
};
309-
(winnt, $($name:ident),*$(,)?) => {
323+
($($name:ident),*$(,)?) => {
310324
extend_module!(vm, module, {
311-
$((stringify!($name)) => vm.new_pyobj(winapi::um::winnt::$name)),*
325+
$((stringify!($name)) => vm.new_pyobj(::winreg::enums::$name as usize)),*
312326
})
313327
};
314328
}
315329

316330
add_constants!(
317-
hkey,
318331
HKEY_CLASSES_ROOT,
319332
HKEY_CURRENT_USER,
320333
HKEY_LOCAL_MACHINE,
@@ -323,37 +336,5 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
323336
HKEY_CURRENT_CONFIG,
324337
HKEY_DYN_DATA,
325338
);
326-
add_constants!(
327-
winnt,
328-
// access rights
329-
KEY_ALL_ACCESS,
330-
KEY_WRITE,
331-
KEY_READ,
332-
KEY_EXECUTE,
333-
KEY_QUERY_VALUE,
334-
KEY_SET_VALUE,
335-
KEY_CREATE_SUB_KEY,
336-
KEY_ENUMERATE_SUB_KEYS,
337-
KEY_NOTIFY,
338-
KEY_CREATE_LINK,
339-
KEY_WOW64_64KEY,
340-
KEY_WOW64_32KEY,
341-
// value types
342-
REG_BINARY,
343-
REG_DWORD,
344-
REG_DWORD_LITTLE_ENDIAN,
345-
REG_DWORD_BIG_ENDIAN,
346-
REG_EXPAND_SZ,
347-
REG_LINK,
348-
REG_MULTI_SZ,
349-
REG_NONE,
350-
REG_QWORD,
351-
REG_QWORD_LITTLE_ENDIAN,
352-
REG_RESOURCE_LIST,
353-
REG_FULL_RESOURCE_DESCRIPTOR,
354-
REG_RESOURCE_REQUIREMENTS_LIST,
355-
REG_SZ,
356-
);
357-
358339
module
359340
}

0 commit comments

Comments
 (0)