Skip to content

Commit 8376ec2

Browse files
committed
Use doc comments for python __doc__
1 parent ab60318 commit 8376ec2

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

derive/src/lib.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ fn impl_py_class(attr: AttributeArgs, item: Item) -> TokenStream2 {
340340
};
341341
let rp_path = rustpython_path_attr(&attr);
342342
let mut class_name = None;
343-
let mut doc = None;
344343
for attr in attr {
345344
if let NestedMeta::Meta(meta) = attr {
346345
if let Meta::NameValue(name_value) = meta {
@@ -350,19 +349,35 @@ fn impl_py_class(attr: AttributeArgs, item: Item) -> TokenStream2 {
350349
} else {
351350
panic!("#[py_class(name = ...)] must be a string");
352351
}
353-
} else if name_value.ident == "doc" {
354-
if let Lit::Str(s) = name_value.lit {
355-
doc = Some(s.value());
356-
} else {
357-
panic!("#[py_class(name = ...)] must be a string");
358-
}
359352
}
360353
}
361354
}
362355
}
363356
let class_name = class_name.expect("#[py_class] must have a name");
357+
let mut doc: Option<Vec<String>> = None;
358+
for attr in imp.attrs.iter() {
359+
if attr.path.is_ident("doc") {
360+
let meta = attr.parse_meta().expect("expected doc attr to be a meta");
361+
if let Meta::NameValue(name_value) = meta {
362+
if let Lit::Str(s) = name_value.lit {
363+
let val = s.value().trim().to_string();
364+
match doc {
365+
Some(ref mut doc) => doc.push(val),
366+
None => doc = Some(vec![val]),
367+
}
368+
} else {
369+
panic!("expected #[doc = ...] to be a string")
370+
}
371+
} else {
372+
panic!("expected #[doc] to be a NameValue, e.g. #[doc = \"...\"");
373+
}
374+
}
375+
}
364376
let doc = match doc {
365-
Some(doc) => quote!(Some(#doc)),
377+
Some(doc) => {
378+
let doc = doc.join("\n");
379+
quote!(Some(#doc))
380+
}
366381
None => quote!(None),
367382
};
368383
let ty = &imp.self_ty;

vm/src/obj/objstr.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,17 @@ fn get_fill_char<'a>(rep: &'a OptionalArg<PyStringRef>, vm: &VirtualMachine) ->
6060
}
6161
}
6262

63-
#[py_class(
64-
__inside_vm,
65-
name = "str",
66-
doc = "str(object='') -> str\n\
67-
str(bytes_or_buffer[, encoding[, errors]]) -> str\n\
68-
\n\
69-
Create a new string object from the given object. If encoding or\n\
70-
errors is specified, then the object must expose a data buffer\n\
71-
that will be decoded using the given encoding and error handler.\n\
72-
Otherwise, returns the result of object.__str__() (if defined)\n\
73-
or repr(object).\n\
74-
encoding defaults to sys.getdefaultencoding().\n\
75-
errors defaults to 'strict'."
76-
)]
63+
#[py_class(__inside_vm, name = "str")]
64+
/// str(object='') -> str
65+
/// str(bytes_or_buffer[, encoding[, errors]]) -> str
66+
///
67+
/// Create a new string object from the given object. If encoding or
68+
/// errors is specified, then the object must expose a data buffer
69+
/// that will be decoded using the given encoding and error handler.
70+
/// Otherwise, returns the result of object.__str__() (if defined)
71+
/// or repr(object).
72+
/// encoding defaults to sys.getdefaultencoding().
73+
/// errors defaults to 'strict'."
7774
impl PyStringRef {
7875
// TODO: should with following format
7976
// class str(object='')

0 commit comments

Comments
 (0)