6
6
package builtin
7
7
8
8
import (
9
+ "fmt"
10
+ "math/big"
9
11
"unicode/utf8"
10
12
11
13
"github.com/go-python/gpython/compile"
@@ -25,7 +27,7 @@ func init() {
25
27
py .MustNewMethod ("all" , builtin_all , 0 , all_doc ),
26
28
py .MustNewMethod ("any" , builtin_any , 0 , any_doc ),
27
29
py .MustNewMethod ("ascii" , builtin_ascii , 0 , ascii_doc ),
28
- // py.MustNewMethod("bin", builtin_bin, 0, bin_doc),
30
+ py .MustNewMethod ("bin" , builtin_bin , 0 , bin_doc ),
29
31
// py.MustNewMethod("callable", builtin_callable, 0, callable_doc),
30
32
py .MustNewMethod ("chr" , builtin_chr , 0 , chr_doc ),
31
33
py .MustNewMethod ("compile" , builtin_compile , 0 , compile_doc ),
@@ -39,10 +41,10 @@ func init() {
39
41
py .MustNewMethod ("globals" , py .InternalMethodGlobals , 0 , globals_doc ),
40
42
py .MustNewMethod ("hasattr" , builtin_hasattr , 0 , hasattr_doc ),
41
43
// py.MustNewMethod("hash", builtin_hash, 0, hash_doc),
42
- // py.MustNewMethod("hex", builtin_hex, 0, hex_doc),
44
+ py .MustNewMethod ("hex" , builtin_hex , 0 , hex_doc ),
43
45
// py.MustNewMethod("id", builtin_id, 0, id_doc),
44
46
// py.MustNewMethod("input", builtin_input, 0, input_doc),
45
- // py.MustNewMethod("isinstance", builtin_isinstance, 0, isinstance_doc),
47
+ py .MustNewMethod ("isinstance" , builtin_isinstance , 0 , isinstance_doc ),
46
48
// py.MustNewMethod("issubclass", builtin_issubclass, 0, issubclass_doc),
47
49
py .MustNewMethod ("iter" , builtin_iter , 0 , iter_doc ),
48
50
py .MustNewMethod ("len" , builtin_len , 0 , len_doc ),
@@ -58,7 +60,7 @@ func init() {
58
60
py .MustNewMethod ("repr" , builtin_repr , 0 , repr_doc ),
59
61
py .MustNewMethod ("round" , builtin_round , 0 , round_doc ),
60
62
py .MustNewMethod ("setattr" , builtin_setattr , 0 , setattr_doc ),
61
- // py.MustNewMethod("sorted", builtin_sorted, 0, sorted_doc),
63
+ py .MustNewMethod ("sorted" , builtin_sorted , 0 , sorted_doc ),
62
64
py .MustNewMethod ("sum" , builtin_sum , 0 , sum_doc ),
63
65
// py.MustNewMethod("vars", builtin_vars, 0, vars_doc),
64
66
}
@@ -85,8 +87,8 @@ func init() {
85
87
"object" : py .ObjectType ,
86
88
"range" : py .RangeType ,
87
89
// "reversed": py.ReversedType,
88
- "set" : py .SetType ,
89
- // "slice": py.SliceType,
90
+ "set" : py .SetType ,
91
+ "slice" : py .SliceType ,
90
92
"staticmethod" : py .StaticMethodType ,
91
93
"str" : py .StringType ,
92
94
// "super": py.SuperType,
@@ -309,7 +311,12 @@ func builtin_any(self, seq py.Object) (py.Object, error) {
309
311
return py .False , nil
310
312
}
311
313
312
- const ascii_doc = `
314
+ const ascii_doc = `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.
313
320
`
314
321
315
322
func builtin_ascii (self , o py.Object ) (py.Object , error ) {
@@ -322,6 +329,29 @@ func builtin_ascii(self, o py.Object) (py.Object, error) {
322
329
return py .String (out ), err
323
330
}
324
331
332
+ const bin_doc = `Return the binary representation of an integer.
333
+
334
+ >>> bin(2796202)
335
+ '0b1010101010101010101010'
336
+ `
337
+
338
+ func builtin_bin (self , o py.Object ) (py.Object , error ) {
339
+ bigint , ok := py .ConvertToBigInt (o )
340
+ if ! ok {
341
+ return nil , py .ExceptionNewf (py .TypeError , "'%s' object cannot be interpreted as an integer" , o .Type ().Name )
342
+ }
343
+
344
+ value := (* big .Int )(bigint )
345
+ var out string
346
+ if value .Sign () < 0 {
347
+ value = new (big.Int ).Abs (value )
348
+ out = fmt .Sprintf ("-0b%b" , value )
349
+ } else {
350
+ out = fmt .Sprintf ("0b%b" , value )
351
+ }
352
+ return py .String (out ), nil
353
+ }
354
+
325
355
const round_doc = `round(number[, ndigits]) -> number
326
356
327
357
Round a number to a given precision in decimal digits (default 0 digits).
@@ -796,6 +826,91 @@ object.
796
826
The globals and locals are dictionaries, defaulting to the current
797
827
globals and locals. If only globals is given, locals defaults to it.`
798
828
829
+ const hex_doc = `hex(number) -> string
830
+
831
+ Return the hexadecimal representation of an integer.
832
+
833
+ >>> hex(12648430)
834
+ '0xc0ffee'
835
+ `
836
+
837
+ func builtin_hex (self , v py.Object ) (py.Object , error ) {
838
+ var (
839
+ i int64
840
+ err error
841
+ )
842
+ switch v := v .(type ) {
843
+ case * py.BigInt :
844
+ // test bigint first to make sure we correctly handle the case
845
+ // where int64 isn't large enough.
846
+ vv := (* big .Int )(v )
847
+ format := "%#x"
848
+ if vv .Cmp (big .NewInt (0 )) == - 1 {
849
+ format = "%+#x"
850
+ }
851
+ str := fmt .Sprintf (format , vv )
852
+ return py .String (str ), nil
853
+ case py.IGoInt64 :
854
+ i , err = v .GoInt64 ()
855
+ case py.IGoInt :
856
+ var vv int
857
+ vv , err = v .GoInt ()
858
+ i = int64 (vv )
859
+ default :
860
+ return nil , py .ExceptionNewf (py .TypeError , "'%s' object cannot be interpreted as an integer" , v .Type ().Name )
861
+ }
862
+
863
+ if err != nil {
864
+ return nil , err
865
+ }
866
+
867
+ format := "%#x"
868
+ if i < 0 {
869
+ format = "%+#x"
870
+ }
871
+ str := fmt .Sprintf (format , i )
872
+ return py .String (str ), nil
873
+ }
874
+
875
+ const isinstance_doc = `isinstance(obj, class_or_tuple) -> bool
876
+
877
+ Return whether an object is an instance of a class or of a subclass thereof.
878
+
879
+ A tuple, as in isinstance(x, (A, B, ...)), may be given as the target to
880
+ check against. This is equivalent to isinstance(x, A) or isinstance(x, B)
881
+ or ... etc.
882
+ `
883
+
884
+ func isinstance (obj py.Object , classOrTuple py.Object ) (py.Bool , error ) {
885
+ switch classOrTuple .(type ) {
886
+ case py.Tuple :
887
+ var class_tuple = classOrTuple .(py.Tuple )
888
+ for idx := range class_tuple {
889
+ res , _ := isinstance (obj , class_tuple [idx ])
890
+ if res {
891
+ return res , nil
892
+ }
893
+ }
894
+ return false , nil
895
+ default :
896
+ if classOrTuple .Type ().ObjectType != py .TypeType {
897
+ return false , py .ExceptionNewf (py .TypeError , "isinstance() arg 2 must be a type or tuple of types" )
898
+ }
899
+ return obj .Type () == classOrTuple , nil
900
+ }
901
+ }
902
+
903
+ func builtin_isinstance (self py.Object , args py.Tuple ) (py.Object , error ) {
904
+ var obj py.Object
905
+ var classOrTuple py.Object
906
+ err := py .UnpackTuple (args , nil , "isinstance" , 2 , 2 , & obj , & classOrTuple )
907
+ if err != nil {
908
+ return nil , err
909
+ }
910
+
911
+ return isinstance (obj , classOrTuple )
912
+ }
913
+
799
914
const iter_doc = `iter(iterable) -> iterator
800
915
iter(callable, sentinel) -> iterator
801
916
@@ -1044,3 +1159,28 @@ func builtin_sum(self py.Object, args py.Tuple) (py.Object, error) {
1044
1159
}
1045
1160
return start , nil
1046
1161
}
1162
+
1163
+ const sorted_doc = `sorted(iterable, key=None, reverse=False)
1164
+
1165
+ Return a new list containing all items from the iterable in ascending order.
1166
+
1167
+ A custom key function can be supplied to customize the sort order, and the
1168
+ reverse flag can be set to request the result in descending order.`
1169
+
1170
+ func builtin_sorted (self py.Object , args py.Tuple , kwargs py.StringDict ) (py.Object , error ) {
1171
+ const funcName = "sorted"
1172
+ var iterable py.Object
1173
+ err := py .UnpackTuple (args , nil , funcName , 1 , 1 , & iterable )
1174
+ if err != nil {
1175
+ return nil , err
1176
+ }
1177
+ l , err := py .SequenceList (iterable )
1178
+ if err != nil {
1179
+ return nil , err
1180
+ }
1181
+ err = py .SortInPlace (l , kwargs , funcName )
1182
+ if err != nil {
1183
+ return nil , err
1184
+ }
1185
+ return l , nil
1186
+ }
0 commit comments