Skip to content

Commit

Permalink
Merge pull request #244 from izo0x90/Hristo/Mojo-v25-1-fixes
Browse files Browse the repository at this point in the history
Hristo/mojo v25 1 fixes
  • Loading branch information
saviorand authored Feb 16, 2025
2 parents 4e8bd73 + 504b146 commit 74c977a
Show file tree
Hide file tree
Showing 31 changed files with 778 additions and 4,216 deletions.
54 changes: 27 additions & 27 deletions lightbug_http/_libc.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn inet_ntop[
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html.
"""
constrained[
int(address_family) in [AF_INET, AF_INET6], "Address family must be either INET_ADDRSTRLEN or INET6_ADDRSTRLEN."
Int(address_family) in [AF_INET, AF_INET6], "Address family must be either INET_ADDRSTRLEN or INET6_ADDRSTRLEN."
]()
constrained[
address_length in [INET_ADDRSTRLEN, INET6_ADDRSTRLEN],
Expand Down Expand Up @@ -511,7 +511,7 @@ fn inet_ntop[
" address."
)
else:
raise Error("inet_ntop Error: An error occurred while converting the address. Error code: " + str(errno))
raise Error("inet_ntop Error: An error occurred while converting the address. Error code: " + String(errno))

# We want the string representation of the address, so it's ok to take ownership of the pointer here.
return dst
Expand Down Expand Up @@ -573,7 +573,7 @@ fn inet_pton[address_family: Int32](src: UnsafePointer[c_char]) raises -> c_uint
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html
* This function is valid for `AF_INET` and `AF_INET6`.
"""
constrained[int(address_family) in [AF_INET, AF_INET6], "Address family must be either AF_INET or AF_INET6."]()
constrained[Int(address_family) in [AF_INET, AF_INET6], "Address family must be either AF_INET or AF_INET6."]()
var ip_buffer: UnsafePointer[c_void]

@parameter
Expand All @@ -587,7 +587,7 @@ fn inet_pton[address_family: Int32](src: UnsafePointer[c_char]) raises -> c_uint
raise Error("inet_pton Error: The input is not a valid address.")
elif result == -1:
var errno = get_errno()
raise Error("inet_pton Error: An error occurred while converting the address. Error code: " + str(errno))
raise Error("inet_pton Error: An error occurred while converting the address. Error code: " + String(errno))

return ip_buffer.bitcast[c_uint]().take_pointee()

Expand Down Expand Up @@ -664,7 +664,7 @@ fn socket(domain: c_int, type: c_int, protocol: c_int) raises -> c_int:
raise Error(
"SocketError (ENFILE): The system-wide limit on the total number of open files has been reached."
)
elif int(errno) in [ENOBUFS, ENOMEM]:
elif Int(errno) in [ENOBUFS, ENOMEM]:
raise Error(
"SocketError (ENOBUFS or ENOMEM): Insufficient memory is available. The socket cannot be created until"
" sufficient resources are freed."
Expand All @@ -675,7 +675,7 @@ fn socket(domain: c_int, type: c_int, protocol: c_int) raises -> c_int:
" this domain."
)
else:
raise Error("SocketError: An error occurred while creating the socket. Error code: " + str(errno))
raise Error("SocketError: An error occurred while creating the socket. Error code: " + String(errno))

return fd

Expand Down Expand Up @@ -766,7 +766,7 @@ fn setsockopt(
elif errno == ENOTSOCK:
raise Error("setsockopt: The argument `socket` is not a socket.")
else:
raise Error("setsockopt: An error occurred while setting the socket option. Error code: " + str(errno))
raise Error("setsockopt: An error occurred while setting the socket option. Error code: " + String(errno))


fn _getsockopt[
Expand Down Expand Up @@ -858,7 +858,7 @@ fn getsockopt(
elif errno == ENOTSOCK:
raise Error("getsockopt: The argument `socket` is not a socket.")
else:
raise Error("getsockopt: An error occurred while setting the socket option. Error code: " + str(errno))
raise Error("getsockopt: An error occurred while setting the socket option. Error code: " + String(errno))

return option_value.bitcast[Int]().take_pointee()

Expand Down Expand Up @@ -935,7 +935,7 @@ fn getsockname[
elif errno == ENOTSOCK:
raise Error("getsockname: The argument `socket` is not a socket, it is a file.")
else:
raise Error("getsockname: An error occurred while getting the socket name. Error code: " + str(errno))
raise Error("getsockname: An error occurred while getting the socket name. Error code: " + String(errno))


fn _getpeername[
Expand Down Expand Up @@ -1010,7 +1010,7 @@ fn getpeername(file_descriptor: c_int) raises -> sockaddr_in:
elif errno == ENOTSOCK:
raise Error("getpeername: The argument `socket` is not a socket, it is a file.")
else:
raise Error("getpeername: An error occurred while getting the socket name. Error code: " + str(errno))
raise Error("getpeername: An error occurred while getting the socket name. Error code: " + String(errno))

# Cast sockaddr struct to sockaddr_in
return remote_address.bitcast[sockaddr_in]().take_pointee()
Expand Down Expand Up @@ -1110,7 +1110,7 @@ fn bind(socket: c_int, mut address: sockaddr_in) raises:
# elif errno == EROFS:
# raise Error("bind: The socket inode would reside on a read-only file system.")

raise Error("bind: An error occurred while binding the socket. Error code: " + str(errno))
raise Error("bind: An error occurred while binding the socket. Error code: " + String(errno))


fn _listen(socket: c_int, backlog: c_int) -> c_int:
Expand Down Expand Up @@ -1168,7 +1168,7 @@ fn listen(socket: c_int, backlog: c_int) raises:
elif errno == EOPNOTSUPP:
raise Error("listen: The socket is not of a type that supports the `listen()` operation.")
else:
raise Error("listen: An error occurred while listening on the socket. Error code: " + str(errno))
raise Error("listen: An error occurred while listening on the socket. Error code: " + String(errno))


fn _accept[
Expand Down Expand Up @@ -1233,7 +1233,7 @@ fn accept(socket: c_int) raises -> c_int:
var result = _accept(socket, Pointer.address_of(remote_address), Pointer.address_of(socklen_t(sizeof[socklen_t]())))
if result == -1:
var errno = get_errno()
if int(errno) in [EAGAIN, EWOULDBLOCK]:
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
raise Error(
"accept: The socket is marked nonblocking and no connections are present to be accepted. POSIX.1-2001"
" allows either error to be returned for this case, and does not require these constants to have the"
Expand All @@ -1258,7 +1258,7 @@ fn accept(socket: c_int) raises -> c_int:
raise Error("accept: The per-process limit of open file descriptors has been reached.")
elif errno == ENFILE:
raise Error("accept: The system limit on the total number of open files has been reached.")
elif int(errno) in [ENOBUFS, ENOMEM]:
elif Int(errno) in [ENOBUFS, ENOMEM]:
raise Error(
"accept: Not enough free memory. This often means that the memory allocation is limited by the socket"
" buffer limits, not by the system memory."
Expand All @@ -1274,7 +1274,7 @@ fn accept(socket: c_int) raises -> c_int:
if os_is_linux():
if errno == EPERM:
raise Error("accept: Firewall rules forbid connection.")
raise Error("accept: An error occurred while listening on the socket. Error code: " + str(errno))
raise Error("accept: An error occurred while listening on the socket. Error code: " + String(errno))

return result

Expand Down Expand Up @@ -1376,7 +1376,7 @@ fn connect(socket: c_int, address: sockaddr_in) raises:
"connect: Timeout while attempting connection. The server may be too busy to accept new connections."
)
else:
raise Error("connect: An error occurred while connecting to the socket. Error code: " + str(errno))
raise Error("connect: An error occurred while connecting to the socket. Error code: " + String(errno))


fn _recv(
Expand Down Expand Up @@ -1442,7 +1442,7 @@ fn recv(
var result = _recv(socket, buffer, length, flags)
if result == -1:
var errno = get_errno()
if int(errno) in [EAGAIN, EWOULDBLOCK]:
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
raise Error(
"ReceiveError: The socket is marked nonblocking and the receive operation would block, or a receive"
" timeout had been set and the timeout expired before data was received."
Expand All @@ -1467,7 +1467,7 @@ fn recv(
else:
raise Error(
"ReceiveError: An error occurred while attempting to receive data from the socket. Error code: "
+ str(errno)
+ String(errno)
)

return result
Expand Down Expand Up @@ -1560,7 +1560,7 @@ fn recvfrom(
var result = _recvfrom(socket, buffer, length, flags, address, Pointer[socklen_t].address_of(sizeof[sockaddr]()))
if result == -1:
var errno = get_errno()
if int(errno) in [EAGAIN, EWOULDBLOCK]:
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
raise "ReceiveError: The socket's file descriptor is marked `O_NONBLOCK` and no data is waiting to be received; or MSG_OOB is set and no out-of-band data is available and either the socket's file descriptor is marked `O_NONBLOCK` or the socket does not support blocking to await out-of-band data."
elif errno == EBADF:
raise "ReceiveError: The socket argument is not a valid file descriptor."
Expand All @@ -1585,7 +1585,7 @@ fn recvfrom(
elif errno == ENOMEM:
raise "ReceiveError: Insufficient memory was available to fulfill the request."
else:
raise "ReceiveError: An error occurred while attempting to receive data from the socket. Error code: " + str(
raise "ReceiveError: An error occurred while attempting to receive data from the socket. Error code: " + String(
errno
)

Expand Down Expand Up @@ -1657,7 +1657,7 @@ fn send(socket: c_int, buffer: UnsafePointer[c_void], length: c_size_t, flags: c
var result = _send(socket, buffer, length, flags)
if result == -1:
var errno = get_errno()
if int(errno) in [EAGAIN, EWOULDBLOCK]:
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
raise Error(
"SendError: The socket is marked nonblocking and the receive operation would block, or a receive"
" timeout had been set and the timeout expired before data was received."
Expand Down Expand Up @@ -1711,7 +1711,7 @@ fn send(socket: c_int, buffer: UnsafePointer[c_void], length: c_size_t, flags: c
else:
raise Error(
"SendError: An error occurred while attempting to receive data from the socket. Error code: "
+ str(errno)
+ String(errno)
)

return result
Expand Down Expand Up @@ -1816,7 +1816,7 @@ fn sendto(
var errno = get_errno()
if errno == EAFNOSUPPORT:
raise "SendToError (EAFNOSUPPORT): Addresses in the specified address family cannot be used with this socket."
elif int(errno) in [EAGAIN, EWOULDBLOCK]:
elif Int(errno) in [EAGAIN, EWOULDBLOCK]:
raise "SendToError (EAGAIN/EWOULDBLOCK): The socket's file descriptor is marked `O_NONBLOCK` and the requested operation would block."
elif errno == EBADF:
raise "SendToError (EBADF): The socket argument is not a valid file descriptor."
Expand Down Expand Up @@ -1857,7 +1857,7 @@ fn sendto(
elif errno == ENAMETOOLONG:
raise "SendToError (ENAMETOOLONG): The length of a pathname exceeds `PATH_MAX`, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds `PATH_MAX`."
else:
raise "SendToError: An error occurred while attempting to send data to the socket. Error code: " + str(
raise "SendToError: An error occurred while attempting to send data to the socket. Error code: " + String(
errno
)

Expand Down Expand Up @@ -1927,7 +1927,7 @@ fn shutdown(socket: c_int, how: c_int) raises:
else:
raise Error(
"ShutdownError: An error occurred while attempting to receive data from the socket. Error code: "
+ str(errno)
+ String(errno)
)


Expand Down Expand Up @@ -2016,10 +2016,10 @@ fn close(file_descriptor: c_int) raises:
raise CloseInterruptedError
elif errno == EIO:
raise CloseRWError
elif int(errno) in [ENOSPC, EDQUOT]:
elif Int(errno) in [ENOSPC, EDQUOT]:
raise CloseOutOfSpaceError
else:
raise Error("SocketError: An error occurred while creating the socket. Error code: " + str(errno))
raise Error("SocketError: An error occurred while creating the socket. Error code: " + String(errno))


fn get_errno() -> c_int:
Expand Down
2 changes: 1 addition & 1 deletion lightbug_http/_owning_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
fn __str__[U: RepresentableCollectionElement, //](self: OwningList[U, *_]) -> String:
"""Returns a string representation of a `List`.
When the compiler supports conditional methods, then a simple `str(my_list)` will
When the compiler supports conditional methods, then a simple `String(my_list)` will
be enough.
The elements' type must implement the `__repr__()` method for this to work.
Expand Down
22 changes: 11 additions & 11 deletions lightbug_http/address.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ struct TCPAddr[network: NetworkType = NetworkType.tcp4](Addr):

fn __str__(self) -> String:
if self.zone != "":
return join_host_port(self.ip + "%" + self.zone, str(self.port))
return join_host_port(self.ip, str(self.port))
return join_host_port(self.ip + "%" + self.zone, String(self.port))
return join_host_port(self.ip, String(self.port))

fn __repr__(self) -> String:
return String.write(self)

fn write_to[W: Writer, //](self, mut writer: W):
writer.write("TCPAddr(", "ip=", repr(self.ip), ", port=", str(self.port), ", zone=", repr(self.zone), ")")
writer.write("TCPAddr(", "ip=", repr(self.ip), ", port=", String(self.port), ", zone=", repr(self.zone), ")")


@value
Expand Down Expand Up @@ -246,14 +246,14 @@ struct UDPAddr[network: NetworkType = NetworkType.udp4](Addr):

fn __str__(self) -> String:
if self.zone != "":
return join_host_port(self.ip + "%" + self.zone, str(self.port))
return join_host_port(self.ip, str(self.port))
return join_host_port(self.ip + "%" + self.zone, String(self.port))
return join_host_port(self.ip, String(self.port))

fn __repr__(self) -> String:
return String.write(self)

fn write_to[W: Writer, //](self, mut writer: W):
writer.write("UDPAddr(", "ip=", repr(self.ip), ", port=", str(self.port), ", zone=", repr(self.zone), ")")
writer.write("UDPAddr(", "ip=", repr(self.ip), ", port=", String(self.port), ", zone=", repr(self.zone), ")")


@value
Expand Down Expand Up @@ -424,9 +424,9 @@ fn validate_no_brackets[
var segment: ByteView[origin]

if end_idx is None:
segment = address[int(start_idx) :]
segment = address[Int(start_idx) :]
else:
segment = address[int(start_idx) : int(end_idx.value())]
segment = address[Int(start_idx) : Int(end_idx.value())]

if segment.find(Byte(ord("["))) != -1:
raise Error("unexpected '[' in address")
Expand All @@ -439,7 +439,7 @@ fn parse_port[origin: ImmutableOrigin](port_str: ByteView[origin]) raises -> UIn
if port_str == AddressConstants.EMPTY.as_bytes():
raise MissingPortError

var port = int(str(port_str))
var port = Int(String(port_str))
if port < MIN_PORT or port > MAX_PORT:
raise Error("Port number out of range (0-65535)")

Expand Down Expand Up @@ -527,7 +527,7 @@ fn binary_port_to_int(port: UInt16) -> Int:
Returns:
The port as an integer.
"""
return int(ntohs(port))
return Int(ntohs(port))


fn binary_ip_to_string[address_family: Int32](owned ip_address: UInt32) raises -> String:
Expand All @@ -542,7 +542,7 @@ fn binary_ip_to_string[address_family: Int32](owned ip_address: UInt32) raises -
Returns:
The IP address as a string.
"""
constrained[int(address_family) in [AF_INET, AF_INET6], "Address family must be either AF_INET or AF_INET6."]()
constrained[Int(address_family) in [AF_INET, AF_INET6], "Address family must be either AF_INET or AF_INET6."]()
var ip: String

@parameter
Expand Down
8 changes: 4 additions & 4 deletions lightbug_http/client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct Client:
conn = self._connections.take(pool_key)
cached_connection = True
except e:
if str(e) == "PoolManager.take: Key not found.":
if String(e) == "PoolManager.take: Key not found.":
conn = create_connection(request.uri.host, port)
else:
logger.error(e)
Expand All @@ -90,7 +90,7 @@ struct Client:
bytes_sent = conn.write(encode(request))
except e:
# Maybe peer reset ungracefully, so try a fresh connection
if str(e) == "SendError: Connection reset by peer.":
if String(e) == "SendError: Connection reset by peer.":
logger.debug("Client.do: Connection reset by peer. Trying a fresh connection.")
conn.teardown()
if cached_connection:
Expand All @@ -103,7 +103,7 @@ struct Client:
try:
_ = conn.read(new_buf)
except e:
if str(e) == "EOF":
if String(e) == "EOF":
conn.teardown()
if cached_connection:
return self.do(request^)
Expand Down Expand Up @@ -149,7 +149,7 @@ struct Client:
try:
new_uri = URI.parse(new_location)
except e:
raise Error("Client._handle_redirect: Failed to parse the new URI: " + str(e))
raise Error("Client._handle_redirect: Failed to parse the new URI: " + String(e))
original_request.headers[HeaderKey.HOST] = new_uri.host
else:
new_uri = original_request.uri
Expand Down
Loading

0 comments on commit 74c977a

Please sign in to comment.