Skip to content

Reorganize os module #2041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions derive/src/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn meta_to_vec(meta: Meta) -> Result<Vec<NestedMeta>, Meta> {

#[derive(Default)]
struct Module {
items: HashSet<ModuleItem>,
items: HashSet<(ModuleItem, Vec<Meta>)>,
}

#[derive(PartialEq, Eq, Hash)]
Expand All @@ -25,13 +25,13 @@ enum ModuleItem {
}

impl Module {
fn add_item(&mut self, item: ModuleItem, span: Span) -> Result<(), Diagnostic> {
fn add_item(&mut self, item: (ModuleItem, Vec<Meta>), span: Span) -> Result<(), Diagnostic> {
if self.items.insert(item) {
Ok(())
} else {
Err(Diagnostic::span_error(
span,
"Duplicate #[py*] attribute on pyimpl".to_owned(),
"Duplicate #[py*] attribute on pymodule".to_owned(),
))
}
}
Expand Down Expand Up @@ -70,12 +70,35 @@ impl Module {
})
}

fn extract_struct_sequence(ident: &Ident, meta: Meta) -> Result<ModuleItem, Diagnostic> {
let nesteds = meta_to_vec(meta).map_err(|meta| {
err_span!(
meta,
"#[pystruct_sequence = \"...\"] cannot be a name/value, you probably meant \
#[pystruct_sequence(name = \"...\")]",
)
})?;

let item_meta = ItemMeta::from_nested_meta(
"pystruct_sequence",
&ident,
&nesteds,
ItemMeta::STRUCT_SEQUENCE_NAMES,
)?;
Ok(ModuleItem::Class {
item_ident: ident.clone(),
py_name: item_meta.simple_name()?,
})
}

fn extract_item_from_syn(
&mut self,
attrs: &mut Vec<Attribute>,
ident: &Ident,
) -> Result<(), Diagnostic> {
let mut attr_idxs = Vec::new();
let mut items = Vec::new();
let mut cfgs = Vec::new();
for (i, meta) in attrs
.iter()
.filter_map(|attr| attr.parse_meta().ok())
Expand All @@ -86,17 +109,28 @@ impl Module {
Some(name) => name,
None => continue,
};
let item = match name.to_string().as_str() {
match name.to_string().as_str() {
"pyfunction" => {
attr_idxs.push(i);
Self::extract_function(ident, meta)?
items.push((Self::extract_function(ident, meta)?, meta_span));
}
"pyclass" => {
items.push((Self::extract_class(ident, meta)?, meta_span));
}
"pystruct_sequence" => {
items.push((Self::extract_struct_sequence(ident, meta)?, meta_span));
}
"cfg" => {
cfgs.push(meta);
continue;
}
"pyclass" => Self::extract_class(ident, meta)?,
_ => {
continue;
}
};
self.add_item(item, meta_span)?;
}
for (item, meta) in items {
self.add_item((item, cfgs.clone()), meta)?;
}
let mut i = 0;
let mut attr_idxs = &*attr_idxs;
Expand Down Expand Up @@ -130,7 +164,7 @@ fn extract_module_items(
);
}

let functions = module.items.into_iter().map(|item| match item {
let functions = module.items.into_iter().map(|(item, cfgs)| match item {
ModuleItem::Function {
item_ident,
py_name,
Expand All @@ -140,6 +174,7 @@ fn extract_module_items(
#module_name.to_owned(),
#py_name.to_owned()));
quote! {
#( #[ #cfgs ])*
vm.__module_set_attr(&module, #py_name, vm.ctx#new_func).unwrap();
}
}
Expand Down Expand Up @@ -192,7 +227,7 @@ pub fn impl_pymodule(attr: AttributeArgs, item: Item) -> Result<TokenStream2, Di
let extend_mod = extract_module_items(items, &module_name)?;
content.extend(vec![
parse_quote! {
const MODULE_NAME: &str = #module_name;
pub(crate) const MODULE_NAME: &str = #module_name;
},
parse_quote! {
pub(crate) fn extend_module(
Expand Down
1 change: 1 addition & 0 deletions derive/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct ItemMeta<'a> {

impl<'a> ItemMeta<'a> {
pub const SIMPLE_NAMES: &'static [&'static str] = &["name"];
pub const STRUCT_SEQUENCE_NAMES: &'static [&'static str] = &["module", "name"];
pub const ATTRIBUTE_NAMES: &'static [&'static str] = &["name", "magic"];
pub const PROPERTY_NAMES: &'static [&'static str] = &["name", "magic", "setter"];

Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ mod fileio {
}
fd
} else {
os::os_open(
os::open(
os::PyPathLike::new_str(name.as_str().to_owned()),
mode as _,
OptionalArg::Missing,
Expand Down
Loading