consortium-py 0.3.0

Python bindings for consortium (ClusterShell-compatible API)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
//! PyO3 wrappers for consortium::range_set.
//!
//! Exposes a Python-visible `RangeSet` class with the same public API as
//! upstream `ClusterShell.RangeSet.RangeSet` (ClusterShell 1.10.1), backed by
//! `consortium::range_set::RangeSet`.

use pyo3::exceptions::{PyAssertionError, PyIndexError, PyKeyError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyIterator, PyList, PySlice, PyTuple, PyType};

use consortium::range_set::{RangeSet as RustRangeSet, RangeSetError, AUTOSTEP_DISABLED};

pyo3::create_exception!(
    ClusterShell.RangeSet,
    RangeSetException,
    pyo3::exceptions::PyException
);
pyo3::create_exception!(ClusterShell.RangeSet, RangeSetParseError, RangeSetException);
pyo3::create_exception!(
    ClusterShell.RangeSet,
    RangeSetPaddingError,
    RangeSetParseError
);

/// RangeSet serial version number (matches upstream RangeSet._VERSION).
const RANGESET_VERSION: i32 = 4;

/// Convert a core RangeSetError into the matching Python exception,
/// formatting the message like upstream RangeSetParseError and attaching
/// the faulty subrange as the `part` attribute.
fn to_py_err(py: Python<'_>, err: RangeSetError) -> PyErr {
    let (part, msg, is_padding) = match err {
        RangeSetError::ParseError { part, msg } => (part, msg, false),
        RangeSetError::PaddingError { part, msg } => (part, msg, true),
    };
    let full = if part.is_empty() {
        msg.clone()
    } else {
        format!("{} : \"{}\"", msg, part)
    };
    let exc: PyErr = if is_padding {
        RangeSetPaddingError::new_err(format!("padding mismatch ({})", full))
    } else {
        RangeSetParseError::new_err(full)
    };
    {
        let val = exc.value_bound(py);
        let _ = val.setattr("part", part);
    }
    exc
}

/// Build a RangeSetParseError (with .part) from static parts.
fn parse_err(py: Python<'_>, part: &str, msg: &str) -> PyErr {
    to_py_err(
        py,
        RangeSetError::ParseError {
            part: part.to_string(),
            msg: msg.to_string(),
        },
    )
}

/// Extract an autostep threshold from an arbitrary Python value, mirroring
/// upstream RangeSet.set_autostep(): None or any value >= AUTOSTEP_DISABLED
/// (eg. the 1E100 float constant) disables stepping. The core stores the
/// threshold as Option<u32>, so values in (u32::MAX, 1E100) are clamped to
/// "disabled" — folding behavior is identical for any such huge threshold,
/// only a getter read-back would diverge (no upstream test does this).
pub(crate) fn extract_autostep(val: Option<&Bound<'_, PyAny>>) -> PyResult<Option<u32>> {
    match val {
        None => Ok(None),
        Some(v) if v.is_none() => Ok(None),
        Some(v) => {
            let f: f64 = v.extract()?;
            if f >= AUTOSTEP_DISABLED || f >= u32::MAX as f64 {
                Ok(None)
            } else {
                // saturating cast also covers negatives (-> 0)
                Ok(Some(f as u32))
            }
        }
    }
}

