Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py3: fix sage.libs.readline.pyx for python3 #27275

Closed
vinklein mannequin opened this issue Feb 13, 2019 · 33 comments
Closed

py3: fix sage.libs.readline.pyx for python3 #27275

vinklein mannequin opened this issue Feb 13, 2019 · 33 comments

Comments

@vinklein
Copy link
Mannequin

vinklein mannequin commented Feb 13, 2019

fix doctests failures : sage -t --long src/sage/libs/readline.pyx # 10 doctests failed

CC: @fchapoton @embray

Component: python3

Author: Vincent Klein

Branch/Commit: 7ccd2a5

Reviewer: Erik Bray, Frédéric Chapoton

Issue created by migration from https://trac.sagemath.org/ticket/27275

@vinklein vinklein mannequin added this to the sage-8.7 milestone Feb 13, 2019
@vinklein vinklein mannequin added c: python3 labels Feb 13, 2019
@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 13, 2019

Branch: u/vklein/27275

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 13, 2019

Commit: aa4cb2a

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 13, 2019

New commits:

aa4cb2aTrac #27275: Fix readline.pyx for python3

@vinklein vinklein mannequin added the s: needs review label Feb 13, 2019
@fchapoton
Copy link
Contributor

comment:3

seems ok at first sight.. Erik, do you approve ?

@embray
Copy link
Contributor

embray commented Feb 21, 2019

comment:4

I'm not sure. I don't think bytes_to_str should be used in doctests if it can be helped, but that's more of an arbitrary stylistic choice. Let me look at whether there's a nicer way.

@embray
Copy link
Contributor

embray commented Feb 21, 2019

comment:5

Couldn't we instead modify the copy_text function to call bytes_to_str on the return value? AFAICT this code isn't even used anywhere in Sage, but it would probably be nicer that way regardless.

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 21, 2019

comment:7

Replying to @embray:

Couldn't we instead modify the copy_text function to call bytes_to_str on the return value? AFAICT this code isn't even used anywhere in Sage, but it would probably be nicer that way regardless.

I will try that.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 21, 2019

Changed commit from aa4cb2a to 689f9a4

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 21, 2019

Branch pushed to git repo; I updated commit sha1. New commits:

689f9a4Trac #27275: wrap copy_text result with a bytes_to_str ...

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 21, 2019

comment:9

@embray I don't see drawback with your solution, let's try this.

@embray
Copy link
Contributor

embray commented Feb 21, 2019

comment:11

Just one other minor nit to pick (with the existing tests before this ticket):

diff --git a/src/sage/libs/readline.pyx b/src/sage/libs/readline.pyx
index e3e7671..43279a7 100644
--- a/src/sage/libs/readline.pyx
+++ b/src/sage/libs/readline.pyx
@@ -20,7 +20,8 @@ line is removed::
 
     sage: with interleaved_output():
     ....:     print('output')
-    ....:     print('current line: ' + repr(copy_text(0, get_end())))
+    ....:     print('current line: ' +
+    ....:            repr(copy_text(0, get_end())))
     ....:     print('cursor position: {}'.format(get_point()))
     output
     current line: ''

with the Python 3 print() function (which I believe we are using in most modules on Python 2 as well), there's no need for explicit string concatenation with +. The prints() in this test could be written like print('current line:', blahblahblah). I think we should promote more idiomatic code (even if in relatively minor tests that most users won't look at).

But don't bother unless you feel like it.

@embray
Copy link
Contributor

embray commented Feb 21, 2019

Reviewer: Erik Bray

@embray
Copy link
Contributor

embray commented Feb 21, 2019

comment:12

Similarly the line print('cursor position: {}'.format(get_point())) could just be written simply

print('cursor position:', get_point())

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 21, 2019

comment:13

Replying to @embray:

Similarly the line print('cursor position: {}'.format(get_point())) could just be written simply

print('cursor position:', get_point())

Oh thanks ! That's good to know.

I will make these little modifications.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 21, 2019

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. New commits:

aadc9c5Trac #27275: Some syntax enhancement.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 21, 2019

Changed commit from 689f9a4 to aadc9c5

@sagetrac-git sagetrac-git mannequin removed the s: positive review label Feb 21, 2019
@sagetrac-git sagetrac-git mannequin added the s: needs review label Feb 21, 2019
@embray
Copy link
Contributor

