Skip to content

Commit

Permalink
Trac #16086: Python 3 preparation: Py2 vs. Py3 return value of write(…
Browse files Browse the repository at this point in the history
…) in doctests

The write() function does return nothing in Py2
{{{
>>> sys.version
'2.7.5+ (default, Feb 27 2014, 19:37:08) \n[GCC 4.8.1]'
>>> with open("tracked", "w") as f: f.write("boo")
...
>>>
}}}
while in Py3 it returns the number of bytes written
{{{
>>> sys.version
'3.3.2+ (default, Feb 28 2014, 00:52:16) \n[GCC 4.8.1]'
>>> with open("tracked", "w") as f: f.write("boo")
...
3
>>>
}}}
This can be unified by adding something like {{{ignore =}}}
{{{
>>> sys.version
'3.3.2+ (default, Feb 28 2014, 00:52:16) \n[GCC 4.8.1]'
>>> with open("tracked", "w") as f: ignore = f.write("boo")
...
>>>
}}}
About 79 py module are effected.

This ticket is tracked as a dependency of meta-ticket ticket:16052.

URL: https://trac.sagemath.org/16086
Reported by: wluebbe
Ticket author(s): John Palmieri
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Apr 25, 2017
2 parents aec9477 + 0234816 commit fa8ec79
Show file tree
Hide file tree
Showing 35 changed files with 109 additions and 109 deletions.
6 changes: 3 additions & 3 deletions src/sage/doctest/control.py
Expand Up @@ -170,7 +170,7 @@ def skipfile(filename):
sage: f = tmp_filename(ext=".pyx")
sage: skipfile(f)
False
sage: open(f, "w").write("# nodoctest")
sage: _ = open(f, "w").write("# nodoctest")
sage: skipfile(f)
True
"""
Expand Down Expand Up @@ -198,7 +198,7 @@ class Logger(object):
sage: from sage.doctest.control import Logger
sage: t = open(tmp_filename(), "w+")
sage: L = Logger(sys.stdout, t)
sage: L.write("hello world\n")
sage: _ = L.write("hello world\n")
hello world
sage: t.seek(0)
sage: t.read()
Expand Down Expand Up @@ -707,7 +707,7 @@ def expand_files_into_sources(self):
sage: dirname = tmp_dir()
sage: filename = os.path.join(dirname, 'not_tested.py')
sage: with open(filename, 'w') as F:
....: F.write("#"*80 + "\n\n\n\n## nodoctest\n sage: 1+1\n 4")
....: _ = F.write("#"*80 + "\n\n\n\n## nodoctest\n sage: 1+1\n 4")
sage: DC = DocTestController(DD, [dirname])
sage: DC.expand_files_into_sources()
sage: DC.sources
Expand Down
2 changes: 1 addition & 1 deletion src/sage/doctest/sources.py
Expand Up @@ -509,7 +509,7 @@ def __iter__(self):
sage: from sage.doctest.sources import FileDocTestSource
sage: filename = tmp_filename(ext=".py")
sage: s = "'''\n sage: 2 + 2\n 4\n'''"
sage: open(filename, 'w').write(s)
sage: _ = open(filename, 'w').write(s)
sage: FDS = FileDocTestSource(filename, DocTestDefaults())
sage: for n, line in FDS:
....: print("{} {}".format(n, line))
Expand Down
4 changes: 2 additions & 2 deletions src/sage/finance/stock.py
Expand Up @@ -521,7 +521,7 @@ def load_from_file(self, file):
object like so. Note that the path must be explicit::
sage: filename = tmp_filename(ext='.csv')
sage: open(filename,'w').write("Date,Open,High,Low,Close,Volume\n1212405780,187.80,187.80,187.80,187.80,100\n1212407640,187.75,188.00,187.75,188.00,2000\n1212407700,188.00,188.00,188.00,188.00,1000\n1212408000,188.00,188.11,188.00,188.00,2877\n1212408060,188.00,188.00,188.00,188.00,687")
sage: _ = open(filename,'w').write("Date,Open,High,Low,Close,Volume\n1212405780,187.80,187.80,187.80,187.80,100\n1212407640,187.75,188.00,187.75,188.00,2000\n1212407700,188.00,188.00,188.00,188.00,1000\n1212408000,188.00,188.11,188.00,188.00,2877\n1212408060,188.00,188.00,188.00,188.00,687")
sage: finance.Stock('aapl').load_from_file(filename)[:5]
[
1212408060 188.00 188.00 188.00 188.00 687,
Expand Down Expand Up @@ -568,7 +568,7 @@ def _load_from_csv(self, R):
This indirectly tests ``_load_from_csv()``::
sage: filename = tmp_filename(ext='.csv')
sage: open(filename,'w').write("Date,Open,High,Low,Close,Volume\n1212405780,187.80,187.80,187.80,187.80,100\n1212407640,187.75,188.00,187.75,188.00,2000\n1212407700,188.00,188.00,188.00,188.00,1000\n1212408000,188.00,188.11,188.00,188.00,2877\n1212408060,188.00,188.00,188.00,188.00,687")
sage: _ = open(filename,'w').write("Date,Open,High,Low,Close,Volume\n1212405780,187.80,187.80,187.80,187.80,100\n1212407640,187.75,188.00,187.75,188.00,2000\n1212407700,188.00,188.00,188.00,188.00,1000\n1212408000,188.00,188.11,188.00,188.00,2877\n1212408060,188.00,188.00,188.00,188.00,687")
sage: finance.Stock('aapl').load_from_file(filename)
[
1212408060 188.00 188.00 188.00 188.00 687,
Expand Down
8 changes: 4 additions & 4 deletions src/sage/game_theory/parser.py
Expand Up @@ -143,9 +143,9 @@ def format_lrs(self):
sage: g2_name = tmp_filename()
sage: g1_file = open(g1_name, 'w')
sage: g2_file = open(g2_name, 'w')
sage: g1_file.write(game1_str)
sage: _ = g1_file.write(game1_str)
sage: g1_file.close()
sage: g2_file.write(game2_str)
sage: _ = g2_file.write(game2_str)
sage: g2_file.close()
sage: process = Popen(['lrsnash', g1_name, g2_name], stdout=PIPE, stderr=PIPE) # optional - lrslib
sage: lrs_output = [row for row in process.stdout] # optional - lrslib
Expand Down Expand Up @@ -188,9 +188,9 @@ def format_lrs(self):
sage: g2_name = tmp_filename()
sage: g1_file = open(g1_name, 'w')
sage: g2_file = open(g2_name, 'w')
sage: g1_file.write(game1_str)
sage: _ = g1_file.write(game1_str)
sage: g1_file.close()
sage: g2_file.write(game2_str)
sage: _ = g2_file.write(game2_str)
sage: g2_file.close()
sage: process = Popen(['lrsnash', g1_name, g2_name], stdout=PIPE, stderr=PIPE) # optional - lrslib
sage: lrs_output = [row for row in process.stdout] # optional - lrslib
Expand Down
12 changes: 6 additions & 6 deletions src/sage/geometry/polyhedron/plot.py
Expand Up @@ -1315,7 +1315,7 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=2,
[x={(-0.939161cm, 0.244762cm)},
y={(0.097442cm, -0.482887cm)},
z={(0.329367cm, 0.840780cm)},
sage: open('polytope-tikz1.tex', 'w').write(Image1) # not tested
sage: _ = open('polytope-tikz1.tex', 'w').write(Image1) # not tested
sage: P2 = Polyhedron(vertices=[[1, 1],[1, 2],[2, 1]])
sage: Image2 = P2.projection().tikz(scale=3, edge_color='blue!95!black', facet_color='orange!95!black', opacity=0.4, vertex_color='yellow', axis=True)
Expand All @@ -1326,7 +1326,7 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=2,
[scale=3.000000,
back/.style={loosely dotted, thin},
edge/.style={color=blue!95!black, thick},
sage: open('polytope-tikz2.tex', 'w').write(Image2) # not tested
sage: _ = open('polytope-tikz2.tex', 'w').write(Image2) # not tested
sage: P3 = Polyhedron(vertices=[[-1, -1, 2],[-1, 2, -1],[2, -1, -1]])
sage: P3
Expand All @@ -1337,7 +1337,7 @@ def tikz(self, view=[0, 0, 1], angle=0, scale=2,
[x={(0.658184cm, -0.242192cm)},
y={(-0.096240cm, 0.912008cm)},
z={(-0.746680cm, -0.331036cm)},
sage: open('polytope-tikz3.tex', 'w').write(Image3) # not tested
sage: _ = open('polytope-tikz3.tex', 'w').write(Image3) # not tested
sage: P=Polyhedron(vertices=[[1,1,0,0],[1,2,0,0],[2,1,0,0],[0,0,1,0],[0,0,0,1]])
sage: P
Expand Down Expand Up @@ -1408,7 +1408,7 @@ def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis):
[scale=3.000000,
back/.style={loosely dotted, thin},
edge/.style={color=black, thick},
sage: open('polytope-tikz2.tex', 'w').write(Image) # not tested
sage: _ = open('polytope-tikz2.tex', 'w').write(Image) # not tested
Scientific notation is not used in the output (:trac:`16519`)::
Expand Down Expand Up @@ -1526,7 +1526,7 @@ def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color,
[x={(0.644647cm, -0.476559cm)},
y={(0.192276cm, 0.857859cm)},
z={(-0.739905cm, -0.192276cm)},
sage: open('polytope-tikz3.tex', 'w').write(Image) # not tested
sage: _ open('polytope-tikz3.tex', 'w').write(Image) # not tested
sage: p = Polyhedron(vertices=[[1,0,0],[0,1,0],[0,0,1]])
sage: proj = p.projection()
Expand Down Expand Up @@ -1654,7 +1654,7 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
[x={(-0.046385cm, 0.837431cm)},
y={(-0.243536cm, 0.519228cm)},
z={(0.968782cm, 0.170622cm)},
sage: open('polytope-tikz1.tex', 'w').write(Image) # not tested
sage: _ = open('polytope-tikz1.tex', 'w').write(Image) # not tested
sage: Associahedron = Polyhedron(vertices=[[1,0,1],[1,0,0],[1,1,0],[0,0,-1],[0,1,0],[-1,0,0],[0,1,1],[0,0,1],[0,-1,0]]).polar()
sage: ImageAsso = Associahedron.projection().tikz([-15,-755,-655], 116, scale=1)
Expand Down
16 changes: 8 additions & 8 deletions src/sage/graphs/bipartite_graph.py
Expand Up @@ -207,10 +207,10 @@ class BipartiteGraph(Graph):
sage: file_name = os.path.join(SAGE_TMP, 'deleteme.alist.txt')
sage: fi = open(file_name, 'w')
sage: fi.write("7 4 \n 3 4 \n 3 3 1 3 1 1 1 \n 3 3 3 4 \n\
1 2 4 \n 1 3 4 \n 1 0 0 \n 2 3 4 \n\
2 0 0 \n 3 0 0 \n 4 0 0 \n\
1 2 3 0 \n 1 4 5 0 \n 2 4 6 0 \n 1 2 4 7 \n")
sage: _ = fi.write("7 4 \n 3 4 \n 3 3 1 3 1 1 1 \n 3 3 3 4 \n\
1 2 4 \n 1 3 4 \n 1 0 0 \n 2 3 4 \n\
2 0 0 \n 3 0 0 \n 4 0 0 \n\
1 2 3 0 \n 1 4 5 0 \n 2 4 6 0 \n 1 2 4 7 \n")
sage: fi.close();
sage: B = BipartiteGraph(file_name)
sage: B == H
Expand Down Expand Up @@ -998,10 +998,10 @@ def load_afile(self, fname):
sage: file_name = os.path.join(SAGE_TMP, 'deleteme.alist.txt')
sage: fi = open(file_name, 'w')
sage: fi.write("7 4 \n 3 4 \n 3 3 1 3 1 1 1 \n 3 3 3 4 \n\
1 2 4 \n 1 3 4 \n 1 0 0 \n 2 3 4 \n\
2 0 0 \n 3 0 0 \n 4 0 0 \n\
1 2 3 0 \n 1 4 5 0 \n 2 4 6 0 \n 1 2 4 7 \n")
sage: _ = fi.write("7 4 \n 3 4 \n 3 3 1 3 1 1 1 \n 3 3 3 4 \n\
1 2 4 \n 1 3 4 \n 1 0 0 \n 2 3 4 \n\
2 0 0 \n 3 0 0 \n 4 0 0 \n\
1 2 3 0 \n 1 4 5 0 \n 2 4 6 0 \n 1 2 4 7 \n")
sage: fi.close();
sage: B = BipartiteGraph()
sage: B.load_afile(file_name)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/graphs/generic_graph.py
Expand Up @@ -19659,7 +19659,7 @@ def graphviz_to_file_named(self, filename, **options):
node_2 -- node_3 [label="foo"];
}
"""
return open(filename, 'wt').write(self.graphviz_string(**options))
open(filename, 'wt').write(self.graphviz_string(**options))

