chiark / gitweb /
General reorganization.
[mLib-python] / utils.pyx
diff --git a/utils.pyx b/utils.pyx
new file mode 100644 (file)
index 0000000..6b62989
--- /dev/null
+++ b/utils.pyx
@@ -0,0 +1,59 @@
+# -*-pyrex-*-
+#
+# $Id$
+#
+# Miscellaneous support gubbins
+#
+# (c) 2005 Straylight/Edgeware
+#
+
+#----- Licensing notice -----------------------------------------------------
+#
+# This file is part of the Python interface to mLib.
+#
+# mLib/Python is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# mLib/Python is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with mLib/Python; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+cdef object _u32(uint32 x):
+  if x < LONG_MAX:
+    return PyInt_FromLong(x)
+  else:
+    return PyLong_FromUnsignedLong(x)
+
+cdef object _oserror():
+  raise OSError, (errno, strerror(errno))
+
+cdef object _tobool(int i):
+  if i:
+    return True
+  else:
+    return False
+
+cdef int _getfd(object fdobj):
+  try:
+    return fdobj
+  except TypeError:
+    return fdobj.fileno()
+
+cdef object _checkcallable(f, what):
+  if f is not None and not callable(f):
+    raise TypeError, '%s must be callable' % what
+  return f
+
+cdef object _maybecall(f, args):
+  if f is None:
+    return None
+  return f(*args)
+
+#----- That's all, folks ----------------------------------------------------