Skip to content

Commit

Permalink
Merge pull request #24 from lsst/tickets/DM-9701
Browse files Browse the repository at this point in the history
DM-9701: pybind11 cleanup
  • Loading branch information
Pim Schellart committed Mar 3, 2017
2 parents 97f2f94 + 32580f6 commit a534a79
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 21 deletions.
2 changes: 1 addition & 1 deletion python/lsst/log/SConscript
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.pybind11(['log'])
scripts.BasicSConscript.pybind11(['log/log'], addUnderscore=False)
24 changes: 24 additions & 0 deletions python/lsst/log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
#
# LSST Data Management System
#
# Copyright 2008-2017 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import absolute_import

from .log import *
from .version import * # generated by sconsUtils unless you tell it not to
26 changes: 26 additions & 0 deletions python/lsst/log/log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# LSST Data Management System
#
# Copyright 2008-2017 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import absolute_import

from .log import *
from .logContinued import *
37 changes: 21 additions & 16 deletions python/lsst/log/log.cc → python/lsst/log/log/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,32 @@

namespace py = pybind11;

using namespace lsst::log;
namespace lsst {
namespace log {

// Wrapper for Python callable object to make sure that we have GIL
// when we call Python. Note that we are leaking Python callable,
// as C++ callables may be (and actually are in our particular case)
// outliving Python interpreter and attempt to delete Python object
// will result in crash.
//
// See DM-9708
class callable_wrapper {
public:
callable_wrapper(PyObject* callable) : _callable(callable) {
Py_XINCREF(_callable);
}
callable_wrapper(PyObject* callable) : _callable(callable) { Py_XINCREF(_callable); }
void operator()() {
// make sure we own GIL before doing Python call
auto state = PyGILState_Ensure();
PyObject_CallObject(_callable, nullptr);
PyGILState_Release(state);
}

private:
PyObject* _callable;
};

PYBIND11_PLUGIN(_log) {
py::module mod("_log", "Python wrapper for log library");
PYBIND11_PLUGIN(log) {
py::module mod("log");

py::class_<Log> cls(mod, "Log");

Expand All @@ -74,28 +76,31 @@ PYBIND11_PLUGIN(_log) {
cls.def("setLevel", &Log::setLevel);
cls.def("getLevel", &Log::getLevel);
cls.def("isEnabledFor", &Log::isEnabledFor);
cls.def("logMsg", [](Log & log, int level, std::string const& filename,
std::string const& funcname, unsigned int lineno, std::string const& msg) {
cls.def("logMsg", [](Log& log, int level, std::string const& filename, std::string const& funcname,
unsigned int lineno, std::string const& msg) {
log.logMsg(log4cxx::Level::toLevel(level),
log4cxx::spi::LocationInfo(filename.c_str(), funcname.c_str(), lineno), msg.c_str());
log4cxx::spi::LocationInfo(filename.c_str(), funcname.c_str(), lineno), msg.c_str());
});
cls.def("lwpID", [](Log const & log) -> unsigned { return lsst::log::lwpID(); });
cls.def("lwpID", [](Log const& log) -> unsigned { return lsst::log::lwpID(); });

cls.def_static("getDefaultLogger", Log::getDefaultLogger);
cls.def_static("getDefaultLoggerName", Log::getDefaultLoggerName);
cls.def_static("configure", (void (*)()) Log::configure);
cls.def_static("configure", (void (*)(std::string const&)) Log::configure);
cls.def_static("configure", (void (*)())Log::configure);
cls.def_static("configure", (void (*)(std::string const&))Log::configure);
cls.def_static("configure_prop", Log::configure_prop);
cls.def_static("getLogger", (Log (*)(Log const&)) Log::getLogger);
cls.def_static("getLogger", (Log (*)(std::string const&)) Log::getLogger);
cls.def_static("getLogger", (Log(*)(Log const&))Log::getLogger);
cls.def_static("getLogger", (Log(*)(std::string const&))Log::getLogger);
cls.def_static("pushContext", Log::pushContext);
cls.def_static("popContext", Log::popContext);
cls.def_static("MDC", Log::MDC);
cls.def_static("MDCRemove", Log::MDCRemove);
cls.def_static("MDCRegisterInit", [](py::function func) {
auto handle = func.release(); // will leak as described in callable_wrapper
Log::MDCRegisterInit(std::function<void()>(callable_wrapper(handle.ptr())));
auto handle = func.release(); // will leak as described in callable_wrapper
Log::MDCRegisterInit(std::function<void()>(callable_wrapper(handle.ptr())));
});

return mod.ptr();
}

} // log
} // lsst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from lsst.utils import continueClass

from ._log import Log
from .log import Log

TRACE = 5000
DEBUG = 10000
Expand Down Expand Up @@ -266,3 +266,4 @@ def translateLevel(self, levelno):
to standard log4cxx levels.
"""
return levelno*1000

3 changes: 0 additions & 3 deletions python/lsst/log/logLib.py

This file was deleted.

0 comments on commit a534a79

Please sign in to comment.