Andrei Moiseev
Andrei Moiseev

Reputation: 4084

Python 3 on Windows: extend stdin.readline() line buffer size

On Windows, interactive Python sys.stdin.readline() truncates Ctrl+V pastes to 512 characters.

How do I extend this limit? I need large pastes for prototyping.

Tried the fdopen() trick and it didn't work.

The buffer seems to be 1024 on macOS.

UPD: Looks like the buffer size is a compile-time constant.

In C, it is possible to expand buffer with setvbuf(), but this function is not wrapped by Python, which is sad. I guess I'll try to call it in MSVCRT.DLL with ctypes.

Upvotes: 3

Views: 732

Answers (2)

Vincent
Vincent

Reputation: 2722

(On Mac OS) Adding import readline to your python script will extend the limit of input()

from https://docs.python.org/3/library/readline.html

The readline module defines a number of functions to facilitate completion and reading/writing of history files from the Python interpreter. This module can be used directly, or via the rlcompleter module, which supports completion of Python identifiers at the interactive prompt. Settings made using this module affect the behaviour of both the interpreter’s interactive prompt and the prompts offered by the built-in input() function.

example

import readline

data = input()

print(data)

Upvotes: 1

Andrei Moiseev
Andrei Moiseev

Reputation: 4084

This is a bug: https://bugs.python.org/issue41849

  • sys.stdin.readline() has 512-character buffer, indeed
  • input() has 16K-character buffer

So currently input() can be used as a workaround.

Upvotes: 0

Related Questions