-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-46398: posixshmem module shm_rename freebsd support. #30621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from functools import partial | ||
import mmap | ||
import os | ||
import platform | ||
import errno | ||
import struct | ||
import secrets | ||
|
@@ -253,6 +254,27 @@ def unlink(self): | |
if self._track: | ||
resource_tracker.unregister(self._name, "shared_memory") | ||
|
||
def rename(self, newname, flags = 0): | ||
"""Renames a shared memory block. | ||
|
||
The policy how the operation is handled depends on the flag passed. | ||
The default behavior is if the newname already exists, it will | ||
be unlinked beforehand. | ||
With the SHM_RENAME_EXCHANGE flag, the old and new name will | ||
be exchanged. | ||
With the SHM_RENAME_NOREPLACE flag, an error will be returned | ||
if the new name exists. | ||
""" | ||
if !platform.hasattr("shm_rename"): | ||
raise OSError("Unsupported operation on this platform") | ||
|
||
if newname: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it simply returns False if newname is empty and returns True otherwise? Why add such check in this method? |
||
newname = "/" + newname if self._prepend_leading_slash else newname | ||
self._fd = _posixshmem.shm_rename(self._name, newname, flags) | ||
self._name = newname | ||
return True | ||
return False | ||
|
||
|
||
_encoding = "utf8" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add :method:`rename` to the `multiprocessing.shared_memory` library for FreeBSD. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -68,6 +68,53 @@ _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags, | |||||||
} | ||||||||
#endif /* HAVE_SHM_OPEN */ | ||||||||
|
||||||||
#ifdef HAVE_SHM_RENAME | ||||||||
/*[clinic input] | ||||||||
_posixshmem.shm_rename | ||||||||
path_from: unicode | ||||||||
path_to: unicode | ||||||||
flags: int | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think it was mistake to make parameters keyword-or-positional in other functions. It is better to make them positional-only, it will help if we decide to rename parameters. |
||||||||
/ | ||||||||
|
||||||||
Rename a shared memory object. | ||||||||
|
||||||||
Remove a shared memory object and relink to another path. | ||||||||
By default, if the destination path already exist, it will be unlinked. | ||||||||
With the SHM_RENAME_EXCHANGE flag, source and destination paths | ||||||||
will be exchanged. | ||||||||
With the SHM_RENAME_NOREPLACE flag, an error will be triggered | ||||||||
if the destination alredady exists. | ||||||||
|
||||||||
[clinic start generated code]*/ | ||||||||
|
||||||||
static int | ||||||||
_posixshmem_shm_rename_impl(PyObject *module, PyObject *path_from, | ||||||||
PyObject *path_to, int flags) | ||||||||
/*[clinic end generated code: output=a9101f606826ad30 input=0373bfc9c491e123]*/ | ||||||||
{ | ||||||||
int rv; | ||||||||
int async_err = 0; | ||||||||
const char *from = PyUnicode_AsUTF8AndSize(path_from, NULL); | ||||||||
const char *to = PyUnicode_AsUTF8AndSize(path_to, NULL); | ||||||||
if (from == NULL || to == NULL) { | ||||||||
return -1; | ||||||||
} | ||||||||
do { | ||||||||
Py_BEGIN_ALLOW_THREADS | ||||||||
rv = shm_rename(from, to, flags); | ||||||||
Py_END_ALLOW_THREADS | ||||||||
} while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||||||
|
||||||||
if (rv < 0) { | ||||||||
if (!async_err) | ||||||||
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path_from, path_to); | ||||||||
return -1; | ||||||||
} | ||||||||
|
||||||||
return rv; | ||||||||
} | ||||||||
#endif /* HAVE_SHM_RENAME */ | ||||||||
|
||||||||
#ifdef HAVE_SHM_UNLINK | ||||||||
/*[clinic input] | ||||||||
_posixshmem.shm_unlink | ||||||||
|
@@ -111,6 +158,9 @@ _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path) | |||||||
|
||||||||
static PyMethodDef module_methods[ ] = { | ||||||||
_POSIXSHMEM_SHM_OPEN_METHODDEF | ||||||||
#if defined(HAVE_SHM_RENAME) | ||||||||
_POSIXSHMEM_SHM_RENAME_METHODDEF | ||||||||
#endif | ||||||||
_POSIXSHMEM_SHM_UNLINK_METHODDEF | ||||||||
{NULL} /* Sentinel */ | ||||||||
}; | ||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this platform.hasattr() function? Where does it come from?