/// Oracle-faithful port of RangeSet._parse(), used instead of the core
/// parser because the binding must reproduce Python behaviors the core
/// parser does not cover: negative ranges ("-9--6") and whitespace-tolerant
/// integer conversion ("0 -8 / 2").
fn parse_oracle(py: Python<'_>, pattern: &str, autostep: Option<u32>) -> PyResult<RustRangeSet> {
    let mut rs = RustRangeSet::new();
    rs.set_autostep(autostep);
    for raw in pattern.split(',') {
        let subrange = raw.trim();

        let (baserange, step): (&str, i64) = match subrange.find('/') {
            None => (subrange, 1),
            Some(pos) => {
                let step_str = subrange[pos + 1..].trim();
                match step_str.parse::<i64>() {
                    Ok(v) => (&subrange[..pos], v),
                    Err(_) => {
                        return Err(parse_err(py, subrange, "cannot convert string to integer"))
                    }
                }
            }
        };

        // parse begin/end parts, handling negative numbers
        let (begin, end, begin_sign, end_sign): (String, String, i64, i64) =
            if !baserange.contains('-') {
                if step != 1 {
                    return Err(parse_err(py, subrange, "invalid step usage"));
                }
                (baserange.to_string(), baserange.to_string(), 1, 1)
            } else {
                let parts: Vec<&str> = baserange.split('-').collect();
                match parts.len() {
                    2 => {
                        let b = parts[0].trim();
                        let e = parts[1].trim();
                        if b.is_empty() {
                            // single negative number "-5"
                            (e.to_string(), e.to_string(), -1, -1)
                        } else {
                            (b.to_string(), e.to_string(), 1, 1)
                        }
                    }
                    // "-0-3"
                    3 => (
                        parts[1].trim().to_string(),
                        parts[2].trim().to_string(),
                        -1,
                        1,
                    ),
                    // "-8--4"
                    4 => (
                        parts[1].trim().to_string(),
                        parts[3].trim().to_string(),
                        -1,
                        -1,
                    ),
                    _ => {
                        return Err(PyValueError::new_err(format!(
                            "too many values to unpack (expected 2): {}",
                            subrange
                        )))
                    }
                }
            };

        // compute padding and numeric bounds (Python int() tolerates
        // surrounding whitespace: inputs are pre-trimmed)
        let int_conv = |txt: &str| -> Result<i64, ()> { txt.trim().parse::<i64>().map_err(|_| ()) };
        let conv_err = |py: Python<'_>| {
            if subrange.is_empty() {
                parse_err(py, subrange, "empty range")
            } else {
                parse_err(py, subrange, "cannot convert string to integer")
            }
        };
        let begin_int = int_conv(&begin).map_err(|_| conv_err(py))?;
        let mut pad = 0usize;
        let start: i64 = if begin_int != 0 {
            let begins = begin.trim_start_matches('0');
            if begin.len() - begins.len() > 0 {
                pad = begin.len();
            }
            begins.parse::<i64>().map_err(|_| conv_err(py))?
        } else {
            if begin.len() > 1 {
                pad = begin.len();
            }
            0
        };
        let end_int_probe = int_conv(&end).map_err(|_| conv_err(py))?;
        let ends = if end_int_probe != 0 {
            end.trim_start_matches('0')
        } else {
            end.as_str()
        };
        let endpad = if end.len() - ends.len() > 0 {
            end.len()
        } else {
            0
        };
        if (pad > 0 || endpad > 0) && begin.len() != end.len() {
            return Err(parse_err(py, subrange, "padding length mismatch"));
        }
        let stop: i64 = ends.parse::<i64>().map_err(|_| conv_err(py))?;

        // check preconditions
        if pad > 0 && begin_sign < 0 {
            return Err(parse_err(
                py,
                subrange,
                "padding not supported in negative ranges",
            ));
        }
        if (stop as f64) > 1e100 || start * begin_sign > stop * end_sign || step < 1 {
            return Err(parse_err(py, subrange, "invalid values in range"));
        }

        let lo = start * begin_sign;
        let hi = stop * end_sign + 1;
        // mirrors add_range() assertions (raised as AssertionError upstream)
        if lo >= hi {
            return Err(PyAssertionError::new_err(
                "please provide ordered node index ranges",
            ));
        }
        if hi - lo >= 1_000_000_000 {
            return Err(PyAssertionError::new_err("range too large"));
        }
        rs.add_range(lo, hi, step, pad as u32);
    }
    Ok(rs)
}

/// Internal (Python-private) autostep value: user-facing value - 1, or
/// AUTOSTEP_DISABLED when stepping is off.
fn internal_autostep(rs: &RustRangeSet) -> f64 {
    match rs.autostep() {
        None => AUTOSTEP_DISABLED,
        Some(user) => (user as f64) - 1.0,
    }
}

/// A (start, stop, step, pad) folded slice, mirroring Python's slice+pad pairs.
#[derive(Debug, Clone)]
struct FoldedSlice {
    start: i64,
    stop: i64, // exclusive
    step: i64,
    pad: usize,
}

/// Port of Python's RangeSet._slices_padding(): fold sorted elements into
/// (slice, pad) pairs honoring the autostep threshold.
fn slices_padding(sorted: &[String], autostep: f64, self_autostep: f64) -> Vec<FoldedSlice> {
    let mut result: Vec<FoldedSlice> = Vec::new();
    if sorted.is_empty() {
        return result;
    }

    let mut cur_pad: usize = 0;
    let mut cur_padded = false;
    let mut cur_start: Option<i64> = None;
    let mut cur_step: Option<i64> = None;
    let mut last_idx: i64 = 0;

    for si in sorted {
        let idx: i64 = si.parse().unwrap_or(0);
        let digitlen = si.len();
        let padded = digitlen > 1 && si.starts_with('0');

        if let Some(cs) = cur_start {
            let padding_mismatch = if cur_padded {
                digitlen != cur_pad
            } else {
                padded
            };
            let step_mismatch = match cur_step {
                Some(cstep) => cstep != idx - last_idx,
                None => false,
            };

            if padding_mismatch || step_mismatch {
                let (stepped, step) = match cur_step {
                    Some(cstep) => {
                        let stepped =
                            (cstep == 1) || ((last_idx - cs) as f64 >= autostep * cstep as f64);
                        (stepped, cstep)
                    }
                    None => (true, 1i64),
                };

                if stepped {
                    result.push(FoldedSlice {
                        start: cs,
                        stop: last_idx + 1,
                        step,
                        pad: if cur_padded { cur_pad } else { 0 },
                    });
                    cur_start = Some(idx);
                    cur_padded = padded;
                    cur_pad = digitlen;
                } else if padding_mismatch {
                    let stop = last_idx + 1;
                    let mut j = cs;
                    while j < stop {
                        result.push(FoldedSlice {
                            start: j,
                            stop: j + 1,
                            step: 1,
                            pad: if cur_padded { cur_pad } else { 0 },
                        });
                        j += step;
                    }
                    cur_start = Some(idx);
                    cur_padded = padded;
                    cur_pad = digitlen;
                } else {
                    let stop = last_idx - step + 1;
                    let mut j = cs;
                    while j < stop {
                        result.push(FoldedSlice {
                            start: j,
                            stop: j + 1,
                            step: 1,
                            pad: if cur_padded { cur_pad } else { 0 },
                        });
                        j += step;
                    }
                    cur_start = Some(last_idx);
                }

                cur_step = if step_mismatch {
                    Some(idx - last_idx)
                } else {
                    None
                };
                last_idx = idx;
                continue;
            }
        } else {
            // first index
            cur_padded = padded;
            cur_pad = digitlen;
            cur_start = Some(idx);
            cur_step = None;
            last_idx = idx;
            continue;
        }

        cur_step = Some(idx - last_idx);
        last_idx = idx;
    }

    if let Some(cs) = cur_start {
        match cur_step {
            Some(cstep) => {
                let stepped = (last_idx - cs) as f64 >= self_autostep * cstep as f64;
                if stepped || cstep == 1 {
                    result.push(FoldedSlice {
                        start: cs,
                        stop: last_idx + 1,
                        step: cstep,
                        pad: if cur_padded { cur_pad } else { 0 },
                    });
                } else {
                    let mut j = cs;
                    while j <= last_idx {
                        result.push(FoldedSlice {
                            start: j,
                            stop: j + 1,
                            step: 1,
                            pad: if cur_padded { cur_pad } else { 0 },
                        });
                        j += cstep;
                    }
                }
            }
            None => {
                result.push(FoldedSlice {
                    start: cs,
                    stop: last_idx + 1,
                    step: 1,
                    pad: if cur_padded { cur_pad } else { 0 },
                });
            }
        }
    }

    result
}

