[Synopsis-changes] Synopsis/Synopsis/tests/CXX-API/Python Command.py,NONE,1.1 Dict.cc,NONE,1.1 Guard.hh,NONE,1.1 Interpreter.cc,NONE,1.1 List.cc,NONE,1.1 Makefile,NONE,1.1 Object.cc,NONE,1.1 Tuple.cc,NONE,1.1 script.py,NONE,1.1

Stefan Seefeld stefan at synopsis.fresco.org
Fri Jan 9 20:03:28 UTC 2004


Update of /cvs/synopsis/Synopsis/tests/CXX-API/Python
In directory frida:/tmp/cvs-serv10107/tests/CXX-API/Python

Added Files:
	Command.py Dict.cc Guard.hh Interpreter.cc List.cc Makefile 
	Object.cc Tuple.cc script.py 
Log Message:
new C++ interface

--- NEW FILE: Command.py ---
class Command:
   def __init__(self, *args, **kwds):
      print 'Command.__init__:'
      print args
      print kwds

--- NEW FILE: Dict.cc ---
#include <Synopsis/Dict.hh>
#include "Guard.hh"
#include <string>
#include <iostream>

using namespace Synopsis;

void test1()
{
  Dict dict;
  dict.set('a', 'A');
  dict.set(1, 2);
  dict.set("hello", "world");
  std::cout << Object::narrow<const char *>(dict.str()) << std::endl;
  std::cout << Object::narrow<const char *>(dict.keys().str()) << std::endl;
  std::cout << Object::narrow<const char *>(dict.values().str()) << std::endl;
  std::cout << Object::narrow<const char *>(dict.items().str()) << std::endl;
}

void test2()
{
  Dict dict;
  dict.set('a', 'A');
  std::cout << Object::narrow<const char *>(dict.get('a').str()) << std::endl;
  std::cout << Object::narrow<const char *>(dict.get('b').str()) << std::endl;
  dict.set(1, 2);
  dict.set("hello", "world");
  for (Dict::iterator i = dict.begin(); i != dict.end(); ++i)
    std::cout << Object::narrow<const char *>((*i).str()) << std::endl;
}

int main(int, char **)
{
  try
  {
    test1();
    test2();
  }
  catch (const std::exception &e)
  {
    std::cout << "Error : " << e.what() << std::endl;
  }
}

--- NEW FILE: Guard.hh ---
#ifndef _Guard_hh
#define _Guard_hh

#include <Python.h>

struct Guard
{
  Guard() { Py_Initialize();}
  ~Guard() { Py_Finalize();}
};

Guard guard;

#endif

--- NEW FILE: Interpreter.cc ---
#include <iostream>
#include <Synopsis/Interpreter.hh>
#include <Synopsis/Callable.hh>
#include <Synopsis/Module.hh>
#include "Guard.hh"
#include <string>
#include <cstdio>
#include <iostream>

using namespace Synopsis;

void print(Dict &d)
{
  List keys = d.keys();
  for (int i = 0; i != keys.size(); ++i)
  {
     Object key = keys.get(i);
     std::cout << Object::narrow<const char *>(key) << ' '
               << Object::narrow<const char *>(d.get(key).str()) << std::endl;
  }
}

void test1()
{
  std::cout << "=== test1 ===" << std::endl;
  Interpreter interp;
  Module module("__main__");
  Dict global = module.dict();
  Dict local;
  Object retn = interp.run_string("print 'hello world'", Interpreter::FILE,
                                  global, local);
}

void test2()
{
  std::cout << "=== test2 ===" << std::endl;
  Interpreter interp;
  Module module("__main__");
  Dict global = module.dict();
  Dict local;
  Object retn = interp.run_file("script.py", Interpreter::FILE,
                                global, local);
  print(global);
  print(local);  
}

void test3()
{
  std::cout << "=== test3 ===" << std::endl;
  Interpreter interp;
  Module module("__main__");
  Dict global = module.dict();
  Dict local;
  Object retn = interp.run_file("Command.py", Interpreter::FILE,
                                global, local);
  if (!retn) PyErr_Print();
  Object o = local.get("Command");
  if (!o) { throw std::runtime_error("missing object 'Command' in 'Command.py'");}
  Callable type = local.get("Command");
  Tuple args("first", "second", "third");
  Dict kwds;
  kwds.set("input", "foo.h");
  kwds.set("output", "foo.i");
  type.call();
  type.call(args);
  type.call(args, kwds);
}

