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
//! Builtin function definitions.
//!
//! Implements functions listed here: https://docs.python.org/3/library/builtins.html

use std::cell::Cell;
use std::char;
use std::io::{self, Write};
use std::str;

use num_bigint::Sign;
use num_traits::{Signed, ToPrimitive, Zero};
#[cfg(feature = "rustpython-compiler")]
use rustpython_compiler::compile;

use crate::exceptions::PyBaseExceptionRef;
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
use crate::obj::objbool::{self, IntoPyBool};
use crate::obj::objbyteinner::PyByteInner;
use crate::obj::objbytes::PyBytesRef;
use crate::obj::objcode::PyCodeRef;
use crate::obj::objdict::PyDictRef;
use crate::obj::objfunction::PyFunctionRef;
use crate::obj::objint::{self, PyIntRef};
use crate::obj::objiter;
use crate::obj::objsequence;
use crate::obj::objstr::{PyString, PyStringRef};
use crate::obj::objtype::{self, PyClassRef};
use crate::pyhash;
use crate::pyobject::{
    Either, IdProtocol, ItemProtocol, PyIterable, PyObjectRef, PyResult, PyValue, TryFromObject,
    TypeProtocol,
};
use crate::scope::Scope;
use crate::stdlib::ast;
#[cfg(not(target_arch = "wasm32"))]
use crate::stdlib::io::io_open;
use crate::vm::VirtualMachine;

fn builtin_abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
    let method = vm.get_method_or_type_error(x.clone(), "__abs__", || {
        format!("bad operand type for abs(): '{}'", x.class().name)
    })?;
    vm.invoke(&method, PyFuncArgs::new(vec![], vec![]))
}

fn builtin_all(iterable: PyIterable<IntoPyBool>, vm: &VirtualMachine) -> PyResult<bool> {
    for item in iterable.iter(vm)? {
        if !item?.to_bool() {
            return Ok(false);
        }
    }
    Ok(true)
}

fn builtin_any(iterable: PyIterable<IntoPyBool>, vm: &VirtualMachine) -> PyResult<bool> {
    for item in iterable.iter(vm)? {
        if item?.to_bool() {
            return Ok(true);
        }
    }
    Ok(false)
}

fn builtin_ascii(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
    let repr = vm.to_repr(&obj)?;
    let ascii = to_ascii(repr.as_str());
    Ok(ascii)
}

/// Convert a string to ascii compatible, escaping unicodes into escape
/// sequences.
pub fn to_ascii(value: &str) -> String {
    let mut ascii = String::new();
    for c in value.chars() {
        if c.is_ascii() {
            ascii.push(c)
        } else {
            let c = c as i64;
            let hex = if c < 0x10000 {
                format!("\\u{:04x}", c)
            } else {
                format!("\\U{:08x}", c)
            };
            ascii.push_str(&hex)
        }
    }
    ascii
}

fn builtin_bin(x: PyIntRef) -> String {
    let x = x.as_bigint();
    if x.is_negative() {
        format!("-0b{:b}", x.abs())
    } else {
        format!("0b{:b}", x)
    }
}

// builtin_breakpoint

fn builtin_callable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
    vm.is_callable(&obj)
}

fn builtin_chr(i: u32, vm: &VirtualMachine) -> PyResult<String> {
    match char::from_u32(i) {
        Some(value) => Ok(value.to_string()),
        None => Err(vm.new_value_error("chr() arg not in range(0x110000)".to_owned())),
    }
}

#[derive(FromArgs)]
#[allow(dead_code)]
struct CompileArgs {
    #[pyarg(positional_only, optional = false)]
    source: Either<PyStringRef, PyBytesRef>,
    #[pyarg(positional_only, optional = false)]
    filename: PyStringRef,
    #[pyarg(positional_only, optional = false)]
    mode: PyStringRef,
    #[pyarg(positional_or_keyword, optional = true)]
    flags: OptionalArg<PyIntRef>,
    #[pyarg(positional_or_keyword, optional = true)]
    dont_inherit: OptionalArg<bool>,
    #[pyarg(positional_or_keyword, optional = true)]
    optimize: OptionalArg<PyIntRef>,
}

fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult {
    // TODO: compile::compile should probably get bytes
    let source = match &args.source {
        Either::A(string) => string.as_str(),
        Either::B(bytes) => str::from_utf8(bytes).unwrap(),
    };

    let mode_str = args.mode.as_str();

    let flags = args
        .flags
        .map_or(Ok(0), |v| i32::try_from_object(vm, v.into_object()))?;

    if (flags & ast::PY_COMPILE_FLAG_AST_ONLY).is_zero() {
        #[cfg(feature = "rustpython-compiler")]
        {
            let mode = mode_str
                .parse::<compile::Mode>()
                .map_err(|err| vm.new_value_error(err.to_string()))?;

            vm.compile(&source, mode, args.filename.as_str().to_owned())
                .map(|o| o.into_object())
                .map_err(|err| vm.new_syntax_error(&err))
        }
        #[cfg(not(feature = "rustpython-compiler"))]
        {
            Err(vm.new_value_error("PyCF_ONLY_AST flag is required without compiler support"))
        }
    } else {
        use rustpython_parser::parser;
        let mode = mode_str
            .parse::<parser::Mode>()
            .map_err(|err| vm.new_value_error(err.to_string()))?;
        ast::parse(&vm, &source, mode)
    }
}

fn builtin_delattr(obj: PyObjectRef, attr: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
    vm.del_attr(&obj, attr.into_object())
}

fn builtin_dir(obj: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
    let seq = match obj {
        OptionalArg::Present(obj) => vm.call_method(&obj, "__dir__", vec![])?,
        OptionalArg::Missing => vm.call_method(&vm.get_locals().into_object(), "keys", vec![])?,
    };
    let sorted = builtin_sorted(vm, PyFuncArgs::new(vec![seq], vec![]))?;
    Ok(sorted)
}

fn builtin_divmod(a: PyObjectRef, b: PyObjectRef, vm: &VirtualMachine) -> PyResult {
    vm.call_or_reflection(
        a.clone(),
        b.clone(),
        "__divmod__",
        "__rdivmod__",
        |vm, a, b| Err(vm.new_unsupported_operand_error(a, b, "divmod")),
    )
}

#[cfg(feature = "rustpython-compiler")]
#[derive(FromArgs)]
struct ScopeArgs {
    #[pyarg(positional_or_keyword, default = "None")]
    globals: Option<PyDictRef>,
    // TODO: support any mapping for `locals`
    #[pyarg(positional_or_keyword, default = "None")]
    locals: Option<PyDictRef>,
}

/// Implements `eval`.
/// See also: https://docs.python.org/3/library/functions.html#eval
#[cfg(feature = "rustpython-compiler")]
fn builtin_eval(
    source: Either<PyStringRef, PyCodeRef>,
    scope: ScopeArgs,
    vm: &VirtualMachine,
) -> PyResult {
    run_code(vm, source, scope, compile::Mode::Eval)
}

/// Implements `exec`
/// https://docs.python.org/3/library/functions.html#exec
#[cfg(feature = "rustpython-compiler")]
fn builtin_exec(
    source: Either<PyStringRef, PyCodeRef>,
    scope: ScopeArgs,
    vm: &VirtualMachine,
) -> PyResult {
    run_code(vm, source, scope, compile::Mode::Exec)
}

fn run_code(
    vm: &VirtualMachine,
    source: Either<PyStringRef, PyCodeRef>,
    scope: ScopeArgs,
    mode: compile::Mode,
) -> PyResult {
    let scope = make_scope(vm, scope)?;

    // Determine code object:
    let code_obj = match source {
        Either::A(string) => vm
            .compile(string.as_str(), mode, "<string>".to_owned())
            .map_err(|err| vm.new_syntax_error(&err))?,
        Either::B(code_obj) => code_obj,
    };

    // Run the code:
    vm.run_code_obj(code_obj, scope)
}

