Skip to content

Commit f863723

Browse files
committed
Make sys.args accurate and implement -cpuprofile flag
1 parent 00ffff0 commit f863723

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55
import (
66
"flag"
77
"fmt"
8+
"runtime/pprof"
89

910
_ "github.com/ncw/gpython/builtin"
1011
"github.com/ncw/gpython/repl"
@@ -17,15 +18,16 @@ import (
1718
"github.com/ncw/gpython/compile"
1819
"github.com/ncw/gpython/marshal"
1920
"github.com/ncw/gpython/py"
20-
_ "github.com/ncw/gpython/sys"
21+
pysys "github.com/ncw/gpython/sys"
2122
_ "github.com/ncw/gpython/time"
2223
"github.com/ncw/gpython/vm"
2324
)
2425

2526
// Globals
2627
var (
2728
// Flags
28-
debug = flag.Bool("d", false, "Print lots of debugging")
29+
debug = flag.Bool("d", false, "Print lots of debugging")
30+
cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to file")
2931
)
3032

3133
// syntaxError prints the syntax
@@ -53,13 +55,26 @@ func main() {
5355
flag.Usage = syntaxError
5456
flag.Parse()
5557
args := flag.Args()
58+
py.MustGetModule("sys").Globals["argv"] = pysys.MakeArgv(args)
5659
if len(args) == 0 {
5760
repl.Run()
5861
return
5962
}
6063
prog := args[0]
6164
fmt.Printf("Running %q\n", prog)
6265

66+
if *cpuprofile != "" {
67+
f, err := os.Create(*cpuprofile)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
err = pprof.StartCPUProfile(f)
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
defer pprof.StopCPUProfile()
76+
}
77+
6378
// FIXME should be using ImportModuleLevelObject() here
6479
f, err := os.Open(prog)
6580
if err != nil {

notes.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ Code Quality
44
* errchk ./...
55
* go vet ./...
66

7+
Limitations
8+
===========
9+
* string keys only in dictionaries
10+
* ints only 64 bit
11+
712
Todo
813
====
914

15+
Speedup
16+
* Make Object a fat interface so it defines all the M__method__ or at least all the common ones
17+
* Make the methods be pointers in *Type more like python
18+
19+
1020
* Getting rid of panic/defer error handling
1121

1222
Speed 35% improvement

py/module.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ func NewModule(name, doc string, methods []*Method, globals StringDict) *Module
6464
return m
6565
}
6666

67+
// Gets a module
68+
func GetModule(name string) (*Module, error) {
69+
m, ok := modules[name]
70+
if !ok {
71+
return nil, ExceptionNewf(ImportError, "Module %q not found", name)
72+
}
73+
return m, nil
74+
}
75+
76+
// Gets a module or panics
77+
func MustGetModule(name string) *Module {
78+
m, err := GetModule(name)
79+
if err != nil {
80+
panic(err)
81+
}
82+
return m
83+
}
84+
6785
// Calls a named method of a module
6886
func (m *Module) Call(name string, args Tuple, kwargs StringDict) (Object, error) {
6987
attr, err := GetAttrString(m, name)

sys/sys.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,11 +648,7 @@ func init() {
648648
py.MustNewMethod("call_tracing", sys_call_tracing, 0, call_tracing_doc),
649649
py.MustNewMethod("_debugmallocstats", sys_debugmallocstats, 0, debugmallocstats_doc),
650650
}
651-
pyargs := os.Args[1:]
652-
argv := py.NewListSized(len(pyargs))
653-
for i, v := range pyargs {
654-
argv.Items[i] = py.String(v)
655-
}
651+
argv := MakeArgv(os.Args[1:])
656652
stdin, stdout, stderr := (*py.File)(os.Stdin), (*py.File)(os.Stdout), (*py.File)(os.Stderr)
657653
globals := py.StringDict{
658654
"argv": argv,
@@ -787,3 +783,12 @@ func init() {
787783
}
788784
py.NewModule("sys", module_doc, methods, globals)
789785
}
786+
787+
// Makes an argv into a tuple
788+
func MakeArgv(pyargs []string) py.Object {
789+
argv := py.NewListSized(len(pyargs))
790+
for i, v := range pyargs {
791+
argv.Items[i] = py.String(v)
792+
}
793+
return argv
794+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy