| 398 | } |
| 399 | |
| 400 | fn generate_class_def( |
| 401 | ident: &Ident, |
| 402 | name: &str, |
| 403 | module_name: Option<&str>, |
| 404 | base: Option<syn::Path>, |
| 405 | metaclass: Option<String>, |
| 406 | unhashable: bool, |
| 407 | attrs: &[Attribute], |
| 408 | ) -> Result<TokenStream> { |
| 409 | let doc = attrs.doc().or_else(|| { |
| 410 | let module_name = module_name.unwrap_or("builtins"); |
| 411 | DB.get(&format!("{module_name}.{name}")) |
| 412 | .copied() |
| 413 | .map(str::to_owned) |
| 414 | }); |
| 415 | let doc = if let Some(doc) = doc { |
| 416 | quote!(Some(#doc)) |
| 417 | } else { |
| 418 | quote!(None) |
| 419 | }; |
| 420 | let module_class_name = if let Some(module_name) = module_name { |
| 421 | format!("{module_name}.{name}") |
| 422 | } else { |
| 423 | name.to_owned() |
| 424 | }; |
| 425 | let module_name = match module_name { |
| 426 | Some(v) => quote!(Some(#v) ), |
| 427 | None => quote!(None), |
| 428 | }; |
| 429 | let unhashable = if unhashable { |
| 430 | quote!(true) |
| 431 | } else { |
| 432 | quote!(false) |
| 433 | }; |
| 434 | let is_pystruct = attrs.iter().any(|attr| { |
| 435 | attr.path().is_ident("derive") |
| 436 | && if let Ok(Meta::List(l)) = attr.parse_meta() { |
| 437 | l.nested |
| 438 | .into_iter() |
| 439 | .any(|n| n.get_ident().is_some_and(|p| p == "PyStructSequence")) |
| 440 | } else { |
| 441 | false |
| 442 | } |
| 443 | }); |
| 444 | // Check if the type has #[repr(transparent)] - only then we can safely |
| 445 | // generate PySubclass impl (requires same memory layout as base type) |
| 446 | let is_repr_transparent = attrs.iter().any(|attr| { |
| 447 | attr.path().is_ident("repr") |
| 448 | && if let Ok(Meta::List(l)) = attr.parse_meta() { |
| 449 | l.nested |
| 450 | .into_iter() |
| 451 | .any(|n| n.get_ident().is_some_and(|p| p == "transparent")) |
| 452 | } else { |
| 453 | false |
| 454 | } |
| 455 | }); |
| 456 | // If repr(transparent) with a base, the type has the same memory layout as base, |
| 457 | // so basicsize should be 0 (no additional space beyond the base type) |