Blame SOURCES/0010-Add-nbdkit.parse_size-Python-function.patch

dad90d
From 52ee1dab95436128b44c37cc495022ff90108b2e Mon Sep 17 00:00:00 2001
dad90d
From: Nikolaus Rath <Nikolaus@rath.org>
dad90d
Date: Mon, 9 May 2022 10:04:30 +0100
dad90d
Subject: [PATCH] Add nbdkit.parse_size() Python function.
dad90d
dad90d
This enables Python plugins to parse sizes the same way as C plugins.
dad90d
dad90d
I'm not sure about the best way to test this - input is appreciated.
dad90d
dad90d
I'm not too happy with the way this code is tested. It workes, but putting the tests into
dad90d
test-python-plugin.py feels misplaced: this file is intended to support the unit tests in
dad90d
test_python.py, not run its own unit tests.
dad90d
dad90d
(cherry picked from commit 1b7d72542be68e254c1ef86ecb1a82b05c78ff63)
dad90d
---
dad90d
 plugins/python/modfunctions.c           | 21 +++++++++++++++++++++
dad90d
 plugins/python/nbdkit-python-plugin.pod |  5 +++++
dad90d
 tests/test-python-plugin.py             | 19 +++++++++++++++++++
dad90d
 3 files changed, 45 insertions(+)
dad90d
dad90d
diff --git a/plugins/python/modfunctions.c b/plugins/python/modfunctions.c
dad90d
index fffbaab2..46b0c904 100644
dad90d
--- a/plugins/python/modfunctions.c
dad90d
+++ b/plugins/python/modfunctions.c
dad90d
@@ -93,11 +93,32 @@ do_shutdown (PyObject *self, PyObject *args)
dad90d
   Py_RETURN_NONE;
dad90d
 }
dad90d
 
dad90d
+/* nbdkit.parse_size */
dad90d
+static PyObject *
dad90d
+parse_size (PyObject *self, PyObject *args)
dad90d
+{
dad90d
+  const char *s;
dad90d
+  if (!PyArg_ParseTuple (args, "s", &s)) {
dad90d
+    PyErr_SetString (PyExc_TypeError, "Expected string, got something else");
dad90d
+    return NULL;
dad90d
+  }
dad90d
+
dad90d
+  int64_t size = nbdkit_parse_size(s);
dad90d
+  if (size == -1) {
dad90d
+    PyErr_SetString (PyExc_ValueError, "Unable to parse string as size");
dad90d
+    return NULL;
dad90d
+  }
dad90d
+
dad90d
+  return PyLong_FromSize_t((size_t)size);
dad90d
+}
dad90d
+
dad90d
 static PyMethodDef NbdkitMethods[] = {
dad90d
   { "debug", debug, METH_VARARGS,
dad90d
     "Print a debug message" },
dad90d
   { "export_name", export_name, METH_VARARGS,
dad90d
     "Return the optional export name negotiated with the client" },
dad90d
+  { "parse_size", parse_size, METH_VARARGS,
dad90d
+    "Parse human-readable size strings into bytes" },
dad90d
   { "set_error", set_error, METH_VARARGS,
dad90d
     "Store an errno value prior to throwing an exception" },
dad90d
   { "shutdown", do_shutdown, METH_VARARGS,
dad90d
diff --git a/plugins/python/nbdkit-python-plugin.pod b/plugins/python/nbdkit-python-plugin.pod
dad90d
index 051b0237..ccc9406f 100644
dad90d
--- a/plugins/python/nbdkit-python-plugin.pod
dad90d
+++ b/plugins/python/nbdkit-python-plugin.pod
dad90d
@@ -131,6 +131,11 @@ Record C<err> as the reason you are about to throw an exception. C<err>
dad90d
 should correspond to usual errno values, where it may help to
dad90d
 C<import errno>.
dad90d
 
dad90d
+=head3 C<nbdkit.parse_size(str)>
dad90d
+
dad90d
+Parse a string (such as "100M") into a size in bytes. Wraps the
dad90d
+C<nbdkit_parse_size()> C function.
dad90d
+
dad90d
 =head3 C<nbdkit.shutdown()>
dad90d
 
dad90d
 Request asynchronous server shutdown.
dad90d
diff --git a/tests/test-python-plugin.py b/tests/test-python-plugin.py
dad90d
index 0b34d532..d4f379fc 100644
dad90d
--- a/tests/test-python-plugin.py
dad90d
+++ b/tests/test-python-plugin.py
dad90d
@@ -34,12 +34,31 @@
dad90d
 import nbdkit
dad90d
 import pickle
dad90d
 import base64
dad90d
+import unittest
dad90d
 
dad90d
 API_VERSION = 2
dad90d
 
dad90d
 cfg = {}
dad90d
 
dad90d
 
dad90d
+# Not nice, but there doesn't seem to be a better way of putting this
dad90d
+class TestAPI(unittest.TestCase):
dad90d
+
dad90d
+    def test_parse_size(self):
dad90d
+        self.assertEqual(nbdkit.parse_size('511'), 511)
dad90d
+        self.assertEqual(nbdkit.parse_size('7k'), 7*1024)
dad90d
+        self.assertEqual(nbdkit.parse_size('17M'), 17*1024*1024)
dad90d
+
dad90d
+        with self.assertRaises(TypeError):
dad90d
+            nbdkit.parse_size(17)
dad90d
+
dad90d
+        with self.assertRaises(ValueError):
dad90d
+            nbdkit.parse_size('foo')
dad90d
+
dad90d
+
dad90d
+TestAPI().test_parse_size()
dad90d
+
dad90d
+
dad90d
 def config(k, v):
dad90d
     global cfg
dad90d
     if k == "cfg":
dad90d
-- 
dad90d
2.31.1
dad90d