fn make_scope(vm: &VirtualMachine, scope: ScopeArgs) -> PyResult<Scope> {
    let globals = scope.globals;
    let current_scope = vm.current_scope();
    let locals = match scope.locals {
        Some(dict) => Some(dict),
        None => {
            if globals.is_some() {
                None
            } else {
                current_scope.get_only_locals()
            }
        }
    };
    let globals = match globals {
        Some(dict) => {
            if !dict.contains_key("__builtins__", vm) {
                let builtins_dict = vm
                    .builtins
                    .dict
                    .as_ref()
                    .unwrap()
                    .borrow()
                    .as_object()
                    .clone();
                dict.set_item("__builtins__", builtins_dict, vm).unwrap();
            }
            dict
        }
        None => current_scope.globals.clone(),
    };

    let scope = Scope::with_builtins(locals, globals, vm);
    Ok(scope)
}

fn builtin_format(
    value: PyObjectRef,
    format_spec: OptionalArg<PyStringRef>,
    vm: &VirtualMachine,
) -> PyResult<PyStringRef> {
    let format_spec = format_spec
        .into_option()
        .unwrap_or_else(|| PyString::from("").into_ref(vm));

    vm.call_method(&value, "__format__", vec![format_spec.into_object()])?
        .downcast()
        .map_err(|obj| {
            vm.new_type_error(format!(
                "__format__ must return a str, not {}",
                obj.class().name
            ))
        })
}

fn catch_attr_exception<T>(ex: PyBaseExceptionRef, default: T, vm: &VirtualMachine) -> PyResult<T> {
    if objtype::isinstance(&ex, &vm.ctx.exceptions.attribute_error) {
        Ok(default)
    } else {
        Err(ex)
    }
}

fn builtin_getattr(
    obj: PyObjectRef,
    attr: PyStringRef,
    default: OptionalArg<PyObjectRef>,
    vm: &VirtualMachine,
) -> PyResult {
    let ret = vm.get_attribute(obj.clone(), attr);
    if let OptionalArg::Present(default) = default {
        ret.or_else(|ex| catch_attr_exception(ex, default, vm))
    } else {
        ret
    }
}

fn builtin_globals(vm: &VirtualMachine) -> PyResult<PyDictRef> {
    Ok(vm.current_scope().globals.clone())
}

fn builtin_hasattr(obj: PyObjectRef, attr: PyStringRef, vm: &VirtualMachine) -> PyResult<bool> {
    if let Err(ex) = vm.get_attribute(obj.clone(), attr) {
        catch_attr_exception(ex, false, vm)
    } else {
        Ok(true)
    }
}

fn builtin_hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
    vm._hash(&obj)
}

// builtin_help

fn builtin_hex(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
    let n = number.as_bigint();
    let s = if n.is_negative() {
        format!("-0x{:x}", -n)
    } else {
        format!("0x{:x}", n)
    };

    Ok(vm.new_str(s))
}

fn builtin_id(obj: PyObjectRef) -> usize {
    obj.get_id()
}

// builtin_input

fn builtin_isinstance(obj: PyObjectRef, typ: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
    single_or_tuple_any(
        typ,
        |cls: PyClassRef| vm.isinstance(&obj, &cls),
        |o| {
            format!(
                "isinstance() arg 2 must be a type or tuple of types, not {}",
                o.class()
            )
        },
        vm,
    )
}

fn builtin_issubclass(
    subclass: PyClassRef,
    typ: PyObjectRef,
    vm: &VirtualMachine,
) -> PyResult<bool> {
    single_or_tuple_any(
        typ,
        |cls: PyClassRef| vm.issubclass(&subclass, &cls),
        |o| {
            format!(
                "issubclass() arg 2 must be a class or tuple of classes, not {}",
                o.class()
            )
        },
        vm,
    )
}

fn builtin_iter(iter_target: PyObjectRef, vm: &VirtualMachine) -> PyResult {
    objiter::get_iter(vm, &iter_target)
}

fn builtin_len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
    objsequence::len(&obj, vm)
}

fn builtin_locals(vm: &VirtualMachine) -> PyDictRef {
    let locals = vm.get_locals();
    locals.copy().into_ref(vm)
}

