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 authored and pablogsal committed Feb 13, 2024
1 parent 91d7016 commit ce382d6
Show file tree
Hide file tree
Showing 2 changed files with 23 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
28 changes: 20 additions & 8 deletions src/memray/_memray_test_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,29 @@ 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 int set_thread_name_impl(const char* new_name)
{
return prctl(PR_SET_NAME, new_name, NULL, NULL, NULL);
}
#else
# include <errno.h>
inline int set_thread_name_impl(const char* new_name)
{
errno = ENOTSUP;
return -1;
}
#endif
"""
int 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
return set_thread_name_impl(new_name)


cdef class MemoryAllocator:
Expand Down

0 comments on commit ce382d6

Please sign in to comment.