Skip to content

Commit

Permalink
Merge pull request #8 from fredboudon/py3
Browse files Browse the repository at this point in the history
Improve python 3 compatibility
  • Loading branch information
fredboudon authored Nov 26, 2019
2 parents 0a3d290 + 89a172c commit 012a481
Show file tree
Hide file tree
Showing 80 changed files with 500 additions and 272 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ set(Boost_NO_SYSTEM_PATHS ON)
set(Boost_USE_MULTITHREAD ON)
set(Boost_USE_STATIC_LIBS OFF)

find_package(Boost 1.67 COMPONENTS system ${BOOST_PYTHON_LIB} REQUIRED)
find_package(Boost COMPONENTS system ${BOOST_PYTHON_LIB} REQUIRED)

# --- Include Directories

Expand Down
4 changes: 2 additions & 2 deletions cmake/FindPlantGL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif()
# Include Directory
find_path(PLANTGL_INCLUDE_DIR
NAMES "plantgl/plantgl.h"
HINTS ${PLANTGL_INCLUDEDIR} $ENV{PATH})
HINTS ${PLANTGL_INCLUDEDIR} $ENV{PATH} $ENV{CONDA_PREFIX}/include)

if (NOT PLANTGL_INCLUDE_DIR)
set(PLANTGL_FOUND OFF)
Expand All @@ -41,7 +41,7 @@ endif()
# Library Directory
find_library(PLANTGL_SG_LIBRARY
NAMES "pglsg" "libpglsg"
PATHS ${PLANTGL_LIBRARYDIR} $ENV{PATH})
PATHS ${PLANTGL_LIBRARYDIR} $ENV{PATH} $ENV{CONDA_PREFIX}/lib $ENV{CONDA_PREFIX}/lib64)

get_filename_component(PLANTGL_LIBRARY_DIR ${PLANTGL_SG_LIBRARY} DIRECTORY)

Expand Down
25 changes: 20 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@
import os, sys
pj = os.path.join

from openalea.deploy.metainfo import read_metainfo
metadata = read_metainfo('metainfo.ini', verbose=True)
for key,value in metadata.items():
exec("%s = '%s'" % (key, value))
# from openalea.deploy.metainfo import read_metainfo
# metadata = read_metainfo('metainfo.ini', verbose=True)
# for key,value in metadata.items():
# exec("%s = '%s'" % (key, value))

version = '2.7.1'
release = '2.7'
project = 'openalea'
package = 'lpy'
name = 'OpenAlea.Lpy'
namespace = 'openalea'
pkg_name = 'openalea.lpy'
description = 'Lindenmayer Systems in Python package for OpenAlea.'
long_description= 'L-Py is a simulation software that mixes L-systems construction with the Python high-level modeling language. '
authors = 'Frederic Boudon'
authors_email = 'frederic.boudon@cirad.fr'
url= 'https://github.com/openalea/lpy'
# LGPL compatible INRIA license
license = 'Cecill-C'

##############
# Setup script
Expand All @@ -31,7 +46,7 @@
build_prefix = "build-cmake"

from setuptools import setup
from openalea.deploy.binary_deps import binary_deps
# from openalea.deploy.binary_deps import binary_deps

def compile_interface():
cwd = os.getcwd()
Expand Down
7 changes: 4 additions & 3 deletions share/examples/apicaldominancy.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def Start():
timing = t.time()

def End():
print 'Execution time :',t.time()-timing
print ('Execution time :',t.time()-timing)

def txt(val):
return str(round(val,2))[:5]
Expand Down Expand Up @@ -96,5 +96,6 @@ endlsystem
###### INITIALISATION ######

