FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

print should use __str__ or __repr__ when available by raff · Pull Request #26 · go-python/gpython · GitHub

print should use __str__ or __repr__ when available - #26

Merged
ncw merged 1 commit into
go-python:masterfrom
raff:print__str__
Sep 8, 2018
Merged

print should use __str__ or __repr__ when available#26
ncw merged 1 commit into
go-python:masterfrom
raff:print__str__

Conversation

raff commented Sep 7, 2018

Copy link
Copy Markdown
Contributor

see #24

Copy link
Copy Markdown

Codecov Report

Merging #26 into master will decrease coverage by 0.03%.
The diff coverage is 0%.

@@            Coverage Diff             @@
##           master      #26      +/-   ##
==========================================
- Coverage   64.59%   64.56%   -0.04%     
==========================================
  Files          55       55              
  Lines        9997    10002       +5     
==========================================
  Hits         6458     6458              
- Misses       3079     3084       +5     
  Partials      460      460
Impacted Files Coverage Δ
builtin/builtin.go 74.45% <0%> (-0.92%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e7b5ec...afa0c4f. Read the comment docs.

ncw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

See inline for notes.

Can you do some tests (in python) in builtin/tests/builtin.py too please?

A useful fix - thank you :-)

Comment thread builtin/builtin.go Outdated
for i, v := range args {
switch sv := v.(type) {
case py.I__str__:
v, _ = sv.M__str__()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This isn't quite right - this needs to call py.Str which will fall back to py.Repr

If you only look for the M__str__ then you'll miss the user defined methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This works with this test:

class X(object):
  def __init__(self, x):
    self.x = x
  def __str__(self):
    return "my name is %s" % self.x

class Y(object):
  def __init__(self, x):
    self.x = x

  def __repr__(self):
    return "my nome is %s" % self.x

print(X(42))
print(Y('hello'))

Note that I cannot add any test to builtin/tests/builtin.py because there is no way to check the result of print (until support for redirects is implemented)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

re-implemented with py.Str

raff commented Sep 8, 2018

Copy link
Copy Markdown
Contributor Author

I have updated with py.Str

Can't really add any test builtin/tests/builtin.py right now, since there is no way to test the result of "print" (but I have some work in progress to support print to file)

ncw commented Sep 8, 2018

Copy link
Copy Markdown
Collaborator

I see what you mean about the tests. I'll merge this as it is a definite improvement thanks :-)

Some tests would be nice at some point!

ncw merged commit ee952c8 into go-python:master Sep 8, 2018

corona10 commented Sep 8, 2018
edited
Loading

Copy link
Copy Markdown
Collaborator

@ncw In the future, we can add the test which capturing sys.stdout. But not this time :)

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL