Show More
@@ -1,66 +1,67 b'' | |||||
1 | """These kinds of tests are less than ideal, but at least they run. |
|
1 | """These kinds of tests are less than ideal, but at least they run. | |
2 |
|
2 | |||
3 | This was an old test that was being run interactively in the top-level tests/ |
|
3 | This was an old test that was being run interactively in the top-level tests/ | |
4 | directory, which we are removing. For now putting this here ensures at least |
|
4 | directory, which we are removing. For now putting this here ensures at least | |
5 | we do run the test, though ultimately this functionality should all be tested |
|
5 | we do run the test, though ultimately this functionality should all be tested | |
6 | with better-isolated tests that don't rely on the global instance in iptest. |
|
6 | with better-isolated tests that don't rely on the global instance in iptest. | |
7 | """ |
|
7 | """ | |
8 | from IPython.core.splitinput import LineInfo |
|
8 | from IPython.core.splitinput import LineInfo | |
9 | from IPython.core.prefilter import AutocallChecker |
|
9 | from IPython.core.prefilter import AutocallChecker | |
10 |
|
10 | |||
|
11 | ||||
11 | def doctest_autocall(): |
|
12 | def doctest_autocall(): | |
12 | """ |
|
13 | """ | |
13 | In [1]: def f1(a,b,c): |
|
14 | In [1]: def f1(a,b,c): | |
14 | ...: return a+b+c |
|
15 | ...: return a+b+c | |
15 | ...: |
|
16 | ...: | |
16 |
|
17 | |||
17 | In [2]: def f2(a): |
|
18 | In [2]: def f2(a): | |
18 | ...: return a + a |
|
19 | ...: return a + a | |
19 | ...: |
|
20 | ...: | |
20 |
|
21 | |||
21 | In [3]: def r(x): |
|
22 | In [3]: def r(x): | |
22 | ...: return True |
|
23 | ...: return True | |
23 | ...: |
|
24 | ...: | |
24 |
|
25 | |||
25 | In [4]: ;f2 a b c |
|
26 | In [4]: ;f2 a b c | |
26 | Out[4]: 'a b ca b c' |
|
27 | Out[4]: 'a b ca b c' | |
27 |
|
28 | |||
28 | In [5]: assert _ == "a b ca b c" |
|
29 | In [5]: assert _ == "a b ca b c" | |
29 |
|
30 | |||
30 | In [6]: ,f1 a b c |
|
31 | In [6]: ,f1 a b c | |
31 | Out[6]: 'abc' |
|
32 | Out[6]: 'abc' | |
32 |
|
33 | |||
33 | In [7]: assert _ == 'abc' |
|
34 | In [7]: assert _ == 'abc' | |
34 |
|
35 | |||
35 | In [8]: print(_) |
|
36 | In [8]: print(_) | |
36 | abc |
|
37 | abc | |
37 |
|
38 | |||
38 | In [9]: /f1 1,2,3 |
|
39 | In [9]: /f1 1,2,3 | |
39 | Out[9]: 6 |
|
40 | Out[9]: 6 | |
40 |
|
41 | |||
41 | In [10]: assert _ == 6 |
|
42 | In [10]: assert _ == 6 | |
42 |
|
43 | |||
43 | In [11]: /f2 4 |
|
44 | In [11]: /f2 4 | |
44 | Out[11]: 8 |
|
45 | Out[11]: 8 | |
45 |
|
46 | |||
46 | In [12]: assert _ == 8 |
|
47 | In [12]: assert _ == 8 | |
47 |
|
48 | |||
48 | In [12]: del f1, f2 |
|
49 | In [12]: del f1, f2 | |
49 |
|
50 | |||
50 | In [13]: ,r a |
|
51 | In [13]: ,r a | |
51 | Out[13]: True |
|
52 | Out[13]: True | |
52 |
|
53 | |||
53 | In [14]: assert _ == True |
|
54 | In [14]: assert _ == True | |
54 |
|
55 | |||
55 | In [15]: r'a' |
|
56 | In [15]: r'a' | |
56 | Out[15]: 'a' |
|
57 | Out[15]: 'a' | |
57 |
|
58 | |||
58 | In [16]: assert _ == 'a' |
|
59 | In [16]: assert _ == 'a' | |
59 | """ |
|
60 | """ | |
60 |
|
61 | |||
61 |
|
62 | |||
62 | def test_autocall_should_ignore_raw_strings(): |
|
63 | def test_autocall_should_ignore_raw_strings(): | |
63 | line_info = LineInfo("r'a'") |
|
64 | line_info = LineInfo("r'a'") | |
64 | pm = ip.prefilter_manager |
|
65 | pm = ip.prefilter_manager | |
65 | ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, config=pm.config) |
|
66 | ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, config=pm.config) | |
66 | assert ac.check(line_info) is None |
|
67 | assert ac.check(line_info) is None |
@@ -1,38 +1,39 b'' | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 |
|
2 | |||
3 | from IPython.core.splitinput import split_user_input, LineInfo |
|
3 | from IPython.core.splitinput import split_user_input, LineInfo | |
4 | from IPython.testing import tools as tt |
|
4 | from IPython.testing import tools as tt | |
5 |
|
5 | |||
6 | tests = [ |
|
6 | tests = [ | |
7 | ("x=1", ("", "", "x", "=1")), |
|
7 | ("x=1", ("", "", "x", "=1")), | |
8 | ("?", ("", "?", "", "")), |
|
8 | ("?", ("", "?", "", "")), | |
9 | ("??", ("", "??", "", "")), |
|
9 | ("??", ("", "??", "", "")), | |
10 | (" ?", (" ", "?", "", "")), |
|
10 | (" ?", (" ", "?", "", "")), | |
11 | (" ??", (" ", "??", "", "")), |
|
11 | (" ??", (" ", "??", "", "")), | |
12 | ("??x", ("", "??", "x", "")), |
|
12 | ("??x", ("", "??", "x", "")), | |
13 | ("?x=1", ("", "?", "x", "=1")), |
|
13 | ("?x=1", ("", "?", "x", "=1")), | |
14 | ("!ls", ("", "!", "ls", "")), |
|
14 | ("!ls", ("", "!", "ls", "")), | |
15 | (" !ls", (" ", "!", "ls", "")), |
|
15 | (" !ls", (" ", "!", "ls", "")), | |
16 | ("!!ls", ("", "!!", "ls", "")), |
|
16 | ("!!ls", ("", "!!", "ls", "")), | |
17 | (" !!ls", (" ", "!!", "ls", "")), |
|
17 | (" !!ls", (" ", "!!", "ls", "")), | |
18 | (",ls", ("", ",", "ls", "")), |
|
18 | (",ls", ("", ",", "ls", "")), | |
19 | (";ls", ("", ";", "ls", "")), |
|
19 | (";ls", ("", ";", "ls", "")), | |
20 | (" ;ls", (" ", ";", "ls", "")), |
|
20 | (" ;ls", (" ", ";", "ls", "")), | |
21 | ("f.g(x)", ("", "", "f.g", "(x)")), |
|
21 | ("f.g(x)", ("", "", "f.g", "(x)")), | |
22 | ("f.g (x)", ("", "", "f.g", "(x)")), |
|
22 | ("f.g (x)", ("", "", "f.g", "(x)")), | |
23 | ("?%hist1", ("", "?", "%hist1", "")), |
|
23 | ("?%hist1", ("", "?", "%hist1", "")), | |
24 | ("?%%hist2", ("", "?", "%%hist2", "")), |
|
24 | ("?%%hist2", ("", "?", "%%hist2", "")), | |
25 | ("??%hist3", ("", "??", "%hist3", "")), |
|
25 | ("??%hist3", ("", "??", "%hist3", "")), | |
26 | ("??%%hist4", ("", "??", "%%hist4", "")), |
|
26 | ("??%%hist4", ("", "??", "%%hist4", "")), | |
27 | ("?x*", ("", "?", "x*", "")), |
|
27 | ("?x*", ("", "?", "x*", "")), | |
28 | ] |
|
28 | ] | |
29 | tests.append(("PΓ©rez Fernando", ("", "", "PΓ©rez", "Fernando"))) |
|
29 | tests.append(("PΓ©rez Fernando", ("", "", "PΓ©rez", "Fernando"))) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | def test_split_user_input(): |
|
32 | def test_split_user_input(): | |
33 | return tt.check_pairs(split_user_input, tests) |
|
33 | return tt.check_pairs(split_user_input, tests) | |
34 |
|
34 | |||
|
35 | ||||
35 | def test_LineInfo(): |
|
36 | def test_LineInfo(): | |
36 | """Simple test for LineInfo construction and str()""" |
|
37 | """Simple test for LineInfo construction and str()""" | |
37 | linfo = LineInfo(" %cd /home") |
|
38 | linfo = LineInfo(" %cd /home") | |
38 | assert str(linfo) == "LineInfo [ |%|cd|/home]" |
|
39 | assert str(linfo) == "LineInfo [ |%|cd|/home]" |
@@ -1,19 +1,20 b'' | |||||
1 | from IPython.core.error import TryNext |
|
1 | from IPython.core.error import TryNext | |
2 | from IPython.lib.clipboard import ClipboardEmpty |
|
2 | from IPython.lib.clipboard import ClipboardEmpty | |
3 | from IPython.testing.decorators import skip_if_no_x11 |
|
3 | from IPython.testing.decorators import skip_if_no_x11 | |
4 |
|
4 | |||
|
5 | ||||
5 | @skip_if_no_x11 |
|
6 | @skip_if_no_x11 | |
6 | def test_clipboard_get(): |
|
7 | def test_clipboard_get(): | |
7 | # Smoketest for clipboard access - we can't easily guarantee that the |
|
8 | # Smoketest for clipboard access - we can't easily guarantee that the | |
8 | # clipboard is accessible and has something on it, but this tries to |
|
9 | # clipboard is accessible and has something on it, but this tries to | |
9 | # exercise the relevant code anyway. |
|
10 | # exercise the relevant code anyway. | |
10 | try: |
|
11 | try: | |
11 | a = get_ipython().hooks.clipboard_get() |
|
12 | a = get_ipython().hooks.clipboard_get() | |
12 | except ClipboardEmpty: |
|
13 | except ClipboardEmpty: | |
13 | # Nothing in clipboard to get |
|
14 | # Nothing in clipboard to get | |
14 | pass |
|
15 | pass | |
15 | except TryNext: |
|
16 | except TryNext: | |
16 | # No clipboard access API available |
|
17 | # No clipboard access API available | |
17 | pass |
|
18 | pass | |
18 | else: |
|
19 | else: | |
19 | assert isinstance(a, str) |
|
20 | assert isinstance(a, str) |
@@ -1,60 +1,61 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Miscellaneous context managers. |
|
2 | """Miscellaneous context managers. | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | import warnings |
|
5 | import warnings | |
6 |
|
6 | |||
7 | # Copyright (c) IPython Development Team. |
|
7 | # Copyright (c) IPython Development Team. | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 |
|
9 | |||
|
10 | ||||
10 | class preserve_keys(object): |
|
11 | class preserve_keys(object): | |
11 | """Preserve a set of keys in a dictionary. |
|
12 | """Preserve a set of keys in a dictionary. | |
12 |
|
13 | |||
13 | Upon entering the context manager the current values of the keys |
|
14 | Upon entering the context manager the current values of the keys | |
14 | will be saved. Upon exiting, the dictionary will be updated to |
|
15 | will be saved. Upon exiting, the dictionary will be updated to | |
15 | restore the original value of the preserved keys. Preserved keys |
|
16 | restore the original value of the preserved keys. Preserved keys | |
16 | which did not exist when entering the context manager will be |
|
17 | which did not exist when entering the context manager will be | |
17 | deleted. |
|
18 | deleted. | |
18 |
|
19 | |||
19 | Examples |
|
20 | Examples | |
20 | -------- |
|
21 | -------- | |
21 |
|
22 | |||
22 | >>> d = {'a': 1, 'b': 2, 'c': 3} |
|
23 | >>> d = {'a': 1, 'b': 2, 'c': 3} | |
23 | >>> with preserve_keys(d, 'b', 'c', 'd'): |
|
24 | >>> with preserve_keys(d, 'b', 'c', 'd'): | |
24 | ... del d['a'] |
|
25 | ... del d['a'] | |
25 | ... del d['b'] # will be reset to 2 |
|
26 | ... del d['b'] # will be reset to 2 | |
26 | ... d['c'] = None # will be reset to 3 |
|
27 | ... d['c'] = None # will be reset to 3 | |
27 | ... d['d'] = 4 # will be deleted |
|
28 | ... d['d'] = 4 # will be deleted | |
28 | ... d['e'] = 5 |
|
29 | ... d['e'] = 5 | |
29 | ... print(sorted(d.items())) |
|
30 | ... print(sorted(d.items())) | |
30 | ... |
|
31 | ... | |
31 | [('c', None), ('d', 4), ('e', 5)] |
|
32 | [('c', None), ('d', 4), ('e', 5)] | |
32 | >>> print(sorted(d.items())) |
|
33 | >>> print(sorted(d.items())) | |
33 | [('b', 2), ('c', 3), ('e', 5)] |
|
34 | [('b', 2), ('c', 3), ('e', 5)] | |
34 | """ |
|
35 | """ | |
35 |
|
36 | |||
36 | def __init__(self, dictionary, *keys): |
|
37 | def __init__(self, dictionary, *keys): | |
37 | self.dictionary = dictionary |
|
38 | self.dictionary = dictionary | |
38 | self.keys = keys |
|
39 | self.keys = keys | |
39 |
|
40 | |||
40 | def __enter__(self): |
|
41 | def __enter__(self): | |
41 | # Actions to perform upon exiting. |
|
42 | # Actions to perform upon exiting. | |
42 | to_delete = [] |
|
43 | to_delete = [] | |
43 | to_update = {} |
|
44 | to_update = {} | |
44 |
|
45 | |||
45 | d = self.dictionary |
|
46 | d = self.dictionary | |
46 | for k in self.keys: |
|
47 | for k in self.keys: | |
47 | if k in d: |
|
48 | if k in d: | |
48 | to_update[k] = d[k] |
|
49 | to_update[k] = d[k] | |
49 | else: |
|
50 | else: | |
50 | to_delete.append(k) |
|
51 | to_delete.append(k) | |
51 |
|
52 | |||
52 | self.to_delete = to_delete |
|
53 | self.to_delete = to_delete | |
53 | self.to_update = to_update |
|
54 | self.to_update = to_update | |
54 |
|
55 | |||
55 | def __exit__(self, *exc_info): |
|
56 | def __exit__(self, *exc_info): | |
56 | d = self.dictionary |
|
57 | d = self.dictionary | |
57 |
|
58 | |||
58 | for k in self.to_delete: |
|
59 | for k in self.to_delete: | |
59 | d.pop(k, None) |
|
60 | d.pop(k, None) | |
60 | d.update(self.to_update) |
|
61 | d.update(self.to_update) |
@@ -1,6 +1,5 b'' | |||||
1 |
|
||||
2 |
|
|
1 | from warnings import warn | |
3 |
|
2 | |||
4 | warn("IPython.utils.eventful has moved to traitlets.eventful", stacklevel=2) |
|
3 | warn("IPython.utils.eventful has moved to traitlets.eventful", stacklevel=2) | |
5 |
|
4 | |||
6 | from traitlets.eventful import * |
|
5 | from traitlets.eventful import * |
@@ -1,6 +1,5 b'' | |||||
1 |
|
||||
2 |
|
|
1 | from warnings import warn | |
3 |
|
2 | |||
4 | warn("IPython.utils.log has moved to traitlets.log", stacklevel=2) |
|
3 | warn("IPython.utils.log has moved to traitlets.log", stacklevel=2) | |
5 |
|
4 | |||
6 | from traitlets.log import * |
|
5 | from traitlets.log import * |
@@ -1,58 +1,59 b'' | |||||
1 | """ This module contains classes - NamedFileInTemporaryDirectory, TemporaryWorkingDirectory. |
|
1 | """ This module contains classes - NamedFileInTemporaryDirectory, TemporaryWorkingDirectory. | |
2 |
|
2 | |||
3 | These classes add extra features such as creating a named file in temporary directory and |
|
3 | These classes add extra features such as creating a named file in temporary directory and | |
4 | creating a context manager for the working directory which is also temporary. |
|
4 | creating a context manager for the working directory which is also temporary. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | import os as _os |
|
7 | import os as _os | |
8 | from pathlib import Path |
|
8 | from pathlib import Path | |
9 | from tempfile import TemporaryDirectory |
|
9 | from tempfile import TemporaryDirectory | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | class NamedFileInTemporaryDirectory(object): |
|
12 | class NamedFileInTemporaryDirectory(object): | |
13 | def __init__(self, filename, mode="w+b", bufsize=-1, add_to_syspath=False, **kwds): |
|
13 | def __init__(self, filename, mode="w+b", bufsize=-1, add_to_syspath=False, **kwds): | |
14 | """ |
|
14 | """ | |
15 | Open a file named `filename` in a temporary directory. |
|
15 | Open a file named `filename` in a temporary directory. | |
16 |
|
16 | |||
17 | This context manager is preferred over `NamedTemporaryFile` in |
|
17 | This context manager is preferred over `NamedTemporaryFile` in | |
18 | stdlib `tempfile` when one needs to reopen the file. |
|
18 | stdlib `tempfile` when one needs to reopen the file. | |
19 |
|
19 | |||
20 | Arguments `mode` and `bufsize` are passed to `open`. |
|
20 | Arguments `mode` and `bufsize` are passed to `open`. | |
21 | Rest of the arguments are passed to `TemporaryDirectory`. |
|
21 | Rest of the arguments are passed to `TemporaryDirectory`. | |
22 |
|
22 | |||
23 | """ |
|
23 | """ | |
24 | self._tmpdir = TemporaryDirectory(**kwds) |
|
24 | self._tmpdir = TemporaryDirectory(**kwds) | |
25 | path = Path(self._tmpdir.name) / filename |
|
25 | path = Path(self._tmpdir.name) / filename | |
26 | encoding = None if "b" in mode else "utf-8" |
|
26 | encoding = None if "b" in mode else "utf-8" | |
27 | self.file = open(path, mode, bufsize, encoding=encoding) |
|
27 | self.file = open(path, mode, bufsize, encoding=encoding) | |
28 |
|
28 | |||
29 | def cleanup(self): |
|
29 | def cleanup(self): | |
30 | self.file.close() |
|
30 | self.file.close() | |
31 | self._tmpdir.cleanup() |
|
31 | self._tmpdir.cleanup() | |
32 |
|
32 | |||
33 | __del__ = cleanup |
|
33 | __del__ = cleanup | |
34 |
|
34 | |||
35 | def __enter__(self): |
|
35 | def __enter__(self): | |
36 | return self.file |
|
36 | return self.file | |
37 |
|
37 | |||
38 | def __exit__(self, type, value, traceback): |
|
38 | def __exit__(self, type, value, traceback): | |
39 | self.cleanup() |
|
39 | self.cleanup() | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | class TemporaryWorkingDirectory(TemporaryDirectory): |
|
42 | class TemporaryWorkingDirectory(TemporaryDirectory): | |
43 | """ |
|
43 | """ | |
44 | Creates a temporary directory and sets the cwd to that directory. |
|
44 | Creates a temporary directory and sets the cwd to that directory. | |
45 | Automatically reverts to previous cwd upon cleanup. |
|
45 | Automatically reverts to previous cwd upon cleanup. | |
46 | Usage example: |
|
46 | Usage example: | |
47 |
|
47 | |||
48 | with TemporaryWorkingDirectory() as tmpdir: |
|
48 | with TemporaryWorkingDirectory() as tmpdir: | |
49 | ... |
|
49 | ... | |
50 | """ |
|
50 | """ | |
|
51 | ||||
51 | def __enter__(self): |
|
52 | def __enter__(self): | |
52 | self.old_wd = Path.cwd() |
|
53 | self.old_wd = Path.cwd() | |
53 | _os.chdir(self.name) |
|
54 | _os.chdir(self.name) | |
54 | return super(TemporaryWorkingDirectory, self).__enter__() |
|
55 | return super(TemporaryWorkingDirectory, self).__enter__() | |
55 |
|
56 | |||
56 | def __exit__(self, exc, value, tb): |
|
57 | def __exit__(self, exc, value, tb): | |
57 | _os.chdir(self.old_wd) |
|
58 | _os.chdir(self.old_wd) | |
58 | return super(TemporaryWorkingDirectory, self).__exit__(exc, value, tb) |
|
59 | return super(TemporaryWorkingDirectory, self).__exit__(exc, value, tb) |
General Comments 0
You need to be logged in to leave comments.
Login now