/// Python-visible RangeSet class.
///
/// Wraps `consortium::range_set::RangeSet` and exposes the same API as
/// `ClusterShell.RangeSet.RangeSet`.
#[pyclass(name = "RangeSet", module = "ClusterShell.RangeSet")]
#[derive(Debug, Clone)]
pub struct PyRangeSet {
    pub inner: RustRangeSet,
}

/// Extract the string elements of a Python iterable argument (used by
/// update() and friends): strings are kept as-is, anything else is
/// stringified like Python's `str(item)`.
fn iterable_to_strings(obj: &Bound<'_, PyAny>) -> PyResult<Vec<String>> {
    let mut items = Vec::new();
    for item in obj.iter()? {
        let item = item?;
        items.push(item.str()?.to_string());
    }
    Ok(items)
}

/// Set-like argument extraction: accepts PyRangeSet, builtin set/frozenset
/// or any iterable; returns element strings.
fn setlike_to_strings(obj: &Bound<'_, PyAny>) -> PyResult<Vec<String>> {
    if let Ok(rs) = obj.downcast::<PyRangeSet>() {
        return Ok(rs.borrow().inner.sorted());
    }
    iterable_to_strings(obj)
}

/// Variant of setlike_to_strings() for in-place (&mut self) methods: if the
/// argument is the same object as self, the mutable borrow is already held,
/// so fall back to the provided snapshot of self's elements.
fn setlike_to_strings_inplace(
    obj: &Bound<'_, PyAny>,
    self_elems: &[String],
) -> PyResult<Vec<String>> {
    if let Ok(rs) = obj.downcast::<PyRangeSet>() {
        return match rs.try_borrow() {
            Ok(b) => Ok(b.inner.sorted()),
            Err(_) => Ok(self_elems.to_vec()),
        };
    }
    iterable_to_strings(obj)
}

/// Build a RangeSet from element strings (no parsing).
fn rs_from_elements(elems: Vec<String>, autostep_user: Option<u32>) -> RustRangeSet {
    let mut rs = RustRangeSet::new();
    rs.set_autostep(autostep_user);
    for e in elems {
        rs.add_str(&e);
    }
    rs
}

impl PyRangeSet {
    /// Shared implementation of updaten().
    fn do_updaten(&mut self, py: Python<'_>, rangesets: &Bound<'_, PyAny>) -> PyResult<()> {
        for rng in rangesets.iter()? {
            let rng = rng?;
            if let Ok(rs) = rng.downcast::<PyRangeSet>() {
                for e in rs.borrow().inner.sorted() {
                    self.inner.add_str(&e);
                }
            } else if rng.downcast::<pyo3::types::PySet>().is_ok()
                || rng.downcast::<pyo3::types::PyFrozenSet>().is_ok()
            {
                for e in iterable_to_strings(&rng)? {
                    self.inner.add_str(&e);
                }
            } else if let Ok(s) = rng.extract::<String>() {
                let other = RustRangeSet::parse(&s, None).map_err(|e| to_py_err(py, e))?;
                for e in other.sorted() {
                    self.inner.add_str(&e);
                }
            } else {
                let s = iterable_to_strings(&rng)?.join(",");
                let other = RustRangeSet::parse(&s, None).map_err(|e| to_py_err(py, e))?;
                for e in other.sorted() {
                    self.inner.add_str(&e);
                }
            }
        }
        Ok(())
    }

    /// Shared implementation of add().
    fn do_add(&mut self, element: &Bound<'_, PyAny>, pad: u32) -> PyResult<()> {
        if let Ok(s) = element.extract::<String>() {
            self.inner.add_str(&s);
        } else {
            let value: i64 = element.extract()?;
            self.inner.add_int(value, pad);
        }
        Ok(())
    }

    /// Shared implementation of add_range().
    fn do_add_range(&mut self, start: i64, stop: i64, step: i64, pad: u32) -> PyResult<()> {
        if start >= stop {
            return Err(PyAssertionError::new_err(
                "please provide ordered node index ranges",
            ));
        }
        if step <= 0 {
            return Err(PyAssertionError::new_err("assertion failed"));
        }
        if stop - start >= 1_000_000_000 {
            return Err(PyAssertionError::new_err("range too large"));
        }
        self.inner.add_range(start, stop, step, pad);
        Ok(())
    }
}

