Skip to content

Commit

Permalink
Stop using IF in our Cython code
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
godlygeek committed Feb 12, 2024
1 parent 91d7016 commit 0265656
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/memray/_memray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import collections
import contextlib
import os
import pathlib
import platform
import sys

cimport cython
Expand Down Expand Up @@ -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

Expand Down
25 changes: 17 additions & 8 deletions src/memray/_memray_test_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/prctl.h>
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:
Expand Down

0 comments on commit 0265656

Please sign in to comment.