Synopsis - Cross-Reference

File: /tests/Cxx-API/Python/src/Interpreter.cc
 1#include <iostream>
 2#include <Synopsis/Python/Interpreter.hh>
 3#include <Synopsis/Python/Module.hh>
 4#include <string>
 5#include <cstdio>
 6#include <iostream>
 7#include "Guard.hh"
 8
 9using namespace Synopsis::Python;
10
11void print(Dict &d)
12{
13  List keys = d.keys();
14  for (int i = 0; i != keys.size(); ++i)
15  {
16     Object key = keys.get(i);
17     std::cout << Object::narrow<const char *>(key) << ' '
18               << Object::narrow<const char *>(d.get(key).str()) << std::endl;
19  }
20}
21
22void test1()
23{
24  std::cout << "=== test1 ===" << std::endl;
25  Interpreter interp;
26  Module module = Module::import("__main__");
27  Dict global = module.dict();
28  Dict local;
29  Object retn = interp.run_string("print 'hello world'", Interpreter::FILE,
30                                  global, local);
31}
32
33void test2()
34{
35  std::cout << "=== test2 ===" << std::endl;
36  Interpreter interp;
37  Module path = Module::import("os.path");
38  std::string dir = Object::narrow<std::string>(path.attr("dirname")(Tuple(__FILE__)));
39  std::string script = Object::narrow<std::string>(path.attr("join")(Tuple(dir, "greeting.py")));
40
41  Module module = Module::import("__main__");
42  Dict global = module.dict();
43  Dict local;
44  Object retn = interp.run_file(script, Interpreter::FILE,
45                                global, local);
46}
47
48void test3()
49{
50  std::cout << "=== test3 ===" << std::endl;
51  Interpreter interp;
52  Module path = Module::import("os.path");
53  std::string dir = Object::narrow<std::string>(path.attr("dirname")(Tuple(__FILE__)));
54  std::string script = Object::narrow<std::string>(path.attr("join")(Tuple(dir, "Command.py")));
55
56  Module module = Module::import("__main__");
57  Dict global = module.dict();
58  Dict local;
59  Object retn = interp.run_file(script, Interpreter::FILE,
60                                global, local);
61  Object type = local.get("Command");
62  Tuple args("first", "second", "third");
63  Dict kwds;
64  kwds.set("input", "foo.h");
65  kwds.set("output", "foo.i");
66  retn = type();
67  retn = type(args);
68  retn = type(args, kwds);
69  Object method = retn.attr("execute");
70  method();
71}
72
73int main(int, char **)
74{  
75  try
76  {
77    test1();
78    test2();
79    test3();
80  }
81  catch (const Interpreter::Exception &)
82  {
83    PyErr_Print();
84  }
85  catch (const std::exception &e)
86  {
87    std::cout << "Error : " << e.what() << std::endl;
88  }
89}