Skip to content

Commit 64cff17

Browse files
committed
pymethod(raw)
1 parent bca90f1 commit 64cff17

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎derive-impl/src/pyclass.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ where
752752
let item_meta = MethodItemMeta::from_attr(ident.clone(), &item_attr)?;
753753

754754
let py_name = item_meta.method_name()?;
755+
let raw = item_meta.raw()?;
755756
let sig_doc = text_signature(func.sig(), &py_name);
756757

757758
let doc = args.attrs.doc().map(|doc| format_doc(&sig_doc, &doc));
@@ -760,6 +761,7 @@ where
760761
cfgs: args.cfgs.to_vec(),
761762
ident: ident.to_owned(),
762763
doc,
764+
raw,
763765
attr_name: self.inner.attr_name,
764766
});
765767
Ok(())
@@ -954,6 +956,7 @@ struct MethodNurseryItem {
954956
py_name: String,
955957
cfgs: Vec<Attribute>,
956958
ident: Ident,
959+
raw: bool,
957960
doc: Option<String>,
958961
attr_name: AttrName,
959962
}
@@ -1005,9 +1008,14 @@ impl ToTokens for MethodNursery {
10051008
// } else {
10061009
// quote_spanned! { ident.span() => #py_name }
10071010
// };
1011+
let method_new = if item.raw {
1012+
quote!(new_raw_const)
1013+
} else {
1014+
quote!(new_const)
1015+
};
10081016
inner_tokens.extend(quote! [
10091017
#(#cfgs)*
1010-
rustpython_vm::function::PyMethodDef::new_const(
1018+
rustpython_vm::function::PyMethodDef::#method_new(
10111019
#py_name,
10121020
Self::#ident,
10131021
#flags,
@@ -1203,7 +1211,7 @@ impl ToTokens for MemberNursery {
12031211
struct MethodItemMeta(ItemMetaInner);
12041212

12051213
impl ItemMeta for MethodItemMeta {
1206-
const ALLOWED_NAMES: &'static [&'static str] = &["name", "magic"];
1214+
const ALLOWED_NAMES: &'static [&'static str] = &["name", "magic", "raw"];
12071215

12081216
fn from_inner(inner: ItemMetaInner) -> Self {
12091217
Self(inner)
@@ -1214,6 +1222,9 @@ impl ItemMeta for MethodItemMeta {
12141222
}
12151223

12161224
impl MethodItemMeta {
1225+
fn raw(&self) -> Result<bool> {
1226+
self.inner()._bool("raw")
1227+
}
12171228
fn method_name(&self) -> Result<String> {
12181229
let inner = self.inner();
12191230
let name = inner._optional_str("name")?;

‎vm/src/function/builtin.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ pub const fn static_func<Kind, F: IntoPyNativeFn<Kind>>(f: F) -> &'static dyn Py
8181
zst_ref_out_of_thin_air(into_func(f))
8282
}
8383

84+
#[inline(always)]
85+
pub const fn static_raw_func<F: PyNativeFn>(f: F) -> &'static dyn PyNativeFn {
86+
zst_ref_out_of_thin_air(f)
87+
}
88+
8489
// TODO: once higher-rank trait bounds are stabilized, remove the `Kind` type
8590
// parameter and impl for F where F: for<T, R, VM> PyNativeFnInternal<T, R, VM>
8691
impl<F, T, R, VM> IntoPyNativeFn<(T, R, VM)> for F

‎vm/src/function/method.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ impl PyMethodDef {
8787
}
8888
}
8989

90+
#[inline]
91+
pub const fn new_raw_const(
92+
name: &'static str,
93+
func: impl PyNativeFn,
94+
flags: PyMethodFlags,
95+
doc: Option<&'static str>,
96+
) -> Self {
97+
Self {
98+
name,
99+
func: super::static_raw_func(func),
100+
flags,
101+
doc,
102+
}
103+
}
104+
90105
pub fn to_proper_method(
91106
&'static self,
92107
class: &'static Py<PyType>,

‎vm/src/function/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use argument::{
1515
};
1616
pub use arithmetic::{PyArithmeticValue, PyComparisonValue};
1717
pub use buffer::{ArgAsciiBuffer, ArgBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike};
18-
pub use builtin::{static_func, IntoPyNativeFn, PyNativeFn};
18+
pub use builtin::{static_func, static_raw_func, IntoPyNativeFn, PyNativeFn};
1919
pub use either::Either;
2020
pub use fspath::FsPath;
2121
pub use getset::PySetterValue;

0 commit comments

Comments
 (0)