def __initialiseContext__(context):
from openalea.plantgl.scenegraph import Material,ImageTexture,Color3
context.turtle.setMaterial(1,Material('Brown',Color3(33,22,6),2.72727,Color3(40,40,40),Color3(0,0,0),1,0))
from openalea.plantgl.all import PglTurtle
from openalea.plantgl.scenegraph import Material,ImageTexture,Color3
context.turtle.setMaterial(1,Material('Brown',Color3(33,22,6),2.72727,Color3(40,40,40),Color3(0,0,0),1,0))
2 changes: 1 addition & 1 deletion share/examples/dynamicprofile.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p = profile.deepcopy()
curves = [(n,v) for n,v in globals().items() if 'axis' in n and type(v) == NurbsCurve2D ]
for n,v in curves: v.name = n
curves = [v for n,v in curves]
curves.sort(lambda x,y : cmp(x.name,y.name))
curves.sort(key = lambda x : x.name)


#time between each curve
Expand Down
2 changes: 1 addition & 1 deletion share/examples/ex_lsystem5.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ derivation length: 3
production:

A :
for i in xrange(100):
for i in range(100):
nproduce K(1,i)

K(x,i)K(y,j) << K(z,m) :
Expand Down
7 changes: 4 additions & 3 deletions src/cpp/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ LPY_API void LsysSyntaxError(const std::string& error)
LPY_API void LsysSyntaxError(const std::string& error,const std::string& filename, int lineno)
{
if (!filename.empty() || lineno >=0){
std::stringstream stream;
stream << (filename.empty()?"<string>":filename) << ':' << lineno << ':' << error;
PyErr_SetString(PyExc_SyntaxError, stream.str().c_str());
// std::stringstream stream;
// stream << (filename.empty()?"<string>":filename) << ':' << lineno << ':' << error;
PyErr_SetString(PyExc_SyntaxError, error.c_str());
PyErr_SyntaxLocation(filename.c_str(), lineno);
boost::python::throw_error_already_set();
}
else LsysSyntaxError(error);
Expand Down
15 changes: 12 additions & 3 deletions src/cpp/lpy_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ Lsystem::set( const std::string& _rules , std::string * pycode,
if (LpyParsing::isValidVariableName(itmod->name))
code += itmod->name+" = ModuleClass.get('"+itmod->name+"')";
}
code+="# "+std::string(_it2,_it);
code+=" # "+std::string(_it2,_it);
beg = _it;
toendlineA(_it,endpycode);
}
Expand All @@ -508,7 +508,7 @@ Lsystem::set( const std::string& _rules , std::string * pycode,
if(has_keyword_pattern(_it,begcode,endpycode,"undeclare")){
code+=std::string(beg,_it2);
LpyParsing::ModNameList modules = LpyParsing::parse_modlist(_it,endpycode,false);
code+="# "+std::string(_it2,_it);
code+=" # "+std::string(_it2,_it);
for(LpyParsing::ModNameList::const_iterator itmod = modules.begin();
itmod != modules.end(); ++itmod){
ModuleClassPtr mod = ModuleClassTable::get().find(*itmod);
Expand Down Expand Up @@ -985,7 +985,16 @@ Lsystem::set( const std::string& _rules , std::string * pycode,
code+='\n'+addedcode;
if(pycode) *pycode = code;
// printf("%s",code.c_str());
__context.compile(code);
try {
__context.compile(code);
}
catch (const error_already_set& e) {
if (PyErr_ExceptionMatches(PyExc_SyntaxError)){
// PyErr_SyntaxLocation(getFilename().c_str(), 0);
}
boost::python::throw_error_already_set();
// boost::python::handle_exception();
}
__importPyFunctions();
if (__context.hasObject(LsysContext::AxiomVariable)){
if (!axiom_is_function){
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/lpyrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ void cleanLpy()
std::cerr << "****** pre-cleaning *******" << std::endl;
Tracker::printReport();
#endif
LsysContext::cleanContexts();
ModuleClassTable::clearModuleClasses ();
ViewerApplication::exit ();
LsysContext::cleanContexts();
#ifdef TRACKER_ENABLED
std::cerr << "****** post-cleaning ******" << std::endl;
Tracker::printReport();
Expand Down
79 changes: 48 additions & 31 deletions src/cpp/lsyscontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,44 @@ static LsysContext * DEFAULT_LSYSCONTEXT = NULL;
static LsysContext * CURRENT_LSYSCONTEXT = NULL;
static QMutex CURRENT_LSYSCONTEXT_MUTEX;


/*
class ContextGarbageCollector
{
public:
ContextGarbageCollector() {}
~ContextGarbageCollector() {
// std::cerr << "context garbage collector" << std::endl;
printf("context garbage collector\n");
if (GLOBAL_LSYSCONTEXT){
LsysContext::cleanContexts();
}
}
protected:
static ContextGarbageCollector __INSTANCE;
};
*/

void LsysContext::cleanContexts(){
// VERY STRANGE: Cannot delete global and default context.
QMutexLocker locker(&CURRENT_LSYSCONTEXT_MUTEX);
if (DEFAULT_LSYSCONTEXT){
delete DEFAULT_LSYSCONTEXT;
// delete DEFAULT_LSYSCONTEXT;
DEFAULT_LSYSCONTEXT = NULL;
}
if (GLOBAL_LSYSCONTEXT)
{
delete GLOBAL_LSYSCONTEXT;
GLOBAL_LSYSCONTEXT = NULL;
if(!(LSYSCONTEXT_STACK.empty() && GLOBAL_LSYSCONTEXT->isCurrent()))
while(!GLOBAL_LSYSCONTEXT->isCurrent()) currentContext()->done();
assert(LSYSCONTEXT_STACK.empty() && GLOBAL_LSYSCONTEXT->isCurrent() && "LsysContext not all done!");

// delete GLOBAL_LSYSCONTEXT;
GLOBAL_LSYSCONTEXT = NULL;
}
}

LsysContext *
LsysContext::globalContext()
{
if(!GLOBAL_LSYSCONTEXT) GLOBAL_LSYSCONTEXT = new GlobalContext();
return GLOBAL_LSYSCONTEXT;
return GlobalContext::get();
}

void createDefaultContext()
Expand Down Expand Up @@ -649,10 +654,10 @@ void
LsysContext::namespaceInitialisation()
{
if (!hasObject("__builtins__")){
copyObjectToGlobals("__builtins__",globalContext());
// copyObjectToGlobals("__builtins__",globalContext());

// object builtin = boost::python::import("__builtin__");
// setObjectToGlobals("__builtins__", builtin);
object builtins = boost::python::import("builtins");
setObjectToGlobals("__builtins__", builtins);
// setObjectToGlobals("__builtins__", object(handle<>(borrowed( PyModule_GetDict(PyImport_AddModule("__builtin__"))))));
}

Expand Down Expand Up @@ -922,37 +927,39 @@ LsysContext::func(const std::string& funcname){
return object();
}

size_t func_nb_args(boost::python::object function) {
const char * attrname =
#if PY_MAJOR_VERSION == 2
"func_code"
#else
"__code__"
#endif
;
try {
return extract<size_t>(function.attr(attrname).attr("co_argcount"))();
}
catch (...) { PyErr_Clear(); return 0; }
}

void
LsysContext::check_init_functions()
{
if (hasObject("StartEach")) {
try {
__nbargs_of_starteach = extract<size_t>(getObject("StartEach").attr("func_code").attr("co_argcount"))();
}
catch (...) { PyErr_Clear(); __nbargs_of_starteach = 0; }
}
__nbargs_of_starteach = func_nb_args(getObject("StartEach"));
}
else __nbargs_of_starteach = 0;

if (hasObject("Start")) {
try {
__nbargs_of_start = extract<size_t>(getObject("Start").attr("func_code").attr("co_argcount"))();
}
catch (...) { PyErr_Clear(); __nbargs_of_start = 0; }
__nbargs_of_start = func_nb_args(getObject("Start"));
}
else __nbargs_of_start = 0;

if (hasObject("EndEach")) {
try {
__nbargs_of_endeach = extract<size_t>(getObject("EndEach").attr("func_code").attr("co_argcount"))();
}
catch (...) { PyErr_Clear(); __nbargs_of_endeach = 0; }
__nbargs_of_endeach = func_nb_args(getObject("EndEach"));
}
else __nbargs_of_endeach = 0;
if (hasObject("End")) {
try {
__nbargs_of_end = extract<size_t>(getObject("End").attr("func_code").attr("co_argcount"))();
}
catch (...) { PyErr_Clear(); __nbargs_of_end = 0; }
__nbargs_of_end = func_nb_args(getObject("End"));
}
else __nbargs_of_end = 0;
}
Expand Down Expand Up @@ -1183,12 +1190,18 @@ LocalContext::globals() const

GlobalContext::GlobalContext():
LsysContext(){
__globals = handle<>(borrowed( PyModule_GetDict(PyImport_AddModule("__main__"))));
__globals = object(handle<>(borrowed(PyModule_GetDict(PyImport_AddModule("__main__")))));
}

GlobalContext::~GlobalContext()
GlobalContext * GlobalContext::get()
{
if(!GLOBAL_LSYSCONTEXT) GLOBAL_LSYSCONTEXT = new GlobalContext();
return GLOBAL_LSYSCONTEXT;
}


GlobalContext::~GlobalContext()
{
if(!(LSYSCONTEXT_STACK.empty() && isCurrent()))
while(!isCurrent()) currentContext()->done();
assert(LSYSCONTEXT_STACK.empty() && isCurrent() && "LsysContext not all done!");
Expand All @@ -1197,7 +1210,7 @@ GlobalContext::~GlobalContext()

PyObject *
GlobalContext::globals() const
{ return __globals.get(); }
{ return __globals.ptr(); }


boost::python::object GlobalContext::__reprFunc;
Expand All @@ -1215,6 +1228,10 @@ GlobalContext::getFunctionRepr() {
return __reprFunc;
}


void
GlobalContext::clearNamespace() {
__locals.clear();
}
/*---------------------------------------------------------------------------*/


7 changes: 4 additions & 3 deletions src/cpp/lsyscontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,16 @@ class LPY_API LocalContext : public LsysContext {

class LPY_API GlobalContext : public LsysContext {
public:
GlobalContext();
~GlobalContext();

virtual PyObject * globals() const ;
static boost::python::object getFunctionRepr();

virtual void clearNamespace();
static GlobalContext * get();
protected:
GlobalContext();

boost::python::handle<> __globals;
boost::python::object __globals;
static boost::python::object __reprFunc;

};
Expand Down
8 changes: 4 additions & 4 deletions src/cpp/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ void processArgList(ModuleClassPtr mclass, ParamModule::ParameterList& args, boo
}
pgl_hash_set<size_t> missingargs;

while( true )
{

do {
boost::python::object obj = iter_obj.next();
if (!iter_obj.is_valid()) break;

std::string pname = extract<std::string>( obj[0] )();
size_t pposition = mclass->getParameterPosition(pname);
Expand Down Expand Up @@ -142,7 +141,8 @@ void processArgList(ModuleClassPtr mclass, ParamModule::ParameterList& args, boo
appendParam(args,obj[1]);
}
}
}
} while( iter_obj.is_valid() );

if (missingargs.size() > 0) {
std::stringstream str;
str << mclass->name << " takes exactly " << mclass->getNamedParameterNb()<< " (" << missingargs.size() << " missing)";
Expand Down
1 change: 1 addition & 0 deletions src/openalea/lpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def __next__(self):
self.axiom = self.lsystem.derive(self.axiom,self.currentstep,1)
self.currentstep += 1
return self.axiom
next = __next__
def __iter__(self):
return self

Expand Down
1 change: 0 additions & 1 deletion src/openalea/lpy/gui/__init__.py

This file was deleted.

1 change: 1 addition & 0 deletions src/openalea/lpy/gui/compile_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def compile_ui(uifname):
""" compile a Ui """
pyfname = get_uifnames_from(uifname)
fstream = open(pyfname,'w')
# compile_args["from_imports"] = ""
compileUi(uifname, fstream, **compile_args)
fstream.close()

Expand Down
Loading

0 comments on commit 012a481

Please sign in to comment.