diff --git a/.gitignore b/.gitignore index 099aa2e..4306ebe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -./build -./meliae/_intset.c -./meliae/_loader.c -./meliae/_scanner.c +build/ +__pycache__/ +meliae/_intset.c +meliae/_loader.c +meliae/_scanner.c ./tags diff --git a/meliae/_scanner_core.c b/meliae/_scanner_core.c index 6ae7d83..8ca2923 100644 --- a/meliae/_scanner_core.c +++ b/meliae/_scanner_core.c @@ -98,7 +98,7 @@ _var_object_size(PyVarObject *c_obj) PyErr_Clear(); } return _basic_object_size((PyObject *)c_obj) - + num_entries * c_obj->ob_type->tp_itemsize; + + num_entries * Py_TYPE(c_obj)->tp_itemsize; } static Py_ssize_t @@ -172,10 +172,17 @@ static Py_ssize_t _size_of_dict(PyDictObject *c_obj) { Py_ssize_t size; +#if PY_MAJOR_VERSION >= 3 + PyObject* long_size; + + long_size = _PyDict_SizeOf(c_obj); + size = PyLong_AsSize_t(long_size); +#else size = _basic_object_size((PyObject *)c_obj); if (c_obj->ma_table != c_obj->ma_smalltable) { size += sizeof(PyDictEntry) * (c_obj->ma_mask + 1); } +#endif return size; } @@ -185,7 +192,7 @@ _size_of_unicode(PyUnicodeObject *c_obj) { Py_ssize_t size; size = _basic_object_size((PyObject *)c_obj); - size += Py_UNICODE_SIZE * c_obj->length; + size += Py_UNICODE_SIZE * PyUnicode_GetSize(c_obj); return size; } @@ -515,7 +522,11 @@ _dump_object_to_ref_info(struct ref_info *info, PyObject *c_obj, int recurse) } else if (PyClass_Check(c_obj)) { /* Old style class */ _write_static_to_info(info, ", \"name\": "); +#if PY_MAJOR_VERSION >= 3 + _dump_string(info, ((PyClassObject *)c_obj)->tp_name); +#else _dump_string(info, ((PyClassObject *)c_obj)->cl_name); +#endif } if (PyString_Check(c_obj)) { _write_to_ref_info(info, ", \"len\": " SSIZET_FMT, PyString_GET_SIZE(c_obj)); diff --git a/meliae/_scanner_core.h b/meliae/_scanner_core.h index 12b5599..1910520 100644 --- a/meliae/_scanner_core.h +++ b/meliae/_scanner_core.h @@ -25,8 +25,14 @@ #include #include #include +#include #include +#if PY_MAJOR_VERSION >= 3 +#define PyClassObject PyTypeObject +#define PyClass_Check(obj) PyObject_IsInstance(obj, (PyObject *)&PyType_Type) +#endif + /** * Compute the size of the data directly addressed by this object. * diff --git a/meliae/files.py b/meliae/files.py index 3575031..9155815 100644 --- a/meliae/files.py +++ b/meliae/files.py @@ -54,7 +54,7 @@ def open_file(filename): process = subprocess.Popen(['gzip', '-d', '-c', filename], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=close_fds) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: # failing that, use another python process return _open_mprocess(filename) @@ -71,7 +71,7 @@ def terminate_or_pass(): # no longer running. try: return terminate() - except OSError, e: + except OSError as e: sys.stderr.write('Ignoring failure to terminate process:' ' %s\n' % (e,)) # We *could* check if process.poll() returns that the diff --git a/meliae/perf_counter.py b/meliae/perf_counter.py index 51481fc..6688ee5 100644 --- a/meliae/perf_counter.py +++ b/meliae/perf_counter.py @@ -111,17 +111,18 @@ def get_memory(self, process): finally: f.close() m = re.search(r'(?i)vmpeak:\s*(?P\d+) kB', content) - peak = current = None + + peak = current = None if m is not None: - peak = int(m.group('peak')) * 1024 + peak = int(m.group('peak')) * 1024 m = re.search(r'(?i)vmsize:\s*(?P\d+) kB', content) if m is not None: - current = int(m.group('current')) * 1024 - return current, peak + current = int(m.group('current')) * 1024 + return current, peak -class _Win32PerformanceCounter(PerformanceCounter): +class _Win32PerformanceCounter(PerformanceCounter): def get_timer(self): # This returns wall-clock time, but using a much higher precision than # time.time() [which has a resolution of only 15ms] diff --git a/setup.py b/setup.py index 6375e12..7fe448a 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import print_function def config(): @@ -64,7 +65,7 @@ def config(): try: from Cython.Distutils import build_ext except ImportError: - print "We require Cython to be installed." + print("We require Cython to be installed.") return kwargs["cmdclass"] = {"build_ext": build_ext}