[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How does one redefine io.stderr:write() function?
- From: Andrew Gierth <andrew@...>
- Date: Sat, 20 Jun 2020 16:07:01 +0100
>>>>> "Robert" == Robert McLay <email@example.com> writes:
Robert> I would like to make calls to io.stderr:write() be silent
Robert> sometimes but I am unable to redefine that function
Robert> temporarily. Below is a tiny example of what I tried. This
Robert> doesn't work. What does?
This intercepts all file:write calls and allows through only the ones
not to io.stderr. Note that all file objects share the same metatable
and hence the same methods, and this can't be changed because it's the
identity of the metatable that is used to identify that it's a valid
file object in the first place.
local file_methods = getmetatable(io.stderr).__index -- any file will do
local old_write = file_methods.write
if f ~= io.stderr then
-- note that errors here will leave the patched function in place,
-- you might need to use pcall to handle that
file_methods.write = old_write