forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.rs
More file actions
1141 lines (1095 loc) · 43.1 KB
/
csv.rs
File metadata and controls
1141 lines (1095 loc) · 43.1 KB
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
pub(crate) use _csv::make_module;
#[pymodule]
mod _csv {
use crate::common::lock::PyMutex;
use crate::vm::{
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
builtins::{PyBaseExceptionRef, PyInt, PyNone, PyStr, PyType, PyTypeError, PyTypeRef},
function::{ArgIterable, ArgumentError, FromArgs, FuncArgs, OptionalArg},
protocol::{PyIter, PyIterReturn},
types::{Constructor, IterNext, Iterable, SelfIter},
};
use csv_core::Terminator;
use itertools::{self, Itertools};
use parking_lot::Mutex;
use rustpython_vm::match_class;
use std::sync::LazyLock;
use std::{collections::HashMap, fmt};
#[pyattr]
const QUOTE_MINIMAL: i32 = QuoteStyle::Minimal as i32;
#[pyattr]
const QUOTE_ALL: i32 = QuoteStyle::All as i32;
#[pyattr]
const QUOTE_NONNUMERIC: i32 = QuoteStyle::Nonnumeric as i32;
#[pyattr]
const QUOTE_NONE: i32 = QuoteStyle::None as i32;
#[pyattr]
const QUOTE_STRINGS: i32 = QuoteStyle::Strings as i32;
#[pyattr]
const QUOTE_NOTNULL: i32 = QuoteStyle::Notnull as i32;
#[pyattr(name = "__version__")]
const __VERSION__: &str = "1.0";
#[pyattr(name = "Error", once)]
fn error(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.new_exception_type(
"_csv",
"Error",
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
)
}
static GLOBAL_HASHMAP: LazyLock<Mutex<HashMap<String, PyDialect>>> = LazyLock::new(|| {
let m = HashMap::new();
Mutex::new(m)
});
static GLOBAL_FIELD_LIMIT: LazyLock<Mutex<isize>> = LazyLock::new(|| Mutex::new(131072));
fn new_csv_error(vm: &VirtualMachine, msg: String) -> PyBaseExceptionRef {
vm.new_exception_msg(super::_csv::error(vm), msg)
}
#[pyattr]
#[pyclass(module = "csv", name = "Dialect")]
#[derive(Debug, PyPayload, Clone, Copy)]
struct PyDialect {
delimiter: u8,
quotechar: Option<u8>,
escapechar: Option<u8>,
doublequote: bool,
skipinitialspace: bool,
lineterminator: csv_core::Terminator,
quoting: QuoteStyle,
strict: bool,
}
impl Constructor for PyDialect {
type Args = PyObjectRef;
fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
PyDialect::try_from_object(vm, ctx)?
.into_ref_with_type(vm, cls)
.map(Into::into)
}
}
#[pyclass(with(Constructor))]
impl PyDialect {
#[pygetset]
fn delimiter(&self, vm: &VirtualMachine) -> PyRef<PyStr> {
vm.ctx.new_str(format!("{}", self.delimiter as char))
}
#[pygetset]
fn quotechar(&self, vm: &VirtualMachine) -> Option<PyRef<PyStr>> {
Some(vm.ctx.new_str(format!("{}", self.quotechar? as char)))
}
#[pygetset]
fn doublequote(&self) -> bool {
self.doublequote
}
#[pygetset]
fn skipinitialspace(&self) -> bool {
self.skipinitialspace
}
#[pygetset]
fn lineterminator(&self, vm: &VirtualMachine) -> PyRef<PyStr> {
match self.lineterminator {
Terminator::CRLF => vm.ctx.new_str("\r\n".to_string()).to_owned(),
Terminator::Any(t) => vm.ctx.new_str(format!("{}", t as char)).to_owned(),
_ => unreachable!(),
}
}
#[pygetset]
fn quoting(&self) -> isize {
self.quoting.into()
}
#[pygetset]
fn escapechar(&self, vm: &VirtualMachine) -> Option<PyRef<PyStr>> {
Some(vm.ctx.new_str(format!("{}", self.escapechar? as char)))
}
#[pygetset(name = "strict")]
fn get_strict(&self) -> bool {
self.strict
}
}
/// Parses the delimiter from a Python object and returns its ASCII value.
///
/// This function attempts to extract the 'delimiter' attribute from the given Python object and ensures that the attribute is a single-character string. If successful, it returns the ASCII value of the character. If the attribute is not a single-character string, an error is returned.
///
/// # Arguments
///
/// * `vm` - A reference to the VirtualMachine, used for executing Python code and manipulating Python objects.
/// * `obj` - A reference to the PyObjectRef from which the 'delimiter' attribute is to be parsed.
///
/// # Returns
///
/// If successful, returns a `PyResult<u8>` representing the ASCII value of the 'delimiter' attribute. If unsuccessful, returns a `PyResult` containing an error message.
///
/// # Errors
///
/// This function can return the following errors:
///
/// * If the 'delimiter' attribute is not a single-character string, a type error is returned.
/// * If the 'obj' is not of string type and does not have a 'delimiter' attribute, a type error is returned.
fn parse_delimiter_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<u8> {
if let Ok(attr) = obj.get_attr("delimiter", vm) {
parse_delimiter_from_obj(vm, &attr)
} else {
match_class!(match obj.clone() {
s @ PyStr => {
Ok(s.as_str().bytes().exactly_one().map_err(|_| {
let msg = r#""delimiter" must be a 1-character string"#;
vm.new_type_error(msg.to_owned())
})?)
}
attr => {
let msg = format!("\"delimiter\" must be string, not {}", attr.class());
Err(vm.new_type_error(msg))
}
})
}
}
fn parse_quotechar_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Option<u8>> {
match_class!(match obj.get_attr("quotechar", vm)? {
s @ PyStr => {
Ok(Some(s.as_str().bytes().exactly_one().map_err(|_| {
vm.new_exception_msg(
super::_csv::error(vm),
r#""quotechar" must be a 1-character string"#.to_owned(),
)
})?))
}
_n @ PyNone => {
Ok(None)
}
_ => {
Err(vm.new_exception_msg(
super::_csv::error(vm),
r#""quotechar" must be string or None, not int"#.to_owned(),
))
}
})
}
fn parse_escapechar_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Option<u8>> {
match_class!(match obj.get_attr("escapechar", vm)? {
s @ PyStr => {
Ok(Some(s.as_str().bytes().exactly_one().map_err(|_| {
vm.new_exception_msg(
super::_csv::error(vm),
r#""escapechar" must be a 1-character string"#.to_owned(),
)
})?))
}
_n @ PyNone => {
Ok(None)
}
attr => {
let msg = format!(
"\"escapechar\" must be string or None, not {}",
attr.class()
);
Err(vm.new_type_error(msg.to_owned()))
}
})
}
fn prase_lineterminator_from_obj(
vm: &VirtualMachine,
obj: &PyObjectRef,
) -> PyResult<Terminator> {
match_class!(match obj.get_attr("lineterminator", vm)? {
s @ PyStr => {
Ok(if s.as_bytes().eq(b"\r\n") {
csv_core::Terminator::CRLF
} else if let Some(t) = s.as_bytes().first() {
// Due to limitations in the current implementation within csv_core
// the support for multiple characters in lineterminator is not complete.
// only capture the first character
csv_core::Terminator::Any(*t)
} else {
return Err(vm.new_exception_msg(
super::_csv::error(vm),
r#""lineterminator" must be a string"#.to_owned(),
));
})
}
_ => {
let msg = "\"lineterminator\" must be a string".to_string();
Err(vm.new_type_error(msg.to_owned()))
}
})
}
fn prase_quoting_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<QuoteStyle> {
match_class!(match obj.get_attr("quoting", vm)? {
i @ PyInt => {
Ok(i.try_to_primitive::<isize>(vm)?.try_into().map_err(|_| {
let msg = r#"bad "quoting" value"#;
vm.new_type_error(msg.to_owned())
})?)
}
attr => {
let msg = format!("\"quoting\" must be string or None, not {}", attr.class());
Err(vm.new_type_error(msg.to_owned()))
}
})
}
impl TryFromObject for PyDialect {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let delimiter = parse_delimiter_from_obj(vm, &obj)?;
let quotechar = parse_quotechar_from_obj(vm, &obj)?;
let escapechar = parse_escapechar_from_obj(vm, &obj)?;
let doublequote = obj.get_attr("doublequote", vm)?.try_to_bool(vm)?;
let skipinitialspace = obj.get_attr("skipinitialspace", vm)?.try_to_bool(vm)?;
let lineterminator = prase_lineterminator_from_obj(vm, &obj)?;
let quoting = prase_quoting_from_obj(vm, &obj)?;
let strict = if let Ok(t) = obj.get_attr("strict", vm) {
t.try_to_bool(vm).unwrap_or(false)
} else {
false
};
Ok(Self {
delimiter,
quotechar,
escapechar,
doublequote,
skipinitialspace,
lineterminator,
quoting,
strict,
})
}
}
#[pyfunction]
fn register_dialect(
name: PyObjectRef,
dialect: OptionalArg<PyObjectRef>,
opts: FormatOptions,
// TODO: handle quote style, etc
mut _rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<()> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
return Err(vm.new_type_error("argument 0 must be a string".to_string()));
};
let dialect = match dialect {
OptionalArg::Present(d) => PyDialect::try_from_object(vm, d)
.map_err(|_| vm.new_type_error("argument 1 must be a dialect object".to_owned()))?,
OptionalArg::Missing => opts.result(vm)?,
};
let dialect = opts.update_py_dialect(dialect);
GLOBAL_HASHMAP
.lock()
.insert(name.as_str().to_owned(), dialect);
Ok(())
}
#[pyfunction]
fn get_dialect(
name: PyObjectRef,
mut _rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<PyDialect> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
return Err(vm.new_exception_msg(
super::_csv::error(vm),
format!("argument 0 must be a string, not '{}'", name.class()),
));
};
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name.as_str()) {
return Ok(*dialect);
}
Err(vm.new_exception_msg(super::_csv::error(vm), "unknown dialect".to_string()))
}
#[pyfunction]
fn unregister_dialect(
name: PyObjectRef,
mut _rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<()> {
let Some(name) = name.payload_if_subclass::<PyStr>(vm) else {
return Err(vm.new_exception_msg(
super::_csv::error(vm),
format!("argument 0 must be a string, not '{}'", name.class()),
));
};
let mut g = GLOBAL_HASHMAP.lock();
if let Some(_removed) = g.remove(name.as_str()) {
return Ok(());
}
Err(vm.new_exception_msg(super::_csv::error(vm), "unknown dialect".to_string()))
}
#[pyfunction]
fn list_dialects(
rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<rustpython_vm::builtins::PyListRef> {
if !rest.args.is_empty() || !rest.kwargs.is_empty() {
return Err(vm.new_type_error("too many argument".to_string()));
}
let g = GLOBAL_HASHMAP.lock();
let t = g
.keys()
.cloned()
.map(|x| vm.ctx.new_str(x).into())
.collect_vec();
// .iter().map(|x| vm.ctx.new_str(x.clone()).into_pyobject(vm)).collect_vec();
Ok(vm.ctx.new_list(t))
}
#[pyfunction]
fn field_size_limit(rest: FuncArgs, vm: &VirtualMachine) -> PyResult<isize> {
let old_size = GLOBAL_FIELD_LIMIT.lock().to_owned();
if !rest.args.is_empty() {
let arg_len = rest.args.len();
if arg_len != 1 {
return Err(vm.new_type_error(format!(
"field_size_limit() takes at most 1 argument ({arg_len} given)"
)));
}
let Ok(new_size) = rest.args.first().unwrap().try_int(vm) else {
return Err(vm.new_type_error("limit must be an integer".to_string()));
};
*GLOBAL_FIELD_LIMIT.lock() = new_size.try_to_primitive::<isize>(vm)?;
}
Ok(old_size)
}
#[pyfunction]
fn reader(
iter: PyIter,
options: FormatOptions,
// TODO: handle quote style, etc
_rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<Reader> {
Ok(Reader {
iter,
state: PyMutex::new(ReadState {
buffer: vec![0; 1024],
output_ends: vec![0; 16],
reader: options.to_reader(),
skipinitialspace: options.get_skipinitialspace(),
delimiter: options.get_delimiter(),
line_num: 0,
}),
dialect: options.result(vm)?,
})
}
#[pyfunction]
fn writer(
file: PyObjectRef,
options: FormatOptions,
// TODO: handle quote style, etc
_rest: FuncArgs,
vm: &VirtualMachine,
) -> PyResult<Writer> {
let write = match vm.get_attribute_opt(file.clone(), "write")? {
Some(write_meth) => write_meth,
None if file.is_callable() => file,
None => {
return Err(vm.new_type_error("argument 1 must have a \"write\" method".to_owned()));
}
};
Ok(Writer {
write,
state: PyMutex::new(WriteState {
buffer: vec![0; 1024],
writer: options.to_writer(),
}),
dialect: options.result(vm)?,
})
}
#[inline]
fn resize_buf<T: num_traits::PrimInt>(buf: &mut Vec<T>) {
let new_size = buf.len() * 2;
buf.resize(new_size, T::zero());
}
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum QuoteStyle {
Minimal = 0,
All = 1,
Nonnumeric = 2,
None = 3,
Strings = 4,
Notnull = 5,
}
impl From<QuoteStyle> for csv_core::QuoteStyle {
fn from(val: QuoteStyle) -> Self {
match val {
QuoteStyle::Minimal => csv_core::QuoteStyle::Always,
QuoteStyle::All => csv_core::QuoteStyle::Always,
QuoteStyle::Nonnumeric => csv_core::QuoteStyle::NonNumeric,
QuoteStyle::None => csv_core::QuoteStyle::Never,
QuoteStyle::Strings => todo!(),
QuoteStyle::Notnull => todo!(),
}
}
}
impl TryFromObject for QuoteStyle {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let num = obj.try_int(vm)?.try_to_primitive::<isize>(vm)?;
num.try_into().map_err(|_| {
vm.new_value_error(
"can not convert to QuoteStyle enum from input argument".to_string(),
)
})
}
}
impl TryFrom<isize> for QuoteStyle {
type Error = PyTypeError;
fn try_from(num: isize) -> Result<QuoteStyle, PyTypeError> {
match num {
0 => Ok(QuoteStyle::Minimal),
1 => Ok(QuoteStyle::All),
2 => Ok(QuoteStyle::Nonnumeric),
3 => Ok(QuoteStyle::None),
4 => Ok(QuoteStyle::Strings),
5 => Ok(QuoteStyle::Notnull),
_ => Err(PyTypeError {}),
}
}
}
impl From<QuoteStyle> for isize {
fn from(val: QuoteStyle) -> Self {
match val {
QuoteStyle::Minimal => 0,
QuoteStyle::All => 1,
QuoteStyle::Nonnumeric => 2,
QuoteStyle::None => 3,
QuoteStyle::Strings => 4,
QuoteStyle::Notnull => 5,
}
}
}
enum DialectItem {
Str(String),
Obj(PyDialect),
None,
}
struct FormatOptions {
dialect: DialectItem,
delimiter: Option<u8>,
quotechar: Option<Option<u8>>,
escapechar: Option<u8>,
doublequote: Option<bool>,
skipinitialspace: Option<bool>,
lineterminator: Option<csv_core::Terminator>,
quoting: Option<QuoteStyle>,
strict: Option<bool>,
}
impl Default for FormatOptions {
fn default() -> Self {
FormatOptions {
dialect: DialectItem::None,
delimiter: None,
quotechar: None,
escapechar: None,
doublequote: None,
skipinitialspace: None,
lineterminator: None,
quoting: None,
strict: None,
}
}
}
/// prase a dialect item from a Python argument and returns a `DialectItem` or an `ArgumentError`.
///
/// This function takes a reference to the VirtualMachine and a PyObjectRef as input and attempts to parse a dialect item from the provided Python argument. It returns a `DialectItem` if successful, or an `ArgumentError` if unsuccessful.
///
/// # Arguments
///
/// * `vm` - A reference to the VirtualMachine, used for executing Python code and manipulating Python objects.
/// * `obj` - The PyObjectRef from which the dialect item is to be parsed.
///
/// # Returns
///
/// If successful, returns a `Result<DialectItem, ArgumentError>` representing the parsed dialect item. If unsuccessful, returns an `ArgumentError`.
///
/// # Errors
///
/// This function can return the following errors:
///
/// * If the provided object is a PyStr, it returns a `DialectItem::Str` containing the string value.
/// * If the provided object is PyNone, it returns an `ArgumentError` with the message "InvalidKeywordArgument('dialect')".
/// * If the provided object is a PyType, it attempts to create a PyDialect from the object and returns a `DialectItem::Obj` containing the PyDialect if successful. If unsuccessful, it returns an `ArgumentError` with the message "InvalidKeywordArgument('dialect')".
/// * If the provided object is none of the above types, it attempts to create a PyDialect from the object and returns a `DialectItem::Obj` containing the PyDialect if successful. If unsuccessful, it returns an `ArgumentError` with the message "InvalidKeywordArgument('dialect')".
fn prase_dialect_item_from_arg(
vm: &VirtualMachine,
obj: PyObjectRef,
) -> Result<DialectItem, ArgumentError> {
match_class!(match obj {
s @ PyStr => {
Ok(DialectItem::Str(s.as_str().to_string()))
}
PyNone => {
Err(ArgumentError::InvalidKeywordArgument("dialect".to_string()))
}
t @ PyType => {
let temp = t
.as_object()
.call(vec![], vm)
.map_err(|_e| ArgumentError::InvalidKeywordArgument("dialect".to_string()))?;
Ok(DialectItem::Obj(
PyDialect::try_from_object(vm, temp).map_err(|_| {
ArgumentError::InvalidKeywordArgument("dialect".to_string())
})?,
))
}
obj => {
if let Ok(cur_dialect_item) = PyDialect::try_from_object(vm, obj) {
Ok(DialectItem::Obj(cur_dialect_item))
} else {
let msg = "dialect".to_string();
Err(ArgumentError::InvalidKeywordArgument(msg))
}
}
})
}
impl FromArgs for FormatOptions {
fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
let mut res = FormatOptions::default();
if let Some(dialect) = args.kwargs.swap_remove("dialect") {
res.dialect = prase_dialect_item_from_arg(vm, dialect)?;
} else if let Some(dialect) = args.args.first() {
res.dialect = prase_dialect_item_from_arg(vm, dialect.clone())?;
} else {
res.dialect = DialectItem::None;
};
if let Some(delimiter) = args.kwargs.swap_remove("delimiter") {
res.delimiter = Some(parse_delimiter_from_obj(vm, &delimiter)?);
}
if let Some(escapechar) = args.kwargs.swap_remove("escapechar") {
res.escapechar = match_class!(match escapechar {
s @ PyStr => Some(s.as_str().bytes().exactly_one().map_err(|_| {
let msg = r#""escapechar" must be a 1-character string"#;
vm.new_type_error(msg.to_owned())
})?),
_ => None,
})
};
if let Some(lineterminator) = args.kwargs.swap_remove("lineterminator") {
res.lineterminator = Some(csv_core::Terminator::Any(
lineterminator
.try_to_value::<&str>(vm)?
.bytes()
.exactly_one()
.map_err(|_| {
let msg = r#""lineterminator" must be a 1-character string"#;
vm.new_type_error(msg.to_owned())
})?,
))
};
if let Some(doublequote) = args.kwargs.swap_remove("doublequote") {
res.doublequote = Some(doublequote.try_to_bool(vm).map_err(|_| {
let msg = r#""doublequote" must be a bool"#;
vm.new_type_error(msg.to_owned())
})?)
};
if let Some(skipinitialspace) = args.kwargs.swap_remove("skipinitialspace") {
res.skipinitialspace = Some(skipinitialspace.try_to_bool(vm).map_err(|_| {
let msg = r#""skipinitialspace" must be a bool"#;
vm.new_type_error(msg.to_owned())
})?)
};
if let Some(quoting) = args.kwargs.swap_remove("quoting") {
res.quoting = match_class!(match quoting {
i @ PyInt =>
Some(i.try_to_primitive::<isize>(vm)?.try_into().map_err(|_e| {
ArgumentError::InvalidKeywordArgument("quoting".to_string())
})?),
_ => {
// let msg = r#""quoting" must be a int enum"#;
return Err(ArgumentError::InvalidKeywordArgument("quoting".to_string()));
}
});
};
if let Some(quotechar) = args.kwargs.swap_remove("quotechar") {
res.quotechar = match_class!(match quotechar {
s @ PyStr => Some(Some(s.as_str().bytes().exactly_one().map_err(|_| {
let msg = r#""quotechar" must be a 1-character string"#;
vm.new_type_error(msg.to_owned())
})?)),
PyNone => {
if let Some(QuoteStyle::All) = res.quoting {
let msg = "quotechar must be set if quoting enabled";
return Err(ArgumentError::Exception(
vm.new_type_error(msg.to_owned()),
));
}
Some(None)
}
_o => {
let msg = r#"quotechar"#;
return Err(
rustpython_vm::function::ArgumentError::InvalidKeywordArgument(
msg.to_string(),
),
);
}
})
};
if let Some(strict) = args.kwargs.swap_remove("strict") {
res.strict = Some(strict.try_to_bool(vm).map_err(|_| {
let msg = r#""strict" must be a int enum"#;
vm.new_type_error(msg.to_owned())
})?)
};
if let Some(last_arg) = args.kwargs.pop() {
let msg = format!(
r#"'{}' is an invalid keyword argument for this function"#,
last_arg.0
);
return Err(rustpython_vm::function::ArgumentError::InvalidKeywordArgument(msg));
}
Ok(res)
}
}
impl FormatOptions {
fn update_py_dialect(&self, mut res: PyDialect) -> PyDialect {
macro_rules! check_and_fill {
($res:ident, $e:ident) => {{
if let Some(t) = self.$e {
$res.$e = t;
}
}};
}
check_and_fill!(res, delimiter);
// check_and_fill!(res, quotechar);
check_and_fill!(res, delimiter);
check_and_fill!(res, doublequote);
check_and_fill!(res, skipinitialspace);
if let Some(t) = self.escapechar {
res.escapechar = Some(t);
};
if let Some(t) = self.quotechar {
if let Some(u) = t {
res.quotechar = Some(u);
} else {
res.quotechar = None;
}
};
check_and_fill!(res, quoting);
check_and_fill!(res, lineterminator);
check_and_fill!(res, strict);
res
}
fn result(&self, vm: &VirtualMachine) -> PyResult<PyDialect> {
match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
Ok(self.update_py_dialect(*dialect))
} else {
Err(new_csv_error(vm, format!("{name} is not registered.")))
}
// TODO
// Maybe need to update the obj from HashMap
}
DialectItem::Obj(o) => Ok(self.update_py_dialect(*o)),
DialectItem::None => {
let g = GLOBAL_HASHMAP.lock();
let res = *g.get("excel").unwrap();
Ok(self.update_py_dialect(res))
}
}
}
fn get_skipinitialspace(&self) -> bool {
let mut skipinitialspace = match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
dialect.skipinitialspace
// RustPython todo
// todo! Perfecting the remaining attributes.
} else {
false
}
}
DialectItem::Obj(obj) => obj.skipinitialspace,
_ => false,
};
if let Some(attr) = self.skipinitialspace {
skipinitialspace = attr
}
skipinitialspace
}
fn get_delimiter(&self) -> u8 {
let mut delimiter = match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
dialect.delimiter
// RustPython todo
// todo! Perfecting the remaining attributes.
} else {
b','
}
}
DialectItem::Obj(obj) => obj.delimiter,
_ => b',',
};
if let Some(attr) = self.delimiter {
delimiter = attr
}
delimiter
}
fn to_reader(&self) -> csv_core::Reader {
let mut builder = csv_core::ReaderBuilder::new();
let mut reader = match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
let mut builder = builder
.delimiter(dialect.delimiter)
.double_quote(dialect.doublequote);
if let Some(t) = dialect.quotechar {
builder = builder.quote(t);
}
builder
// RustPython todo
// todo! Perfecting the remaining attributes.
} else {
&mut builder
}
}
DialectItem::Obj(obj) => {
let mut builder = builder
.delimiter(obj.delimiter)
.double_quote(obj.doublequote);
if let Some(t) = obj.quotechar {
builder = builder.quote(t);
}
builder
}
_ => {
let name = "excel";
let g = GLOBAL_HASHMAP.lock();
let dialect = g.get(name).unwrap();
let mut builder = builder
.delimiter(dialect.delimiter)
.double_quote(dialect.doublequote);
if let Some(quotechar) = dialect.quotechar {
builder = builder.quote(quotechar);
}
builder
}
};
if let Some(t) = self.delimiter {
reader = reader.delimiter(t);
}
if let Some(t) = self.quotechar {
if let Some(u) = t {
reader = reader.quote(u);
} else {
reader = reader.quoting(false);
}
} else {
match self.quoting {
Some(QuoteStyle::None) => {
reader = reader.quoting(false);
}
// None => reader = reader.quoting(true),
_ => reader = reader.quoting(true),
}
}
if let Some(t) = self.lineterminator {
reader = reader.terminator(t);
}
if let Some(t) = self.doublequote {
reader = reader.double_quote(t);
}
if self.escapechar.is_some() {
reader = reader.escape(self.escapechar);
}
reader = match self.lineterminator {
Some(u) => reader.terminator(u),
None => reader.terminator(Terminator::CRLF),
};
reader.build()
}
fn to_writer(&self) -> csv_core::Writer {
let mut builder = csv_core::WriterBuilder::new();
let mut writer = match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
let mut builder = builder
.delimiter(dialect.delimiter)
.double_quote(dialect.doublequote)
.terminator(dialect.lineterminator);
if let Some(t) = dialect.quotechar {
builder = builder.quote(t);
}
builder
// RustPython todo
// todo! Perfecting the remaining attributes.
} else {
&mut builder
}
}
DialectItem::Obj(obj) => {
let mut builder = builder
.delimiter(obj.delimiter)
.double_quote(obj.doublequote)
.terminator(obj.lineterminator);
if let Some(t) = obj.quotechar {
builder = builder.quote(t);
}
builder
}
_ => &mut builder,
};
if let Some(t) = self.delimiter {
writer = writer.delimiter(t);
}
if let Some(t) = self.quotechar {
if let Some(u) = t {
writer = writer.quote(u);
} else {
todo!()
}
}
if let Some(t) = self.doublequote {
writer = writer.double_quote(t);
}
writer = match self.lineterminator {
Some(u) => writer.terminator(u),
None => writer.terminator(Terminator::CRLF),
};
if let Some(e) = self.escapechar {
writer = writer.escape(e);
}
if let Some(e) = self.quoting {
writer = writer.quote_style(e.into());
}
writer.build()
}
}
struct ReadState {
buffer: Vec<u8>,
output_ends: Vec<usize>,
reader: csv_core::Reader,
skipinitialspace: bool,
delimiter: u8,
line_num: u64,
}
#[pyclass(no_attr, module = "_csv", name = "reader", traverse)]
#[derive(PyPayload)]
pub(super) struct Reader {
iter: PyIter,
#[pytraverse(skip)]
state: PyMutex<ReadState>,
#[pytraverse(skip)]
dialect: PyDialect,
}
impl fmt::Debug for Reader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "_csv.reader")
}
}
#[pyclass(with(IterNext, Iterable))]
impl Reader {
#[pygetset]
fn line_num(&self) -> u64 {
self.state.lock().line_num
}
#[pygetset]
fn dialect(&self, _vm: &VirtualMachine) -> PyDialect {
self.dialect
}
}
impl SelfIter for Reader {}
impl IterNext for Reader {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let string = match zelf.iter.next(vm)? {
PyIterReturn::Return(obj) => obj,
PyIterReturn::StopIteration(v) => return Ok(PyIterReturn::StopIteration(v)),
};
let string = string.downcast::<PyStr>().map_err(|obj| {
new_csv_error(
vm,
format!(
"iterator should return strings, not {} (the file should be opened in text mode)",
obj.class().name()
),
)
})?;
let input = string.as_bytes();
if input.is_empty() || input.starts_with(b"\n") {
return Ok(PyIterReturn::Return(vm.ctx.new_list(vec![]).into()));
}
let mut state = zelf.state.lock();
let ReadState {
buffer,
output_ends,
reader,
skipinitialspace,
delimiter,
line_num,
} = &mut *state;
let mut input_offset = 0;
let mut output_offset = 0;
let mut output_ends_offset = 0;
let field_limit = GLOBAL_FIELD_LIMIT.lock().to_owned();
#[inline]
fn trim_spaces(input: &[u8]) -> &[u8] {
let trimmed_start = input.iter().position(|&x| x != b' ').unwrap_or(input.len());
let trimmed_end = input
.iter()
.rposition(|&x| x != b' ')
.map(|i| i + 1)
.unwrap_or(0);
&input[trimmed_start..trimmed_end]
}
let input = if *skipinitialspace {
let t = input.split(|x| x == delimiter);
t.map(|x| {
let trimmed = trim_spaces(x);
String::from_utf8(trimmed.to_vec()).unwrap()
})
.join(format!("{}", *delimiter as char).as_str())
} else {
String::from_utf8(input.to_vec()).unwrap()
};
loop {
let (res, n_read, n_written, n_ends) = reader.read_record(
&input.as_bytes()[input_offset..],
&mut buffer[output_offset..],
&mut output_ends[output_ends_offset..],
);
input_offset += n_read;
output_offset += n_written;
output_ends_offset += n_ends;
match res {
csv_core::ReadRecordResult::InputEmpty => {}
csv_core::ReadRecordResult::OutputFull => resize_buf(buffer),
csv_core::ReadRecordResult::OutputEndsFull => resize_buf(output_ends),
csv_core::ReadRecordResult::Record => break,
csv_core::ReadRecordResult::End => {
return Ok(PyIterReturn::StopIteration(None));
}
}
}
let rest = &input.as_bytes()[input_offset..];
if !rest.iter().all(|&c| matches!(c, b'\r' | b'\n')) {
return Err(new_csv_error(