@@ -31,7 +31,7 @@ func init() {
31
31
py .MustNewMethod ("chr" , builtin_chr , 0 , chr_doc ),
32
32
py .MustNewMethod ("compile" , builtin_compile , 0 , compile_doc ),
33
33
// py.MustNewMethod("delattr", builtin_delattr, 0, delattr_doc),
34
- // py.MustNewMethod("dir", builtin_dir, 0, dir_doc),
34
+ py .MustNewMethod ("dir" , builtin_dir , 0 , dir_doc ),
35
35
py .MustNewMethod ("divmod" , builtin_divmod , 0 , divmod_doc ),
36
36
py .MustNewMethod ("eval" , py .InternalMethodEval , 0 , eval_doc ),
37
37
py .MustNewMethod ("exec" , py .InternalMethodExec , 0 , exec_doc ),
@@ -642,6 +642,39 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Ob
642
642
return result , nil
643
643
}
644
644
645
+ const dir_doc = `dir([object]) -> list of strings
646
+
647
+ If called without an argument, return the names in the current scope.
648
+ Else, return an alphabetized list of names comprising (some of) the attributes
649
+ of the given object, and of attributes reachable from it.
650
+ If the object supplies a method named __dir__, it will be used; otherwise
651
+ the default dir() logic is used and returns:
652
+ for a module object: the module's attributes.
653
+ for a class object: its attributes, and recursively the attributes
654
+ of its bases.
655
+ for any other object: its attributes, its class's attributes, and
656
+ recursively the attributes of its class's base classes.
657
+ `
658
+
659
+ func builtin_dir (self py.Object , args py.Tuple ) (py.Object , error ) {
660
+ n , err := args .M__len__ ()
661
+ if err != nil {
662
+ return nil , err
663
+ }
664
+
665
+ nn := n .(py.Int )
666
+ if nn == 0 {
667
+ // list current scope.
668
+ panic ("dir() not implemented" )
669
+ }
670
+
671
+ if nn > 1 {
672
+ return nil , py .ExceptionNewf (py .TypeError , "dir expected at most 1 arguments, got %d" , nn )
673
+ }
674
+
675
+ panic ("dir(n) not implemented" )
676
+ }
677
+
645
678
const divmod_doc = `divmod(x, y) -> (quotient, remainder)
646
679
647
680
Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.`
0 commit comments