#[pymethods]
impl PyRangeSet {
    #[new]
    #[pyo3(signature = (pattern=None, autostep=None))]
    fn new(
        py: Python<'_>,
        pattern: Option<&Bound<'_, PyAny>>,
        autostep: Option<&Bound<'_, PyAny>>,
    ) -> PyResult<Self> {
        // NB: like upstream, passing autostep=None disables stepping, even
        // when pattern is another RangeSet.
        let autostep = extract_autostep(autostep)?;
        match pattern {
            None => {
                let mut rs = RustRangeSet::new();
                rs.set_autostep(autostep);
                Ok(Self { inner: rs })
            }
            Some(obj) => {
                let pat_string: String = if let Ok(s) = obj.extract::<String>() {
                    s
                } else if let Ok(other) = obj.downcast::<PyRangeSet>() {
                    // another RangeSet is an iterable of its elements
                    other.borrow().inner.sorted().join(",")
                } else {
                    // any other iterable of integers/strings
                    iterable_to_strings(obj)?.join(",")
                };
                let rs = parse_oracle(py, &pat_string, autostep)?;
                Ok(Self { inner: rs })
            }
        }
    }

    /// Class method: new RangeSet from a list of ranges (patterns or sets).
    #[classmethod]
    #[pyo3(signature = (rnglist, autostep=None))]
    fn fromlist(
        cls: &Bound<'_, PyType>,
        rnglist: &Bound<'_, PyAny>,
        autostep: Option<&Bound<'_, PyAny>>,
    ) -> PyResult<Py<PyRangeSet>> {
        let mut rs = RustRangeSet::new();
        rs.set_autostep(extract_autostep(autostep)?);
        let mut inst = PyRangeSet { inner: rs };
        inst.do_updaten(cls.py(), rnglist)?;
        Py::new(cls.py(), inst)
    }

    /// Class method: new RangeSet of one single item or single range.
    /// NB: pad=None is tolerated (upstream NodeSet passes its autostep
    /// positionally, which lands here; pad is ignored for string indexes).
    #[classmethod]
    #[pyo3(signature = (index, pad=None, autostep=None))]
    fn fromone(
        cls: &Bound<'_, PyType>,
        index: &Bound<'_, PyAny>,
        pad: Option<usize>,
        autostep: Option<&Bound<'_, PyAny>>,
    ) -> PyResult<Py<PyRangeSet>> {
        let py = cls.py();
        let pad = pad.unwrap_or(0);
        let mut rs = RustRangeSet::new();
        rs.set_autostep(extract_autostep(autostep)?);
        let mut inst = PyRangeSet { inner: rs };
        match inst.do_add(index, pad as u32) {
            Ok(_) => Py::new(py, inst),
            Err(err) => {
                if err.is_instance_of::<PyTypeError>(py) {
                    // support slice object with duck-typing
                    if let Ok(slice) = index.downcast::<PySlice>() {
                        let stop = slice.getattr("stop")?;
                        if stop.is_none() {
                            return Err(PyValueError::new_err(format!(
                                "Invalid range upper limit ({})",
                                stop.str()?
                            )));
                        }
                        let start = slice.getattr("start")?;
                        let step = slice.getattr("step")?;
                        let start: i64 = if start.is_none() { 0 } else { start.extract()? };
                        let stop: i64 = stop.extract()?;
                        let step: i64 = if step.is_none() { 1 } else { step.extract()? };
                        inst.do_add_range(start, stop, step, pad as u32)?;
                        return Py::new(py, inst);
                    }
                }
                Err(err)
            }
        }
    }

    fn __str__(&self) -> String {
        self.inner.to_string()
    }

    fn __repr__(&self) -> String {
        self.inner.to_string()
    }

    fn __len__(&self) -> usize {
        self.inner.len()
    }

    fn dim(&self) -> usize {
        if self.inner.is_empty() {
            0
        } else {
            1
        }
    }

    fn __contains__(&self, element: &Bound<'_, PyAny>) -> PyResult<bool> {
        // A set argument is treated as a subset check (upstream behavior).
        if let Ok(rs) = element.downcast::<PyRangeSet>() {
            let other = rs.borrow();
            return Ok(other
                .inner
                .sorted()
                .iter()
                .all(|e| self.inner.contains_str(e)));
        }
        if element.downcast::<pyo3::types::PySet>().is_ok()
            || element.downcast::<pyo3::types::PyFrozenSet>().is_ok()
        {
            for item in element.iter()? {
                let item = item?;
                // items are compared as-is: only strings may match
                match item.extract::<String>() {
                    Ok(s) => {
                        if !self.inner.contains_str(&s) {
                            return Ok(false);
                        }
                    }
                    Err(_) => return Ok(false),
                }
            }
            return Ok(true);
        }
        // otherwise, stringified membership test
        Ok(self.inner.contains_str(&element.str()?.to_string()))
    }

    fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<RangeSetStrIterator>> {
        let values: Vec<String> = slf.inner.sorted();
        Py::new(slf.py(), RangeSetStrIterator { values, index: 0 })
    }

    /// Iterate over each element as strings with optional zero-padding.
    fn striter<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyIterator>> {
        PyIterator::from_bound_object(&PyList::new_bound(py, self.inner.sorted()))
    }

    /// Iterate over each element as integers (padding ignored).
    fn intiter<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyIterator>> {
        let ints: Vec<i64> = self
            .inner
            .sorted()
            .iter()
            .map(|s| s.parse::<i64>().unwrap_or(0))
            .collect();
        PyIterator::from_bound_object(&PyList::new_bound(py, ints))
    }