fn builtin_max(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
    let candidates = match args.args.len().cmp(&1) {
        std::cmp::Ordering::Greater => args.args.clone(),
        std::cmp::Ordering::Equal => vm.extract_elements(&args.args[0])?,
        std::cmp::Ordering::Less => {
            // zero arguments means type error:
            return Err(vm.new_type_error("Expected 1 or more arguments".to_owned()));
        }
    };

    if candidates.is_empty() {
        let default = args.get_optional_kwarg("default");
        return default
            .ok_or_else(|| vm.new_value_error("max() arg is an empty sequence".to_owned()));
    }

    let key_func = args.get_optional_kwarg("key");

    // Start with first assumption:
    let mut candidates_iter = candidates.into_iter();
    let mut x = candidates_iter.next().unwrap();
    // TODO: this key function looks pretty duplicate. Maybe we can create
    // a local function?
    let mut x_key = if let Some(ref f) = &key_func {
        vm.invoke(f, vec![x.clone()])?
    } else {
        x.clone()
    };

    for y in candidates_iter {
        let y_key = if let Some(ref f) = &key_func {
            vm.invoke(f, vec![y.clone()])?
        } else {
            y.clone()
        };
        let order = vm._gt(x_key.clone(), y_key.clone())?;

        if !objbool::get_value(&order) {
            x = y.clone();
            x_key = y_key;
        }
    }

    Ok(x)
}

fn builtin_min(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
    let candidates = match args.args.len().cmp(&1) {
        std::cmp::Ordering::Greater => args.args.clone(),
        std::cmp::Ordering::Equal => vm.extract_elements(&args.args[0])?,
        std::cmp::Ordering::Less => {
            // zero arguments means type error:
            return Err(vm.new_type_error("Expected 1 or more arguments".to_owned()));
        }
    };

    if candidates.is_empty() {
        let default = args.get_optional_kwarg("default");
        return default
            .ok_or_else(|| vm.new_value_error("min() arg is an empty sequence".to_owned()));
    }

    let key_func = args.get_optional_kwarg("key");

    let mut candidates_iter = candidates.into_iter();
    let mut x = candidates_iter.next().unwrap();
    // TODO: this key function looks pretty duplicate. Maybe we can create
    // a local function?
    let mut x_key = if let Some(ref f) = &key_func {
        vm.invoke(f, vec![x.clone()])?
    } else {
        x.clone()
    };

    for y in candidates_iter {
        let y_key = if let Some(ref f) = &key_func {
            vm.invoke(f, vec![y.clone()])?
        } else {
            y.clone()
        };
        let order = vm._gt(x_key.clone(), y_key.clone())?;

        if objbool::get_value(&order) {
            x = y.clone();
            x_key = y_key;
        }
    }

    Ok(x)
}

fn builtin_next(
    iterator: PyObjectRef,
    default_value: OptionalArg<PyObjectRef>,
    vm: &VirtualMachine,
) -> PyResult {
    match vm.call_method(&iterator, "__next__", vec![]) {
        Ok(value) => Ok(value),
        Err(value) => {
            if objtype::isinstance(&value, &vm.ctx.exceptions.stop_iteration) {
                match default_value {
                    OptionalArg::Missing => Err(value),
                    OptionalArg::Present(value) => Ok(value.clone()),
                }
            } else {
                Err(value)
            }
        }
    }
}

fn builtin_oct(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
    let n = number.as_bigint();
    let s = if n.is_negative() {
        format!("-0o{:o}", n.abs())
    } else {
        format!("0o{:o}", n)
    };

    Ok(vm.new_str(s))
}

fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) -> PyResult<u32> {
    match string {
        Either::A(bytes) => {
            let bytes_len = bytes.elements.len();
            if bytes_len != 1 {
                return Err(vm.new_type_error(format!(
                    "ord() expected a character, but string of length {} found",
                    bytes_len
                )));
            }
            Ok(u32::from(bytes.elements[0]))
        }
        Either::B(string) => {
            let string = string.as_str();
            let string_len = string.chars().count();
            if string_len != 1 {
                return Err(vm.new_type_error(format!(
                    "ord() expected a character, but string of length {} found",
                    string_len
                )));
            }
            match string.chars().next() {
                Some(character) => Ok(character as u32),
                None => Err(vm.new_type_error(
                    "ord() could not guess the integer representing this character".to_owned(),
                )),
            }
        }
    }
}