### Spectrum

Expand Down
4 changes: 2 additions & 2 deletions src/sage/graphs/graph_generators.py
Expand Up @@ -1048,9 +1048,9 @@ def _read_planar_code(self, code_input):
sage: from six import StringIO
sage: code_input = StringIO('>>planar_code<<')
sage: code_input.write('>>planar_code<<')
sage: _ = code_input.write('>>planar_code<<')
sage: for c in [4,2,3,4,0,1,4,3,0,1,2,4,0,1,3,2,0]:
....: code_input.write('{:c}'.format(c))
....: _ = code_input.write('{:c}'.format(c))
sage: code_input.seek(0)
sage: gen = graphs._read_planar_code(code_input)
sage: l = list(gen)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/axiom.py
Expand Up @@ -262,7 +262,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename(ext='.input')
sage: f = open(filename, 'w')
sage: f.write('xx := 22;\n')
sage: _ = f.write('xx := 22;\n')
sage: f.close()
sage: axiom.read(filename) # optional - axiom
sage: axiom.get('xx') # optional - axiom
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gap.py
Expand Up @@ -477,7 +477,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('xx := 22;\n')
sage: _ = f.write('xx := 22;\n')
sage: f.close()
sage: gap.read(filename)
sage: gap.get('xx').strip()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/giac.py
Expand Up @@ -372,7 +372,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename,'w')
sage: f.write('xx := 22;\n')
sage: _ = f.write('xx := 22;\n')
sage: f.close()
sage: giac.read(filename)
sage: giac.get('xx').strip()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gp.py
Expand Up @@ -328,7 +328,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('x = 22;\n')
sage: _ = f.write('x = 22;\n')
sage: f.close()
sage: gp.read(filename)
sage: gp.get('x').strip()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/interface.py
Expand Up @@ -185,7 +185,7 @@ def read(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('x = 2\n')
sage: _ = f.write('x = 2\n')
sage: f.close()
sage: octave.read(filename) # optional - octave
sage: octave.get('x') # optional - octave
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/lie.py
Expand Up @@ -554,7 +554,7 @@ def read(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('x = 2\n')
sage: _ = f.write('x = 2\n')
sage: f.close()
sage: lie.read(filename) # optional - lie
sage: lie.get('x') # optional - lie
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/macaulay2.py
Expand Up @@ -231,7 +231,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, "w")
sage: f.write("sage_test = 7;")
sage: _ = f.write("sage_test = 7;")
sage: f.close()
sage: command = macaulay2._read_in_file_command(filename)
sage: macaulay2.eval(command) # optional - macaulay2
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/magma.py
Expand Up @@ -1044,7 +1044,7 @@ def load(self, filename):
EXAMPLES::
sage: filename = os.path.join(SAGE_TMP, 'a.m')
sage: open(filename, 'w').write('function f(n) return n^2; end function;\nprint "hi";')
sage: _ = open(filename, 'w').write('function f(n) return n^2; end function;\nprint "hi";')
sage: print(magma.load(filename)) # optional - magma
Loading ".../a.m"
hi
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/maple.py
Expand Up @@ -360,7 +360,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename() # optional - maple
sage: f = open(filename, 'w') # optional - maple
sage: f.write('xx := 22;\n') # optional - maple
sage: _ = f.write('xx := 22;\n') # optional - maple
sage: f.close() # optional - maple
sage: maple.read(filename) # optional - maple
sage: maple.get('xx').strip() # optional - maple
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/mupad.py
Expand Up @@ -166,7 +166,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('x := 2;\n')
sage: _ = f.write('x := 2;\n')
sage: f.close()
sage: mupad.read(filename) # optional - MuPAD
sage: mupad.get('x').strip() # optional - mupad
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/r.py
Expand Up @@ -567,7 +567,7 @@ def read(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('a <- 2+2\n')
sage: _ = f.write('a <- 2+2\n')
sage: f.close()
sage: r.read(filename)
sage: r.get('a')
Expand Down
4 changes: 2 additions & 2 deletions src/sage/interfaces/read_data.py
Expand Up @@ -28,12 +28,12 @@ def read_data(f,t):
sage: indata = tmp_filename()
sage: f = open(indata, "w")
sage: f.write("17\n42\n")
sage: _ = f.write("17\n42\n")
sage: f.close()
sage: l = read_data(indata, ZZ); l
[17, 42]
sage: f = open(indata, "w")
sage: f.write("1.234\n5.678\n")
sage: _ = f.write("1.234\n5.678\n")
sage: f.close()
sage: l = read_data(indata, RealField(17)); l
[1.234, 5.678]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/singular.py
Expand Up @@ -538,7 +538,7 @@ def _read_in_file_command(self, filename):
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('int x = 2;\n')
sage: _ = f.write('int x = 2;\n')
sage: f.close()
sage: singular.read(filename)
sage: singular.get('x')
Expand Down
2 changes: 1 addition & 1 deletion src/sage/libs/eclib/mwrank.pyx
Expand Up @@ -128,7 +128,7 @@ def initprimes(filename, verb=False):
EXAMPLES::
sage: file = os.path.join(SAGE_TMP, 'PRIMES')
sage: open(file,'w').write(' '.join([str(p) for p in prime_range(10^7,10^7+20)]))
sage: _ = open(file,'w').write(' '.join([str(p) for p in prime_range(10^7,10^7+20)]))
sage: mwrank_initprimes(file, verb=True)
Computed 78519 primes, largest is 1000253
reading primes from file ...
Expand Down
8 changes: 4 additions & 4 deletions src/sage/libs/pari/tests.py
Expand Up @@ -181,10 +181,10 @@
sage: import tempfile
sage: gpfile = tempfile.NamedTemporaryFile(mode="w")
sage: gpfile.file.write("mysquare(n) = {\n")
sage: gpfile.file.write(" n^2;\n")
sage: gpfile.file.write("}\n")
sage: gpfile.file.write("polcyclo(5)\n")
sage: __ = gpfile.file.write("mysquare(n) = {\n")
sage: __ = gpfile.file.write(" n^2;\n")
sage: __ = gpfile.file.write("}\n")
sage: __ = gpfile.file.write("polcyclo(5)\n")
sage: gpfile.file.flush()
sage: pari.read(gpfile.name)
x^4 + x^3 + x^2 + x + 1
Expand Down
4 changes: 2 additions & 2 deletions src/sage/misc/cython.py
Expand Up @@ -381,7 +381,7 @@ def cython(filename, verbose=False, compile_message=False,
sage: d = sage.misc.temporary_file.tmp_dir()
sage: os.chdir(d)
sage: with open("test.pyx", 'w') as f:
....: f.write("#clang C++\n"
....: _ = f.write("#clang C++\n"
....: "from libcpp.vector cimport vector\n"
....: "cdef vector[int] * v = new vector[int](4)\n")
sage: output = sage.misc.cython.cython("test.pyx", create_local_c_file=True)
Expand Down Expand Up @@ -721,7 +721,7 @@ def cython_create_local_so(filename):
sage: dir = tmp_dir(); os.chdir(dir)
sage: f = open('hello.spyx', 'w')
sage: s = "def hello():\n print('hello')\n"
sage: f.write(s)
sage: _ = f.write(s)
sage: f.close()
sage: cython_create_local_so('hello.spyx')
Compiling hello.spyx...
Expand Down
4 changes: 2 additions & 2 deletions src/sage/misc/latex.py
Expand Up @@ -690,7 +690,7 @@ def _run_latex_(filename, debug=False, density=150, engine=None, png=False, do_i
sage: from sage.misc.latex import _run_latex_, _latex_file_
sage: file = os.path.join(SAGE_TMP, "temp.tex")
sage: O = open(file, 'w')
sage: O.write(_latex_file_([ZZ['x'], RR])); O.close()
sage: _ = O.write(_latex_file_([ZZ['x'], RR])); O.close()
sage: _run_latex_(file) # random - depends on whether latex is installed
'dvi'
"""
Expand Down Expand Up @@ -2150,7 +2150,7 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
sage: g = sage.misc.latex.latex_examples.graph()
sage: latex.add_to_preamble(r"\usepackage{tkz-graph}")
sage: file = os.path.join(SAGE_TMP, "temp.tex")
sage: O = open(file, 'w'); O.write(_latex_file_(g)); O.close()
sage: O = open(file, 'w'); _ = O.write(_latex_file_(g)); O.close()
sage: _run_latex_(file, engine="pdflatex") # optional - latex
'pdf'
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/reset.pyx
Expand Up @@ -39,7 +39,7 @@ def reset(vars=None, attached=False):
sage: fn = tmp_filename(ext='foo.py')
sage: sage.misc.reset.EXCLUDE.add('fn')
sage: open(fn, 'w').write('a = 111')
sage: _ = open(fn, 'w').write('a = 111')
sage: attach(fn)
sage: [fn] == attached_files()
True
Expand Down

0 comments on commit fa8ec79

Please sign in to comment.