    /// Get sorted list of elements (upstream _sorted()).
    fn _sorted(&self) -> Vec<String> {
        self.inner.sorted()
    }

    /// Iterate over contiguous range sets.
    fn contiguous(slf: PyRef<'_, Self>) -> PyResult<Vec<PyRangeSet>> {
        let sorted = slf.inner.sorted();
        let result = slices_padding(&sorted, AUTOSTEP_DISABLED, internal_autostep(&slf.inner));
        Ok(result
            .iter()
            .map(|sli| {
                let mut rs = RustRangeSet::new();
                rs.set_autostep(slf.inner.autostep());
                rs.add_range(sli.start, sli.stop, sli.step, sli.pad as u32);
                PyRangeSet { inner: rs }
            })
            .collect())
    }

    /// Iterate over RangeSet ranges as Python slice objects.
    fn slices(slf: PyRef<'_, Self>) -> PyResult<Py<PyList>> {
        let py = slf.py();
        let sorted = slf.inner.sorted();
        let autostep = internal_autostep(&slf.inner);
        let folded = slices_padding(&sorted, autostep, autostep);
        let list = PyList::empty_bound(py);
        for sli in folded {
            let slice =
                PySlice::new_bound(py, sli.start as isize, sli.stop as isize, sli.step as isize);
            list.append(slice)?;
        }
        Ok(list.into())
    }

    /// Return state information for pickling. Empty sets store a None
    /// pattern (upstream 9df4c5a) so that unpickling works.
    fn __reduce__(slf: PyRef<'_, Self>) -> PyResult<(Py<PyType>, (Option<String>,), Py<PyDict>)> {
        let py = slf.py();
        let pattern: Option<String> = if slf.inner.is_empty() {
            None
        } else {
            Some(slf.inner.to_string())
        };
        let state = PyDict::new_bound(py);
        state.set_item("padding", slf.inner.padding())?;
        state.set_item("_autostep", internal_autostep(&slf.inner))?;
        state.set_item("_version", RANGESET_VERSION)?;
        let cls: Py<PyType> = slf.py().get_type_bound::<PyRangeSet>().unbind();
        Ok((cls, (pattern,), state.unbind()))
    }

    /// Called upon unpickling: restore state, converting legacy formats.
    fn __setstate__(&mut self, _py: Python<'_>, dic: &Bound<'_, PyDict>) -> PyResult<()> {
        // restore internal autostep value
        if let Some(val) = dic.get_item("_autostep")? {
            let internal: f64 = val.extract()?;
            if internal >= AUTOSTEP_DISABLED || internal + 1.0 >= u32::MAX as f64 {
                self.inner.set_autostep(None);
            } else {
                self.inner.set_autostep(Some(internal as u32 + 1));
            }
        }
        let version: i32 = match dic.get_item("_version")? {
            Some(v) => v.extract().unwrap_or(0),
            None => 0,
        };
        if version < RANGESET_VERSION {
            // legacy formats (ClusterShell < 1.9): convert _ranges
            if let Some(ranges) = dic.get_item("_ranges")? {
                let ranges = ranges.downcast::<PyList>()?.clone();
                for entry in ranges.iter() {
                    let entry = entry.downcast::<PyTuple>()?.clone();
                    let first = entry.get_item(0)?;
                    let pad: usize = entry.get_item(entry.len() - 1)?.extract()?;
                    if let Ok(slice) = first.downcast::<PySlice>() {
                        // (slice(start, stop, step), pad)
                        let start: i64 = slice.getattr("start")?.extract()?;
                        let stop: i64 = slice.getattr("stop")?.extract()?;
                        let step: i64 = slice.getattr("step")?.extract()?;
                        self.inner.add_range(start, stop, step, pad as u32);
                    } else if entry.len() == 2 {
                        // v2: ((start, stop, step), pad)
                        let inner_t = first.downcast::<PyTuple>()?.clone();
                        let start: i64 = inner_t.get_item(0)?.extract()?;
                        let stop: i64 = inner_t.get_item(1)?.extract()?;
                        let step: i64 = inner_t.get_item(2)?.extract()?;
                        self.inner.add_range(start, stop, step, pad as u32);
                    } else {
                        // v1: (start, stop, step, pad) with inclusive stop
                        let start: i64 = first.extract()?;
                        let stop: i64 = entry.get_item(1)?.extract()?;
                        let step: i64 = entry.get_item(2)?.extract()?;
                        self.inner.add_range(start, stop + 1, step, pad as u32);
                    }
                }
            }
        }
        Ok(())
    }

    fn __getitem__(slf: PyRef<'_, Self>, index: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = slf.py();
        let sorted = slf.inner.sorted();
        if let Ok(slice) = index.downcast::<PySlice>() {
            let n = sorted.len() as isize;
            let indices = slice.indices(n)?;
            let mut rs = RustRangeSet::new();
            rs.set_autostep(slf.inner.autostep());
            let mut i = indices.start;
            if indices.step > 0 {
                while i < indices.stop {
                    rs.add_str(&sorted[i as usize]);
                    i += indices.step;
                }
            } else {
                while i > indices.stop {
                    rs.add_str(&sorted[i as usize]);
                    i += indices.step;
                }
            }
            let out = Py::new(py, PyRangeSet { inner: rs })?;
            return Ok(out.into_py(py));
        }
        if let Ok(idx) = index.extract::<i64>() {
            let n = sorted.len() as i64;
            let real = if idx < 0 { idx + n } else { idx };
            if real < 0 || real >= n {
                return Err(PyIndexError::new_err("list index out of range"));
            }
            return Ok(sorted[real as usize].clone().into_py(py));
        }
        Err(PyTypeError::new_err("RangeSet indices must be integers"))
    }

