-
Notifications
You must be signed in to change notification settings - Fork 96
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this 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 :-)
builtin/builtin.go
Outdated
@@ -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__() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
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) |
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 In the future, we can add the test which capturing sys.stdout. But not this time :) |
see #24