? work
Index: PLIST
===================================================================
RCS file: /cvsroot/pkgsrc/devel/py-sysctl/PLIST,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 PLIST
--- PLIST	11 Apr 2010 06:48:41 -0000	1.1.1.1
+++ PLIST	8 Sep 2019 19:45:28 -0000
@@ -1,2 +1,2 @@
-@comment $NetBSD: PLIST,v 1.1.1.1 2010/04/11 06:48:41 cherry Exp $
-${PYSITELIB}/sysctlmodule.so
+@comment $NetBSD$
+${PYSITELIB}/sysctl.so
Index: distinfo
===================================================================
RCS file: /cvsroot/pkgsrc/devel/py-sysctl/distinfo,v
retrieving revision 1.2
diff -u -r1.2 distinfo
--- distinfo	3 Nov 2015 03:29:16 -0000	1.2
+++ distinfo	8 Sep 2019 19:45:28 -0000
@@ -4,3 +4,6 @@
 RMD160 (py-sysctl-0.1.tar.gz) = 053144711e41f5518efc418795eec5e6e06750a1
 SHA512 (py-sysctl-0.1.tar.gz) = 4b6123625a212364ef5a2a367f13ec32194503b8881248e3909f5958adac05149ed2c22aa49b67468c3d1fc23386c16d97af747b8045b322c749f37d1a086005
 Size (py-sysctl-0.1.tar.gz) = 6746 bytes