fn builtin_pow(
    x: PyObjectRef,
    y: PyObjectRef,
    mod_value: OptionalArg<PyIntRef>,
    vm: &VirtualMachine,
) -> PyResult {
    match mod_value {
        OptionalArg::Missing => {
            vm.call_or_reflection(x.clone(), y.clone(), "__pow__", "__rpow__", |vm, x, y| {
                Err(vm.new_unsupported_operand_error(x, y, "pow"))
            })
        }
        OptionalArg::Present(m) => {
            // Check if the 3rd argument is defined and perform modulus on the result
            if !(objtype::isinstance(&x, &vm.ctx.int_type())
                && objtype::isinstance(&y, &vm.ctx.int_type()))
            {
                return Err(vm.new_type_error(
                    "pow() 3rd argument not allowed unless all arguments are integers".to_owned(),
                ));
            }
            let y = objint::get_value(&y);
            if y.sign() == Sign::Minus {
                return Err(vm.new_value_error(
                    "pow() 2nd argument cannot be negative when 3rd argument specified".to_owned(),
                ));
            }
            let m = m.as_bigint();
            if m.is_zero() {
                return Err(vm.new_value_error("pow() 3rd argument cannot be 0".to_owned()));
            }
            let x = objint::get_value(&x);
            Ok(vm.new_int(x.modpow(&y, &m)))
        }
    }
}

#[derive(Debug, FromArgs)]
pub struct PrintOptions {
    #[pyarg(keyword_only, default = "None")]
    sep: Option<PyStringRef>,
    #[pyarg(keyword_only, default = "None")]
    end: Option<PyStringRef>,
    #[pyarg(keyword_only, default = "IntoPyBool::FALSE")]
    flush: IntoPyBool,
    #[pyarg(keyword_only, default = "None")]
    file: Option<PyObjectRef>,
}

trait Printer {
    fn write(&mut self, vm: &VirtualMachine, obj: PyStringRef) -> PyResult<()>;
    fn flush(&mut self, vm: &VirtualMachine) -> PyResult<()>;
}

impl Printer for &'_ PyObjectRef {
    fn write(&mut self, vm: &VirtualMachine, obj: PyStringRef) -> PyResult<()> {
        vm.call_method(self, "write", vec![obj.into_object()])?;
        Ok(())
    }

    fn flush(&mut self, vm: &VirtualMachine) -> PyResult<()> {
        vm.call_method(self, "flush", vec![])?;
        Ok(())
    }
}

impl Printer for std::io::StdoutLock<'_> {
    fn write(&mut self, _vm: &VirtualMachine, s: PyStringRef) -> PyResult<()> {
        write!(self, "{}", s).unwrap();
        Ok(())
    }

    fn flush(&mut self, _vm: &VirtualMachine) -> PyResult<()> {
        <Self as io::Write>::flush(self).unwrap();
        Ok(())
    }
}

pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
    let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
    Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
}

pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
    let stdout = io::stdout();

    let mut printer: Box<dyn Printer> = if let Some(file) = &options.file {
        Box::new(file)
    } else {
        Box::new(stdout.lock())
    };

    let sep = options
        .sep
        .unwrap_or_else(|| PyString::from(" ").into_ref(vm));

    let mut first = true;
    for object in objects {
        if first {
            first = false;
        } else {
            printer.write(vm, sep.clone())?;
        }

        printer.write(vm, vm.to_str(&object)?)?;
    }

    let end = options
        .end
        .unwrap_or_else(|| PyString::from("\n").into_ref(vm));
    printer.write(vm, end)?;

    if options.flush.to_bool() {
        printer.flush(vm)?;
    }

    Ok(())
}

