chiark / gitweb /
*.c: Separate string function calls according to text/binary usage.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 21 Oct 2019 01:38:59 +0000 (02:38 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:44:21 +0000 (12:44 +0100)
Don't use the `PyString_...' functions directly.  Instead, define two
collections of macros `TEXT_...' and `BIN_...', which can be used on
text and binary strings respectively.  The two sets are slightly
different, because they've been designed to adapt to Python 3's
different interfaces.

There are no `PyString...' calls remaining outside of these macros.

pyke.c
pyke.h

diff --git a/pyke.c b/pyke.c
index d874763b760f611e037b8ca653a215e96969e1be..cef4f822882d7036aaa1ce0dde7d067ba964be3d 100644 (file)
--- a/pyke.c
+++ b/pyke.c
@@ -105,9 +105,9 @@ int convbin(PyObject *o, void *pp)
 {
   struct bin *r = pp;
 
-  if (PyString_Check(o)) {
-    r->p = PyString_AS_STRING(o);
-    r->sz = PyString_GET_SIZE(o);
+  if (BIN_CHECK(o)) {
+    r->p = BIN_PTR(o);
+    r->sz = BIN_LEN(o);
     return (1);
   }
   if (PyUnicode_Check(o)) {
@@ -143,7 +143,7 @@ void report_lost_exception_v(struct excinfo *exc,
   assert(!PyErr_Occurred());
 
   /* Format the explanation. */
-  if (why) whyobj = PyString_FromFormatV(why, ap);
+  if (why) whyobj = TEXT_VFORMAT(why, ap);
   else { whyobj = Py_None; Py_INCREF(whyobj); }
 
   /* Find our home module's `lostexchook' function.  This won't work if
@@ -172,8 +172,7 @@ ouch:
    * `sys.excepthook'.
    */
 sys:
-  PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n",
-                   PyString_AS_STRING(whyobj));
+  PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", TEXT_PTR(whyobj));
   RESTORE_EXCINFO(exc);
   PyErr_Print();
   /* drop through... */
@@ -251,11 +250,13 @@ void *newtype(PyTypeObject *metaty,
   COPY(buffer);
 #undef COPY
   if (name)
-    ty->ht_name = PyString_FromString(name);
+    ty->ht_name = TEXT_FROMSTR(name);
   else if (ty->ht_type.tp_name)
-    ty->ht_name = PyString_FromString(ty->ht_type.tp_name);
+    ty->ht_name = TEXT_FROMSTR(ty->ht_type.tp_name);
+  else
+    ty->ht_name = 0;
   if (ty->ht_name)
-    ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name);
+    ty->ht_type.tp_name = TEXT_STR(ty->ht_name);
   ty->ht_slots = 0;
   (void)PyObject_INIT(&ty->ht_type, metaty);
   Py_INCREF(metaty);
@@ -304,11 +305,8 @@ PyObject *mkexc(PyObject *mod, PyObject *base,
     }
   }
 
-  if ((nameobj = PyString_FromFormat("%s.%s",
-                                    PyModule_GetName(mod),
-                                    name)) == 0 ||
-      (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
-                               base, dict)) == 0)
+  if ((nameobj = TEXT_FORMAT("%s.%s", PyModule_GetName(mod), name)) == 0 ||
+      (exc = PyErr_NewException(TEXT_STR(nameobj), base, dict)) == 0)
     goto fail;
 
 done:
diff --git a/pyke.h b/pyke.h
index 984b5d553a03e5161c13f46ba674734d4c7dc2ef..654f7517439ccef52003f3f9a809f9985af6f2a7 100644 (file)
--- a/pyke.h
+++ b/pyke.h
@@ -89,6 +89,97 @@ PRIVATE_SYMBOLS;
   typedef long Py_hash_t;
 #endif
 
+/* Plain octet strings.  Python 2 calls these `str', while Python 3 calls
+ * them `bytes'.  We call them `bin' here, and define the following.
+ *
+ *   * `BINOBJ' is the C type of a `bin' object.
+ *   * `BIN_TYPE' is the Python `type' object for `bin'.
+ *   * `BIN_CHECK(OBJ)' is true if OBJ is a `bin' object.
+ *   * `BIN_PTR(OBJ)' points to the first octet of OBJ, without checking!
+ *   * `BIN_LEN(OBJ)' yields the length of OBJ in octets, without checking!
+ *   * `BIN_FROMSTR(STR)' makes a `bin' object from a null-terminated string.
+ *   * `BIN_FORMAT(FMT, ARGS...)' and `BIN_VFORMAT(FMT, AP)' make a `bin'
+ *     object from a `printf'-like format string and arguments.
+ *   * `BIN_PREPAREWRITE(OBJ, PTR, LEN)' prepares to make a `bin' object: it
+ *     sets PTR to point to a buffer of LEN bytes; call `BIN_DONEWRITE' when
+ *     finished.  The variable OBJ will eventually be the resulting object,
+ *     but until `BIN_DONEWRITE' is called, it may in fact be some different
+ *     object.
+ *   * `BIN_DONEWRITE(OBJ, LEN)' completes making a `bin' object: it adjusts
+ *     its length to be LEN, which must not be larger than the LEN given to
+ *     `BIN_PREPAREWRITE', and sets OBJ to point to the finished object.
+ *   * `BIN_SETLEN(OBJ, LEN)' adjusts the length of OBJ downwards to LEN,
+ *     without checking!
+ #   * `Y' is a format character for `PyArg_ParseTuple...' for retrieving a
+ *     null-terminated octet string from a `bin' object.
+ #   * `YN' is a format character for `PyArg_ParseTuple...' for retrieving an
+ *     octet string and length from any sort-of vaguely binary-ish object.
+ */
+#define BINOBJ PyStringObject
+#define BIN_TYPE PyString_Type
+#define BIN_CHECK(obj) PyString_Check(obj)
+#define BIN_PTR(obj) PyString_AS_STRING(obj)
+#define BIN_LEN(obj) PyString_GET_SIZE(obj)
+#define BIN_FROMSTR(str) PyString_FromString(str)
+#define BIN_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len)
+#define BIN_FORMAT PyString_FromFormat
+#define BIN_VFORMAT PyString_FromFormatV
+#define BIN_PREPAREWRITE(obj, ptr, sz) do {                            \
+  (obj) = PyString_FromStringAndSize(0, (sz));                         \
+  (ptr) = PyString_AS_STRING(obj);                                     \
+} while (0)
+#define BIN_DONEWRITE(obj, sz) do Py_SIZE(obj) = (sz); while (0)
+#define BIN_SETLEN(obj, len) do Py_SIZE(obj) = (len); while (0)
+#define Y "s"
+#define YN "s#"
+
+/* Text strings.  Both Python 2 and Python 3 call these `str', but they're
+ * very different because a Python 3 `str' is Unicode inside.  When dealing
+ * with Python 3 text, the data is UTF-8 encoded.  We call them `text' here,
+ * and define the following.
+ *
+ *   * `TEXTOBJ' is the C type of a `text' object.
+ *   * `TEXT_TYPE' is the Python `type' object for `text'.
+ *   * `TEXT_CHECK(OBJ)' is true if OBJ is a `text' object.
+ *   * `TEXT_STR(OBJ)' points to the first byte of a null-terminated string
+ *     OBJ, or is null.
+ *   * `TEXT_PTR(OBJ)' points to the first byte of OBJ, without checking!
+ *   * `TEXT_LEN(OBJ)' yields the length of OBJ in octets, without checking!
+ *   * `TEXT_FROMSTR(STR)' makes a `text' object from a null-terminated
+ *     string.
+ *   * `TEXT_FORMAT(FMT, ARGS...)' and `TEST_VFORMAT(FMT, AP)' make a `text'
+ *     object from a `printf'-like format string and arguments.
+ *   * `TEXT_PREPAREWRITE(OBJ, PTR, LEN)' prepares to make a `text' object:
+ *     it sets PTR to point to a buffer of LEN bytes; call `TEXT_DONEWRITE'
+ *     when finished.  The variable OBJ will eventually be the resulting
+ *     object, but until `TEXT_DONEWRITE' is called, it may in fact be some
+ *     different object.
+ *   * `TEXT_DONEWRITE(OBJ, LEN)' completes making a `text' object: it
+ *     adjusts its length to be LEN, which must not be larger than the LEN
+ *     given to `TEXT_PREPAREWRITE', and sets OBJ to point to the finished
+ *     object.
+ *
+ * (Use `s' and `s#' in `PyArg_ParseTuple...'.)
+ */
+#define TEXTOBJ PyStringObject
+#define TEXT_TYPE PyString_Type
+#define TEXT_CHECK(obj) PyString_Check(obj)
+#define TEXT_PTR(obj) PyString_AS_STRING(obj)
+#define TEXT_STR(obj) PyString_AsString(obj)
+#define TEXT_PTRLEN(obj, ptr, len) do {                                        \
+  (ptr) = PyString_AS_STRING(obj);                                     \
+  (len) = PyString_GET_SIZE(obj);                                      \
+} while (0)
+#define TEXT_FORMAT PyString_FromFormat
+#define TEXT_VFORMAT PyString_FromFormatV
+#define TEXT_PREPAREWRITE(obj, ptr, sz) do {                           \
+  (obj) = PyString_FromStringAndSize(0, (sz));                         \
+  (ptr) = PyString_AS_STRING(obj);                                     \
+} while (0)
+#define TEXT_DONEWRITE(obj, sz) do { Py_SIZE(obj) = (sz); } while (0)
+#define TEXT_FROMSTR(str) PyString_FromString(str)
+#define TEXT_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len)
+
 /*----- Utilities for returning values and exceptions ---------------------*/
 
 /* Returning values. */
@@ -326,7 +417,7 @@ extern PyTypeObject *inittype(const PyTypeObject */*skel*/,
 /*----- Populating modules ------------------------------------------------*/
 
 extern PyObject *modname;
-  /* The overall module name.  Set this with `PyString_FromString'. */
+  /* The overall module name.  Set this with `TEXT_FROMSTR'. */
 
 extern PyObject *home_module;
   /* The overall module object. */