embray commented Feb 21, 2019

comment:15

Did you test this? When you do print(a, b) it automatically inserts spaces, so I think now you have an extra space.

@tscrim
Copy link
Collaborator

tscrim commented Feb 22, 2019

comment:16

Replying to @embray:

Did you test this? When you do print(a, b) it automatically inserts spaces, so I think now you have an extra space.

Be careful with print on Python2:

>>> print('a','b')
('a', 'b')

versus Python3:

>>> print('a', 'b')
a b

So I would not use the comma version in order to be Python2/3 compatible.

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 22, 2019

comment:17

Replying to @embray:

Did you test this? When you do print(a, b) it automatically inserts spaces, so I think now you have an extra space.

I think i have. But i am not sure anymore. And i don't understand why the last patchbot is green. I am currently doing more tests.

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 22, 2019

comment:18

Well that's pretty strange even if with python2 you get that in sage console:

sage: from sage.libs.readline import *
sage: replace_line('foobar', 0)
sage: set_point(3)
sage: print('cursor position: ', get_point())
('cursor position: ', 3)

The doctest

sage: print('cursor position: ', get_point())
cursor position: 3

still pass.

And for python3 the doctest framework doesn't seem to care if there is one or two spaces between cursor position: and 3.

Even if it pass i think it's more clear and consistent to rollback to the format syntax.

@embray
Copy link
Contributor

embray commented Feb 22, 2019

comment:19

Replying to @tscrim:

Replying to @embray:

Did you test this? When you do print(a, b) it automatically inserts spaces, so I think now you have an extra space.

Be careful with print on Python2:

>>> print('a','b')
('a', 'b')

versus Python3:

>>> print('a', 'b')
a b

So I would not use the comma version in order to be Python2/3 compatible.

We're using from future import print_function almost everywhere. If there's a module that's not using it that's a mistake.

@embray
Copy link
Contributor

embray commented Feb 22, 2019

comment:20

Replying to @vinklein:

Well that's pretty strange even if with python2 you get that in sage console:

sage: from sage.libs.readline import *
sage: replace_line('foobar', 0)
sage: set_point(3)
sage: print('cursor position: ', get_point())
('cursor position: ', 3)

The doctest

sage: print('cursor position: ', get_point())
cursor position: 3

still pass.

And for python3 the doctest framework doesn't seem to care if there is one or two spaces between cursor position: and 3.

I'm not sure why it is because that's wrong.

For the doctests we are using the Python 3 print() function where possible.

@embray
Copy link
Contributor

embray commented Feb 22, 2019

comment:21

See e.g. #23551.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 22, 2019

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 22, 2019

Changed commit from aadc9c5 to 689f9a4

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 22, 2019

comment:23

Replying to @embray:

See e.g. #23551.

Ok that explain things !
But is it a good thing to use the print('a', 'b') syntax right now ?
Travis comment and mines show that it's confusing if you are not aware of #23551.

@embray
Copy link
Contributor

embray commented Feb 22, 2019

comment:24

I think for miscellaneous doctests where it's really beside the point, I think it should be fine, and better to use future-proof best practices. It's just in tutorials and other educational documentation for beginners where one needs to be a little more careful about this (or better yet, teach the difference!)

I hope we can move toward using the print function in the REPL even on Python 2 before long...

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 22, 2019

Changed commit from 689f9a4 to 7ccd2a5

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 22, 2019

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

f21acf8Trac #27275: Fix readline.pyx for python3
a291512Trac #27275: wrap copy_text result with a bytes_to_str ...
7ccd2a5Trac #27275: Enhance doctest syntax.

@vinklein
Copy link
Mannequin Author

vinklein mannequin commented Feb 22, 2019

comment:26

Rebased on 8.7.beta4 and fix doctests with print('a', 'b') syntax with automatically inserted spaces taken into account.

@fchapoton
Copy link
Contributor

comment:27

ok, let it be.

@fchapoton
Copy link
Contributor

Changed reviewer from Erik Bray to Erik Bray, Frédéric Chapoton

@vbraun
Copy link
Member

vbraun commented Feb 23, 2019

Changed branch from u/vklein/27275 to 7ccd2a5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants