From 02656560e01778c4fcc98a35392478f825620010 Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Thu, 8 Feb 2024 13:10:53 -0500 Subject: [PATCH] Stop using `IF` in our Cython code This has been deprecated for a while. Let's switch to using runtime checks for things that can be checked at runtime, and inline C++ code with conditional compilation for things that need to be checked at compile time. Signed-off-by: Matt Wozniski --- src/memray/_memray.pyx | 5 +++-- src/memray/_memray_test_utils.pyx | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/memray/_memray.pyx b/src/memray/_memray.pyx index 578db87f37..32053383cc 100644 --- a/src/memray/_memray.pyx +++ b/src/memray/_memray.pyx @@ -2,6 +2,7 @@ import collections import contextlib import os import pathlib +import platform import sys cimport cython @@ -865,9 +866,9 @@ cdef class FileReader: except OSError as exc: raise OSError(f"Could not open file {file_name}: {exc.strerror}") from None - IF UNAME_SYSNAME == "Linux": + if platform.system() == "Linux": self._path = "/proc/self/fd/" + str(self._file.fileno()) - ELSE: + else: self._path = str(file_name) self._report_progress = report_progress diff --git a/src/memray/_memray_test_utils.pyx b/src/memray/_memray_test_utils.pyx index 1e144eee6a..0d40209385 100644 --- a/src/memray/_memray_test_utils.pyx +++ b/src/memray/_memray_test_utils.pyx @@ -46,17 +46,26 @@ from libcpp.vector cimport vector from ._destination import Destination -IF UNAME_SYSNAME == "Linux": - cdef extern from "sys/prctl.h": - int prctl(int, char*, char*, char*, char*) + +cdef extern from *: + """ + #ifdef __linux__ + # include + inline void set_thread_name_impl(const char* new_name) + { + prctl(PR_SET_NAME, new_name, NULL, NULL, NULL); + } + #else + inline void set_thread_name_impl(const char* new_name) + { + } + #endif + """ + void set_thread_name_impl(const char* new_name) def set_thread_name(new_name): - cdef int PR_SET_NAME = 15 - IF UNAME_SYSNAME == "Linux": - return prctl(PR_SET_NAME, new_name, NULL, NULL, NULL) - ELSE: - return None + set_thread_name_impl(new_name) cdef class MemoryAllocator: