From b2f7d709b7cb532bd56f1e5df99dd12bacdc938c Mon Sep 17 00:00:00 2001 From: Artem Szubowicz Date: Mon, 8 Feb 2016 16:01:20 +0100 Subject: [PATCH] Added python error catching --- lib/pygments/popen.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/pygments/popen.rb b/lib/pygments/popen.rb index 9598ebfc..3789c8fa 100644 --- a/lib/pygments/popen.rb +++ b/lib/pygments/popen.rb @@ -255,6 +255,8 @@ def mentos(method, args=[], kwargs={}, original_code=nil) # mentos is now waiting for the header, and, potentially, code. write_data(out_header, code) + check_for_error + # mentos will now return data to us. First it sends the header. header = get_header @@ -275,6 +277,25 @@ def mentos(method, args=[], kwargs={}, original_code=nil) raise MentosError, "EPIPE" end + def check_for_error + return if @err.closed? + + timeout_time = 0.25 # set a very little timeout so that we do not hang the parser + + Timeout::timeout(timeout_time) do + error_msg = @err.read + + unless error_msg.empty? + @log.error "[#{Time.now.iso8601}] Error running python script: #{error_msg}" + stop "Error running python script: #{error_msg}" + raise MentosError, error_msg + end + end + rescue Timeout::Error + # during the specified time no error were found + @err.close + end + # Based on the header we receive, determine if we need # to read more bytes, and read those bytes if necessary.