示例#1
0
文件: test.go 项目: gemrs/gem
func PythonTest(testDir string, t *testing.T) {
	// Disable panic recovery
	safe.Unsafe = true

	gopath := os.Getenv("GOPATH")
	pythonDir := fmt.Sprintf("%s/src/github.com/gemrs/gem/content", gopath) // There's probably a better way to locate this

	pyLaunchTest := fmt.Sprintf(`import pytest; test_result = pytest.main("-s %v/%v")`, pythonDir, testDir)

	argv := make([]*C.char, len(os.Args))

	for i, arg := range os.Args {
		argv[i] = C.CString(arg)
		defer C.free(unsafe.Pointer(argv[i]))
	}

	C.PySys_SetArgv(C.int(len(argv)), &argv[0])
	if main, err := py.NewDict(); err != nil {
		t.Fatal(err)
	} else if g, err := py.GetBuiltins(); err != nil {
		t.Fatal(err)
	} else if err := main.SetItemString("__builtins__", g); err != nil {
		t.Fatal(err)
	} else if _, err := py.RunString(pyLaunchTest, py.FileInput, main, nil); err != nil {
		t.Fatal(err)
	} else if a, err := main.GetItemString("test_result"); err != nil {
		t.Fatal(err)
	} else if b, ok := a.(*py.Int); !ok || b.Int() != 0 {
		t.Error("pytest returned non-zero exit code: %v", b.Int())
	}
}
示例#2
0
// PySys_SetArgv initializes the 'sys.argv' array in python.
func PySys_SetArgv(argv []string) {
	argc := C.int(len(argv))
	cargs := make([]*C.char, len(argv))
	for idx, arg := range argv {
		cargs[idx] = C.CString(arg)
		defer C.free(unsafe.Pointer(cargs[idx]))
	}
	C.PySys_SetArgv(argc, &cargs[0])
}