Skip to content

Commit 84cc91a

Browse files
authored
stop sending/receiving ids between Ruby and Python (#218)
1 parent a58aaf9 commit 84cc91a

File tree

4 files changed

+9
-92
lines changed

4 files changed

+9
-92
lines changed

CHANGELOG.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
This document provides a high-level view of the changes to the {project-name} by release.
66
For a detailed view of what has changed, refer to the {uri-repo}/commits/master[commit history] on GitHub.
77

8+
== Unreleased
9+
10+
* stop sending/receiving `ids` between Ruby and Python
11+
812
== 2.0.0.rc3 (2021-01-08) - @slonopotamus
913

1014
* fix watchdog race condition leading to `ThreadError(<killed thread>)` on JRuby ({uri-repo}/pull/215[#215])

lib/pygments/mentos.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,27 +196,6 @@ def _send_data(self, res, method):
196196
sys.stdout.buffer.write(res_bytes)
197197
sys.stdout.flush()
198198

199-
200-
def _get_ids(self, text):
201-
start_id = text[:8]
202-
end_id = text[-8:]
203-
return start_id, end_id
204-
205-
def _check_and_return_text(self, text, start_id, end_id):
206-
207-
# Sanity check.
208-
id_regex = re.compile('[A-Z]{8}')
209-
210-
if not id_regex.match(start_id) and not id_regex.match(end_id):
211-
_write_error("ID check failed. Not an ID.")
212-
213-
if not start_id == end_id:
214-
_write_error("ID check failed. ID's did not match.")
215-
216-
# Passed the sanity check. Remove the id's and return
217-
text = text[10:-10]
218-
return text
219-
220199
def _parse_header(self, header):
221200
method = header["method"]
222201
args = header.get("args", [])
@@ -261,18 +240,9 @@ def start(self):
261240
# Read up to the given number of *bytes* (not chars) (possibly 0)
262241
text = sys.stdin.buffer.read(_bytes).decode('utf-8')
263242

264-
# Sanity check the return.
265-
if _bytes:
266-
start_id, end_id = self._get_ids(text)
267-
text = self._check_and_return_text(text, start_id, end_id)
268-
269243
# Get the actual data from pygments.
270244
res = self.get_data(method, lexer, args, kwargs, text)
271245

272-
# Put back the sanity check values.
273-
if method == "highlight":
274-
res = start_id + " " + res + " " + end_id
275-
276246
self._send_data(res, method)
277247

278248
except:

lib/pygments/popen.rb

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def with_watchdog(timeout_time, error_message)
237237
stop error_message
238238
state = :timeout
239239
end
240-
241240
end
242241
end : nil
243242
begin
@@ -258,7 +257,7 @@ def with_watchdog(timeout_time, error_message)
258257

259258
# Our 'rpc'-ish request to mentos. Requires a method name, and then optional
260259
# args, kwargs, code.
261-
def mentos(method, args = [], kwargs = {}, original_code = nil)
260+
def mentos(method, args = [], kwargs = {}, code = nil)
262261
# Open the pipe if necessary
263262
start unless alive?
264263

@@ -273,12 +272,6 @@ def mentos(method, args = [], kwargs = {}, original_code = nil)
273272
end
274273
end
275274

276-
# For sanity checking on both sides of the pipe when highlighting, we prepend and
277-
# append an id. mentos checks that these are 8 character ids and that they match.
278-
# It then returns the id's back to Rubyland.
279-
id = (0...8).map { rand(65..89).chr }.join
280-
code = original_code ? add_ids(original_code, id) : nil
281-
282275
# Add metadata to the header and generate it.
283276
bytesize = if code
284277
code.bytesize
@@ -287,7 +280,7 @@ def mentos(method, args = [], kwargs = {}, original_code = nil)
287280
end
288281

289282
kwargs.freeze
290-
kwargs = kwargs.merge('fd' => @out.to_i, 'id' => id, 'bytes' => bytesize)
283+
kwargs = kwargs.merge('fd' => @out.to_i, 'bytes' => bytesize)
291284
out_header = JSON.generate(method: method, args: args, kwargs: kwargs)
292285

293286
begin
@@ -315,7 +308,7 @@ def mentos(method, args = [], kwargs = {}, original_code = nil)
315308
header = @out.read(header_len)
316309

317310
# Now handle the header, any read any more data required.
318-
handle_header_and_return(header, id)
311+
handle_header_and_return(header)
319312
end
320313

321314
# Finally, return what we got.
@@ -341,10 +334,8 @@ def mentos(method, args = [], kwargs = {}, original_code = nil)
341334
# Based on the header we receive, determine if we need
342335
# to read more bytes, and read those bytes if necessary.
343336
#
344-
# Then, do a sanity check with the ids.
345-
#
346337
# Returns a result - either highlighted text or metadata.
347-
def handle_header_and_return(header, id)
338+
def handle_header_and_return(header)
348339
if header
349340
@log.info "In header: #{header}"
350341
header = header_to_json(header)
@@ -356,37 +347,14 @@ def handle_header_and_return(header, id)
356347
if header[:method] == 'highlight'
357348
# Make sure we have a result back; else consider this an error.
358349
raise MentosError, 'No highlight result back from mentos.' if res.nil?
359-
360-
@log.info 'Highlight in process.'
361-
362-
# Get the id's
363-
start_id = res[0..7]
364-
end_id = res[-8..-1]
365-
366-
# Sanity check.
367-
if !((start_id == id) && (end_id == id))
368-
raise MentosError, "ID's did not match. Aborting."
369-
else
370-
# We're good. Remove the padding
371-
res = res[10..-11]
372-
@log.info 'Highlighting complete.'
373-
res
374-
end
375350
end
351+
376352
res
377353
else
378354
raise MentosError, 'No header received back.'
379355
end
380356
end
381357

382-
# With the code, prepend the id (with two spaces to avoid escaping weirdness if
383-
# the following text starts with a slash (like terminal code), and append the
384-
# id, with two padding also. This means we are sending over the 8 characters +
385-
# code + 8 characters.
386-
def add_ids(code, id)
387-
(id + " #{code} #{id}").freeze
388-
end
389-
390358
# Return the final result for the API. Return Ruby objects for the methods that
391359
# want them, text otherwise.
392360
def return_result(res, method)

test/test_pygments.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,6 @@ def test_highlight_on_multi_threads
119119
end
120120
end
121121

122-
# Philosophically, I'm not the biggest fan of testing private
123-
# methods, but given the relative delicacy of validity checking
124-
# over the pipe I think it's necessary and informative.
125-
class PygmentsValidityTest < Test::Unit::TestCase
126-
def test_add_ids_with_padding
127-
res = PE.send(:add_ids, 'herp derp baz boo foo', 'ABCDEFGH')
128-
assert_equal 'ABCDEFGH herp derp baz boo foo ABCDEFGH', res
129-
end
130-
131-
def test_add_ids_on_empty_string
132-
res = PE.send(:add_ids, '', 'ABCDEFGH')
133-
assert_equal 'ABCDEFGH ABCDEFGH', res
134-
end
135-
136-
def test_add_ids_with_unicode_data
137-
res = PE.send(:add_ids, '# ø ø ø', 'ABCDEFGH')
138-
assert_equal 'ABCDEFGH # ø ø ø ABCDEFGH', res
139-
end
140-
141-
def test_add_ids_with_starting_slashes
142-
res = PE.send(:add_ids, '\\# ø ø ø..//', 'ABCDEFGH')
143-
assert_equal 'ABCDEFGH \\# ø ø ø..// ABCDEFGH', res
144-
end
145-
end
146-
147122
class PygmentsLexerTest < Test::Unit::TestCase
148123
RUBY_CODE = "#!/usr/bin/ruby\nputs 'foo'"
149124

0 commit comments

Comments
 (0)