Show More
@@ -3,10 +3,10 b'' | |||
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | import re | |
|
6 | 7 | import json |
|
7 | 8 | |
|
8 | 9 | from IPython.core.release import kernel_protocol_version_info |
|
9 | from IPython.utils.tokenutil import token_at_cursor | |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | def code_to_line(code, cursor_pos): |
@@ -26,6 +26,33 b' def code_to_line(code, cursor_pos):' | |||
|
26 | 26 | return line, cursor_pos |
|
27 | 27 | |
|
28 | 28 | |
|
29 | _match_bracket = re.compile(r'\([^\(\)]+\)', re.UNICODE) | |
|
30 | _end_bracket = re.compile(r'\([^\(]*$', re.UNICODE) | |
|
31 | _identifier = re.compile(r'[a-z_][0-9a-z._]*', re.I|re.UNICODE) | |
|
32 | ||
|
33 | def extract_oname_v4(code, cursor_pos): | |
|
34 | """Reimplement token-finding logic from IPython 2.x javascript | |
|
35 | ||
|
36 | for adapting object_info_request from v5 to v4 | |
|
37 | """ | |
|
38 | ||
|
39 | line, _ = code_to_line(code, cursor_pos) | |
|
40 | ||
|
41 | oldline = line | |
|
42 | line = _match_bracket.sub(u'', line) | |
|
43 | while oldline != line: | |
|
44 | oldline = line | |
|
45 | line = _match_bracket.sub(u'', line) | |
|
46 | ||
|
47 | # remove everything after last open bracket | |
|
48 | line = _end_bracket.sub('', line) | |
|
49 | matches = _identifier.findall(line) | |
|
50 | if matches: | |
|
51 | return matches[-1] | |
|
52 | else: | |
|
53 | return '' | |
|
54 | ||
|
55 | ||
|
29 | 56 | class Adapter(object): |
|
30 | 57 | """Base class for adapting messages |
|
31 | 58 | |
@@ -158,7 +185,7 b' class V5toV4(Adapter):' | |||
|
158 | 185 | line, _ = code_to_line(code, cursor_pos) |
|
159 | 186 | |
|
160 | 187 | new_content = msg['content'] = {} |
|
161 |
new_content['oname'] = |
|
|
188 | new_content['oname'] = extract_oname_v4(code, cursor_pos) | |
|
162 | 189 | new_content['detail_level'] = content['detail_level'] |
|
163 | 190 | return msg |
|
164 | 191 |
@@ -311,6 +311,20 b' class V5toV4TestCase(AdapterTest):' | |||
|
311 | 311 | self.assertEqual(v4c['oname'], 'apple') |
|
312 | 312 | self.assertEqual(v5c['detail_level'], v4c['detail_level']) |
|
313 | 313 | |
|
314 | def test_inspect_request_token(self): | |
|
315 | line = 'something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,' | |
|
316 | msg = self.msg("inspect_request", { | |
|
317 | 'code' : line, | |
|
318 | 'cursor_pos': len(line)-1, | |
|
319 | 'detail_level' : 1, | |
|
320 | }) | |
|
321 | v5, v4 = self.adapt(msg) | |
|
322 | self.assertEqual(v4['header']['msg_type'], 'object_info_request') | |
|
323 | v4c = v4['content'] | |
|
324 | v5c = v5['content'] | |
|
325 | self.assertEqual(v4c['oname'], 'xxx.xxx.xxx') | |
|
326 | self.assertEqual(v5c['detail_level'], v4c['detail_level']) | |
|
327 | ||
|
314 | 328 | def test_inspect_reply(self): |
|
315 | 329 | msg = self.msg("inspect_reply", { |
|
316 | 330 | 'name' : 'foo', |
General Comments 0
You need to be logged in to leave comments.
Login now