@@ -24,7 +24,7 @@ func init() {
24
24
// py.NewMethod("ascii", builtin_ascii, 0, ascii_doc),
25
25
// py.NewMethod("bin", builtin_bin, 0, bin_doc),
26
26
// py.NewMethod("callable", builtin_callable, 0, callable_doc),
27
- // py.NewMethod("chr", builtin_chr, 0, chr_doc),
27
+ py .NewMethod ("chr" , builtin_chr , 0 , chr_doc ),
28
28
py .NewMethod ("compile" , builtin_compile , 0 , compile_doc ),
29
29
// py.NewMethod("delattr", builtin_delattr, 0, delattr_doc),
30
30
// py.NewMethod("dir", builtin_dir, 0, dir_doc),
@@ -557,3 +557,21 @@ Return the number of items of a sequence or mapping.`
557
557
func builtin_len (self , v py.Object ) py.Object {
558
558
return py .Len (v )
559
559
}
560
+
561
+ const chr_doc = `chr(i) -> Unicode character
562
+
563
+ Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.`
564
+
565
+ func builtin_chr (self py.Object , args py.Tuple ) py.Object {
566
+ var xObj py.Object
567
+
568
+ py .ParseTuple (args , "i:chr" , & xObj )
569
+
570
+ x := xObj .(py.Int )
571
+ if x < 0 || x >= 0x110000 {
572
+ panic (py .ExceptionNewf (py .ValueError , "chr() arg not in range(0x110000)" ))
573
+ }
574
+ buf := make ([]byte , 8 )
575
+ n := utf8 .EncodeRune (buf , rune (x ))
576
+ return py .String (buf [:n ])
577
+ }
0 commit comments