fn builtin_repr(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStringRef> {
    vm.to_repr(&obj)
}

fn builtin_reversed(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
    if let Some(reversed_method) = vm.get_method(obj.clone(), "__reversed__") {
        vm.invoke(&reversed_method?, PyFuncArgs::default())
    } else {
        vm.get_method_or_type_error(obj.clone(), "__getitem__", || {
            "argument to reversed() must be a sequence".to_owned()
        })?;
        let len = vm.call_method(&obj.clone(), "__len__", PyFuncArgs::default())?;
        let obj_iterator = objiter::PySequenceIterator {
            position: Cell::new(objint::get_value(&len).to_isize().unwrap() - 1),
            obj: obj.clone(),
            reversed: true,
        };
        Ok(obj_iterator.into_ref(vm).into_object())
    }
}

fn builtin_round(
    number: PyObjectRef,
    ndigits: OptionalArg<Option<PyIntRef>>,
    vm: &VirtualMachine,
) -> PyResult {
    let rounded = match ndigits {
        OptionalArg::Present(ndigits) => match ndigits {
            Some(int) => {
                let ndigits = vm.call_method(int.as_object(), "__int__", vec![])?;
                vm.call_method(&number, "__round__", vec![ndigits])?
            }
            None => vm.call_method(&number, "__round__", vec![])?,
        },
        OptionalArg::Missing => {
            // without a parameter, the result type is coerced to int
            vm.call_method(&number, "__round__", vec![])?
        }
    };
    Ok(rounded)
}

fn builtin_setattr(
    obj: PyObjectRef,
    attr: PyStringRef,
    value: PyObjectRef,
    vm: &VirtualMachine,
) -> PyResult<()> {
    vm.set_attr(&obj, attr.into_object(), value)?;
    Ok(())
}

// builtin_slice

fn builtin_sorted(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
    arg_check!(vm, args, required = [(iterable, None)]);
    let items = vm.extract_elements(iterable)?;
    let lst = vm.ctx.new_list(items);

    args.shift();
    vm.call_method(&lst, "sort", args)?;
    Ok(lst)
}

fn builtin_sum(iterable: PyIterable, start: OptionalArg, vm: &VirtualMachine) -> PyResult {
    // Start with zero and add at will:
    let mut sum = start.into_option().unwrap_or_else(|| vm.ctx.new_int(0));
    for item in iterable.iter(vm)? {
        sum = vm._add(sum, item?)?;
    }
    Ok(sum)
}

// Should be renamed to builtin___import__?
fn builtin_import(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
    vm.invoke(&vm.import_func.borrow(), args)
}

fn builtin_vars(obj: OptionalArg, vm: &VirtualMachine) -> PyResult {
    if let OptionalArg::Present(obj) = obj {
        vm.get_attribute(obj, "__dict__")
    } else {
        Ok(vm.get_locals().into_object())
    }
}

// builtin_vars

pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
    let ctx = &vm.ctx;

    #[cfg(target_arch = "wasm32")]
    let open = vm.ctx.none();
    #[cfg(not(target_arch = "wasm32"))]
    let open = vm.ctx.new_function(io_open);

    #[cfg(feature = "rustpython-compiler")]
    {
        extend_module!(vm, module, {
            "eval" => ctx.new_function(builtin_eval),
            "exec" => ctx.new_function(builtin_exec),
        });
    }

    let debug_mode: bool = vm.settings.optimize == 0;
    extend_module!(vm, module, {
        "__debug__" => ctx.new_bool(debug_mode),
        //set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
        "__name__" => ctx.new_str(String::from("__main__")),

        "abs" => ctx.new_function(builtin_abs),
        "all" => ctx.new_function(builtin_all),
        "any" => ctx.new_function(builtin_any),
        "ascii" => ctx.new_function(builtin_ascii),
        "bin" => ctx.new_function(builtin_bin),
        "bool" => ctx.bool_type(),
        "bytearray" => ctx.bytearray_type(),
        "bytes" => ctx.bytes_type(),
        "callable" => ctx.new_function(builtin_callable),
        "chr" => ctx.new_function(builtin_chr),
        "classmethod" => ctx.classmethod_type(),
        "compile" => ctx.new_function(builtin_compile),
        "complex" => ctx.complex_type(),
        "delattr" => ctx.new_function(builtin_delattr),
        "dict" => ctx.dict_type(),
        "divmod" => ctx.new_function(builtin_divmod),
        "dir" => ctx.new_function(builtin_dir),
        "enumerate" => ctx.enumerate_type(),
        "float" => ctx.float_type(),
        "frozenset" => ctx.frozenset_type(),
        "filter" => ctx.filter_type(),
        "format" => ctx.new_function(builtin_format),
        "getattr" => ctx.new_function(builtin_getattr),
        "globals" => ctx.new_function(builtin_globals),
        "hasattr" => ctx.new_function(builtin_hasattr),
        "hash" => ctx.new_function(builtin_hash),
        "hex" => ctx.new_function(builtin_hex),
        "id" => ctx.new_function(builtin_id),
        "int" => ctx.int_type(),
        "isinstance" => ctx.new_function(builtin_isinstance),
        "issubclass" => ctx.new_function(builtin_issubclass),
        "iter" => ctx.new_function(builtin_iter),
        "len" => ctx.new_function(builtin_len),
        "list" => ctx.list_type(),
        "locals" => ctx.new_function(builtin_locals),
        "map" => ctx.map_type(),
        "max" => ctx.new_function(builtin_max),
        "memoryview" => ctx.memoryview_type(),
        "min" => ctx.new_function(builtin_min),
        "object" => ctx.object(),
        "oct" => ctx.new_function(builtin_oct),
        "open" => open,
        "ord" => ctx.new_function(builtin_ord),
        "next" => ctx.new_function(builtin_next),
        "pow" => ctx.new_function(builtin_pow),
        "print" => ctx.new_function(builtin_print),
        "property" => ctx.property_type(),
        "range" => ctx.range_type(),
        "repr" => ctx.new_function(builtin_repr),
        "reversed" => ctx.new_function(builtin_reversed),
        "round" => ctx.new_function(builtin_round),
        "set" => ctx.set_type(),
        "setattr" => ctx.new_function(builtin_setattr),
        "sorted" => ctx.new_function(builtin_sorted),
        "slice" => ctx.slice_type(),
        "staticmethod" => ctx.staticmethod_type(),
        "str" => ctx.str_type(),
        "sum" => ctx.new_function(builtin_sum),
        "super" => ctx.super_type(),
        "tuple" => ctx.tuple_type(),
        "type" => ctx.type_type(),
        "vars" => ctx.new_function(builtin_vars),
        "zip" => ctx.zip_type(),
        "exit" => ctx.new_function(builtin_exit),
        "quit" => ctx.new_function(builtin_exit),
        "__import__" => ctx.new_function(builtin_import),
        "__build_class__" => ctx.new_function(builtin_build_class_),

        // Constants
        "NotImplemented" => ctx.not_implemented(),
        "Ellipsis" => vm.ctx.ellipsis.clone(),

        // Exceptions:
        "BaseException" => ctx.exceptions.base_exception_type.clone(),
        "Exception" => ctx.exceptions.exception_type.clone(),
        "ArithmeticError" => ctx.exceptions.arithmetic_error.clone(),
        "AssertionError" => ctx.exceptions.assertion_error.clone(),
        "AttributeError" => ctx.exceptions.attribute_error.clone(),
        "NameError" => ctx.exceptions.name_error.clone(),
        "OverflowError" => ctx.exceptions.overflow_error.clone(),
        "RuntimeError" => ctx.exceptions.runtime_error.clone(),
        "ReferenceError" => ctx.exceptions.reference_error.clone(),
        "SyntaxError" =>  ctx.exceptions.syntax_error.clone(),
        "IndentationError" =>  ctx.exceptions.indentation_error.clone(),
        "TabError" =>  ctx.exceptions.tab_error.clone(),
        "NotImplementedError" => ctx.exceptions.not_implemented_error.clone(),
        "RecursionError" => ctx.exceptions.recursion_error.clone(),
        "TypeError" => ctx.exceptions.type_error.clone(),
        "ValueError" => ctx.exceptions.value_error.clone(),
        "IndexError" => ctx.exceptions.index_error.clone(),
        "ImportError" => ctx.exceptions.import_error.clone(),
        "LookupError" => ctx.exceptions.lookup_error.clone(),
        "StopIteration" => ctx.exceptions.stop_iteration.clone(),
        "StopAsyncIteration" => ctx.exceptions.stop_async_iteration.clone(),
        "SystemError" => ctx.exceptions.system_error.clone(),
        "UnicodeError" => ctx.exceptions.unicode_error.clone(),
        "UnicodeDecodeError" => ctx.exceptions.unicode_decode_error.clone(),
        "UnicodeEncodeError" => ctx.exceptions.unicode_encode_error.clone(),
        "UnicodeTranslateError" => ctx.exceptions.unicode_translate_error.clone(),
        "ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
        "KeyError" => ctx.exceptions.key_error.clone(),
        "ModuleNotFoundError" => ctx.exceptions.module_not_found_error.clone(),
        "EOFError" => ctx.exceptions.eof_error.clone(),
        "MemoryError" => ctx.exceptions.memory_error.clone(),

        "OSError" => ctx.exceptions.os_error.clone(),
        "FileNotFoundError" => ctx.exceptions.file_not_found_error.clone(),
        "PermissionError" => ctx.exceptions.permission_error.clone(),
        "FileExistsError" => ctx.exceptions.file_exists_error.clone(),
        "BlockingIOError" => ctx.exceptions.blocking_io_error.clone(),
        "InterruptedError" => ctx.exceptions.interrupted_error.clone(),
        "ConnectionError" => ctx.exceptions.connection_error.clone(),
        "ConnectionResetError" => ctx.exceptions.connection_reset_error.clone(),
        "ConnectionRefusedError" => ctx.exceptions.connection_refused_error.clone(),
        "ConnectionAbortedError" => ctx.exceptions.connection_aborted_error.clone(),
        "BrokenPipeError" => ctx.exceptions.broken_pipe_error.clone(),

        // Warnings
        "Warning" => ctx.exceptions.warning.clone(),
        "BytesWarning" => ctx.exceptions.bytes_warning.clone(),
        "UnicodeWarning" => ctx.exceptions.unicode_warning.clone(),
        "DeprecationWarning" => ctx.exceptions.deprecation_warning.clone(),
        "PendingDeprecationWarning" => ctx.exceptions.pending_deprecation_warning.clone(),
        "FutureWarning" => ctx.exceptions.future_warning.clone(),
        "ImportWarning" => ctx.exceptions.import_warning.clone(),
        "SyntaxWarning" => ctx.exceptions.syntax_warning.clone(),
        "ResourceWarning" => ctx.exceptions.resource_warning.clone(),
        "RuntimeWarning" => ctx.exceptions.runtime_warning.clone(),
        "UserWarning" => ctx.exceptions.user_warning.clone(),

        "KeyboardInterrupt" => ctx.exceptions.keyboard_interrupt.clone(),
        "GeneratorExit" => ctx.exceptions.generator_exit.clone(),
        "SystemExit" => ctx.exceptions.system_exit.clone(),
    });
}

