Skip to content

Commit

Permalink
Merge pull request #496 from dalthviz/spyder_22181
Browse files Browse the repository at this point in the history
PR: Add filtering logic for stream data and constraint `setuptools` version
  • Loading branch information
ccordoba12 authored Jul 30, 2024
2 parents 67954ae + 4ad9b7e commit 782fddc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements/posix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ jupyter_client>=7.4.9,<9
pyzmq>=24.0.0
wurlitzer>=1.0.3
pyxdg>=0.26
setuptools<71.0
1 change: 1 addition & 0 deletions requirements/windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ ipykernel>=6.29.3,<7
ipython>=8.12.2,<9
jupyter_client>=7.4.9,<9
pyzmq>=24.0.0
setuptools<71.0
50 changes: 50 additions & 0 deletions spyder_kernels/console/outstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"""
Custom Spyder Outstream class.
"""
import os
import sys

from ipykernel.iostream import OutStream

Expand All @@ -20,3 +22,51 @@ def __init__(self, session, pub_thread, name, pipe=None, echo=None, *,
watchfd=True):
super().__init__(session, pub_thread, name, pipe,
echo=echo, watchfd=watchfd, isatty=True)

def _flush(self):
"""This is where the actual send happens.
_flush should generally be called in the IO thread,
unless the thread has been destroyed (e.g. forked subprocess).
NOTE: Overrided method to be able to filter messages.
See spyder-ide/spyder#22181
"""
self._flush_pending = False
self._subprocess_flush_pending = False

if self.echo is not None:
try:
self.echo.flush()
except OSError as e:
if self.echo is not sys.__stderr__:
print(f"Flush failed: {e}", file=sys.__stderr__)

for parent, data in self._flush_buffers():
# Messages that will not be printed to the console. This allows us
# to deal with issues such as spyder-ide/spyder#22181
filter_messages = ["Parent poll failed."]

if data and not any(
[message in data for message in filter_messages]
):
# FIXME: this disables Session's fork-safe check,
# since pub_thread is itself fork-safe.
# There should be a better way to do this.
self.session.pid = os.getpid()
content = {"name": self.name, "text": data}
msg = self.session.msg("stream", content, parent=parent)

# Each transform either returns a new
# message or None. If None is returned,
# the message has been 'used' and we return.
for hook in self._hooks:
msg = hook(msg)
if msg is None:
return

self.session.send(
self.pub_thread,
msg,
ident=self.topic,
)

0 comments on commit 782fddc

Please sign in to comment.