Skip to content

Commit e32e72b

Browse files
committed
Fixed dict usage before Python initialization (thanks to spitice)
1 parent 284fe5b commit e32e72b

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/core/export_main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@ bool modulsp_init( void )
8585
END_BOOST_PY(false)
8686
return true;
8787
}
88+
89+
90+
dict& DeferredDict::get() {
91+
if (!_pDict) {
92+
_pDict = std::make_shared<dict>();
93+
}
94+
95+
return *_pDict;
96+
}

src/core/export_main.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ typedef void (*ModuleInitFn)( scope );
5555
static CSPModule g_##package##_##name##_Init( XSTRINGIFY(package.name), &PyInit_##package##_##name ); \
5656
void PyInit_##package##_##name( scope name )
5757

58+
//---------------------------------------------------------------------------------
59+
// A deferred version of boost::python::dict. Thanks to spitice!
60+
// There was a change in Python that results in an access violation when Python is
61+
// not ready, yet.
62+
//---------------------------------------------------------------------------------
63+
class DeferredDict {
64+
private:
65+
std::shared_ptr<dict> _pDict;
66+
public:
67+
dict& get();
68+
};
69+
5870
//---------------------------------------------------------------------------------
5971
// This is a module definition structure. The way our module system is going to
6072
// work is you declare a module via BOOST_PYTHON_MODULE. Then the CPythonManager

src/core/modules/memory/memory_utilities.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ T* __obj__(CPointer* pPtr)
147147
// ============================================================================
148148
// Use this macro to add the class to the ExposedClasses dict
149149
#define STORE_CLASS(classname, pyname) \
150-
extern dict g_oExposedClasses; \
151-
g_oExposedClasses[XSTRINGIFY(classname)] = scope().attr(pyname);
150+
extern DeferredDict g_oExposedClasses; \
151+
g_oExposedClasses.get()[XSTRINGIFY(classname)] = scope().attr(pyname);
152152

153153

154154
// ============================================================================
@@ -202,12 +202,12 @@ END_CLASS_INFO()
202202
#define BEGIN_CLASS_INFO(classname) \
203203
{ \
204204
typedef classname functionInfoClass; \
205-
extern dict g_oClassInfo; \
205+
extern DeferredDict g_oClassInfo; \
206206
dict classInfoDict; \
207-
if (g_oClassInfo.contains( #classname )) \
208-
classInfoDict = extract<dict>(g_oClassInfo[ #classname ]); \
207+
if (g_oClassInfo.get().contains( #classname )) \
208+
classInfoDict = extract<dict>(g_oClassInfo.get()[ #classname ]); \
209209
else \
210-
g_oClassInfo[ #classname ] = classInfoDict;
210+
g_oClassInfo.get()[ #classname ] = classInfoDict;
211211

212212
// Finish a class info dictionary
213213
#define END_CLASS_INFO() \

src/core/modules/memory/memory_wrap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,16 +1018,16 @@ void export_functions(scope _memory)
10181018
// ============================================================================
10191019
// >> GLOBAL VARIABLES
10201020
// ============================================================================
1021-
dict g_oExposedClasses;
1022-
dict g_oClassInfo;
1021+
DeferredDict g_oExposedClasses;
1022+
DeferredDict g_oClassInfo;
10231023

10241024
#define ADD_NATIVE_TYPE_SIZE(name, type) \
10251025
scope().attr("TYPE_SIZES")[name] = sizeof(type);
10261026

10271027
void export_global_variables(scope _memory)
10281028
{
1029-
_memory.attr("EXPOSED_CLASSES") = g_oExposedClasses;
1030-
_memory.attr("CLASS_INFO") = g_oClassInfo;
1029+
_memory.attr("EXPOSED_CLASSES") = g_oExposedClasses.get();
1030+
_memory.attr("CLASS_INFO") = g_oClassInfo.get();
10311031

10321032
// Don't remove this! It's required for the ADD_NATIVE_TYPE_SIZE macro.
10331033
_memory.attr("TYPE_SIZES") = dict();

0 commit comments

Comments
 (0)