3535#include <Python.h>
3636#include <fuse.h>
3737
38+ #if PY_MAJOR_VERSION >= 3
39+ #define PyInt_FromLong PyLong_FromLong
40+ #define PyInt_AsLong PyLong_AsLong
41+ #define PyInt_Check PyLong_Check
42+ #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
43+ #define PyString_AsString PyUnicode_AsUTF8
44+ #define PyString_Check PyUnicode_Check
45+ #define PyString_Size PyUnicode_GET_SIZE
46+ #endif
47+
3848static PyObject * getattr_cb = NULL , * readlink_cb = NULL , * readdir_cb = NULL ,
3949 * mknod_cb = NULL , * mkdir_cb = NULL , * unlink_cb = NULL , * rmdir_cb = NULL ,
4050 * symlink_cb = NULL , * rename_cb = NULL , * link_cb = NULL , * chmod_cb = NULL ,
@@ -51,20 +61,31 @@ static PyInterpreterState *interp;
5161
5262#ifdef WITH_THREAD
5363
54- #define PYLOCK () \
55- PyThreadState *_state = NULL; \
56- if (interp) { \
64+ #if PY_MAJOR_VERSION >= 3
65+ #define PYLOCK () \
66+ PyGILState_STATE gstate; \
67+ gstate = PyGILState_Ensure();
68+ #else
69+ #define PYLOCK () \
70+ PyThreadState *_state = NULL; \
71+ if (interp) { \
5772 PyEval_AcquireLock(); \
5873 _state = PyThreadState_New(interp); \
5974 PyThreadState_Swap(_state); \
60- }
75+ }
76+ #endif
6177
62- #define PYUNLOCK () if (interp) { \
78+ #if PY_MAJOR_VERSION >= 3
79+ #define PYUNLOCK () PyGILState_Release(gstate);
80+ #else
81+ #define PYUNLOCK () \
82+ if (interp) { \
6383 PyThreadState_Clear(_state); \
6484 PyThreadState_Swap(NULL); \
6585 PyThreadState_Delete(_state); \
6686 PyEval_ReleaseLock(); \
67- }
87+ }
88+ #endif
6889
6990#else
7091#define PYLOCK ()
@@ -1203,21 +1224,41 @@ static PyMethodDef Fuse_methods[] = {
12031224 {NULL , NULL } /* sentinel */
12041225};
12051226
1227+ #if PY_MAJOR_VERSION >= 3
1228+ static struct PyModuleDef fuse_module = {
1229+ PyModuleDef_HEAD_INIT ,
1230+ "_fuse" , /* m_name */
1231+ "FUSE module" , /* m_doc */
1232+ -1 , /* m_size */
1233+ Fuse_methods
1234+ };
1235+ #endif
12061236
1207- /* Initialization function for the module (*must* be called init_fuse) */
1208-
1209- DL_EXPORT (void )
1210- init_fuse (void )
1237+ PyObject * PyInit__fuse (void )
12111238{
12121239 PyObject * m , * d ;
12131240
12141241 /* Create the module and add the functions */
1215- m = Py_InitModule ("_fuse" , Fuse_methods );
1242+ #if PY_MAJOR_VERSION >= 3
1243+ m = PyModule_Create (& fuse_module );
1244+ #else
1245+ m = Py_InitModule3 ("_fuse" , Fuse_methods , "FUSE module" );
1246+ #endif
12161247
12171248 /* Add some symbolic constants to the module */
12181249 d = PyModule_GetDict (m );
12191250 Py_FuseError = PyErr_NewException ("fuse.FuseError" , NULL , NULL );
12201251 PyDict_SetItemString (d , "FuseError" , Py_FuseError );
12211252 /* compat */
12221253 PyDict_SetItemString (d , "error" , Py_FuseError );
1254+
1255+ return m ;
12231256}
1257+
1258+ #if PY_MAJOR_VERSION == 2
1259+ DL_EXPORT (void )
1260+ init_fuse (void )
1261+ {
1262+ PyInit__fuse ();
1263+ }
1264+ #endif
0 commit comments