Unintended behaviour caused by redirect_stdout in Dev #6097

Closed
opened 2023-04-22 20:04:44 +02:00 by Vexed01 · 1 comment
Vexed01 commented 2023-04-22 20:04:44 +02:00 (Migrated from github.com)

What Red version are you using?

3.5.0.dev350+gf98db62b and also present on 3.4.19

Cog name

Dev

Command name

eval and repl

What did you expect to happen?

[p]eval and [p]repl to only send relevant outputs to the channel where it was executed.

What actually happened?

Text printed (print() only, not logged text) anywhere in the bot (print() only, not logged text) during an eval or repl execution is sent to the chat along with the output from the code execution. Note for repl sessions this only applies when code is actually being executed and not for the whole session duration.

How can we reproduce this error?

  1. Load the dev cog
  2. Make/load something that prints to the console from the on_message listener. Instacmd example below.
  3. Run an eval which passes control back to the event loop eg [p]eval await asyncio.sleep(0)

Instacmd listener

async def on_message(msg):
    print(f"{msg.author.display_name} sent a message which instacmd loves")

return on_message

Anything else?

Instacmd listener listed above was enabled for this demo:
image

The documentation for contextlib.redirect_stdout, which causes this behaviour, mentions that it should not be used in library code and threaded applications.

### What Red version are you using? 3.5.0.dev350+gf98db62b and also present on 3.4.19 ### Cog name Dev ### Command name eval and repl ### What did you expect to happen? `[p]eval` and `[p]repl` to only send relevant outputs to the channel where it was executed. ### What actually happened? Text printed (`print()` only, not logged text) anywhere in the bot (`print()` only, not logged text) during an eval or repl execution is sent to the chat along with the output from the code execution. Note for repl sessions this only applies when code is actually being executed and not for the whole session duration. ### How can we reproduce this error? 0. Load the dev cog 1. Make/load something that prints to the console from the on_message listener. Instacmd example below. 2. Run an eval which passes control back to the event loop eg `[p]eval await asyncio.sleep(0)` Instacmd listener ```py async def on_message(msg): print(f"{msg.author.display_name} sent a message which instacmd loves") return on_message ``` ### Anything else? Instacmd listener listed above was enabled for this demo: ![image](https://user-images.githubusercontent.com/51716387/233784195-988ab33e-6fcc-4f50-9361-dc265b6f22cf.png) The documentation for contextlib.redirect_stdout, which causes this behaviour, mentions that it should not be used in library code and threaded applications.
Flame442 commented 2023-09-27 23:18:35 +02:00 (Migrated from github.com)

dev is a development only cog locked behind a special flag, and print is suboptimal for logging compared to logger. While this is a valid issue, I don't think it is one that is reasonable to fix without sacrificing usability of the dev cog. If a workaround is needed, use logger for printing messages from cogs, and don't use dev in production.

`dev` is a development only cog locked behind a special flag, and `print` is suboptimal for logging compared to `logger`. While this is a valid issue, I don't think it is one that is reasonable to fix without sacrificing usability of the `dev` cog. If a workaround is needed, use `logger` for printing messages from cogs, and don't use `dev` in production.
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot#6097
No description provided.