polyxml 0.35.0__tar.gz → 0.35.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. {polyxml-0.35.0 → polyxml-0.35.1}/Cargo.lock +20 -6
  2. {polyxml-0.35.0 → polyxml-0.35.1}/Cargo.toml +1 -1
  3. {polyxml-0.35.0 → polyxml-0.35.1}/PKG-INFO +1 -1
  4. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/Cargo.toml +2 -0
  5. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/src/lib.rs +148 -4
  6. polyxml-0.35.1/crates/polyxml-python/tests/test_binary_fields.py +168 -0
  7. {polyxml-0.35.0 → polyxml-0.35.1}/pyproject.toml +1 -1
  8. {polyxml-0.35.0 → polyxml-0.35.1}/README.md +0 -0
  9. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/Cargo.toml +0 -0
  10. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/README.md +0 -0
  11. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/benches/core_benchmarks.rs +0 -0
  12. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/benches/support/tag_dispatch_fixtures.rs +0 -0
  13. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/benches/tag_dispatch.rs +0 -0
  14. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/cpp/mod.rs +0 -0
  15. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/csharp/mod.rs +0 -0
  16. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/go/mod.rs +0 -0
  17. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/java/codec.rs +0 -0
  18. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/java/mod.rs +0 -0
  19. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/java/models.rs +0 -0
  20. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/mod.rs +0 -0
  21. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/python/aot.rs +0 -0
  22. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/python/mod.rs +0 -0
  23. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/rust/mod.rs +0 -0
  24. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/codegen/typescript/mod.rs +0 -0
  25. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/converters.rs +0 -0
  26. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/error.rs +0 -0
  27. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/integer.rs +0 -0
  28. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/ir/chunker.rs +0 -0
  29. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/ir/mod.rs +0 -0
  30. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/ir/particle.rs +0 -0
  31. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/ir/tarjan.rs +0 -0
  32. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/json.rs +0 -0
  33. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/lib.rs +0 -0
  34. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/parser.rs +0 -0
  35. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/pattern.rs +0 -0
  36. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/schema.rs +0 -0
  37. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/schema_parser/mod.rs +0 -0
  38. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/schema_parser/upa.rs +0 -0
  39. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/serializer.rs +0 -0
  40. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/transcoder.rs +0 -0
  41. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/src/value.rs +0 -0
  42. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/fixtures/lexical_union.xsd +0 -0
  43. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/fixtures/mixed_content.xsd +0 -0
  44. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/fixtures/ts_mixed_content_inheritance.xsd +0 -0
  45. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_adversarial_identifiers.rs +0 -0
  46. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_any_attribute.rs +0 -0
  47. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_any_element.rs +0 -0
  48. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_conformance.rs +0 -0
  49. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_core.rs +0 -0
  50. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_cpp_codegen.rs +0 -0
  51. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_csharp_codegen.rs +0 -0
  52. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_default_literal_codegen.rs +0 -0
  53. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_enum_literal_codegen.rs +0 -0
  54. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_go_codegen.rs +0 -0
  55. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_inherited_simple_content.rs +0 -0
  56. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_inherited_text_codegen.rs +0 -0
  57. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_java_codegen.rs +0 -0
  58. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_json.rs +0 -0
  59. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_lexical_lists.rs +0 -0
  60. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_lexical_union_codegen.rs +0 -0
  61. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_mixed_branch_lookup.rs +0 -0
  62. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_mixed_content.rs +0 -0
  63. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_mixed_nil.rs +0 -0
  64. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_namespace_entities.rs +0 -0
  65. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_pattern_codegen.rs +0 -0
  66. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_python_codegen.rs +0 -0
  67. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_qualified_attributes.rs +0 -0
  68. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_runtime_fast_paths.rs +0 -0
  69. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_rust_codecs.rs +0 -0
  70. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_rust_codegen.rs +0 -0
  71. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_schema_attribute_entities.rs +0 -0
  72. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_schema_audit.rs +0 -0
  73. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_schema_ir.rs +0 -0
  74. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_security.rs +0 -0
  75. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_temporal_union.rs +0 -0
  76. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_transcoder.rs +0 -0
  77. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_ts_codegen.rs +0 -0
  78. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_ubl_csharp.rs +0 -0
  79. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_unbounded_choice.rs +0 -0
  80. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_upa.rs +0 -0
  81. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_value_constraints.rs +0 -0
  82. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_variant_registry.rs +0 -0
  83. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-core/tests/test_xsi_type.rs +0 -0
  84. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/README.md +0 -0
  85. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/benches/aot_vs_dataclass.py +0 -0
  86. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/tests/test_conformance.py +0 -0
  87. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/tests/test_generated_models.py +0 -0
  88. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/tests/test_json.py +0 -0
  89. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/tests/test_mixed_content.py +0 -0
  90. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/tests/test_polyxml.py +0 -0
  91. {polyxml-0.35.0 → polyxml-0.35.1}/crates/polyxml-python/tests/test_xsi_type.py +0 -0
  92. {polyxml-0.35.0 → polyxml-0.35.1}/python/polyxml/AGENT_GUIDE.md +0 -0
  93. {polyxml-0.35.0 → polyxml-0.35.1}/python/polyxml/__init__.py +0 -0
  94. {polyxml-0.35.0 → polyxml-0.35.1}/python/polyxml/compat/__init__.py +0 -0
  95. {polyxml-0.35.0 → polyxml-0.35.1}/python/polyxml/compat/xsdata.py +0 -0
  96. {polyxml-0.35.0 → polyxml-0.35.1}/python/polyxml/py.typed +0 -0
