Skip to content

Commit 50a1259

Browse files
authored
Merge pull request #8 from sdelafond/python3
Python3
2 parents b2e7217 + 40815de commit 50a1259

7 files changed

Lines changed: 93 additions & 55 deletions

File tree

example/_find_fuse_parts.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
from os.path import realpath, dirname, join
33
from traceback import format_exception
44

5+
PYTHON_MAJOR_MINOR = "%s.%s" % (sys.version_info[0], sys.version_info[1])
6+
57
ddd = realpath(join(dirname(sys.argv[0]), '..'))
68

79
for d in [ddd, '.']:
8-
for p in glob.glob(join(d, 'build', 'lib.*')):
10+
for p in glob.glob(join(d, 'build', 'lib.*%s' % PYTHON_MAJOR_MINOR)):
911
sys.path.insert(0, p)
1012

1113
try:
1214
import fuse
1315
except ImportError:
14-
raise RuntimeError, """
16+
raise RuntimeError("""
1517
1618
! Got exception:
1719
""" + "".join([ "> " + x for x in format_exception(*sys.exc_info()) ]) + """
1820
! Have you ran `python setup.py build'?
1921
!
2022
! We've done our best to find the necessary components of the FUSE bindings
2123
! even if it's not installed, we've got no clue what went wrong for you...
22-
"""
24+
""")

example/hello.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818

1919
if not hasattr(fuse, '__version__'):
20-
raise RuntimeError, \
21-
"your fuse-py doesn't know of fuse.__version__, probably it's too old."
20+
raise RuntimeError("your fuse-py doesn't know of fuse.__version__, probably it's too old.")
2221

2322
fuse.fuse_python_api = (0, 2)
2423

@@ -43,10 +42,10 @@ class HelloFS(Fuse):
4342
def getattr(self, path):
4443
st = MyStat()
4544
if path == '/':
46-
st.st_mode = stat.S_IFDIR | 0755
45+
st.st_mode = stat.S_IFDIR | 0o755
4746
st.st_nlink = 2
4847
elif path == hello_path:
49-
st.st_mode = stat.S_IFREG | 0444
48+
st.st_mode = stat.S_IFREG | 0o444
5049
st.st_nlink = 1
5150
st.st_size = len(hello_str)
5251
else:

example/xmp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222

2323
if not hasattr(fuse, '__version__'):
24-
raise RuntimeError, \
25-
"your fuse-py doesn't know of fuse.__version__, probably it's too old."
24+
raise RuntimeError("your fuse-py doesn't know of fuse.__version__, probably it's too old.")
2625

2726
fuse.fuse_python_api = (0, 2)
2827

@@ -269,7 +268,7 @@ def main():
269268
if server.fuse_args.mount_expected():
270269
os.chdir(server.root)
271270
except OSError:
272-
print >> sys.stderr, "can't enter root of underlying filesystem"
271+
print("can't enter root of underlying filesystem", file=sys.stderr)
273272
sys.exit(1)
274273

275274
server.main()

fuse.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
except:
1818
pass
1919

20-
from string import join
2120
import sys
2221
import os
2322
from errno import *
@@ -56,7 +55,7 @@ def __getenv__(var, pattern = '.', trans = lambda x: x):
5655
rpat = re.compile(rpat)
5756
if not rpat.search(val):
5857
raise RuntimeError("env var %s doesn't match required pattern %s" % \
59-
(var, `pattern`))
58+
(var, repr(pattern)))
6059
return trans(val)
6160

6261
def get_fuse_python_api():
@@ -134,12 +133,12 @@ def assemble(self):
134133
args = [sys.argv and sys.argv[0] or "python"]
135134
if self.mountpoint:
136135
args.append(self.mountpoint)
137-
for m, v in self.modifiers.iteritems():
136+
for m, v in self.modifiers.items():
138137
if v:
139138
args.append(self.fuse_modifiers[m])
140139

141140
opta = []
142-
for o, v in self.optdict.iteritems():
141+
for o, v in self.optdict.items():
143142
opta.append(o + '=' + v)
144143
opta.extend(self.optlist)
145144

@@ -283,15 +282,15 @@ def __init__(self, *args, **kw):
283282

284283
if dsd == 'whine':
285284
def dsdcb(option, opt_str, value, parser):
286-
raise RuntimeError, """
285+
raise RuntimeError("""
287286
288287
! If you want the "-s" option to work, pass
289288
!
290289
! dash_s_do='setsingle'
291290
!
292291
! to the Fuse constructor. See docstring of the FuseOptParse class for an
293292
! explanation why is it not set by default.
294-
"""
293+
""")
295294

296295
elif dsd == 'setsingle':
297296
def dsdcb(option, opt_str, value, parser):
@@ -300,7 +299,7 @@ def dsdcb(option, opt_str, value, parser):
300299
elif dsd == 'undef':
301300
dsdcb = None
302301
else:
303-
raise ArgumentError, "key `dash_s_do': uninterpreted value " + str(dsd)
302+
raise ArgumentError("key `dash_s_do': uninterpreted value " + str(dsd))
304303