int main(int, char **)
{  
  try
  {
    test1();
    test2();
    test3();
  }
  catch (const std::exception &e)
  {
    std::cout << "Error : " << e.what() << std::endl;
  }
}

--- NEW FILE: List.cc ---
#include <Synopsis/List.hh>
#include "Guard.hh"
#include <string>
#include <iostream>

using namespace Synopsis;

void test1()
{
  List list;
  list.append('a');
  list.append(1);
  list.append("hello");
  list.append("world");
  std::cout << Object::narrow<const char *>(list.str()) << std::endl;
  list.sort();
  std::cout << Object::narrow<const char *>(list.str()) << std::endl;
  list.reverse();
  std::cout << Object::narrow<const char *>(list.str()) << std::endl;
  Object o = list.get(0);
  std::cout << Object::narrow<const char *>(o.str()) << std::endl;
}

int main(int, char **)
{  
  try
  {
    test1();
  }
  catch (const std::exception &e)
  {
    std::cout << "Error : " << e.what() << std::endl;
  }
}

--- NEW FILE: Makefile ---
# $Id: Makefile,v 1.1 2004/01/09 20:03:26 stefan Exp $
#
# Copyright (C) 2003 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

SHELL	:= /bin/sh

python	:= python
PYPREFIX:=$(shell $(python) -c "import sys; print sys.prefix")
PYVERSION:=$(shell $(python) -c "from distutils import sysconfig; print sysconfig.get_config_var('VERSION')")
PYINC	:=$(shell $(python) -c "from distutils import sysconfig; print sysconfig.get_python_inc()")
PYLIBS	:=-L $(PYPREFIX)/lib/python$(PYVERSION)/config -lpython$(PYVERSION) -ldl -lutil -lpthread

CPPFLAGS:= -I .. -I $(PYINC)
LIBS	:= $(PYLIBS)

TESTS	:= Object List Tuple Dict Interpreter

all:	$(TESTS)

%.o: %.cc
	$(CXX) $(CPPFLAGS) -ggdb $(CXXFLAGS) -c -o $@ $<

$(TESTS): %: %.o
	$(CXX) -ggdb $(LDFLAGS) -o $@ $^ $(LIBS)

clean:
	rm -rf *.o $(TESTS)
--- NEW FILE: Object.cc ---
#include <Synopsis/Object.hh>
#include "Guard.hh"
#include <string>
#include <iostream>

using namespace Synopsis;

void test1()
{
  Object object;
  Object character('q');
  std::cout << Object::narrow<char>(character) << std::endl;
  std::cout << Object::narrow<const char *>(character.str()) << std::endl;
  Object string("hello world");
  std::cout << Object::narrow<const char *>(string) << std::endl;
  std::cout << Object::narrow<const char *>(string.str()) << std::endl;
  Object integer(42);
  std::cout << Object::narrow<long>(integer) << std::endl;
  std::cout << Object::narrow<const char *>(integer.str()) << std::endl;
  try { std::cout << Object::narrow<long>(string) << std::endl;}
  catch (Object::TypeError &e) { std::cout << e.what() << std::endl;}
}

int main(int, char **)
{  
  try
  {
    test1();
  }
  catch (const std::exception &e)
  {
    std::cout << "Error : " << e.what() << std::endl;
  }
}

--- NEW FILE: Tuple.cc ---
#include <Synopsis/Tuple.hh>
#include "Guard.hh"
#include <string>
#include <iostream>

using namespace Synopsis;

void test1()
{
  List list;
  list.append('a');
  list.append(1);
  list.append("hello");
  list.append("world");
  Tuple tuple(list);
  std::cout << Object::narrow<const char *>(tuple.str()) << std::endl;
  Object object(tuple);
  tuple = Object::narrow<Tuple>(object);
  std::cout << Object::narrow<const char *>(tuple.str()) << std::endl;
}

void test2()
{
  Tuple t1('1', '2', '3');
  std::cout << Object::narrow<const char *>(t1.str()) << std::endl;
  Tuple t2("hello", "world", 42);
  std::cout << Object::narrow<const char *>(t2.str()) << std::endl;
}

int main(int, char **)
{  
  try
  {
    test1();
    test2();
  }
  catch (const std::exception &e)
  {
    std::cout << "Error : " << e.what() << std::endl;
  }
}

--- NEW FILE: script.py ---

foo = 'bar'
print 'hello world'





More information about the Synopsis-changes mailing list