summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/compat.h15
-rw-r--r--src/dmidecodemodule.c75
-rw-r--r--src/setup_common.py20
-rw-r--r--src/xmlpythonizer.c7
4 files changed, 93 insertions, 24 deletions
diff --git a/src/compat.h b/src/compat.h
index 80a08c6..170f596 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -40,4 +40,19 @@
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
#endif
+// Python 2 vs Python 3 compat
+#if PY_MAJOR_VERSION >= 3
+#define IS_PY3K
+#define MODINITERROR return NULL
+#define PYNUMBER_FROMLONG PyLong_FromLong
+#define PYTEXT_FROMSTRING PyUnicode_FromString
+#else
+#include <bytesobject.h>
+#define MODINITERROR return
+#define PYNUMBER_FROMLONG PyInt_FromLong
+#define PYTEXT_FROMSTRING PyString_FromString
+#define PyCapsule_New(pointer, name, destructor) \
+ (PyCObject_FromVoidPtr(pointer, destructor))
+#endif
+
#endif
diff --git a/src/dmidecodemodule.c b/src/dmidecodemodule.c
index 1056a8f..8db99cb 100644
--- a/src/dmidecodemodule.c
+++ b/src/dmidecodemodule.c
@@ -53,6 +53,17 @@
#include "dmidump.h"
#include <mcheck.h>
+#if (PY_VERSION_HEX < 0x03030000)
+char *PyUnicode_AsUTF8(PyObject *unicode) {
+ PyObject *as_bytes = PyUnicode_AsUTF8String(unicode);
+ if (!as_bytes) {
+ return NULL;
+ }
+
+ return PyBytes_AsString(as_bytes);
+}
+#endif
+
static void init(options *opt)
{
opt->devmem = DEFAULT_MEM_DEV;
@@ -470,7 +481,12 @@ static PyObject *dmidecode_get_slot(PyObject * self, PyObject * args)
static PyObject *dmidecode_get_section(PyObject *self, PyObject *args)
{
- char *section = PyString_AsString(args);
+ char *section = NULL;
+ if (PyUnicode_Check(args)) {
+ section = PyUnicode_AsUTF8(args);
+ } else if (PyBytes_Check(args)) {
+ section = PyBytes_AsString(args);
+ }
if( section != NULL ) {
return dmidecode_get_group(global_options, section);
@@ -588,7 +604,7 @@ static PyObject *dmidecode_dump(PyObject * self, PyObject * null)
static PyObject *dmidecode_get_dev(PyObject * self, PyObject * null)
{
PyObject *dev = NULL;
- dev = PyString_FromString((global_options->dumpfile != NULL
+ dev = PYTEXT_FROMSTRING((global_options->dumpfile != NULL
? global_options->dumpfile : global_options->devmem));
Py_INCREF(dev);
return dev;
@@ -596,9 +612,14 @@ static PyObject *dmidecode_get_dev(PyObject * self, PyObject * null)
static PyObject *dmidecode_set_dev(PyObject * self, PyObject * arg)
{
- if(PyString_Check(arg)) {
+ char *f = NULL;
+ if(PyUnicode_Check(arg)) {
+ f = PyUnicode_AsUTF8(arg);
+ } else if(PyBytes_Check(arg)) {
+ f = PyBytes_AsString(arg);
+ }
+ if(f) {
struct stat buf;
- char *f = PyString_AsString(arg);
if( (f != NULL) && (global_options->dumpfile != NULL )
&& (strcmp(global_options->dumpfile, f) == 0) ) {
@@ -638,9 +659,9 @@ static PyObject *dmidecode_set_dev(PyObject * self, PyObject * arg)
static PyObject *dmidecode_set_pythonxmlmap(PyObject * self, PyObject * arg)
{
- if(PyString_Check(arg)) {
+ if(PyBytes_Check(arg)) {
struct stat fileinfo;
- char *fname = PyString_AsString(arg);
+ char *fname = PyBytes_AsString(arg);
memset(&fileinfo, 0, sizeof(struct stat));
@@ -664,7 +685,7 @@ static PyObject * dmidecode_get_warnings(PyObject *self, PyObject *null)
warn = log_retrieve(global_options->logdata, LOG_WARNING);
if( warn ) {
- ret = PyString_FromString(warn);
+ ret = PYTEXT_FROMSTRING(warn);
free(warn);
} else {
ret = Py_None;
@@ -711,7 +732,7 @@ static PyMethodDef DMIDataMethods[] = {
{(char *)"pythonmap", dmidecode_set_pythonxmlmap, METH_O,
(char *) "Use another python dict map definition. The default file is " PYTHON_XML_MAP},
- {(char *)"xmlapi", dmidecode_xmlapi, METH_KEYWORDS,
+ {(char *)"xmlapi", dmidecode_xmlapi, METH_VARARGS | METH_KEYWORDS,
(char *) "Internal API for retrieving data as raw XML data"},
@@ -726,6 +747,9 @@ static PyMethodDef DMIDataMethods[] = {
void destruct_options(void *ptr)
{
+#ifdef IS_PY3K
+ ptr = PyCapsule_GetPointer(ptr, NULL);
+#endif
options *opt = (options *) ptr;
if( opt->mappingxml != NULL ) {
@@ -763,8 +787,25 @@ void destruct_options(void *ptr)
free(ptr);
}
+#ifdef IS_PY3K
+static struct PyModuleDef dmidecodemod_def = {
+ PyModuleDef_HEAD_INIT,
+ "dmidecodemod",
+ NULL,
+ -1,
+ DMIDataMethods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
-PyMODINIT_FUNC initdmidecodemod(void)
+PyMODINIT_FUNC
+PyInit_dmidecodemod(void)
+#else
+PyMODINIT_FUNC
+initdmidecodemod(void)
+#endif
{
char *dmiver = NULL;
PyObject *module = NULL;
@@ -777,19 +818,29 @@ PyMODINIT_FUNC initdmidecodemod(void)
opt = (options *) malloc(sizeof(options)+2);
memset(opt, 0, sizeof(options)+2);
init(opt);
+#ifdef IS_PY3K
+ module = PyModule_Create(&dmidecodemod_def);
+#else
module = Py_InitModule3((char *)"dmidecodemod", DMIDataMethods,
"Python extension module for dmidecode");
+#endif
+ if (module == NULL)
+ MODINITERROR;
- version = PyString_FromString(VERSION);
+ version = PYTEXT_FROMSTRING(VERSION);
Py_INCREF(version);
PyModule_AddObject(module, "version", version);
opt->dmiversion_n = dmidecode_get_version(opt);
dmiver = dmixml_GetContent(opt->dmiversion_n);
- PyModule_AddObject(module, "dmi", dmiver ? PyString_FromString(dmiver) : Py_None);
+ PyModule_AddObject(module, "dmi", dmiver ? PYTEXT_FROMSTRING(dmiver) : Py_None);
// Assign this options struct to the module as well with a destructor, that way it will
// clean up the memory for us.
- PyModule_AddObject(module, "options", PyCObject_FromVoidPtr(opt, destruct_options));
+ // TODO: destructor has wrong type under py3?
+ PyModule_AddObject(module, "options", PyCapsule_New(opt, NULL, destruct_options));
global_options = opt;
+#ifdef IS_PY3K
+ return module;
+#endif
}
diff --git a/src/setup_common.py b/src/setup_common.py
index 209ccef..aec1f9b 100644
--- a/src/setup_common.py
+++ b/src/setup_common.py
@@ -26,17 +26,19 @@
# are deemed to be part of the source code.
#
-import commands, sys
+import subprocess, sys
+if sys.version_info[0] < 3:
+ import commands as subprocess
from os import path as os_path
from distutils.sysconfig import get_python_lib
# libxml2 - C flags
def libxml2_include(incdir):
- (res, libxml2_cflags) = commands.getstatusoutput("xml2-config --cflags")
+ (res, libxml2_cflags) = subprocess.getstatusoutput("xml2-config --cflags")
if res != 0:
- print "Could not build python-dmidecode."
- print "Could not run xml2-config, is libxml2 installed?"
- print "Also the development libraries?"
+ print("Could not build python-dmidecode.")
+ print("Could not run xml2-config, is libxml2 installed?")
+ print("Also the development libraries?")
sys.exit(1)
# Parse the xml2-config --cflags response
@@ -52,11 +54,11 @@ def libxml2_lib(libdir, libs):
if os_path.exists("/etc/debian_version"): #. XXX: Debian Workaround...
libdir.append("/usr/lib/pymodules/python%d.%d"%sys.version_info[0:2])
- (res, libxml2_libs) = commands.getstatusoutput("xml2-config --libs")
+ (res, libxml2_libs) = subprocess.getstatusoutput("xml2-config --libs")
if res != 0:
- print "Could not build python-dmidecode."
- print "Could not run xml2-config, is libxml2 installed?"
- print "Also the development libraries?"
+ print("Could not build python-dmidecode.")
+ print("Could not run xml2-config, is libxml2 installed?")
+ print("Also the development libraries?")
sys.exit(1)
# Parse the xml2-config --libs response
diff --git a/src/xmlpythonizer.c b/src/xmlpythonizer.c
index e318023..e9c9242 100644
--- a/src/xmlpythonizer.c
+++ b/src/xmlpythonizer.c
@@ -85,6 +85,7 @@
#include "dmilog.h"
#include "xmlpythonizer.h"
#include "version.h"
+#include "compat.h"
/**
@@ -646,7 +647,7 @@ inline PyObject *StringToPyObj(Log_t *logp, ptzMAP *val_m, const char *instr) {
switch( val_m->type_value ) {
case ptzINT:
case ptzLIST_INT:
- value = PyInt_FromLong(atoi(workstr));
+ value = PYNUMBER_FROMLONG(atoi(workstr));
break;
case ptzFLOAT:
@@ -661,7 +662,7 @@ inline PyObject *StringToPyObj(Log_t *logp, ptzMAP *val_m, const char *instr) {
case ptzSTR:
case ptzLIST_STR:
- value = PyString_FromString(workstr);
+ value = PyBytes_FromString(workstr);
break;
default:
@@ -850,7 +851,7 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
switch( map_p->type_value ) {
case ptzCONST:
if( _get_key_value(logp, key, 256, map_p, xpctx, 0) != NULL ) {
- value = PyString_FromString(map_p->value);
+ value = PyBytes_FromString(map_p->value);
PyADD_DICT_VALUE(retdata, key, value);
} else {
PyReturnError(PyExc_ValueError, "Could not get key value: %s [%i] (Defining key: %s)",