@@ -82,6 +82,12 @@ version = "1.5.1"
82
82
  source = "registry+https://github.com/rust-lang/crates.io-index"
83
83
  checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
84
84
 
85
+ [[package]]
86
+ name = "base64"
87
+ version = "0.22.1"
88
+ source = "registry+https://github.com/rust-lang/crates.io-index"
89
+ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
90
+
85
91
  [[package]]
86
92
  name = "bitflags"
87
93
  version = "2.13.2"
@@ -327,6 +333,12 @@ version = "0.5.0"
327
333
  source = "registry+https://github.com/rust-lang/crates.io-index"
328
334
  checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
329
335
 
336
+ [[package]]
337
+ name = "hex"
338
+ version = "0.4.3"
339
+ source = "registry+https://github.com/rust-lang/crates.io-index"
340
+ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
341
+
330
342
  [[package]]
331
343
  name = "indexmap"
332
344
  version = "2.14.2"
@@ -608,7 +620,7 @@ dependencies = [
608
620
 
609
621
  [[package]]
610
622
  name = "polyxml"
611
- version = "0.35.0"
623
+ version = "0.35.1"
612
624
  dependencies = [
613
625
  "criterion",
614
626
  "heck",
@@ -629,14 +641,14 @@ dependencies = [
629
641
 
630
642
  [[package]]
631
643
  name = "polyxml-c"
632
- version = "0.35.0"
644
+ version = "0.35.1"
633
645
  dependencies = [
634
646
  "polyxml",
635
647
  ]
636
648
 
637
649
  [[package]]
638
650
  name = "polyxml-cli"
639
- version = "0.35.0"
651
+ version = "0.35.1"
640
652
  dependencies = [
641
653
  "clap",
642
654
  "glob",
@@ -650,7 +662,7 @@ dependencies = [
650
662
 
651
663
  [[package]]
652
664
  name = "polyxml-js"
653
- version = "0.35.0"
665
+ version = "0.35.1"
654
666
  dependencies = [
655
667
  "napi",
656
668
  "napi-build",
@@ -660,8 +672,10 @@ dependencies = [
660
672
 
661
673
  [[package]]
662
674
  name = "polyxml-python"
663
- version = "0.35.0"
675
+ version = "0.35.1"
664
676
  dependencies = [
677
+ "base64",
678
+ "hex",
665
679
  "lexical-core",
666
680
  "polyxml",
667
681
  "pyo3",
@@ -670,7 +684,7 @@ dependencies = [
670
684
 
671
685
  [[package]]
672
686
  name = "polyxml-wasm"
673
- version = "0.35.0"
687
+ version = "0.35.1"
674
688
  dependencies = [
675
689
  "polyxml",
676
690
  "quick-xml",
@@ -3,7 +3,7 @@ resolver = "2"
3
3
  members = ["crates/polyxml-core", "crates/polyxml-python"]
4
4
 
5
5
  [workspace.package]
6
- version = "0.35.0"
6
+ version = "0.35.1"
7
7
  edition = "2021"
8
8
  rust-version = "1.80"
9
9
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyxml
3
- Version: 0.35.0
3
+ Version: 0.35.1
4
4
  Classifier: Development Status :: 4 - Beta
5
5
  Classifier: Intended Audience :: Developers
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -17,3 +17,5 @@ polyxml = { path = "../polyxml-core" }
17
17
  pyo3 = { version = "0.29", features = ["abi3-py312", "extension-module"] }
18
18
  lexical-core = "1.0"
19
19
  smallvec = "1.16"
20
+ hex = "0.4"
21
+ base64 = "0.22"
@@ -16,11 +16,114 @@ use polyxml::schema::{
16
16
  };
17
17
  use polyxml::value::PolyValue;
18
18
 
19
+ use base64::Engine;
19
20
  use smallvec::SmallVec;
21
+ use std::borrow::Cow;
20
22
 
21
23
  type PyObject = Py<PyAny>;
22
24
 
25
+ #[derive(Clone, Copy)]
26
+ enum BinaryCodec {
27
+ Base16,
28
+ Base64,
29
+ }
30
+
31
+ impl BinaryCodec {
32
+ fn from_metadata(
33
+ annotation: &Bound<'_, PyAny>,
34
+ metadata: Option<&Bound<'_, PyAny>>,
35
+ ) -> PyResult<Option<Self>> {
36
+ let mut target = unwrap_optional_type(annotation);
37
+ while let Ok(args) = target.getattr("__args__") {
38
+ let Ok(item) = args.get_item(0) else { break };
39
+ target = unwrap_optional_type(&item);
40
+ }
41
+ if !target.is(annotation.py().get_type::<PyBytes>()) {
42
+ return Ok(None);
43
+ }
44
+ let format = metadata
45
+ .and_then(|m| m.get_item("format").ok())
46
+ .filter(|v| !v.is_none());
47
+ let Some(format) = format else {
48
+ return Ok(None);
49
+ };
50
+ match format.extract::<String>()?.as_str() {
51
+ "base16" => Ok(Some(Self::Base16)),
52
+ "base64" => Ok(Some(Self::Base64)),
53
+ other => Err(pyo3::exceptions::PyValueError::new_err(format!(
54
+ "Unsupported bytes format: {other}"
55
+ ))),
56
+ }
57
+ }
58
+
59
+ fn decode(self, py: Python<'_>, text: &str) -> PyResult<PyObject> {
60
+ // Match xsdata binary fields: ignore XML whitespace, reject malformed data.
61
+ let text = if text
62
+ .bytes()
63
+ .any(|b| matches!(b, b' ' | b'\t' | b'\r' | b'\n'))
64
+ {
65
+ Cow::Owned(
66
+ text.chars()
67
+ .filter(|c| !matches!(c, ' ' | '\t' | '\r' | '\n'))
68
+ .collect::<String>(),
69
+ )
70
+ } else {
71
+ Cow::Borrowed(text)
72
+ };
73
+ let bytes = match self {
74
+ Self::Base16 => PyBytes::new_with(py, text.len() / 2, |out| {
75
+ hex::decode_to_slice(text.as_bytes(), out).map_err(|e| {
76
+ pyo3::exceptions::PyValueError::new_err(format!("Invalid base16 bytes: {e}"))
77
+ })
78
+ })?,
79
+ Self::Base64 => {
80
+ let padding = text.bytes().rev().take_while(|b| *b == b'=').count().min(2);
81
+ let len = (text.len() / 4 * 3).saturating_sub(padding);
82
+ PyBytes::new_with(py, len, |out| {
83
+ base64::engine::general_purpose::STANDARD
84
+ .decode_slice(text.as_bytes(), out)
85
+ .map(|_| ())
86
+ .map_err(|e| {
87
+ pyo3::exceptions::PyValueError::new_err(format!(
88
+ "Invalid base64 bytes: {e}"
89
+ ))
90
+ })
91
+ })?
92
+ }
93
+ };
94
+ Ok(bytes.into_any().unbind())
95
+ }
96
+
97
+ fn encode(self, value: &Bound<'_, PyAny>, val_type: &ValueType) -> PyResult<PolyValue> {
98
+ if value.is_none() {
99
+ return Ok(PolyValue::Null);
100
+ }
101
+ if let ValueType::List(inner) = val_type {
102
+ let list = value.cast::<PyList>()?;
103
+ return Ok(PolyValue::List(
104
+ list.iter()
105
+ .map(|item| self.encode(&item, inner))
106
+ .collect::<PyResult<_>>()?,
107
+ ));
108
+ }
109
+ let bytes = value.cast::<PyBytes>()?;
110
+ let text = match self {
111
+ Self::Base16 => hex::encode_upper(bytes.as_bytes()),
112
+ Self::Base64 => base64::engine::general_purpose::STANDARD.encode(bytes.as_bytes()),
113
+ };
114
+ Ok(PolyValue::String(text))
115
+ }
116
+ }
117
+
118
+ fn binary_lexical_type(val_type: &mut ValueType) {
119
+ match val_type {
120
+ ValueType::List(inner) => binary_lexical_type(inner),
121
+ _ => *val_type = ValueType::Scalar(ScalarType::String),
122
+ }
123
+ }
124
+
23
125
  struct CachedFieldMeta {
126
+ binary_codec: Option<BinaryCodec>,
24
127
  py_string: Py<PyString>,
25
128
  is_init: bool,
26
129
  #[allow(dead_code)]
@@ -28,6 +131,21 @@ struct CachedFieldMeta {
28
131
  py_type: Option<PyObject>,
29
132
  }
30
133
 
134
+ // The streaming core represents an empty record text field as absent. At the
135
+ // Python binary boundary, empty text is an empty byte string, not a missing arg.
136
+ fn record_field_value<'a>(
137
+ values: &'a [Option<PolyValue>],
138
+ index: usize,
139
+ field: &FieldSchema,
140
+ metadata: Option<&CachedFieldMeta>,
141
+ ) -> Option<&'a PolyValue> {
142
+ static EMPTY_TEXT: PolyValue = PolyValue::String(String::new());
143
+ values.get(index).and_then(Option::as_ref).or_else(|| {
144
+ (field.kind == FieldKind::Text && metadata.is_some_and(|m| m.binary_codec.is_some()))
145
+ .then_some(&EMPTY_TEXT)
146
+ })
147
+ }
148
+
31
149
  struct CachedSchemaMeta {
32
150
  schema: Arc<ModelSchema>,
33
151
  py_cls: PyObject,
@@ -414,9 +532,14 @@ fn extract_schema_from_class<'py>(
414
532
  field_obj.getattr("annotation")?
415
533
  };
416
534
  let unwrapped_type = unwrap_optional_type(&field_type);
417
- let val_type = resolve_value_type(py, &field_type)?;
535
+ let mut val_type = resolve_value_type(py, &field_type)?;
418
536
 
537
+ let binary_codec = BinaryCodec::from_metadata(&field_type, meta.as_ref())?;
538
+ if binary_codec.is_some() {
539
+ binary_lexical_type(&mut val_type);
540
+ }
419
541
  cached_fields.push(CachedFieldMeta {
542
+ binary_codec,
420
543
  py_string,
421
544
  is_init: true,
422
545
  is_kw_only: false,
@@ -528,9 +651,15 @@ fn extract_schema_from_class<'py>(
528
651
  field_obj.getattr("type")?
529
652
  };
530
653
  let unwrapped_type = unwrap_optional_type(&field_type);
531
- let val_type = resolve_value_type(py, &field_type)?;
654
+ let mut val_type = resolve_value_type(py, &field_type)?;
532
655
 
656
+ let metadata = field_obj.getattr("metadata").ok();
657
+ let binary_codec = BinaryCodec::from_metadata(&field_type, metadata.as_ref())?;
658
+ if binary_codec.is_some() {
659
+ binary_lexical_type(&mut val_type);
660
+ }
533
661
  cached_fields.push(CachedFieldMeta {
662
+ binary_codec,
534
663
  py_string,
535
664
  is_init,
536
665
  is_kw_only,
@@ -876,6 +1005,9 @@ fn poly_value_to_py<'py>(
876
1005
  cls: Option<&Bound<'py, PyAny>>,
877
1006
  field_meta: Option<&CachedFieldMeta>,
878
1007
  ) -> PyResult<PyObject> {
1008
+ if let (PolyValue::String(text), Some(codec)) = (val, field_meta.and_then(|m| m.binary_codec)) {
1009
+ return codec.decode(py, text);
1010
+ }
879
1011
  match val {
880
1012
  PolyValue::Null => Ok(py.None()),
881
1013
  PolyValue::Bool(b) => b.into_py_any(py),
@@ -964,7 +1096,9 @@ fn poly_value_to_py<'py>(
964
1096
  let mut all_found = true;
965
1097
 
966
1098
  for (i, field) in schema.fields.iter().enumerate() {
967
- if let Some(Some(field_val)) = values.get(i) {
1099
+ if let Some(field_val) =
1100
+ record_field_value(values, i, field, meta.fields.get(i))
1101
+ {
968
1102
  let field_cls =
969
1103
  if let ValueType::Nested(ref s) = field.val_type {
970
1104
  lookup_py_class(py, &s.name)
@@ -1011,7 +1145,12 @@ fn poly_value_to_py<'py>(
1011
1145
  let mut post_init_fields: Vec<(Py<PyString>, PyObject)> = Vec::new();
1012
1146
 
1013
1147
  for (i, field) in schema.fields.iter().enumerate() {
1014
- if let Some(Some(field_val)) = values.get(i) {
1148
+ if let Some(field_val) = record_field_value(
1149
+ values,
1150
+ i,
1151
+ field,
1152
+ meta_opt.as_ref().and_then(|m| m.fields.get(i)),
1153
+ ) {
1015
1154
  let field_cls = if let ValueType::Nested(ref s) = field.val_type {
1016
1155
  lookup_py_class(py, &s.name)
1017
1156
  } else {
@@ -1363,6 +1502,11 @@ fn py_to_poly_value<'py>(
1363
1502
  }
1364
1503
  }
1365
1504
 
1505
+ if let Some(codec) = meta.fields.get(i).and_then(|m| m.binary_codec) {
1506
+ values[i] = Some(codec.encode(&val, &field.val_type)?);
1507
+ continue;
1508
+ }
1509
+
1366
1510
  match &field.val_type {
1367
1511
  ValueType::Scalar(st) => match st {
1368
1512
  ScalarType::List(inner) => {
@@ -0,0 +1,168 @@
1
+ """Native codecs preserve Python bytes through cached serde."""
2
+
3
+ import io
4
+ from dataclasses import dataclass, field
5
+
6
+ import pytest
7
+ from pydantic import BaseModel, Field
8
+
9
+ import polyxml
10
+
11
+
12
+ @pytest.mark.parametrize(("codec", "encoded"), [("base16", "0001FEFF"), ("base64", "AAH+/w==")])
13
+ @pytest.mark.parametrize("kind", ["Element", "Attribute", "Text"])
14
+ @pytest.mark.parametrize("pydantic", [False, True])
15
+ def test_binary_roundtrip(codec, encoded, kind, pydantic):
16
+ metadata = {"type": kind, "format": codec}
17
+ if pydantic:
18
+
19
+ class Binary(BaseModel):
20
+ value: bytes = Field(json_schema_extra=metadata)
21
+ else:
22
+
23
+ @dataclass
24
+ class Binary:
25
+ value: bytes = field(metadata=metadata)
26
+
27
+ value = Binary(value=b"\x00\x01\xfe\xff")
28
+ for _ in range(2):
29
+ xml = polyxml.serialize(value)
30
+ assert encoded.encode() in xml
31
+ parsed = polyxml.deserialize(xml, Binary)
32
+ assert parsed.value == value.value
33
+ assert type(parsed.value) is bytes
34
+
35
+
36
+ @pytest.mark.parametrize("codec", ["base16", "base64"])
37
+ @pytest.mark.parametrize("pydantic", [False, True])
38
+ def test_binary_optional_repeated_nested_and_empty(codec, pydantic):
39
+ if pydantic:
40
+
41
+ class Binary(BaseModel):
42
+ values: list[bytes] = Field(json_schema_extra={"format": codec})
43
+ optional: bytes | None = Field(default=None, json_schema_extra={"format": codec})
44
+ else:
45
+
46
+ @dataclass
47
+ class Binary:
48
+ values: list[bytes] = field(metadata={"format": codec})
49
+ optional: bytes | None = field(default=None, metadata={"format": codec})
50
+
51
+ @dataclass
52
+ class Container:
53
+ binary: Binary
54
+
55
+ for optional in (None, b"", b"\x00\xff"):
56
+ value = Container(Binary(values=[b"", b"\x00\xff", b"123"], optional=optional))
57
+ assert polyxml.deserialize(polyxml.serialize(value), Container) == value
58
+
59
+
60
+ @pytest.mark.parametrize(
61
+ ("codec", "text", "expected"),
62
+ [
63
+ ("base16", " 00 aB\nCD\tff ", b"\x00\xab\xcd\xff"),
64
+ ("base64", " A A\nH+\t/w== ", b"\x00\x01\xfe\xff"),
65
+ ("base16", "", b""),
66
+ ("base64", "", b""),
67
+ ],
68
+ )
69
+ def test_binary_lexical_input(codec, text, expected):
70
+ @dataclass
71
+ class Binary:
72
+ value: bytes = field(metadata={"format": codec})
73
+
74
+ parsed = polyxml.deserialize(f"<Binary><value>{text}</value></Binary>", Binary)
75
+ assert parsed.value == expected
76
+ assert type(parsed.value) is bytes
77
+
78
+
79
+ @pytest.mark.parametrize(
80
+ ("codec", "text"),
81
+ [("base16", "ABC"), ("base16", "GG"), ("base64", "!!!!"), ("base64", "Zg=")],
82
+ )
83
+ def test_binary_malformed_data(codec, text):
84
+ @dataclass
85
+ class Binary:
86
+ value: bytes = field(metadata={"format": codec})
87
+
88
+ with pytest.raises(ValueError, match=f"Invalid {codec} bytes"):
89
+ polyxml.deserialize(f"<Binary><value>{text}</value></Binary>", Binary)
90
+
91
+
92
+ def test_binary_unknown_codec():
93
+ @dataclass
94
+ class Binary:
95
+ value: bytes = field(metadata={"format": "base85"})
96
+
97
+ with pytest.raises(ValueError, match="Unsupported bytes format: base85"):
98
+ polyxml.serialize(Binary(b"abc"))
99
+
100
+
101
+ def test_binary_wrong_python_type():
102
+ @dataclass
103
+ class Binary:
104
+ value: bytes = field(metadata={"format": "base16"})
105
+
106
+ with pytest.raises(TypeError):
107
+ polyxml.serialize(Binary("ABCD"))
108
+
109
+
110
+ def test_binary_streaming_and_json():
111
+ @dataclass
112
+ class Binary:
113
+ value: bytes = field(metadata={"format": "base16"})
114
+
115
+ source = b"<root><Binary><value>00FF</value></Binary></root>"
116
+ assert list(polyxml.iterparse(io.BytesIO(source), Binary, tag="Binary")) == [
117
+ Binary(b"\x00\xff")
118
+ ]
119
+ encoded = polyxml.serialize_json(Binary(b"\x00\xff"))
120
+ assert polyxml.deserialize_json(encoded, Binary) == Binary(b"\x00\xff")
121
+
122
+
123
+ def test_nonbinary_format_metadata_is_unchanged():
124
+ @dataclass
125
+ class Text:
126
+ value: str = field(metadata={"format": "base16"})
127
+
128
+ value = Text("00FF")
129
+ assert polyxml.deserialize(polyxml.serialize(value), Text) == value
130
+
131
+
132
+ @pytest.mark.parametrize("codec", ["base16", "base64"])
133
+ def test_binary_all_byte_values(codec):
134
+ @dataclass
135
+ class Packet:
136
+ value: bytes = field(metadata={"format": codec})
137
+
138
+ for length in range(260):
139
+ value = Packet((bytes(range(256)) * 2)[:length])
140
+ assert polyxml.deserialize(polyxml.serialize(value), Packet) == value
141
+
142
+
143
+ def test_binary_nil_is_distinct_from_empty():
144
+ @dataclass
145
+ class Packet:
146
+ value: bytes | None = field(default=None, metadata={"format": "base16"})
147
+
148
+ xml = '<Packet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><value xsi:nil="true"/></Packet>'
149
+ assert polyxml.deserialize(xml, Packet).value is None
150
+ assert polyxml.deserialize("<Packet><value/></Packet>", Packet).value == b""
151
+
152
+
153
+ @pytest.mark.parametrize("codec", ["base16", "base64"])
154
+ @pytest.mark.parametrize("pydantic", [False, True])
155
+ def test_binary_empty_text(codec, pydantic):
156
+ metadata = {"type": "Text", "format": codec}
157
+ if pydantic:
158
+
159
+ class EmptyText(BaseModel):
160
+ value: bytes = Field(json_schema_extra=metadata)
161
+ else:
162
+
163
+ @dataclass
164
+ class EmptyText:
165
+ value: bytes = field(metadata=metadata)
166
+
167
+ value = EmptyText(value=b"")
168
+ assert polyxml.deserialize(polyxml.serialize(value), EmptyText) == value
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "polyxml"
7
- version = "0.35.0"
7
+ version = "0.35.1"
8
8
  description = "High-performance, polyglot XML data-binding engine built in Rust"
9
9
  authors = [
10
10
  { name = "Bailey Nguyen", email = "bailey.tan.nguyen@gmail.com" }
File without changes