305304
if dsdcb:
306305
self.add_option('-s', action='callback', callback=dsdcb,
@@ -313,11 +312,10 @@ def exit(self, status=0, msg=None):
313312

314313
def error(self, msg):
315314
SubbedOptParse.error(self, msg)
316-
raise OptParseError, msg
315+
raise OptParseError(msg)
317316

318317
def print_help(self, file=sys.stderr):
319318
SubbedOptParse.print_help(self, file)
320-
print >> file
321319
self.fuse_args.setmod('showhelp')
322320

323321
def print_version(self, file=sys.stderr):
@@ -359,8 +357,8 @@ def __init__(self, func):
359357

360358
def __call__(self, *args, **kw):
361359
try:
362-
return apply(self.func, args, kw)
363-
except (IOError, OSError), detail:
360+
return self.func(*args, **kw)
361+
except (IOError, OSError) as detail:
364362
# Sometimes this is an int, sometimes an instance...
365363
if hasattr(detail, "errno"): detail = detail.errno
366364
return -detail
@@ -578,7 +576,7 @@ def resolve(args, maxva):
578576
mag = ma.groups()
579577
fp = re.compile(mag[1])
580578
neg = bool(mag[0])
581-
for f in fmap.keys() + [ 'has_' + a for a in Fuse._attrs ]:
579+
for f in list(fmap.keys()) + [ 'has_' + a for a in Fuse._attrs ]:
582580
if neg != bool(re.search(fp, f)):
583581
yield f
584582
continue
@@ -663,11 +661,11 @@ def __init__(self, *args, **kw):
663661
"""
664662

665663
if not fuse_python_api:
666-
raise RuntimeError, __name__ + """.fuse_python_api not defined.
664+
raise RuntimeError(__name__ + """.fuse_python_api not defined.
667665
668666
! Please define """ + __name__ + """.fuse_python_api internally (eg.
669667
!
670-
! (1) """ + __name__ + """.fuse_python_api = """ + `FUSE_PYTHON_API_VERSION` + """
668+
! (1) """ + __name__ + """.fuse_python_api = """ + repr(FUSE_PYTHON_API_VERSION) + """
671669
!
672670
! ) or in the enviroment (eg.
673671
!
@@ -678,22 +676,21 @@ def __init__(self, *args, **kw):
678676
! If you are actually developing a filesystem, probably (1) is the way to go.
679677
! If you are using a filesystem written before 2007 Q2, probably (2) is what
680678
! you want."
681-
"""
679+
""")
682680

683681
def malformed():
684-
raise RuntimeError, \
685-
"malformatted fuse_python_api value " + `fuse_python_api`
682+
raise RuntimeError("malformatted fuse_python_api value " + repr(fuse_python_api))
686683
if not isinstance(fuse_python_api, tuple):
687684
malformed()
688685
for i in fuse_python_api:
689686
if not isinstance(i, int) or i < 0:
690687
malformed()
691688

692689
if fuse_python_api > FUSE_PYTHON_API_VERSION:
693-
raise RuntimeError, """
694-
! You require FUSE-Python API version """ + `fuse_python_api` + """.
695-
! However, the latest available is """ + `FUSE_PYTHON_API_VERSION` + """.
696-
"""
690+
raise RuntimeError("""
691+
! You require FUSE-Python API version """ + repr(fuse_python_api) + """.
692+
! However, the latest available is """ + repr(FUSE_PYTHON_API_VERSION) + """.
693+
""")
697694

698695
self.fuse_args = \
699696
'fuse_args' in kw and kw.pop('fuse_args') or FuseArgs()
@@ -719,7 +716,7 @@ def parse(self, *args, **kw):
719716

720717
ev = 'errex' in kw and kw.pop('errex')
721718
if ev and not isinstance(ev, int):
722-
raise TypeError, "error exit value should be an integer"
719+
raise TypeError("error exit value should be an integer")
723720

724721
try:
725722
self.cmdline = self.parser.parse_args(*args, **kw)
@@ -893,7 +890,7 @@ def __getattr__(self, meth):
893890
if m:
894891
return m
895892

896-
raise AttributeError, "Fuse instance has no attribute '%s'" % meth
893+
raise AttributeError("Fuse instance has no attribute '%s'" % meth)
897894

898895

899896

fuseparts/_fusemodule.c

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
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+
3848
static 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

fuseparts/subbedopts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self):
3030
def _str_core(self):
3131

3232
sa = []
33-
for k, v in self.optdict.iteritems():
33+
for k, v in self.optdict.items():
3434
sa.append(str(k) + '=' + str(v))
3535

3636
ra = (list(self.optlist) + sa) or ["(none)"]
@@ -47,7 +47,7 @@ def canonify(self):
4747
with True value to optlist, stringify other values.
4848
"""
4949

50-
for k, v in self.optdict.iteritems():
50+
for k, v in self.optdict.items():
5151
if v == False:
5252
self.optdict.pop(k)
5353
elif v == True:
@@ -84,7 +84,7 @@ def add(self, opt, val=None):
8484

8585
if (v):
8686
if val != None:
87-
raise AttributeError, "ambiguous option value"
87+
raise AttributeError("ambiguous option value")
8888
val = v
8989

9090
if val == False:

0 commit comments

Comments
 (0)