diff --git a/hwcounter.c b/hwcounter.c index 5916762..07a4de7 100644 --- a/hwcounter.c +++ b/hwcounter.c @@ -3,6 +3,10 @@ #include "Python.h" #include "structmember.h" +#if PY_MAJOR_VERSION >= 3 +#define PY3K +#endif + #define HWCOUNTER_GET_TIMESTAMP(count_ptr) \ do { \ uint32_t count_high, count_low; \ @@ -179,6 +183,8 @@ static PyMethodDef hwcounter_methods[] = { {NULL, NULL, 0, NULL} }; +#ifdef PY3K +// module initialization for python3 static struct PyModuleDef hwcounter_module = { PyModuleDef_HEAD_INIT, "hwcounter", @@ -204,3 +210,23 @@ PyInit_hwcounter(void) return m; } +#else +// module initialization for python2 +PyMODINIT_FUNC +inithwcounter(void) +{ + PyObject *m; + + hwcounter_TimerType.tp_new = PyType_GenericNew; + if (PyType_Ready(&hwcounter_TimerType) < 0) + return NULL; + + if ((m = Py_InitModule3("hwcounter", hwcounter_methods, "This module provides access to a very accurate, high-resolution hardware counter for measurement of time, in terms of processor clock cycles.")) == NULL) + return NULL; + + Py_INCREF(&hwcounter_TimerType); + PyModule_AddObject(m, "Timer", (PyObject *)&hwcounter_TimerType); + + return m; +} +#endif \ No newline at end of file diff --git a/setup.py b/setup.py index 4c88e35..0279ddb 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,18 @@ from setuptools import setup, Extension from os import path +import sys here = path.abspath(path.dirname(__file__)) -with open(path.join(here, 'README.md'), encoding='utf-8') as f: - long_description = f.read() +if sys.version_info[0] >= 3: + with open(path.join(here, 'README.md'), encoding='utf-8') as f: + long_description = f.read() +else: + with open(path.join(here, 'README.md')) as f: + long_description = f.read() setup(name = 'hwcounter', - version = '0.1.0', + version = '0.1.2', description = 'Highly accurate counter for measuring elapsed time in Python', long_description = long_description, url = 'https://github.com/paulsmith/hwcounter', @@ -18,6 +23,8 @@ 'Intended Audience :: Developers', 'Topic :: System :: Benchmark', 'License :: OSI Approved :: Apache Software License', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5',