Skip to content

Commit

Permalink
Python embedding: fix symbol conflicts with python library (OSGeo#3215)
Browse files Browse the repository at this point in the history
there is a project called pystack . which used to print the python
process's stack info . pystack will use gdb to attach the python process
and call some function defined in python's executable file or python
library
such as
call (void *) PyGILState_Ensure()
call (void) PyRun_SimpleString("exec(r\"f = open('/tmp/pystack', 'w'); \
itervalues = lambda d:getattr(d,'itervalues',d.values)();import gc,traceback,itertools,sys; \
f.write('Dumping Threads\\n\\n\\n');f.write('\\n---------------\\n'.join(str(traceback.format_stack(o)
) for o in itervalues(sys._current_frames())));f.close()\")")
...

but the symbol PyGILState_Ensure also defined in gdal .so if a python
script use gdal , we can't use pystack to debug this .

so i changed the symbol of gdal by add Lib infix

Co-authored-by: kk <kaihaojiang@tencent.com>
  • Loading branch information
cs-chicken and kk authored Nov 24, 2020
1 parent eba850f commit b0a16e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
.idea
.vscode
.vs
.vim

# clangd lsp
.clangd
compile_commands.json

# Python
__pycache__/
Expand All @@ -28,3 +33,5 @@ CMakeLists.txt
# NuGet package manager storage
gdal/packages*
gdal/share


12 changes: 6 additions & 6 deletions gdal/gcore/gdalpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ static std::mutex gMutex;
static bool gbHasInitializedPython = false;
static PyThreadState* gphThreadState = nullptr;

static PyGILState_STATE (*PyGILState_Ensure)(void) = nullptr;
static void (*PyGILState_Release)(PyGILState_STATE) = nullptr;
static PyGILState_STATE (*PyLibGILState_Ensure)(void) = nullptr;
static void (*PyLibGILState_Release)(PyGILState_STATE) = nullptr;

// Emulate Py_CompileString with Py_CompileStringExFlags
// Probably just a temporary measure for a bug of Python 3.8.0 on Windows
Expand Down Expand Up @@ -735,8 +735,8 @@ static bool LoadPythonAPI()
LOAD(libHandle, PySequence_Size);
LOAD(libHandle, PySequence_GetItem);
LOAD(libHandle, PyArg_ParseTuple);
LOAD(libHandle, PyGILState_Ensure);
LOAD(libHandle, PyGILState_Release);
LOAD_WITH_NAME(libHandle, PyLibGILState_Ensure, "PyGILState_Ensure");
LOAD_WITH_NAME(libHandle, PyLibGILState_Release, "PyGILState_Release");
LOAD(libHandle, PyErr_Fetch);
LOAD(libHandle, PyErr_Clear);
LOAD(libHandle, Py_GetVersion);
Expand Down Expand Up @@ -815,7 +815,7 @@ GIL_Holder::GIL_Holder(bool bExclusiveLock):
{
gMutex.lock();
}
m_eState = PyGILState_Ensure();
m_eState = PyLibGILState_Ensure();
}

/************************************************************************/
Expand All @@ -824,7 +824,7 @@ GIL_Holder::GIL_Holder(bool bExclusiveLock):

GIL_Holder::~GIL_Holder()
{
PyGILState_Release(m_eState);
PyLibGILState_Release(m_eState);
if( m_bExclusiveLock )
{
gMutex.unlock();
Expand Down

0 comments on commit b0a16e6

Please sign in to comment.