File: _posixshmem.py

package info (click to toggle)
pypy3 7.3.20%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212,628 kB
  • sloc: python: 2,101,020; ansic: 540,684; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (40 lines) | stat: -rw-r--r-- 1,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'POSIX shared memory module'

from _posixshmem_cffi import lib, ffi

import errno
import os

def shm_open(path, flags, mode=0o777):
    'Open a shared memory object.  Returns a file descriptor (integer).'
    path_utf8 = path.encode("utf-8")
    if b'\x00' in path_utf8:
        raise ValueError('embedded null character')
    while 1:
        fd = lib.shm_open(path_utf8, flags, mode)
        if fd < 0:
            e = ffi.errno
            if e != errno.EINTR:
                raise OSError(e, os.strerror(e))
        else:
            return fd

def shm_unlink(path):
    '''Remove a shared memory object (similar to unlink()).

Remove a shared memory object name, and, once all processes  have  unmapped
the object, de-allocates and destroys the contents of the associated memory
region.
    '''

    path_utf8 = path.encode("utf-8")
    if b'\x00' in path_utf8:
        raise ValueError('embedded null character')
    while 1:
        rv = lib.shm_unlink(path_utf8)
        if rv < 0:
            e = ffi.errno
            if e != errno.EINTR:
                raise OSError(e, os.strerror(e))
        else:
            return