Skip to content

Commit 46083e3

Browse files
committed
Add missing getline method on py.File
1 parent ebe6cab commit 46083e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

py/file.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ func init() {
3131
FileType.Dict["flush"] = MustNewMethod("flush", func(self Object) (Object, error) {
3232
return self.(*File).Flush()
3333
}, 0, "flush() -> Flush the write buffers of the stream if applicable. This does nothing for read-only and non-blocking streams.")
34+
FileType.Dict["readline"] = MustNewMethod("readline", func(self Object) (Object, error) {
35+
return self.(*File).Readline()
36+
}, 0, "readline() -> next line from the file, as a string.\n\nRetains newline. A non-empty string returned implies that readline() returned\na line, empty string returned implies that EOF is reached.")
3437
}
3538

3639
type FileMode int
@@ -143,6 +146,27 @@ func (o *File) Read(args Tuple, kwargs StringDict) (Object, error) {
143146
return o.readResult(b)
144147
}
145148

149+
func (o *File) Readline() (Object, error) {
150+
var buf []byte
151+
b := make([]byte, 1)
152+
for {
153+
n, err := o.File.Read(b)
154+
if n > 0 {
155+
buf = append(buf, b[0])
156+
if b[0] == '\n' {
157+
break
158+
}
159+
}
160+
if err != nil {
161+
if err == io.EOF {
162+
break
163+
}
164+
return nil, err
165+
}
166+
}
167+
return o.readResult(buf)
168+
}
169+
146170
func (o *File) Close() (Object, error) {
147171
_ = o.File.Close()
148172
return None, nil

0 commit comments

Comments
 (0)