buffer is now not used with default parameters of TextIOConsoleWrapper

This commit is contained in:
devfred78
2022-02-19 16:30:37 +01:00
parent 2a2fe6fa01
commit 52b5bd68e9
2 changed files with 18 additions and 8 deletions

View File

@@ -81,7 +81,7 @@ Those lines initalize a full HD, fullscreen display, with a background image nam
Initialize an I/O text, buffered stream with:
```python
iotextstream = pygconsole.io.TextIOConsoleWrapper(console_name="pygame_console", newline='\r\n', line_buffering=True)
iotextstream = pygconsole.io.TextIOConsoleWrapper(console_name="pygame_console")
```
Retrieve the console created "under the hoods" with this I/O stream:
@@ -106,9 +106,9 @@ while True:
if event.key == K_ESCAPE:
sys.exit() # Exit when hitting the ESCAPE key
elif event.key == K_RETURN:
print("", file=iotextstream, flush=True) # New line
print("", file=iotextstream) # New line
if event.type == TEXTINPUT: # When typing a key with a writable character...
print(event.text, end='', file=iotextstream, flush=True) # Display the character
print(event.text, end='', file=iotextstream) # Display the character
# Background display
screen_surface.blit(background_surface,(0,0))
@@ -125,7 +125,7 @@ If you wish to custom the console, you can initialize it independantly, and assi
```python
my_console = pygconsole.console.get_console(name="custom_console",width=120,height=50)
iotextstream = pygconsole.io.TextIOConsoleWrapper(console_name="custom_console", newline='\r\n', line_buffering=True)
iotextstream = pygconsole.io.TextIOConsoleWrapper(console_name="custom_console")
```
A console is identified by its name, 2 consoles with the same name are in reality the same instance of the Console class.

View File

@@ -472,7 +472,7 @@ class TextIOConsoleWrapper(io.TextIOWrapper):
#############################################################
# Initialisation
def __init__(self, console_name = "pygame_console", errors=None, newline=None, line_buffering=False, write_through=False):
def __init__(self, console_name = "pygame_console", errors=None, newline='\r\n', line_buffering=True, write_through=True):
"""
Parameters
----------
@@ -481,13 +481,23 @@ class TextIOConsoleWrapper(io.TextIOWrapper):
error: str
optional string that specifies how encoding and decoding errors are to be handled.
newline: str
controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'.
controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'. Default is '\r\n'.
line_buffering: bool
If True, flush() is implied when a call to write contains a newline character or a carriage return. Default is False.
If True, flush() is implied when a call to write contains a newline character or a carriage return. Default is True.
write_through: bool
If True, calls to write() are guaranteed not to be buffered. Default is False.
If True, calls to write() are guaranteed not to be buffered. Default is True.
"""
buffer = BufferedIOConsole(console_name)
# To be compliant with RawIOConsole, encoding must be utf-8.
encoding = 'utf-8'
super().__init__(buffer, encoding, errors, newline, line_buffering, write_through)
# write() needs re-implementation since the `write_through` parameter seems not to work properly (or at least not as expected)
def write(self, s):
"""
Write the string s to the stream and return the number of characters written. if the `write_through` attribute is set to True, the string is readily written to the underlying stream. Otherwise, it is held in the buffer.
"""
nb_char = super().write(s)
if self.write_through: self.flush()
return nb_char