+SHA1 (patch-setup.py) = eef1ae1e7a6d60f332514670ebcd1ee08dece131
+SHA1 (patch-sysctlmodule.c) = a0adc6d699081bb12256e5501a3367db2512ea81
+SHA1 (patch-test.py) = ff610e1eb2371147454861dd80e4d31878ec0ae7
Index: patches/patch-setup.py
===================================================================
RCS file: patches/patch-setup.py
diff -N patches/patch-setup.py
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-setup.py	8 Sep 2019 19:45:28 -0000
@@ -0,0 +1,11 @@
+$NetBSD$
+
+--- setup.py.orig	2010-04-10 17:41:18.000000000 +0000
++++ setup.py
+@@ -2,5 +2,5 @@
+ from distutils.core import setup, Extension
+ 
+-module1 = Extension('sysctlmodule',
++module1 = Extension('sysctl',
+                     sources = ['sysctlmodule.c'])
+ 
Index: patches/patch-sysctlmodule.c
===================================================================
RCS file: patches/patch-sysctlmodule.c
diff -N patches/patch-sysctlmodule.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-sysctlmodule.c	8 Sep 2019 19:45:28 -0000
@@ -0,0 +1,210 @@
+$NetBSD$
+
+Port to python 3
+
+--- sysctlmodule.c.orig	2010-04-10 17:41:18.000000000 +0000
++++ sysctlmodule.c
+@@ -33,6 +33,21 @@
+ #include <errno.h>
+ #include <sys/sysctl.h>
+ 
++struct module_state {
++    PyObject *error;
++};
++
++#if PY_MAJOR_VERSION >= 3
++#define PyInt_AsLong			PyLong_AsLong
++#define PyInt_CheckExact		PyLong_CheckExact
++#define PyString_CheckExact(val)	PyUnicode_CheckExact(val)
++#define PyString_FromStringAndSize	PyUnicode_FromStringAndSize
++#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
++#else
++#define GETSTATE(m) (&_state)
++static struct module_state _state;
++#endif
++
+ /* 
+  * create Python object of type similar to the MIB node 'name'
+  * This is basically the laziest way to do this, as we outsource the
+@@ -104,14 +119,12 @@ nodetype(char *nodepath)
+ 
+ 	int rv;
+ 	char cname[SYSCTL_NAMELEN];
+-	int csz;
++	size_t csz = SYSCTL_NAMELEN;
+ 
+ 	struct sysctlnode *rnode = NULL;
+ 
+ 	assert (nodepath != NULL);
+ 
+-	csz = SYSCTL_NAMELEN;
+-
+ 	rv = sysctlgetmibinfo(nodepath, NULL, NULL, cname, &csz, &rnode, SYSCTL_VERSION);
+ 
+ 	if (rv == -1 || rnode == NULL) {
+@@ -149,7 +162,7 @@ write_sysctl_string(char *name, char *va
+ 	size_t nodelen;
+ 
+ 	char cname[SYSCTL_NAMELEN];
+-	int csz = SYSCTL_NAMELEN;
++	size_t csz = SYSCTL_NAMELEN;
+ 
+ 	/* Sanity checks */
+ 
+@@ -237,7 +250,7 @@ read_sysctl(PyObject *self, PyObject *ar
+ {
+ 	PyObject *o;
+ 	void *val;
+-	size_t len;
++	size_t len = 0;
+ 	const char *name;
+ 	int rv;
+ 
+@@ -245,9 +258,7 @@ read_sysctl(PyObject *self, PyObject *ar
+ 		return NULL;
+ 
+ getbyname:
+-	val = NULL;
+-	len = 0;
+-
++	len += 4;
+ 	rv = sysctlbyname(name, NULL, &len, NULL, 0);
+ 
+ 	if (rv == -1) {
+@@ -256,13 +267,15 @@ getbyname:
+ 	}
+ 
+ 	val = malloc(len);
+-	if (val ==NULL) {
++	if (val == NULL) {
+ 		PyErr_NoMemory();
+ 		return NULL;
+ 	}
+ 
+ 	rv = sysctlbyname(name, val, &len, NULL, 0);
+ 
++	printf("XXX mysteriously weight bearing printf (?!) %d\n", rv);
++
+ 	if (rv == ENOMEM) { /* Try again */
+ 		assert (!"retry implementation untested");
+ 		const size_t ARBITRARYLEN = 4096; /* XXX: Tune this */
+@@ -309,7 +322,7 @@ write_sysctl(PyObject *self, PyObject *a
+ 
+ 	/* Arrange to obtain the oldvalue */
+ 	oldval = NULL;
+-	len = 0;
++	len = 4;
+ 
+ 	rv = sysctlbyname(name, NULL, &len, NULL, 0);
+ 
+@@ -445,7 +458,7 @@ create_node(const char *name, int ctl_ty
+ {
+ 
+ 	int mib[CTL_MAXNAME];
+-	u_int miblen = CTL_MAXNAME;
++	size_t miblen = CTL_MAXNAME;
+ 
+ 	char cname[SYSCTL_NAMELEN]; /* Canonical name */
+ 	char pname[SYSCTL_NAMELEN]; /* The Canonical prefix */
+@@ -456,6 +469,7 @@ create_node(const char *name, int ctl_ty
+ 	struct sysctlnode node;
+ 	size_t nodelen;
+ 
++
+ 	/* Check for NULL ptr dereference */
+ 	assert (value != NULL || vlen == 0);
+ 
+@@ -533,7 +547,7 @@ create_sysctl(PyObject *self, PyObject *
+ 	const char *typename = NULL;
+ 	size_t typelen = 0;
+ 
+-	int len, rv;
++	int rv;
+ 
+ 	/* XXX: Royal mess... needs more thought */
+ 	if (!PyArg_ParseTuple(args, "s|s#O: Incorrect values passed to sysctl.write", &name, &typename, &typelen, &value)) {
+@@ -546,7 +560,7 @@ create_sysctl(PyObject *self, PyObject *
+ 	}
+ 
+ 	/* XXX: Arrange to obtain the oldvalue */
+-	len = 0;
++	/*len = 4;*/
+ 
+ 	if ((typename == NULL && typelen == 0)
+ 	    || (!strncmp(typename, "CTLTYPE_NODE", typelen)) ) {
+@@ -593,7 +607,12 @@ create_sysctl(PyObject *self, PyObject *
+ 				PyErr_SetString(PyExc_TypeError, "Value passed is of wrong type");
+ 				return NULL;
+ 			}
++#if PY_MAJOR_VERSION >= 3
++			strval = PyUnicode_AsUTF8AndSize(value, &vlen);
++			if (strval == NULL) {
++#else
+ 			if (-1 == PyString_AsStringAndSize(value, &strval, &vlen)) {
++#endif
+ 				PyErr_SetString(PyExc_TypeError, "Error decoding string from buffer \n");
+ 				return NULL;
+ 			}
+@@ -694,8 +713,61 @@ static PyMethodDef sysctl_methods[] = {
+ };
+ 
+ 
++
++#if PY_MAJOR_VERSION >= 3
++
++static int sysctl_traverse(PyObject *m, visitproc visit, void *arg) {
++    Py_VISIT(GETSTATE(m)->error);
++    return 0;
++}
++
++static int sysctl_clear(PyObject *m) {
++    Py_CLEAR(GETSTATE(m)->error);
++    return 0;
++}
++
++
++static struct PyModuleDef moduledef = {
++        PyModuleDef_HEAD_INIT,
++        "sysctl",
++        NULL,
++        sizeof(struct module_state),
++        sysctl_methods,
++        NULL,
++        sysctl_traverse,
++        sysctl_clear,
++        NULL
++};
++
++#define INITERROR return NULL
++
+ PyMODINIT_FUNC
++PyInit_sysctl(void)
++
++#else
++#define INITERROR return
++
++void
+ initsysctl(void)
++#endif
+ {
+-	(void) Py_InitModule("sysctl", sysctl_methods);
++#if PY_MAJOR_VERSION >= 3
++    PyObject *module = PyModule_Create(&moduledef);
++#else
++    PyObject *module = Py_InitModule("sysctl", sysctl_methods);
++#endif
++
++    if (module == NULL)
++        INITERROR;
++    struct module_state *st = GETSTATE(module);
++
++    st->error = PyErr_NewException("sysctl.Error", NULL, NULL);
++    if (st->error == NULL) {
++        Py_DECREF(module);
++        INITERROR;
++    }
++
++#if PY_MAJOR_VERSION >= 3
++    return module;
++#endif
+ }
Index: patches/patch-test.py
===================================================================
RCS file: patches/patch-test.py
diff -N patches/patch-test.py
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-test.py	8 Sep 2019 19:45:28 -0000
@@ -0,0 +1,304 @@
+$NetBSD$
+
+Port to python 3.
+
+--- test.py.orig	2010-04-10 17:41:18.000000000 +0000
++++ test.py
+@@ -9,111 +9,111 @@ import sysctl
+ 
+ # Invalid node
+ try:
+-    print "Testing invalid node: sysctl.read(\"unknown\")"
++    print("Testing invalid node: sysctl.read(\"unknown\")")
+     sysctl.read("unknown")
+ except OSError as detail:
+-    print detail
+-    print "PASSED"
++    print(detail)
++    print("PASSED")
+ else:
+-    print "FAILED"
++    print("FAILED")
+ 
+ # Node with Struct/Table val.
+ tmp = 666 #Variable Declaration
+ try:
+-    print "Testing (integer) node sysctl.read(\"kern.maxproc\")"
++    print("Testing (integer) node sysctl.read(\"kern.maxproc\")")
+     tmp = sysctl.read("kern.maxproc")
+ except ValueError as detail:
+-    print detail
+-    print "FAILED"
++    print(detail)
++    print("FAILED")
+ else:
+-    print "kern.maxproc == %i" % tmp
+-    print "PASSED"
++    print("kern.maxproc == %i" % tmp)
++    print("PASSED")
+ 
+ # Node with String.
+ try:
+-    print "Testing (string) node sysctl.read(\"kern.ostype\")"
++    print("Testing (string) node sysctl.read(\"kern.ostype\")")
+     tmp = sysctl.read("kern.ostype")
+ except ValueError as detail:
+-    print detail
+-    print "FAILED"
++    print(detail)
++    print("FAILED")
+ else:
+-    print "kern.ostype = %s" % tmp
+-    print "PASSED"
++    print("kern.ostype = %s" % tmp)
++    print("PASSED")
+ 
+ # Node with long integer.
+ try:
+-    print "Testing read (long integer) node sysctl.read(\"kern.osrevision\")"
++    print("Testing read (long integer) node sysctl.read(\"kern.osrevision\")")
+     tmp = sysctl.read("kern.osrevision")
+ except ValueError as detail:
+-    print detail
+-    print "FAILED"
++    print(detail)
++    print("FAILED")
+ else:
+-    print "kern.osrevision = %li" % tmp
+-    print "PASSED"
++    print("kern.osrevision = %li" % tmp)
++    print("PASSED")
+ 
+ 
+ # Write a read-only node.
+ osrev = 0
+ try:
+-    print "Testing write to read only node sysctl.write(\"kern.osrevision\", 2)"
++    print("Testing write to read only node sysctl.write(\"kern.osrevision\", 2)")
+     osrev = sysctl.write("kern.osrevision", 2)
+ except OSError as detail:
+-    print detail
+-    print "sysctl.write() failed."
+-    print "PASSED"
++    print(detail)
++    print("sysctl.write() failed.")
++    print("PASSED")
+ else:
+     if osrev == sysctl.read("kern.osrevision"):
+-        print "FAILED: write succeed to read-only node! (review test? )"
++        print("FAILED: write succeed to read-only node! (review test? )")
+     else:
+-        print "FAILED"
++        print("FAILED")
+ 
+ # Write an integer to a writeable node.
+ # Create a writeable node of type integer.
+ try:
+     node="mynode"
+-    print "Testing write (integer) to node %s ..." % node
++    print("Testing write (integer) to node %s ..." % node)
+     subprocess.call(["sysctl", "-w", "//create=%s,type=int" % node])
+     oldval = sysctl.read(node)
+     oldval1 = sysctl.write(node, oldval + 1)
+ except ValueError as detail:
+-    print detail
++    print(detail)
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-    print "FAILED"
++    print("FAILED")
+ else:
+     if oldval != oldval1:
+-        print "FAILED: sysctl.write() returns incorrect old value"
++        print("FAILED: sysctl.write() returns incorrect old value")
+     else:
+         if (oldval1 + 1) != sysctl.read(node):
+-            print "FAILED: update failed."
++            print("FAILED: update failed.")
+         else:
+             sysctl.write(node, oldval1);
+             subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-            print "PASSED"
++            print("PASSED")
+ 
+ # Write a string to a writeable node.
+ # Create a writeable node of type string.
+ try:
+     node="mynode"
+-    print "Testing write (string) to node %s ..." % node
++    print("Testing write (string) to node %s ..." % node)
+     subprocess.call(["sysctl", "-w", "//create=%s,type=string,value=\"nothing\"" % node])
+     oldstring = sysctl.read(node)
+-    print "oldstring == %s" % oldstring
++    print("oldstring == %s" % oldstring)
+     oldstring1 = sysctl.write(node, "FOOBAR")
+-    print "write read oldstring1 == %s" % oldstring1
++    print("write read oldstring1 == %s" % oldstring1)
+ except ValueError as detail:
+-    print detail
++    print(detail)
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-    print "FAILED"
++    print("FAILED")
+ else:
+     if oldstring != oldstring1:
+-        print "FAILED: sysctl.write() returns incorrect old string value"
++        print("FAILED: sysctl.write() returns incorrect old string value")
+     else:
+         if "FOOBAR" != sysctl.read(node):
+-            print "FAILED: update failed."
++            print("FAILED: update failed.")
+         else:
+             sysctl.write(node, oldstring1);
+             subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-            print "PASSED"
++            print("PASSED")
+ 
+ # Write a string to a writeable 2-level node.
+ # Create a writeable node of type string.
+@@ -121,35 +121,35 @@ try:
+     node="mynode"
+     leaf="myleaf"
+     branch = node + '.' + leaf
+-    print "Testing write (string) to node %s ..." % branch
++    print("Testing write (string) to node %s ..." % branch)
+     subprocess.call(["sysctl", "-w", "//create=%s,type=node,value=\"nothing\"" % node])
+     subprocess.call(["sysctl", "-w", "//create=%s,type=string,value=\"nothing\"" % branch])
+     oldstring = sysctl.read(branch)
+-    print "oldstring == %s" % oldstring
++    print("oldstring == %s" % oldstring)
+     oldstring1 = sysctl.write(branch, "FOOBAR")
+-    print "write read oldstring1 == %s" % oldstring1
++    print("write read oldstring1 == %s" % oldstring1)
+ except ValueError as detail:
+-    print detail
++    print(detail)
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % branch])
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-    print "FAILED"
++    print("FAILED")
+ else:
+     if oldstring != oldstring1:
+-        print "FAILED: sysctl.write() returns incorrect old string value"
++        print("FAILED: sysctl.write() returns incorrect old string value")
+     else:
+         if "FOOBAR" != sysctl.read(branch):
+-            print "FAILED: update failed."
++            print("FAILED: update failed.")
+         else:
+             sysctl.write(branch, oldstring1);
+             subprocess.call(["sysctl", "-w", "//destroy=%s" % branch])
+             subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-            print "PASSED"
++            print("PASSED")
+ 
+ # Create a 3-level node using sysctl.create() and delete it using
+ # sysctl.destroy() 
+ 
+ try:
+-    print "Testing sysctl.create()"
++    print("Testing sysctl.create()")
+     node="mynode"
+     digit=node + "." + "mydigit"
+     leaf=digit + "." + "myleaf"
+@@ -159,7 +159,7 @@ try:
+     sysctl.create(leaf, "CTLTYPE_STRING")
+ # XXX: read/readwrite/permanent 
+ except OSError as detail:
+-    print "FAILED"
++    print("FAILED")
+ 
+ else:
+     try:
+@@ -172,10 +172,10 @@ else:
+         sysctl.destroy(node)
+ 
+     except OSError as detail:
+-        print "FAILED"
++        print("FAILED")
+ 
+     else:
+-        print "PASSED"
++        print("PASSED")
+ 
+ # Create a 3-level node and delete it using sysctl.delete()
+ 
+@@ -183,12 +183,12 @@ try:
+     node="mynode"
+     digit=node + "." + "mydigit"
+     leaf=digit + "." + "myleaf"
+-    print "Testing write (string) to node %s ..." % leaf
++    print("Testing write (string) to node %s ..." % leaf)
+     subprocess.call(["sysctl", "-w", "//create=%s,type=node,value=\"nothing\"" % node])
+     subprocess.call(["sysctl", "-w", "//create=%s,type=node,value=\"nothing\"" % digit])
+     subprocess.call(["sysctl", "-w", "//create=%s,type=string,value=\"nothing\"" % leaf])
+     oldstring = sysctl.read(leaf)
+-    print "oldstring == %s" % oldstring
++    print("oldstring == %s" % oldstring)
+ 
+     sysctl.destroy(leaf)
+ 
+@@ -197,51 +197,51 @@ try:
+         sysctl.destroy(node)
+     except OSError as detail:
+         # Should fail
+-        print "continue test with %s" % detail
++        print("continue test with %s" % detail)
+     else: 
+-        print "FAILED"
++        print("FAILED")
+ 
+     sysctl.destroy(digit)
+     sysctl.destroy(node)
+-    print "PASSED"
++    print("PASSED")
+ 
+ except ValueError as detail:
+-    print detail
++    print(detail)
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % leaf])
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % digit])
+     subprocess.call(["sysctl", "-w", "//destroy=%s" % node])
+-    print "FAILED"
++    print("FAILED")
+ 
+ def createdestroy(nodename, type, value):
+ 
+     if type == "CTLTYPE_STRING":
+-        print "Testing string: " + value
++        print("Testing string: " + value)
+     else:
+         if type == "CTLTYPE_INT":
+-            print "Testing integer: %d" % value
++            print("Testing integer: %d" % value)
+         else:
+-            print "Unknown type requested"
++            print("Unknown type requested")
+             return
+ 
+     try:
+         sysctl.create(nodename, type, value)
+     except OSError as detail:
+-        print "Failed to create node " + nodename + detail
++        print("Failed to create node " + nodename + detail)
+ 
+     newval = sysctl.read(nodename)
+ 
+     if newval != value:
+-        print "FAILED Value read mismatch", newval
++        print("FAILED Value read mismatch", newval)
+     else:
+-        print "PASSED"
++        print("PASSED")
+ 
+     try:
+         sysctl.destroy(nodename)
+     except OSError as detail:
+-        print "Failed to destroy node " + nodename + detail
++        print("Failed to destroy node " + nodename + detail)
+ 
+ 
+ # Try creating and verifying different types of nodes.
+-print "Testing writing to different types of nodes"
++print("Testing writing to different types of nodes")
+ createdestroy("tnode", "CTLTYPE_STRING", "TESTSTRING")
+ createdestroy("tnode", "CTLTYPE_INT", 65535)
