classification
Title: OSError preadv()
Type: behavior Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: YoSTEALTH, pablogsal, vstinner
Priority: normal Keywords:

Created on 2019-07-05 17:50 by YoSTEALTH, last changed 2019-12-25 15:43 by YoSTEALTH. This issue is now closed.

Messages (2)
msg347364 - (view) Author: (YoSTEALTH) * Date: 2019-07-05 17:50
import os
import ctypes

# Stdlib
# ------
def test_preadv_stdlib(path):
    fd = os.open(path, os.O_RDWR | os.O_CREAT)
    buffer = bytearray(10)
    buffers = [buffer]
    try:
        length = os.preadv(fd, buffers, 0, os.RWF_NOWAIT)
        # OSError: [Errno 95] Operation not supported
        print('length:', length)
        print('buffer:', buffer)
    finally:
        os.close(fd)


# Cyptes Libc
# -----------
class iovec(ctypes.Structure):
    _fields_ = [('iov_base', ctypes.c_char_p), ('iov_len', ctypes.c_size_t)]


libc = ctypes.CDLL('libc.so.6')


def test_preadv_libc(path):
    fd = os.open(path, os.O_RDWR | os.O_CREAT)
    buffer = bytes(10)
    buffers = (iovec*1)()
    buffers[0].iov_base = buffer
    buffers[0].iov_len = 10
    try:
        length = libc.preadv2(fd, buffers, len(buffers), 0, os.RWF_NOWAIT)
        print('length:', length)  # length: -1
        print('buffer:', buffer)  # buffer: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    finally:
        os.close(fd)


# Test Run
# --------
def test():
    path = '/dev/shm/test-preadv-in-ram-file'

    # Test-1 stdlib implementation
    try:
        test_preadv_stdlib(path)  # OSError: [Errno 95] Operation not supported
    except OSError:
        print('OSError is raised. This should not raise OSError')

    # Test-2 custom ctype `libc` implementation
    test_preadv_libc(path)


if __name__ == '__main__':
    test()


This code is just to demonstrate working vs receiving an OSError in this specific usage.

System:   Linux 4.19.56-1-MANJARO
Python:   3.8.0b2

When the file in question is stored in ram ('/dev/shm') stdlib implementation raises an OSError while libc version does not.
msg358869 - (view) Author: (YoSTEALTH) * Date: 2019-12-25 15:43
I am closing this topic as its right for python to raise OSError as `-errno` must be assigned to e.g. `OSError(-errno, os.strerror(-errno))` to raise appropriate exception.
History
Date User Action Args
2019-12-25 15:43:58YoSTEALTHsetstatus: open -> closed
resolution: not a bug
messages: + msg358869

stage: resolved
2019-07-05 17:50:39YoSTEALTHcreate