    /// Return the zero-based index of element in the sorted RangeSet.
    ///
    /// Reverse operation of __getitem__(), behaving like list.index().
    #[pyo3(signature = (elem, start=0, stop=None))]
    fn index(&self, elem: &Bound<'_, PyAny>, start: isize, stop: Option<isize>) -> PyResult<usize> {
        let key = elem.str()?.to_string();
        match self.inner.index_str_within(&key, start, stop) {
            Some(pos) => Ok(pos),
            None => Err(PyValueError::new_err(format!(
                "{} is not in RangeSet",
                elem.str()?
            ))),
        }
    }

    /// Split the rangeset into nbr sub-rangesets (at most).
    fn split(slf: PyRef<'_, Self>, nbr: usize) -> PyResult<Vec<PyRangeSet>> {
        if nbr == 0 {
            return Err(PyAssertionError::new_err("assertion failed"));
        }
        let sorted = slf.inner.sorted();
        let total = sorted.len();
        let slice_size = total / nbr;
        let leftover = total % nbr;
        let mut result = Vec::new();
        let mut begin = 0usize;
        for i in 0..nbr.min(total) {
            let length = slice_size + usize::from(i < leftover);
            let mut rs = RustRangeSet::new();
            rs.set_autostep(slf.inner.autostep());
            for e in &sorted[begin..begin + length] {
                rs.add_str(e);
            }
            result.push(PyRangeSet { inner: rs });
            begin += length;
        }
        Ok(result)
    }

    /// Add a range (start, stop, step, pad) to the RangeSet. Like range(),
    /// the last element is the largest start + i * step less than stop.
    #[pyo3(signature = (start, stop, step=1, pad=0))]
    fn add_range(&mut self, start: i64, stop: i64, step: i64, pad: u32) -> PyResult<()> {
        self.do_add_range(start, stop, step, pad)
    }

    /// Return a shallow copy of this RangeSet.
    fn copy(&self) -> Self {
        self.clone()
    }

    fn __copy__(&self) -> Self {
        self.clone()
    }

    fn __eq__(&self, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = other.py();
        match other.downcast::<PyRangeSet>() {
            Ok(rs) => Ok((self.inner == rs.borrow().inner).into_py(py)),
            Err(_) => Ok(py.NotImplemented().into_py(py)),
        }
    }

    fn __ne__(&self, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = other.py();
        match other.downcast::<PyRangeSet>() {
            Ok(rs) => Ok((self.inner != rs.borrow().inner).into_py(py)),
            Err(_) => Ok(py.NotImplemented().into_py(py)),
        }
    }

    /// Check that the other argument to a binary operation is also a set.
    fn _binary_sanity_check(&self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        if other.downcast::<PyRangeSet>().is_err()
            && other.downcast::<pyo3::types::PySet>().is_err()
            && other.downcast::<pyo3::types::PyFrozenSet>().is_err()
        {
            return Err(PyTypeError::new_err(
                "Binary operation only permitted between sets",
            ));
        }
        Ok(())
    }

    fn __or__(&self, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = other.py();
        if other.downcast::<PyRangeSet>().is_err()
            && other.downcast::<pyo3::types::PySet>().is_err()
            && other.downcast::<pyo3::types::PyFrozenSet>().is_err()
        {
            return Ok(py.NotImplemented().into_py(py));
        }
        let mut rs = self.inner.clone();
        for e in setlike_to_strings(other)? {
            rs.add_str(&e);
        }
        Ok(Py::new(py, PyRangeSet { inner: rs })?.into_py(py))
    }

    fn union(&self, other: &Bound<'_, PyAny>) -> PyResult<PyRangeSet> {
        let mut rs = self.inner.clone();
        for e in setlike_to_strings(other)? {
            rs.add_str(&e);
        }
        Ok(PyRangeSet { inner: rs })
    }

    fn __and__(&self, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = other.py();
        if other.downcast::<PyRangeSet>().is_err()
            && other.downcast::<pyo3::types::PySet>().is_err()
            && other.downcast::<pyo3::types::PyFrozenSet>().is_err()
        {
            return Ok(py.NotImplemented().into_py(py));
        }
        let elems: std::collections::HashSet<String> =
            setlike_to_strings(other)?.into_iter().collect();
        let kept: Vec<String> = self
            .inner
            .sorted()
            .into_iter()
            .filter(|e| elems.contains(e))
            .collect();
        let rs = rs_from_elements(kept, self.inner.autostep());
        Ok(Py::new(py, PyRangeSet { inner: rs })?.into_py(py))
    }

    fn intersection(&self, other: &Bound<'_, PyAny>) -> PyResult<PyRangeSet> {
        let elems: std::collections::HashSet<String> =
            setlike_to_strings(other)?.into_iter().collect();
        let kept: Vec<String> = self
            .inner
            .sorted()
            .into_iter()
            .filter(|e| elems.contains(e))
            .collect();
        Ok(PyRangeSet {
            inner: rs_from_elements(kept, self.inner.autostep()),
        })
    }

