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