From be956dfdae833dd193396c8d45c2c3869ff15277 Mon Sep 17 00:00:00 2001 From: "Erik M. Bray" Date: Mon, 27 Nov 2017 14:03:35 +0000 Subject: [PATCH 1/3] =?UTF-8?q?#=20Ceci=20est=20la=20combinaison=20de=202?= =?UTF-8?q?=20commits.=20#=20Ceci=20est=20le=20premier=20message=20de=20va?= =?UTF-8?q?lidation=C2=A0:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update these tests to make sense on Python 3: * Remove some "# optional - python2" tags since they don't really help to skip tests on Python 3 * Rewrite the load() tests to work on Python 3: 1) replace print statements with print function 2) the main point of some of these tests is to demonstrate when .sage format parsing is or is not taking place; to demonstrate this the tests used division as an example, but since the default '/' operator has different behavior on Python 3 this did not work, so instead we use interpretation of the '^' for our examples * Replaced more examples of 'open(...)' with 'with open(...)' to prevent ResourceWarnings from causing the tests to fail # Ceci est le message de validation numéro 2 : Fixed this code to account for how the base64 module works differently on Python 3 --- src/sage/repl/load.py | 57 ++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/sage/repl/load.py b/src/sage/repl/load.py index e6028426aef..f1591e0d64a 100644 --- a/src/sage/repl/load.py +++ b/src/sage/repl/load.py @@ -16,6 +16,8 @@ import os import base64 +from sage.cpython.string import str_to_bytes, bytes_to_str, FS_ENCODING + def is_loadable_filename(filename): """ Returns whether a file can be loaded into Sage. This checks only @@ -96,33 +98,36 @@ def load(filename, globals, attach=False): Note that ``.py`` files are *not* preparsed:: sage: t = tmp_filename(ext='.py') - sage: _ = open(t,'w').write("print 'hi', 2/3; z = -2/7") + sage: with open(t, 'w') as f: + ....: _ = f.write("print(('hi', 2^3)); z = -2^7") sage: z = 1 - sage: sage.repl.load.load(t, globals()) # py2 - hi 0 - sage: z # py2 - -1 + sage: sage.repl.load.load(t, globals()) + ('hi', 1) + sage: z + -7 A ``.sage`` file *is* preparsed:: sage: t = tmp_filename(ext='.sage') - sage: _ = open(t,'w').write("print 'hi', 2/3; z = -2/7") + sage: with open(t, 'w') as f: + ....: _ = f.write("print(('hi', 2^3)); z = -2^7") sage: z = 1 sage: sage.repl.load.load(t, globals()) - hi 2/3 + ('hi', 8) sage: z - -2/7 + -128 Cython files are *not* preparsed:: sage: t = tmp_filename(ext='.pyx') - sage: _ = open(t,'w').write("print 'hi', 2/3; z = -2/7") + sage: with open(t, 'w') as f: + ....: _ = f.write("print(('hi', 2^3)); z = -2^7") sage: z = 1 sage: sage.repl.load.load(t, globals()) Compiling ... - hi 0 + ('hi', 1) sage: z - -1 + -7 If the file isn't a Cython, Python, or a Sage file, a ValueError is raised:: @@ -145,7 +150,8 @@ def load(filename, globals, attach=False): We attach a file:: sage: t = tmp_filename(ext='.py') - sage: _ = open(t,'w').write("print 'hello world'") + sage: with open(t, 'w') as f: + ....: _ = f.write("print('hello world')") sage: sage.repl.load.load(t, globals(), attach=True) hello world sage: t in attached_files() @@ -166,10 +172,12 @@ def load(filename, globals, attach=False): sage: load_attach_path() ['.'] sage: t_dir = tmp_dir() - sage: fullpath = os.path.join(t_dir, 'test.py') - sage: _ = open(fullpath, 'w').write("print 37 * 3") + sage: fname = tmp_filename(ext='.py') + sage: fullpath = os.path.join(t_dir, fname) + sage: with open(fullpath, 'w') as f: + ....: _ = f.write("print(37 * 3)") sage: load_attach_path(t_dir) - sage: attach('test.py') + sage: attach(fname) 111 sage: sage.repl.attach.reset(); reset_load_attach_path() # clean up @@ -185,7 +193,9 @@ def load(filename, globals, attach=False): Make sure that load handles filenames with spaces in the name or path:: - sage: t = tmp_filename(ext=' b.sage'); _ = open(t,'w').write("print 2") + sage: t = tmp_filename(ext=' b.sage') + sage: with open(t, 'w') as f: + ....: _ = f.write("print(2)") sage: sage.repl.load.load(t, globals()) 2 @@ -244,7 +254,8 @@ def load(filename, globals, attach=False): # Preparse in memory only for speed. if attach: add_attached_file(fpath) - exec(preparse_file(open(fpath).read()) + "\n", globals) + with open(fpath) as f: + exec(preparse_file(f.read()) + "\n", globals) elif ext == '.spyx' or ext == '.pyx': if attach: add_attached_file(fpath) @@ -286,8 +297,14 @@ def load_wrap(filename, attach=False): 'sage.repl.load.load(sage.repl.load.base64.b64decode("Zm9vLnB5"),globals(),True)' sage: sage.repl.load.load_wrap('foo.sage') 'sage.repl.load.load(sage.repl.load.base64.b64decode("Zm9vLnNhZ2U="),globals(),False)' - sage: sage.repl.load.base64.b64decode("Zm9vLnNhZ2U=") - 'foo.sage' + sage: m = sage.repl.load.base64.b64decode("Zm9vLnNhZ2U=") + sage: m == b'foo.sage' + True """ + + # Note: On Python 3 b64encode only accepts bytes, and returns bytes (yet + # b64decode does accept str, but always returns bytes) + b64 = base64.b64encode(str_to_bytes(filename, FS_ENCODING, + "surrogateescape")) return 'sage.repl.load.load(sage.repl.load.base64.b64decode("{}"),globals(),{})'.format( - base64.b64encode(filename), attach) + bytes_to_str(b64, 'ascii'), attach) From 040cc3ff694bfc05ee9a7dff0b14025642335204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Mon, 12 Feb 2018 17:56:28 +0100 Subject: [PATCH 2/3] trac 24286 fixing doctest --- src/sage/repl/attach.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/repl/attach.py b/src/sage/repl/attach.py index f3a33c47e42..f8935a42189 100644 --- a/src/sage/repl/attach.py +++ b/src/sage/repl/attach.py @@ -39,7 +39,7 @@ ....: traceback.print_exc() Traceback (most recent call last): ... - exec(preparse_file(open(fpath).read()) + "\n", globals) + exec(preparse_file(f.read()) + "\n", globals) File "", line 3, in ValueError: third sage: detach(src) From ede1402a8ec9f4a2012584b3017d54e1c925e692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 13 Feb 2018 11:49:19 +0100 Subject: [PATCH 3/3] trac 24286 detail --- src/sage/repl/load.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/repl/load.py b/src/sage/repl/load.py index f1591e0d64a..f2abb04e358 100644 --- a/src/sage/repl/load.py +++ b/src/sage/repl/load.py @@ -172,7 +172,7 @@ def load(filename, globals, attach=False): sage: load_attach_path() ['.'] sage: t_dir = tmp_dir() - sage: fname = tmp_filename(ext='.py') + sage: fname = 'test.py' sage: fullpath = os.path.join(t_dir, fname) sage: with open(fullpath, 'w') as f: ....: _ = f.write("print(37 * 3)")