Skip to content

print should use __str__ or __repr__ when available #26

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

Merged
merged 1 commit into from
Sep 8, 2018

Conversation

raff
Copy link
Contributor

@raff raff commented Sep 7, 2018

see #24

@codecov-io
Copy link

Codecov Report

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

Impacted file tree graph

@@            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.

Copy link
Collaborator

@ncw ncw left a comment

Choose a reason for hiding this comment

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

See inline for notes.

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

A useful fix - thank you :-)

@@ -189,6 +189,14 @@ func builtin_print(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Obje
end := endObj.(py.String)
// FIXME ignoring file and flush
for i, v := range args {
switch sv := v.(type) {
case py.I__str__:
v, _ = sv.M__str__()
Copy link
Collaborator

Choose a reason for hiding this comment

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

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
Contributor Author

Choose a reason for hiding this comment

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

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
Contributor Author

Choose a reason for hiding this comment

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

re-implemented with py.Str

@raff
Copy link
Contributor Author

raff commented Sep 8, 2018

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
Copy link
Collaborator

ncw commented Sep 8, 2018

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 ncw merged commit ee952c8 into go-python:master Sep 8, 2018
@corona10
Copy link
Collaborator

corona10 commented Sep 8, 2018

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

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