Skip to content

Commit b142c4a

Browse files
committed
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 <mwozniski@bloomberg.net>
1 parent 6fbadf7 commit b142c4a

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/memray/_memray.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import collections
22
import contextlib
33
import os
44
import pathlib
5+
import platform
56
import sys
67

78
cimport cython
@@ -865,9 +866,9 @@ cdef class FileReader:
865866
except OSError as exc:
866867
raise OSError(f"Could not open file {file_name}: {exc.strerror}") from None
867868

868-
IF UNAME_SYSNAME == "Linux":
869+
if platform.system() == "Linux":
869870
self._path = "/proc/self/fd/" + str(self._file.fileno())
870-
ELSE:
871+
else:
871872
self._path = str(file_name)
872873
self._report_progress = report_progress
873874

src/memray/_memray_test_utils.pyx

+17-8
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,26 @@ from libcpp.vector cimport vector
4646

4747
from ._destination import Destination
4848

49-
IF UNAME_SYSNAME == "Linux":
50-
cdef extern from "sys/prctl.h":
51-
int prctl(int, char*, char*, char*, char*)
49+
50+
cdef extern from *:
51+
"""
52+
#ifdef __linux__
53+
# include <sys/prctl.h>
54+
inline void set_thread_name_impl(const char* new_name)
55+
{
56+
prctl(PR_SET_NAME, new_name, NULL, NULL, NULL);
57+
}
58+
#else
59+
inline void set_thread_name_impl(const char* new_name)
60+
{
61+
}
62+
#endif
63+
"""
64+
void set_thread_name_impl(const char* new_name)
5265

5366

5467
def set_thread_name(new_name):
55-
cdef int PR_SET_NAME = 15
56-
IF UNAME_SYSNAME == "Linux":
57-
return prctl(PR_SET_NAME, new_name, NULL, NULL, NULL)
58-
ELSE:
59-
return None
68+
set_thread_name_impl(new_name)
6069

6170

6271
cdef class MemoryAllocator:

0 commit comments

Comments
 (0)