[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How does one redefine io.stderr:write() function?
- From: Robert McLay <mclay@...>
- Date: Sat, 20 Jun 2020 11:04:49 -0500
I really appreciate the answers. Thanks to you both, Andrew and Luis, for a very clear explanation.
>>>>> "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
>> This message is from an external sender. Learn more about why this <<
>> matters at https://links.utexas.edu/rtyclf. <<
Robert McLay, Ph.D.
Manager of Software Tools, HPC