    fn __xor__(&self, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = other.py();
        if other.downcast::<PyRangeSet>().is_err()
            && other.downcast::<pyo3::types::PySet>().is_err()
            && other.downcast::<pyo3::types::PyFrozenSet>().is_err()
        {
            return Ok(py.NotImplemented().into_py(py));
        }
        Ok(Py::new(py, self.symmetric_difference(other)?)?.into_py(py))
    }

    fn symmetric_difference(&self, other: &Bound<'_, PyAny>) -> PyResult<PyRangeSet> {
        let other_elems: std::collections::HashSet<String> =
            setlike_to_strings(other)?.into_iter().collect();
        let self_elems: std::collections::HashSet<String> =
            self.inner.sorted().into_iter().collect();
        let sym: std::collections::HashSet<String> = self_elems
            .symmetric_difference(&other_elems)
            .cloned()
            .collect();
        Ok(PyRangeSet {
            inner: rs_from_elements(sym.into_iter().collect(), self.inner.autostep()),
        })
    }

    fn __sub__(&self, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
        let py = other.py();
        if other.downcast::<PyRangeSet>().is_err()
            && other.downcast::<pyo3::types::PySet>().is_err()
            && other.downcast::<pyo3::types::PyFrozenSet>().is_err()
        {
            return Ok(py.NotImplemented().into_py(py));
        }
        Ok(Py::new(py, self.difference(other)?)?.into_py(py))
    }

    fn difference(&self, other: &Bound<'_, PyAny>) -> PyResult<PyRangeSet> {
        let elems: std::collections::HashSet<String> =
            setlike_to_strings(other)?.into_iter().collect();
        let kept: Vec<String> = self
            .inner
            .sorted()
            .into_iter()
            .filter(|e| !elems.contains(e))
            .collect();
        Ok(PyRangeSet {
            inner: rs_from_elements(kept, self.inner.autostep()),
        })
    }

    fn issubset(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
        self._binary_sanity_check(other)?;
        let elems: std::collections::HashSet<String> =
            setlike_to_strings(other)?.into_iter().collect();
        Ok(self.inner.sorted().iter().all(|e| elems.contains(e)))
    }

    fn issuperset(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
        self._binary_sanity_check(other)?;
        let elems = setlike_to_strings(other)?;
        Ok(elems.iter().all(|e| self.inner.contains_str(e)))
    }

    fn __lt__(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
        self._binary_sanity_check(other)?;
        let other_len = if let Ok(rs) = other.downcast::<PyRangeSet>() {
            rs.borrow().inner.len()
        } else {
            other.len()?
        };
        Ok(self.inner.len() < other_len && self.issubset(other)?)
    }

    fn __gt__(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
        self._binary_sanity_check(other)?;
        let other_len = if let Ok(rs) = other.downcast::<PyRangeSet>() {
            rs.borrow().inner.len()
        } else {
            other.len()?
        };
        Ok(self.inner.len() > other_len && self.issuperset(other)?)
    }

    fn __le__(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
        self.issubset(other)
    }

    fn __ge__(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
        self.issuperset(other)
    }

    fn __ior__(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        self._binary_sanity_check(other)?;
        let snapshot = self.inner.sorted();
        for e in setlike_to_strings_inplace(other, &snapshot)? {
            self.inner.add_str(&e);
        }
        Ok(())
    }

    fn union_update(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        self.update(other)
    }

    fn __iand__(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        self._binary_sanity_check(other)?;
        self.intersection_update(other)
    }

    fn intersection_update(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        let snapshot = self.inner.sorted();
        let elems: std::collections::HashSet<String> =
            setlike_to_strings_inplace(other, &snapshot)?
                .into_iter()
                .collect();
        let kept: Vec<String> = self
            .inner
            .sorted()
            .into_iter()
            .filter(|e| elems.contains(e))
            .collect();
        self.inner = rs_from_elements(kept, self.inner.autostep());
        Ok(())
    }

    fn __ixor__(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        self._binary_sanity_check(other)?;
        self.symmetric_difference_update(other)
    }

    fn symmetric_difference_update(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        let snapshot = self.inner.sorted();
        let other_elems: std::collections::HashSet<String> =
            setlike_to_strings_inplace(other, &snapshot)?
                .into_iter()
                .collect();
        let self_elems: std::collections::HashSet<String> =
            self.inner.sorted().into_iter().collect();
        let sym: std::collections::HashSet<String> = self_elems
            .symmetric_difference(&other_elems)
            .cloned()
            .collect();
        self.inner = rs_from_elements(sym.into_iter().collect(), self.inner.autostep());
        Ok(())
    }

    fn __isub__(&mut self, other: &Bound<'_, PyAny>) -> PyResult<()> {
        self._binary_sanity_check(other)?;
        self.difference_update(other, false)
    }

    /// Remove all elements of another set from this RangeSet. If strict is
    /// True, raise KeyError if an element cannot be removed.
    #[pyo3(signature = (other, strict=false))]
    fn difference_update(&mut self, other: &Bound<'_, PyAny>, strict: bool) -> PyResult<()> {
        let snapshot = self.inner.sorted();
        if strict && !self.__contains__(other)? {
            // raise KeyError of first missing element
            let elems = setlike_to_strings_inplace(other, &snapshot)?;
            for e in &elems {
                if !self.inner.contains_str(e) {
                    return Err(PyKeyError::new_err(e.clone()));
                }
            }
        }
        let elems: std::collections::HashSet<String> =
            setlike_to_strings_inplace(other, &snapshot)?
                .into_iter()
                .collect();
        let kept: Vec<String> = self
            .inner
            .sorted()
            .into_iter()
            .filter(|e| !elems.contains(e))
            .collect();
        self.inner = rs_from_elements(kept, self.inner.autostep());
        Ok(())
    }

