Make all python code compatible with Python 2.x and Python 3.x.

* wrap/python/buddy.i (__le__, __lt__, __eq__, __ne__, __ge__
__gt__): New operators for bdd.
* wrap/python/spot.i (__le__, __lt__, __eq__, __ne__, __ge__
__gt__, __hash__): New operators for formula.
(nl_cout, nl_cerr): New functions.
* wrap/python/tests/bddnqueen.py,
wrap/python/tests/interdep.py, wrap/python/tests/ltl2tgba.py,
wrap/python/tests/ltlparse.py, wrap/python/tests/ltlsimple.py,
wrap/python/tests/minato.py, wrap/python/tests/modgray.py: Adjust
to the new print syntax by using sys.output.write() or nl_cout()
instead.
* wrap/python/tests/optionmap.py: Remove all print calls.
* wrap/python/ajax/spot.in: Massive adjustments in order to work
with both Python 2 and 3.  In python 3, reopening stdout as
unbuffered requires it to be open as binary, which in turns
requires any string output to be encoded manually.  BaseHTTPServer
and CGIHTTPServer have been merged into http.server, so we have
to try two different import syntaxes.  execfile no longer exists,
so it has to be emulated.
This also fixes two bugs where the script would segfault on
empty input, or when calling Tau03 on automata with less then
one acceptance conditions.
This commit is contained in:
Alexandre Duret-Lutz 2012-02-25 13:36:29 +01:00
parent 5e77b2498a
commit 61127a3fd5
12 changed files with 262 additions and 155 deletions

View file

@ -1,9 +1,9 @@
// Copyright (C) 2009, 2010, 2011 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
// -*- encoding: utf-8 -*-
// Copyright (C) 2009, 2010, 2011, 2012 Laboratoire de Recherche
// et Développement de l'Epita (LRDE).
// Copyright (C) 2003, 2004, 2005, 2006 Laboratoire d'Informatique
// de Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
// de Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
//
// This file is part of Spot, a model checking library.
//
@ -114,6 +114,11 @@ using namespace spot;
"$result = t_output_helper($result, SWIG_FromCharPtr(*$1));";
%apply char** OUTPUT { char** err };
// False and True cannot be redefined in Python3, even in a class.
// Spot uses these in an enum of the constant class.
%rename(FalseVal) False;
%rename(TrueVal) True;
%include "misc/version.hh"
%include "misc/bddalloc.hh"
%include "misc/minato.hh"
@ -203,11 +208,23 @@ using namespace spot;
// When comparing formula, make sure Python compare our
// pointers, not the pointers to its wrappers.
int
__cmp__(const spot::ltl::formula* b)
{
return b - self;
}
// __cmp__ is for Python 2.0
int __cmp__(const spot::ltl::formula* b) { return self - b; }
// These are for Python 2.1+ or 3.x. They more closely match
// the logic in Spot.
bool __lt__(const spot::ltl::formula* b)
{ spot::ltl::formula_ptr_less_than lt; return lt(self, b); }
bool __le__(const spot::ltl::formula* b)
{ spot::ltl::formula_ptr_less_than lt; return !lt(b, self); }
bool __eq__(const spot::ltl::formula* b) { return self == b; }
bool __ne__(const spot::ltl::formula* b) { return self != b; }
bool __gt__(const spot::ltl::formula* b)
{ spot::ltl::formula_ptr_less_than lt; return lt(b, self); }
bool __ge__(const spot::ltl::formula* b)
{ spot::ltl::formula_ptr_less_than lt; return !lt(self, b); }
size_t __hash__() { return self->hash(); }
std::string
__str__(void)
@ -265,12 +282,24 @@ get_cout()
return std::cout;
}
void
nl_cout()
{
std::cout << std::endl;
}
std::ostream&
get_cerr()
{
return std::cerr;
}
void
nl_cerr()
{
std::cerr << std::endl;
}
void
print_on(std::ostream& on, const std::string& what)
{