Skip to content

Commit

Permalink
remove abort usage
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Sep 13, 2024
1 parent c253146 commit 8ce56e8
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - yyyy-mm-dd

## [0.1.9] - 2024-09-13

- Fix usage of abort instead of panic.

## [0.1.8] - 2024-09-13

- Lot's of changes since Mojo 24.5. Sorry, I don't have a more granualar changelog!
Expand Down
2 changes: 1 addition & 1 deletion mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels = ["conda-forge", "https://conda.modular.com/max"]
description = "Experiments in porting over Golang stdlib into Mojo."
name = "gojo"
platforms = ["osx-arm64", "linux-64"]
version = "0.1.8"
version = "0.1.9"

[tasks]
tests = "bash scripts/tests.sh"
Expand Down
12 changes: 12 additions & 0 deletions src/gojo/__init__.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from sys import exit


fn panic[T: Stringable](message: T, code: Int = 1):
"""Panics the program with the given message and exit code.
Args:
message: The message to panic with.
code: The exit code to panic with.
"""
print("panic:", str(message))
exit(code)
12 changes: 6 additions & 6 deletions src/gojo/bufio/reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct Reader[R: io.Reader, //](Sized, io.Reader, io.ByteReader, io.ByteScanner,
# Compares to the capacity of the internal buffer.
# IE. var b = List[UInt8, True](capacity=4096), then trying to write at b[4096] and onwards will fail.
if self.write_pos >= self.buf.capacity:
abort("bufio.Reader: tried to fill full buffer")
panic("bufio.Reader: tried to fill full buffer")

# Read new data: try a limited number of times.
var i: Int = MAX_CONSECUTIVE_EMPTY_READS
Expand All @@ -152,7 +152,7 @@ struct Reader[R: io.Reader, //](Sized, io.Reader, io.ByteReader, io.ByteScanner,
var err: Error
bytes_read, err = self.reader._read(dest_ptr, self.buf.capacity - self.buf.size)
if bytes_read < 0:
abort(ERR_NEGATIVE_READ)
panic(ERR_NEGATIVE_READ)

self.buf.size += bytes_read
self.write_pos += bytes_read
Expand Down Expand Up @@ -283,7 +283,7 @@ struct Reader[R: io.Reader, //](Sized, io.Reader, io.ByteReader, io.ByteScanner,
bytes_read, self.err = self.reader._read(dest, capacity)

if bytes_read < 0:
abort(ERR_NEGATIVE_READ)
panic(ERR_NEGATIVE_READ)

if bytes_read > 0:
self.last_byte = int(dest[bytes_read - 1])
Expand All @@ -300,7 +300,7 @@ struct Reader[R: io.Reader, //](Sized, io.Reader, io.ByteReader, io.ByteScanner,
bytes_read, self.err = self.reader._read(buf, self.buf.capacity - self.buf.size)

if bytes_read < 0:
abort(ERR_NEGATIVE_READ)
panic(ERR_NEGATIVE_READ)

if bytes_read == 0:
return 0, self.read_error()
Expand Down Expand Up @@ -498,7 +498,7 @@ struct Reader[R: io.Reader, //](Sized, io.Reader, io.ByteReader, io.ByteScanner,
# Let the next call to read_line check for "\r\n".
if self.read_pos == 0:
# should be unreachable
abort("bufio: tried to rewind past start of buffer")
panic("bufio: tried to rewind past start of buffer")

self.read_pos -= 1
line = line[: len(line) - 1]
Expand Down Expand Up @@ -672,7 +672,7 @@ struct Reader[R: io.Reader, //](Sized, io.Reader, io.ByteReader, io.ByteScanner,
return bytes_written, err

if bytes_written < 0:
abort(ERR_NEGATIVE_WRITE)
panic(ERR_NEGATIVE_WRITE)

self.read_pos += bytes_written
return Int(bytes_written), Error()
2 changes: 1 addition & 1 deletion src/gojo/bufio/scan.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ struct Scanner[R: io.Reader, //, split: SplitFunction = scan_lines]():
# Returning tokens not advancing input at EOF.
self.empties += 1
if self.empties > MAX_CONSECUTIVE_EMPTY_READS:
abort("bufio.Scan: too many empty tokens without progressing")
panic("bufio.Scan: too many empty tokens without progressing")

return True

Expand Down
2 changes: 1 addition & 1 deletion src/gojo/bytes/buffer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ struct Buffer(
var err: Error
bytes_written, err = writer.write(self.as_bytes_slice()[self.offset :])
if bytes_written > bytes_to_write:
abort("bytes.Buffer.write_to: invalid write count")
panic("bytes.Buffer.write_to: invalid write count")

self.offset += bytes_written
total_bytes_written = bytes_written
Expand Down
2 changes: 1 addition & 1 deletion src/gojo/bytes/reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ struct Reader(
var err: Error
write_count, err = writer.write(bytes)
if write_count > len(bytes):
abort("bytes.Reader.write_to: invalid Write count")
panic("bytes.Reader.write_to: invalid Write count")

self.index += write_count
if write_count != len(bytes):
Expand Down
2 changes: 1 addition & 1 deletion src/gojo/strings/reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ struct Reader(
var bytes_written: Int
bytes_written, err = writer.write(chunk_to_write)
if bytes_written > len(chunk_to_write):
abort("strings.Reader.write_to: invalid write_string count")
panic("strings.Reader.write_to: invalid write_string count")

self.read_pos += bytes_written
if bytes_written != len(chunk_to_write) and not err:
Expand Down
2 changes: 1 addition & 1 deletion src/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ context:

package:
name: "gojo"
version: 0.1.8
version: 0.1.9

source:
- path: .
Expand Down

0 comments on commit 8ce56e8

Please sign in to comment.