    /// Add all indexes (as strings) from an iterable.
    fn update(&mut self, iterable: &Bound<'_, PyAny>) -> PyResult<()> {
        if iterable.extract::<String>().is_ok() {
            return Err(PyAssertionError::new_err("assertion failed"));
        }
        let snapshot = self.inner.sorted();
        for e in setlike_to_strings_inplace(iterable, &snapshot)? {
            self.inner.add_str(&e);
        }
        Ok(())
    }

    /// Update a rangeset with the union of itself and several others.
    fn updaten(&mut self, py: Python<'_>, rangesets: &Bound<'_, PyAny>) -> PyResult<()> {
        self.do_updaten(py, rangesets)
    }

    /// Remove all elements from this RangeSet.
    fn clear(&mut self) {
        self.inner = rs_from_elements(Vec::new(), self.inner.autostep());
    }

    /// Add an element to the RangeSet (string, or integer with padding).
    #[pyo3(signature = (element, pad=0))]
    fn add(&mut self, element: &Bound<'_, PyAny>, pad: u32) -> PyResult<()> {
        self.do_add(element, pad)
    }

    /// Remove an element from the RangeSet.
    #[pyo3(signature = (element, pad=0))]
    fn remove(&mut self, element: &Bound<'_, PyAny>, pad: u32) -> PyResult<()> {
        let key = if let Ok(s) = element.extract::<String>() {
            s
        } else {
            let value: i64 = element.extract().map_err(|_| {
                PyValueError::new_err(format!(
                    "invalid literal for int(): {}",
                    element.str().unwrap()
                ))
            })?;
            if pad > 0 {
                format!("{:0>width$}", value, width = pad as usize)
            } else {
                format!("{}", value)
            }
        };
        if !self.inner.contains_str(&key) {
            return Err(PyKeyError::new_err(key));
        }
        self.inner.discard_str(&key);
        Ok(())
    }

    /// Discard an element from the RangeSet if present.
    #[pyo3(signature = (element, pad=0))]
    fn discard(&mut self, element: &Bound<'_, PyAny>, pad: u32) -> PyResult<()> {
        if let Ok(s) = element.extract::<String>() {
            self.inner.discard_str(&s);
        } else if let Ok(value) = element.extract::<i64>() {
            let key = if pad > 0 {
                format!("{:0>width$}", value, width = pad as usize)
            } else {
                format!("{}", value)
            };
            self.inner.discard_str(&key);
        }
        // ignore other object types
        Ok(())
    }

    /// Get largest padding value of whole set (property getter).
    #[getter]
    fn get_padding(&self) -> Option<usize> {
        self.inner.padding()
    }

    /// Force padding length on the whole set (property setter).
    #[setter]
    fn set_padding(&mut self, value: Option<usize>) -> PyResult<()> {
        let pad = value.unwrap_or(1);
        let elems: Vec<i64> = self
            .inner
            .sorted()
            .iter()
            .map(|s| s.parse::<i64>().unwrap_or(0))
            .collect();
        let autostep = self.inner.autostep();
        let mut rs = RustRangeSet::new();
        rs.set_autostep(autostep);
        for v in elems {
            rs.add_int(v, pad as u32);
        }
        self.inner = rs;
        Ok(())
    }

    /// Get autostep value (property getter): None when disabled.
    #[getter(autostep)]
    fn get_autostep_prop(&self) -> Option<u32> {
        self.inner.autostep()
    }

    /// Set autostep value (property setter): accepts None, int or float.
    #[setter(autostep)]
    fn set_autostep_prop(&mut self, val: Option<&Bound<'_, PyAny>>) -> PyResult<()> {
        self.inner.set_autostep(extract_autostep(val)?);
        Ok(())
    }

    /// Get autostep value (method form).
    fn get_autostep(&self) -> Option<u32> {
        self.inner.autostep()
    }

    /// Set autostep value (method form): accepts None, int or float.
    #[pyo3(signature = (val=None))]
    fn set_autostep(&mut self, val: Option<&Bound<'_, PyAny>>) -> PyResult<()> {
        self.inner.set_autostep(extract_autostep(val)?);
        Ok(())
    }
}

#[pyclass]
struct RangeSetStrIterator {
    values: Vec<String>,
    index: usize,
}

#[pymethods]
impl RangeSetStrIterator {
    fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
        slf
    }

    fn __next__(&mut self) -> Option<String> {
        if self.index < self.values.len() {
            let val = self.values[self.index].clone();
            self.index += 1;
            Some(val)
        } else {
            None
        }
    }
}

/// Register range_set types into the parent module.
pub fn register(parent: &Bound<'_, PyModule>) -> PyResult<()> {
    parent.add_class::<PyRangeSet>()?;
    parent.add(
        "RangeSetException",
        parent.py().get_type_bound::<RangeSetException>(),
    )?;
    parent.add(
        "RangeSetParseError",
        parent.py().get_type_bound::<RangeSetParseError>(),
    )?;
    parent.add(
        "RangeSetPaddingError",
        parent.py().get_type_bound::<RangeSetPaddingError>(),
    )?;
    Ok(())
}