MCPcopy Create free account
hub / github.com/numpy/numpy / test_py2_float_print

Method test_py2_float_print

numpy/core/tests/test_scalarprint.py:50–92  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

48 check(1e16)
49
50 def test_py2_float_print(self):
51 # gh-10753
52 # In python2, the python float type implements an obsolete method
53 # tp_print, which overrides tp_repr and tp_str when using "print" to
54 # output to a "real file" (ie, not a StringIO). Make sure we don't
55 # inherit it.
56 x = np.double(0.1999999999999)
57 with TemporaryFile('r+t') as f:
58 print(x, file=f)
59 f.seek(0)
60 output = f.read()
61 assert_equal(output, str(x) + '\n')
62 # In python2 the value float('0.1999999999999') prints with reduced
63 # precision as '0.2', but we want numpy's np.double('0.1999999999999')
64 # to print the unique value, '0.1999999999999'.
65
66 # gh-11031
67 # Only in the python2 interactive shell and when stdout is a "real"
68 # file, the output of the last command is printed to stdout without
69 # Py_PRINT_RAW (unlike the print statement) so `>>> x` and `>>> print
70 # x` are potentially different. Make sure they are the same. The only
71 # way I found to get prompt-like output is using an actual prompt from
72 # the 'code' module. Again, must use tempfile to get a "real" file.
73
74 # dummy user-input which enters one line and then ctrl-Ds.
75 def userinput():
76 yield 'np.sqrt(2)'
77 raise EOFError
78 gen = userinput()
79 input_func = lambda prompt="": next(gen)
80
81 with TemporaryFile('r+t') as fo, TemporaryFile('r+t') as fe:
82 orig_stdout, orig_stderr = sys.stdout, sys.stderr
83 sys.stdout, sys.stderr = fo, fe
84
85 code.interact(local={'np': np}, readfunc=input_func, banner='')
86
87 sys.stdout, sys.stderr = orig_stdout, orig_stderr
88
89 fo.seek(0)
90 capture = fo.read().strip()
91
92 assert_equal(capture, repr(np.sqrt(2)))
93
94 def test_dragon4(self):
95 # these tests are adapted from Ryan Juckett's dragon4 implementation,

Callers

nothing calls this directly

Calls 7

assert_equalFunction · 0.90
printFunction · 0.85
doubleMethod · 0.80
seekMethod · 0.80
stripMethod · 0.80
nextFunction · 0.50
readMethod · 0.45

Tested by

no test coverage detected