Skip to content

Commit 1b13b0c

Browse files
Merge pull request RustPython#231 from johndaniels/format
Add format builtin and String formatting stuff.
2 parents 62c53d8 + 95d4073 commit 1b13b0c

File tree

9 files changed

+826
-2
lines changed

9 files changed

+826
-2
lines changed

tests/snippets/builtin_format.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
assert format(5, "b") == "101"
2+
3+
try:
4+
format(2, 3)
5+
except TypeError:
6+
pass
7+
else:
8+
assert False, "TypeError not raised when format is called with a number"

tests/snippets/strings.py

+6
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@
4040

4141
c = 'hallo'
4242
assert c.capitalize() == 'Hallo'
43+
44+
# String Formatting
45+
assert "{} {}".format(1,2) == "1 2"
46+
assert "{0} {1}".format(2,3) == "2 3"
47+
assert "--{:s>4}--".format(1) == "--sss1--"
48+
assert "{keyword} {0}".format(1, keyword=2) == "2 1"

vm/src/builtins.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,15 @@ fn builtin_filter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
317317
Ok(vm.ctx.new_list(new_items))
318318
}
319319

320-
// builtin_format
320+
fn builtin_format(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
321+
arg_check!(
322+
vm,
323+
args,
324+
required = [(obj, None), (format_spec, Some(vm.ctx.str_type()))]
325+
);
326+
327+
vm.call_method(obj, "__format__", vec![format_spec.clone()])
328+
}
321329

322330
fn builtin_getattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
323331
arg_check!(
@@ -756,6 +764,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
756764
ctx.set_attr(&py_mod, "float", ctx.float_type());
757765
ctx.set_attr(&py_mod, "frozenset", ctx.frozenset_type());
758766
ctx.set_attr(&py_mod, "filter", ctx.new_rustfunc(builtin_filter));
767+
ctx.set_attr(&py_mod, "format", ctx.new_rustfunc(builtin_format));
759768
ctx.set_attr(&py_mod, "getattr", ctx.new_rustfunc(builtin_getattr));
760769
ctx.set_attr(&py_mod, "hasattr", ctx.new_rustfunc(builtin_hasattr));
761770
ctx.set_attr(&py_mod, "hash", ctx.new_rustfunc(builtin_hash));

vm/src/exceptions.rs

+16
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ pub struct ExceptionZoo {
8686
pub syntax_error: PyObjectRef,
8787
pub assertion_error: PyObjectRef,
8888
pub attribute_error: PyObjectRef,
89+
pub index_error: PyObjectRef,
90+
pub key_error: PyObjectRef,
8991
pub name_error: PyObjectRef,
9092
pub runtime_error: PyObjectRef,
9193
pub not_implemented_error: PyObjectRef,
@@ -129,6 +131,18 @@ impl ExceptionZoo {
129131
&exception_type.clone(),
130132
&dict_type,
131133
);
134+
let index_error = create_type(
135+
&String::from("IndexError"),
136+
&type_type,
137+
&exception_type.clone(),
138+
&dict_type,
139+
);
140+
let key_error = create_type(
141+
&String::from("KeyError"),
142+
&type_type,
143+
&exception_type.clone(),
144+
&dict_type,
145+
);
132146
let name_error = create_type(
133147
&String::from("NameError"),
134148
&type_type,
@@ -184,6 +198,8 @@ impl ExceptionZoo {
184198
syntax_error: syntax_error,
185199
assertion_error: assertion_error,
186200
attribute_error: attribute_error,
201+
index_error: index_error,
202+
key_error: key_error,
187203
name_error: name_error,
188204
runtime_error: runtime_error,
189205
not_implemented_error: not_implemented_error,

0 commit comments

Comments
 (0)