From 277ffa614e262be55fac6c3530f1c3b98ad7cf13 Mon Sep 17 00:00:00 2001 From: Abhijit Gadgil Date: Mon, 15 Oct 2018 13:43:13 +0530 Subject: [PATCH 1/2] Updated README to reflect Python 3 support explicitly --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7f14f714c..36ba0212c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # RustPython -A Python Interpreter written in Rust :snake: :scream: :metal:. +A Python-3 (CPython >= 3.5.0) Interpreter written in Rust :snake: :scream: :metal:. [![Build Status](https://travis-ci.org/RustPython/RustPython.svg?branch=master)](https://travis-ci.org/RustPython/RustPython) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) @@ -26,16 +26,16 @@ Or use the interactive shell: # Goals -- Full python environment entirely in Rust (not CPython bindings) +- Full Python-3 environment entirely in Rust (not CPython bindings) - A clean implementation without compatibility hacks # Code organization -- `parser`: python lexing, parsing and ast -- `vm`: python virtual machine -- `src`: using the other subcrates to bring rustpython to life. -- `docs`: documentation (work in progress) -- `py_code_object`: CPython bytecode to rustpython bytecode convertor (work in progress) +- `parser`: python lexing, parsing and ast +- `vm`: python virtual machine +- `src`: using the other subcrates to bring rustpython to life. +- `docs`: documentation (work in progress) +- `py_code_object`: CPython bytecode to rustpython bytecode convertor (work in progress) - `tests`: integration test snippets # Contributing From 3e7f1683599fa6715b5328e8e3206afe34c60779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Tue, 16 Oct 2018 17:19:43 +0200 Subject: [PATCH 2/2] builtin ord basic implementation. Fixes #151 --- tests/snippets/builtin_ord.py | 23 +++++++++++++++++++++++ vm/src/builtins.rs | 23 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/snippets/builtin_ord.py diff --git a/tests/snippets/builtin_ord.py b/tests/snippets/builtin_ord.py new file mode 100644 index 000000000..2dd03f91a --- /dev/null +++ b/tests/snippets/builtin_ord.py @@ -0,0 +1,23 @@ +assert ord("a") == 97 +assert ord("é") == 233 +assert ord("🤡") == 129313 +try: + ord() +except TypeError: + pass +else: + assert False, "TypeError not raised when ord() is called with no argument" + +try: + ord("") +except TypeError: + pass +else: + assert False, "TypeError not raised when ord() is called with an empty string" + +try: + ord("ab") +except TypeError: + pass +else: + assert False, "TypeError not raised when ord() is called with more than one character" diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index 4c4c936e7..85eb80a10 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -323,7 +323,27 @@ fn builtin_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { // builtin_object // builtin_oct // builtin_open -// builtin_ord + +fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]); + let string = objstr::get_value(string); + 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 + ) + .to_string(), + )); + } + match string.chars().next() { + Some(character) => Ok(vm.context().new_int(character as i32)), + None => Err(vm.new_type_error( + "ord() could not guess the integer representing this character".to_string(), + )), + } +} fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( @@ -454,6 +474,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef { dict.insert(String::from("list"), ctx.list_type()); dict.insert(String::from("locals"), ctx.new_rustfunc(builtin_locals)); dict.insert(String::from("map"), ctx.new_rustfunc(builtin_map)); + dict.insert(String::from("ord"), ctx.new_rustfunc(builtin_ord)); dict.insert(String::from("next"), ctx.new_rustfunc(builtin_next)); dict.insert(String::from("pow"), ctx.new_rustfunc(builtin_pow)); dict.insert(String::from("print"), ctx.new_rustfunc(builtin_print));