Thanatos
Thanatos

Reputation: 44256

fcntl.ioctl always fails on Python 2

The following always fails:

import fcntl
import termios

buffer = bytearray(8)
fcntl.ioctl(2, termios.TIOCGWINSZ, buffer, True)

Always fails with:

Traceback (most recent call last):
  File "testit.py", line 5, in <module>
    fcntl.ioctl(2, termios.TIOCGWINSZ, buffer, True)
TypeError: ioctl requires a file or file descriptor, an integer and optionally an integer or buffer argument

Why? It executes fine in Python 3, but alas, we use Python 2 in production.

Edit: This is very similar to Python's issue #10345, except unlike the filer of that bug, I am using a mutable buffer.

Upvotes: 3

Views: 1000

Answers (1)

9000
9000

Reputation: 40884

The problem is that bytearray is not the buffer type you're looking for.

This works:

import fcntl
import termios
import array

buffer = array.array('h', [0]*8)
assert fcntl.ioctl(2, termios.TIOCGWINSZ, buffer, True) == 0
print buffer  # first two bytes are set to terminal's height and width.

Upvotes: 2

Related Questions