pub fn builtin_build_class_(
    function: PyFunctionRef,
    qualified_name: PyStringRef,
    bases: Args<PyClassRef>,
    mut kwargs: KwArgs,
    vm: &VirtualMachine,
) -> PyResult {
    let name = qualified_name.as_str().split('.').next_back().unwrap();
    let name_obj = vm.new_str(name.to_owned());

    let mut metaclass = if let Some(metaclass) = kwargs.pop_kwarg("metaclass") {
        PyClassRef::try_from_object(vm, metaclass)?
    } else {
        vm.get_type()
    };

    for base in bases.clone() {
        if objtype::issubclass(&base.class(), &metaclass) {
            metaclass = base.class();
        } else if !objtype::issubclass(&metaclass, &base.class()) {
            return Err(vm.new_type_error(
                "metaclass conflict: the metaclass of a derived class must be a (non-strict) \
                 subclass of the metaclasses of all its bases"
                    .to_owned(),
            ));
        }
    }

    let bases = bases.into_tuple(vm);

    // Prepare uses full __getattribute__ resolution chain.
    let prepare = vm.get_attribute(metaclass.clone().into_object(), "__prepare__")?;
    let namespace = vm.invoke(&prepare, vec![name_obj.clone(), bases.clone()])?;

    let namespace: PyDictRef = TryFromObject::try_from_object(vm, namespace)?;

    let cells = vm.ctx.new_dict();

    let scope = function
        .scope()
        .new_child_scope_with_locals(cells.clone())
        .new_child_scope_with_locals(namespace.clone());

    function.invoke_with_scope(vec![].into(), &scope, vm)?;

    let class = vm.invoke(
        metaclass.as_object(),
        vec![name_obj, bases, namespace.into_object()],
    )?;
    cells.set_item("__class__", class.clone(), vm)?;
    Ok(class)
}