diff options
author | Ondrej Oprala | 2013-08-29 14:57:04 +0200 |
---|---|---|
committer | Karel Zak | 2013-09-20 12:48:53 +0200 |
commit | 464074532c0d1898f945a00c2f143e5b3ca59601 (patch) | |
tree | beb19a7e647b6738c57a822d7c4fdb8992042bb0 /libmount/python | |
parent | build-sys: support --with-python[={2,3}] (diff) | |
download | kernel-qcow2-util-linux-464074532c0d1898f945a00c2f143e5b3ca59601.tar.gz kernel-qcow2-util-linux-464074532c0d1898f945a00c2f143e5b3ca59601.tar.xz kernel-qcow2-util-linux-464074532c0d1898f945a00c2f143e5b3ca59601.zip |
pylibmount: rewrite to be py2/py3 universal
Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/python')
-rw-r--r-- | libmount/python/context.c | 15 | ||||
-rw-r--r-- | libmount/python/fs.c | 86 | ||||
-rw-r--r-- | libmount/python/pylibmount.c | 89 | ||||
-rw-r--r-- | libmount/python/pylibmount.h | 1 | ||||
-rw-r--r-- | libmount/python/tab.c | 41 | ||||
-rwxr-xr-x | libmount/python/test_mount_context.py | 33 | ||||
-rwxr-xr-x | libmount/python/test_mount_tab.py | 45 | ||||
-rwxr-xr-x | libmount/python/test_mount_tab_update.py | 9 |
8 files changed, 203 insertions, 116 deletions
diff --git a/libmount/python/context.c b/libmount/python/context.c index 662c89437..ce7711b3f 100644 --- a/libmount/python/context.c +++ b/libmount/python/context.c @@ -53,7 +53,7 @@ static void Context_dealloc(ContextObjext *self) Py_XDECREF(mnt_context_get_mtab_userdata(self->cxt)); mnt_free_context(self->cxt); - self->ob_type->tp_free((PyObject*) self); + PyFree(self); } static PyObject *Context_new(PyTypeObject *type, @@ -570,11 +570,11 @@ static int Context_set_optsmode(ContextObjext *self, PyObject *value, void *clos PyErr_SetString(PyExc_TypeError, NODEL_ATTR); return -1; } - else if (!PyInt_Check(value)) { + else if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, ARG_ERR); return -1; } - optsmode = PyInt_AsLong(value); + optsmode = PyLong_AsLong(value); return mnt_context_set_optsmode(self->cxt, optsmode); } @@ -586,11 +586,11 @@ static int Context_set_syscall_status(ContextObjext *self, PyObject *value, void PyErr_SetString(PyExc_TypeError, NODEL_ATTR); return -1; } - else if (!PyInt_Check(value)) { + else if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, ARG_ERR); return -1; } - syscall_status = PyInt_AsLong(value); + syscall_status = PyLong_AsLong(value); return mnt_context_set_syscall_status(self->cxt, syscall_status); } @@ -1169,13 +1169,12 @@ static PyMethodDef Context_methods[] = { static PyObject *Context_repr(ContextObjext *self) { - return PyString_FromFormat("<libmount.Context object at %p, restricted=%s>", + return PyUnicode_FromFormat("<libmount.Context object at %p, restricted=%s>", self, mnt_context_is_restricted(self->cxt) ? "True" : "False"); } PyTypeObject ContextType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + PyVarObject_HEAD_INIT(NULL, 0) "libmount.Context", /*tp_name*/ sizeof(ContextObjext), /*tp_basicsize*/ 0, /*tp_itemsize*/ diff --git a/libmount/python/fs.c b/libmount/python/fs.c index a1e8a1837..9cf2555e4 100644 --- a/libmount/python/fs.c +++ b/libmount/python/fs.c @@ -26,6 +26,7 @@ */ #include "pylibmount.h" +#include <errno.h> #define Fs_HELP "Fs(source=None, root=None, target=None, fstype=None, options=None, attributes=None, freq=0, passno=0)" @@ -61,23 +62,57 @@ static PyObject *Fs_get_devno(FsObject *self) return PyObjectResultInt(mnt_fs_get_devno(self->fs)); } -#define Fs_print_debug_HELP "print_debug(ostream)\n\n" -static PyObject *Fs_print_debug(FsObject *self, PyObject *args, PyObject *kwds) -{ - PyFileObject *stream = NULL; - int rc; - FILE *f = NULL; - char *kwlist[] = { "ostream", NULL }; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist, - &PyFile_Type, &stream)) { - PyErr_SetString(PyExc_TypeError, ARG_ERR); - return NULL; - } - - f = PyFile_AsFile((PyObject *) stream); - rc = mnt_fs_print_debug(self->fs, f); - return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); +#define Fs_print_debug_HELP "print_debug()\n\n" +static PyObject *Fs_print_debug(FsObject *self) +{ + PySys_WriteStdout("------ fs: %p\n", self->fs); + PySys_WriteStdout("source: %s\n", mnt_fs_get_source(self->fs)); + PySys_WriteStdout("target: %s\n", mnt_fs_get_target(self->fs)); + PySys_WriteStdout("fstype: %s\n", mnt_fs_get_fstype(self->fs)); + + if (mnt_fs_get_options(self->fs)) + PySys_WriteStdout("optstr: %s\n", mnt_fs_get_options(self->fs)); + if (mnt_fs_get_vfs_options(self->fs)) + PySys_WriteStdout("VFS-optstr: %s\n", mnt_fs_get_vfs_options(self->fs)); + if (mnt_fs_get_fs_options(self->fs)) + PySys_WriteStdout("FS-opstr: %s\n", mnt_fs_get_fs_options(self->fs)); + if (mnt_fs_get_user_options(self->fs)) + PySys_WriteStdout("user-optstr: %s\n", mnt_fs_get_user_options(self->fs)); + if (mnt_fs_get_optional_fields(self->fs)) + PySys_WriteStdout("optional-fields: '%s'\n", mnt_fs_get_optional_fields(self->fs)); + if (mnt_fs_get_attributes(self->fs)) + PySys_WriteStdout("attributes: %s\n", mnt_fs_get_attributes(self->fs)); + + if (mnt_fs_get_root(self->fs)) + PySys_WriteStdout("root: %s\n", mnt_fs_get_root(self->fs)); + + if (mnt_fs_get_swaptype(self->fs)) + PySys_WriteStdout("swaptype: %s\n", mnt_fs_get_swaptype(self->fs)); + if (mnt_fs_get_size(self->fs)) + PySys_WriteStdout("size: %jd\n", mnt_fs_get_size(self->fs)); + if (mnt_fs_get_usedsize(self->fs)) + PySys_WriteStdout("usedsize: %jd\n", mnt_fs_get_usedsize(self->fs)); + if (mnt_fs_get_priority(self->fs)) + PySys_WriteStdout("priority: %d\n", mnt_fs_get_priority(self->fs)); + + if (mnt_fs_get_bindsrc(self->fs)) + PySys_WriteStdout("bindsrc: %s\n", mnt_fs_get_bindsrc(self->fs)); + if (mnt_fs_get_freq(self->fs)) + PySys_WriteStdout("freq: %d\n", mnt_fs_get_freq(self->fs)); + if (mnt_fs_get_passno(self->fs)) + PySys_WriteStdout("pass: %d\n", mnt_fs_get_passno(self->fs)); + if (mnt_fs_get_id(self->fs)) + PySys_WriteStdout("id: %d\n", mnt_fs_get_id(self->fs)); + if (mnt_fs_get_parent_id(self->fs)) + PySys_WriteStdout("parent: %d\n", mnt_fs_get_parent_id(self->fs)); + if (mnt_fs_get_devno(self->fs)) + PySys_WriteStdout("devno: %d:%d\n", major(mnt_fs_get_devno(self->fs)), + minor(mnt_fs_get_devno(self->fs))); + if (mnt_fs_get_tid(self->fs)) + PySys_WriteStdout("tid: %d\n", mnt_fs_get_tid(self->fs)); + if (mnt_fs_get_comment(self->fs)) + PySys_WriteStdout("comment: '%s'\n", mnt_fs_get_comment(self->fs)); + return UL_IncRef(self); } /* ** Fs getters/setters @@ -304,12 +339,12 @@ static int Fs_set_freq(FsObject *self, PyObject *value, PyErr_SetString(PyExc_TypeError, NODEL_ATTR); return -1; - } else if (!PyInt_Check(value)) { + } else if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, ARG_ERR); return -1; } - freq = PyInt_AsLong(value); + freq = PyLong_AsLong(value); if (freq == -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "type conversion failed"); return -1; @@ -329,12 +364,12 @@ static int Fs_set_passno(FsObject *self, PyObject *value, void *closure __attrib if (!value) { PyErr_SetString(PyExc_TypeError, NODEL_ATTR); return -1; - } else if (!PyInt_Check(value)) { + } else if (!PyLong_Check(value)) { PyErr_SetString(PyExc_TypeError, ARG_ERR); return -1; } - passno = PyInt_AsLong(value); + passno = PyLong_AsLong(value); if (passno == -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "type conversion failed"); return -1; @@ -567,7 +602,7 @@ static PyMethodDef Fs_methods[] = { {"match_options", (PyCFunction)Fs_match_options, METH_VARARGS|METH_KEYWORDS, Fs_match_options_HELP}, {"streq_srcpath", (PyCFunction)Fs_streq_srcpath, METH_VARARGS|METH_KEYWORDS, Fs_streq_srcpath_HELP}, {"streq_target", (PyCFunction)Fs_streq_target, METH_VARARGS|METH_KEYWORDS, Fs_streq_target_HELP}, - {"print_debug", (PyCFunction)Fs_print_debug, METH_VARARGS|METH_KEYWORDS, Fs_print_debug_HELP}, + {"print_debug", (PyCFunction)Fs_print_debug, METH_NOARGS, Fs_print_debug_HELP}, {NULL} }; @@ -576,7 +611,7 @@ static void Fs_destructor(FsObject *self) DBG(FS, pymnt_debug_h(self->fs, "destrutor py-obj: %p, py-refcnt=%d", self, (int) ((PyObject *) self)->ob_refcnt)); mnt_unref_fs(self->fs); - self->ob_type->tp_free((PyObject*)self); + PyFree(self); } static PyObject *Fs_new(PyTypeObject *type, PyObject *args __attribute__((unused)), @@ -686,7 +721,7 @@ static PyObject *Fs_repr(FsObject *self) *tgt = mnt_fs_get_target(self->fs), *type = mnt_fs_get_fstype(self->fs); - return PyString_FromFormat( + return PyUnicode_FromFormat( "<libmount.Fs object at %p, " "source=%s, target=%s, fstype=%s>", self, @@ -762,8 +797,7 @@ static PyObject *Fs_copy_fs(FsObject *self, PyObject *args, PyObject *kwds) PyTypeObject FsType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + PyVarObject_HEAD_INIT(NULL, 0) "libmount.Fs", /*tp_name*/ sizeof(FsObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ diff --git a/libmount/python/pylibmount.c b/libmount/python/pylibmount.c index d2e79fbcc..1403bfe3c 100644 --- a/libmount/python/pylibmount.c +++ b/libmount/python/pylibmount.c @@ -24,13 +24,21 @@ PyObject *LibmountError; int pylibmount_debug_mask; - PyObject *UL_IncRef(void *killme) { Py_INCREF(killme); return killme; } +void PyFree(void *o) +{ +#if PY_MAJOR_VERSION >= 3 + Py_TYPE(o)->tp_free((PyObject *)o); +#else + ((PyObject *)o)->ob_type->tp_free((PyObject *)o); +#endif +} + /* Demultiplexer for various possible error conditions across the libmount library */ void *UL_RaiseExc(int e) { @@ -95,14 +103,22 @@ PyObject *PyObjectResultStr(const char *s) return result; } -/* wrapper around a common use case for PyString_AsString() */ +/* wrapper around a common use case for PyUnicode_AsASCIIString() */ char *pystos(PyObject *pys) { +#if PY_MAJOR_VERSION >= 3 + if (!PyUnicode_Check(pys)) { + PyErr_SetString(PyExc_TypeError, ARG_ERR); + return NULL; + } + return (char *)PyUnicode_1BYTE_DATA(pys); +#else if (!PyString_Check(pys)) { PyErr_SetString(PyExc_TypeError, ARG_ERR); return NULL; } return PyString_AsString(pys); +#endif } /* @@ -118,20 +134,73 @@ char *pystos(PyObject *pys) "and returns a (tag, value) tuple. Every attribute is \"filtered\"" \ "through appropriate getters/setters, no values are set directly." -static PyMethodDef libmount_methods[] = { - {NULL} /* Sentinel */ + +struct module_state { + PyObject *error; }; -#ifndef PyMODINIT_FUNC -# define PyMODINIT_FUNC void +#if PY_MAJOR_VERSION >= 3 +#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) +#else +#define GETSTATE(m) (&_state) +static struct module_state _state; #endif + +static PyObject * +error_out(PyObject *m __attribute__((unused))) { + struct module_state *st = GETSTATE(m); + PyErr_SetString(st->error, "something bad happened"); + return NULL; +} + +static PyMethodDef pylibmount_methods[] = { + {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL}, + {NULL, NULL} +}; + +#if PY_MAJOR_VERSION >= 3 + +static int pylibmount_traverse(PyObject *m, visitproc visit, void *arg) { + Py_VISIT(GETSTATE(m)->error); + return 0; +} + +static int pylibmount_clear(PyObject *m) { + Py_CLEAR(GETSTATE(m)->error); + return 0; +} + +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "pylibmount", + NULL, + sizeof(struct module_state), + pylibmount_methods, + NULL, + pylibmount_traverse, + pylibmount_clear, + NULL +}; +#define INITERROR return NULL +PyObject * PyInit_pylibmount(void); +PyObject * PyInit_pylibmount(void) +#else +#define INITERROR return +# ifndef PyMODINIT_FUNC +# define PyMODINIT_FUNC void +# endif PyMODINIT_FUNC initpylibmount(void); PyMODINIT_FUNC initpylibmount(void) +#endif { - PyObject *m = Py_InitModule3("pylibmount", libmount_methods, PYLIBMOUNT_DESC); +#if PY_MAJOR_VERSION >= 3 + PyObject *m = PyModule_Create(&moduledef); +#else + PyObject *m = Py_InitModule3("pylibmount", pylibmount_methods, PYLIBMOUNT_DESC); +#endif if (!m) - return; + INITERROR; /* * init debug stuff */ @@ -223,5 +292,9 @@ PyMODINIT_FUNC initpylibmount(void) /* Still useful for functions using iterators internally */ PyModule_AddIntConstant(m, "MNT_ITER_FORWARD", MNT_ITER_FORWARD); PyModule_AddIntConstant(m, "MNT_ITER_BACKWARD", MNT_ITER_BACKWARD); + +#if PY_MAJOR_VERSION >= 3 + return m; +#endif } diff --git a/libmount/python/pylibmount.h b/libmount/python/pylibmount.h index d3003af69..1a08796dc 100644 --- a/libmount/python/pylibmount.h +++ b/libmount/python/pylibmount.h @@ -119,6 +119,7 @@ extern PyObject *PyObjectResultInt(int i); extern PyObject *PyObjectResultStr(const char *s); extern char *pystos(PyObject *pys); +extern void PyFree(void *o); diff --git a/libmount/python/tab.c b/libmount/python/tab.c index 817643cf8..9ea6924c0 100644 --- a/libmount/python/tab.c +++ b/libmount/python/tab.c @@ -148,22 +148,25 @@ static PyObject *Table_replace_file(TableObject *self, PyObject *args, PyObject return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); } -#define Table_write_file_HELP "write_file(file)\n\n" \ +#define Table_write_file_HELP "write_file(path)\n\n" \ "This function writes tab to file(stream)" static PyObject *Table_write_file(TableObject *self, PyObject *args, PyObject *kwds) { int rc; - PyFileObject *stream = NULL; + //PyObject *stream = NULL; FILE *f = NULL; - char *kwlist[] = {"file", NULL}; + char *path = NULL; + char *kwlist[] = {"path", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist, - &PyFile_Type, &stream)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, + &path)) { PyErr_SetString(PyExc_TypeError, ARG_ERR); return NULL; } - f = PyFile_AsFile((PyObject *)stream); + if (!(f = fopen(path, "w"))) + return UL_RaiseExc(errno); rc = mnt_table_write_file(self->tab, f); + fclose(f); return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); } @@ -427,24 +430,6 @@ static PyObject *Table_parse_swaps(TableObject *self, PyObject* args, PyObject * return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); } -#define Table_parse_stream_HELP "parse_stream(stream, filename)\n\n" \ - "Returns self or raises an exception in case of an error." -static PyObject *Table_parse_stream(TableObject *self, PyObject* args, PyObject *kwds) -{ - int rc; - PyFileObject *stream = NULL; - char *filename = NULL; - FILE *f; - char *kwlist[] = {"stream", "filename", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!s", kwlist, &PyFile_Type, &stream, &filename)) { - PyErr_SetString(PyExc_TypeError, ARG_ERR); - return NULL; - } - f = PyFile_AsFile((PyObject *)stream); - rc = mnt_table_parse_stream(self->tab, f, filename); - return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); -} #define Table_add_fs_HELP "add_fs(fs)\n\nAdds a new entry to tab.\n" \ "Returns self or raises an exception in case of an error." @@ -528,7 +513,6 @@ static PyMethodDef Table_methods[] = { {"parse_dir", (PyCFunction)Table_parse_dir, METH_VARARGS|METH_KEYWORDS, Table_parse_dir_HELP}, {"parse_swaps", (PyCFunction)Table_parse_swaps, METH_VARARGS|METH_KEYWORDS, Table_parse_swaps_HELP}, {"is_fs_mounted", (PyCFunction)Table_is_fs_mounted, METH_VARARGS|METH_KEYWORDS, Table_is_fs_mounted_HELP}, - {"parse_stream", (PyCFunction)Table_parse_stream, METH_VARARGS|METH_KEYWORDS, Table_parse_stream_HELP}, {"add_fs", (PyCFunction)Table_add_fs, METH_VARARGS|METH_KEYWORDS, Table_add_fs_HELP}, {"remove_fs", (PyCFunction)Table_remove_fs, METH_VARARGS|METH_KEYWORDS, Table_remove_fs_HELP}, {"next_fs", (PyCFunction)Table_next_fs, METH_NOARGS, Table_next_fs_HELP}, @@ -569,7 +553,7 @@ static void Table_destructor(TableObject *self) mnt_free_iter(self->iter); Py_XDECREF(self->errcb); - self->ob_type->tp_free((PyObject*)self); + PyFree(self); } static PyObject *Table_new(PyTypeObject *type, @@ -734,7 +718,7 @@ static PyGetSetDef Table_getseters[] = { static PyObject *Table_repr(TableObject *self) { - return PyString_FromFormat( + return PyUnicode_FromFormat( "<libmount.Table object at %p, entries=%d, comments_enabled=%s, errcb=%s>", self, mnt_table_get_nents(self->tab), @@ -743,8 +727,7 @@ static PyObject *Table_repr(TableObject *self) } PyTypeObject TableType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + PyVarObject_HEAD_INIT(NULL, 0) "libmount.Table", /*tp_name*/ sizeof(TableObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ diff --git a/libmount/python/test_mount_context.py b/libmount/python/test_mount_context.py index 7cdacc5a5..deb1c2ccf 100755 --- a/libmount/python/test_mount_context.py +++ b/libmount/python/test_mount_context.py @@ -1,4 +1,3 @@ -#!/bin/python2 import os import sys import stat @@ -6,11 +5,11 @@ import errno import libmount as mnt def usage(tss): - print "\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0]) + print("\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0])) for i in tss: - print "\t{15:-s}".format(i[0]) + print("\t{15:-s}".format(i[0])) if i[2] != "": - print " {:s}\n".format(i[2]) + print(" {:s}\n".format(i[2])) print("\n") return 1 @@ -27,7 +26,7 @@ def mnt_run_test(tss, argv): if i[0] == argv[1]: rc = i[1](i, argv[1:]) if rc: - print "FAILED [rc={:d}]".format(rc) + print("FAILED [rc={:d}]".format(rc)) break if ((rc < 0) and (i == ())): @@ -61,9 +60,9 @@ def test_mount(ts, argv): try: cxt.mount() except Exception: - print "failed to mount" + print("failed to mount") return -1 - print "successfully mounted" + print("successfully mounted") return rc def test_umount(ts, argv): @@ -96,9 +95,9 @@ def test_umount(ts, argv): try: cxt.umount() except Exception: - print "failed to umount" + print("failed to umount") return 1 - print "successfully umounted" + print("successfully umounted") return rc def test_flags(ts, argv): @@ -119,13 +118,13 @@ def test_flags(ts, argv): cxt.prepare_mount() # catch ioerror here except IOError as e: - print "failed to prepare mount {:s}".format(e.strerror) + print("failed to prepare mount {:s}".format(e.strerror)) opt = cxt.fs.options if (opt): - print "options: {:s}", opt + print("options: {:s}", opt) - print "flags: {08:lx}".format(cxt.mflags()) + print("flags: {08:lx}".format(cxt.mflags())) return rc def test_mountall(ts, argv): @@ -146,17 +145,17 @@ def test_mountall(ts, argv): while (cxt.next_mount()): tgt = i.target if (ignored == 1): - print "{:s}: ignored: not match".format(tgt) + print("{:s}: ignored: not match".format(tgt)) elif (ignored == 2): - print "{:s}: ignored: already mounted".format(tgt) + print("{:s}: ignored: already mounted".format(tgt)) elif (not cxt.status): if (mntrc > 0): # ?? errno = mntrc - print "{:s}: mount failed".format(tgt) + print("{:s}: mount failed".format(tgt)) else: - print "{:s}: mount failed".format(tgt) + print("{:s}: mount failed".format(tgt)) else: - print "{:s}: successfully mounted".format(tgt) + print("{:s}: successfully mounted".format(tgt)) return 0 diff --git a/libmount/python/test_mount_tab.py b/libmount/python/test_mount_tab.py index 33ceb5285..9d7d4edca 100755 --- a/libmount/python/test_mount_tab.py +++ b/libmount/python/test_mount_tab.py @@ -1,4 +1,3 @@ -#!/bin/python2 import os import sys import stat @@ -7,11 +6,11 @@ import functools as ft import libmount as mnt def usage(tss): - print "\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0]) + print("\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0])) for i in tss: - print "\t{15:-s}".format(i[0]) + print("\t{15:-s}".format(i[0])) if i[2] != "": - print " {:s}\n".format(i[2]) + print(" {:s}\n".format(i[2])) print("\n") return 1 @@ -28,7 +27,7 @@ def mnt_run_test(tss, argv): if i[0] == argv[1]: rc = i[1](i, argv[1:]) if rc: - print "FAILED [rc={:d}]".format(rc) + print("FAILED [rc={:d}]".format(rc)) break if ((rc < 0) and (i == ())): @@ -36,7 +35,7 @@ def mnt_run_test(tss, argv): return not not rc #because !!rc is too mainstream for python def parser_errcb(tb, fname, line): - print "{:s}:{:d}: parse error".format(fname, line) + print("{:s}:{:d}: parse error".format(fname, line)) return 1 def create_table(f, comments): @@ -50,7 +49,7 @@ def create_table(f, comments): try: tb.parse_file(f) except Exception: - print "{:s}: parsing failed".format(f) + print("{:s}: parsing failed".format(f)) return None return tb @@ -61,14 +60,14 @@ def test_copy_fs(ts, argv): if not fs: return rc - print "ORIGINAL:" - fs.print_debug(sys.stdout) + print("ORIGINAL:") + fs.print_debug() fs = fs.copy_fs(None) if not fs: return rc - print "COPY:" - fs.print_debug(sys.stdout) + print("COPY:") + fs.print_debug() return 0 def test_parse(ts, argv): @@ -79,17 +78,17 @@ def test_parse(ts, argv): tb = create_table(argv[1], parse_comments) if tb.intro_comment: - print "Initial comment:\n\"{:s}\"".format(tb.intro_comment) + print("Initial comment:\n\"{:s}\"".format(tb.intro_comment)) #while ((fs = tb.next_fs()) != None): for fs in iter(ft.partial(tb.next_fs), None): - fs.print_debug(sys.stdout) + fs.print_debug() if tb.trailing_comment: - print "Trailing comment:\n\"{:s}\"".format(tb.trailing_comment) + print("Trailing comment:\n\"{:s}\"".format(tb.trailing_comment)) return 0 def test_find(ts, argv, dr): if len(argv) != 4: - print "try --help" + print("try --help") return -errno.EINVAL f, find, what = argv[1:] @@ -101,9 +100,9 @@ def test_find(ts, argv, dr): fs = tb.find_target(what, dr) if not fs: - print "{:s}: not found {:s} '{:s}'".format(f, find, what) + print("{:s}: not found {:s} '{:s}'".format(f, find, what)) else: - fs.print_debug(sys.stdout) + fs.print_debug() return 0 def test_find_fw(ts, argv): @@ -118,14 +117,14 @@ def test_find_pair(ts, argv): fs = tb.find_pair(argv[2], argv[3], mnt.MNT_ITER_FORWARD) if not fs: return rc - fs.print_debug(sys.stdout) + fs.print_debug() return 0 def test_is_mounted(ts, argv): rc = -1 tb = mnt.Tab(path="/proc/self/mountinfo") if not tb: - print "failed to parse mountinto" + print("failed to parse mountinto") return rc fstab = create_table(argv[1], False) @@ -134,9 +133,9 @@ def test_is_mounted(ts, argv): fs = () for fs in ft.iter(tb.next_fs(), -1): if tb.is_fs_mounted(fs): - print "{:s} already mounted on {:s}".format(fs.source, fs.target) + print("{:s} already mounted on {:s}".format(fs.source, fs.target)) else: - print "{:s} not mounted on {:s}".format(fs.source, fs.target) + print("{:s} not mounted on {:s}".format(fs.source, fs.target)) return 0 def test_find_mountpoint(ts, argv): @@ -147,12 +146,12 @@ def test_find_mountpoint(ts, argv): fs = tb.find_mountpoint(argv[1], mnt.MNT_ITER_BACKWARD) if not fs: return rc - fs.print_debug(sys.stdout) + fs.print_debug() return 0 tss = ( - ( "--parse", test_parse, "<file> [--comments] parse and print tab" ), + ( "--parse", test_parse, "<file> [--comments] parse and print(tab" ), ( "--find-forward", test_find_fw, "<file> <source|target> <string>" ), ( "--find-backward", test_find_bw, "<file> <source|target> <string>" ), ( "--find-pair", test_find_pair, "<file> <source> <target>" ), diff --git a/libmount/python/test_mount_tab_update.py b/libmount/python/test_mount_tab_update.py index 5433cf580..f1b0332d0 100755 --- a/libmount/python/test_mount_tab_update.py +++ b/libmount/python/test_mount_tab_update.py @@ -1,4 +1,3 @@ -#!/bin/python2 import os import sys import stat @@ -6,11 +5,11 @@ import errno import libmount as mnt def usage(tss): - print "\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0]) + print("\nUsage:\n\t{:s} <test> [testoptions]\nTests:\n".format(sys.argv[0])) for i in tss: - print "\t{15:-s}".format(i[0]) + print("\t{15:-s}".format(i[0])) if i[2] != "": - print " {:s}\n".format(i[2]) + print(" {:s}\n".format(i[2])) print("\n") return 1 @@ -27,7 +26,7 @@ def mnt_run_test(tss, argv): if i[0] == argv[1]: rc = i[1](i, argv[1:]) if rc: - print "FAILED [rc={:d}]".format(rc) + print("FAILED [rc={:d}]".format(rc)) break if ((rc < 0) and (i == ())): |