6
6
package builtin
7
7
8
8
import (
9
+ "unicode"
9
10
"unicode/utf8"
10
11
11
12
"github.com/go-python/gpython/compile"
@@ -24,7 +25,7 @@ func init() {
24
25
py .MustNewMethod ("abs" , builtin_abs , 0 , abs_doc ),
25
26
py .MustNewMethod ("all" , builtin_all , 0 , all_doc ),
26
27
py .MustNewMethod ("any" , builtin_any , 0 , any_doc ),
27
- // py.MustNewMethod("ascii", builtin_ascii, 0, ascii_doc),
28
+ py .MustNewMethod ("ascii" , builtin_ascii , 0 , ascii_doc ),
28
29
// py.MustNewMethod("bin", builtin_bin, 0, bin_doc),
29
30
// py.MustNewMethod("callable", builtin_callable, 0, callable_doc),
30
31
py .MustNewMethod ("chr" , builtin_chr , 0 , chr_doc ),
@@ -309,6 +310,37 @@ func builtin_any(self, seq py.Object) (py.Object, error) {
309
310
return py .False , nil
310
311
}
311
312
313
+ const ascii_doc = `ascii(obj, /)
314
+ Return an ASCII-only representation of an object.
315
+
316
+ As repr(), return a string containing a printable representation of an
317
+ object, but escape the non-ASCII characters in the string returned by
318
+ repr() using \\x, \\u or \\U escapes. This generates a string similar
319
+ to that returned by repr() in Python 2
320
+ `
321
+
322
+ func builtin_ascii (self , o py.Object ) (py.Object , error ) {
323
+ reprObject , err := py .Repr (o )
324
+ if err != nil {
325
+ return nil , err
326
+ }
327
+
328
+ repr := reprObject .(py.String )
329
+ asciiOnly := true
330
+ for _ , c := range repr {
331
+ if c > unicode .MaxASCII {
332
+ asciiOnly = false
333
+ break
334
+ }
335
+ }
336
+
337
+ if asciiOnly {
338
+ return repr , nil
339
+ }
340
+
341
+ return nil , py .ExceptionNewf (py .NotImplementedError , "" )
342
+ }
343
+
312
344
const round_doc = `round(number[, ndigits]) -> number
313
345
314
346
Round a number to a given precision in decimal digits (default 0 digits).
0 commit comments