Skip to content

Commit 74c977a

Browse files
authored
Merge pull request #244 from izo0x90/Hristo/Mojo-v25-1-fixes
Hristo/mojo v25 1 fixes
2 parents 4e8bd73 + 504b146 commit 74c977a

31 files changed

+778
-4216
lines changed

lightbug_http/_libc.mojo

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ fn inet_ntop[
482482
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html.
483483
"""
484484
constrained[
485-
int(address_family) in [AF_INET, AF_INET6], "Address family must be either INET_ADDRSTRLEN or INET6_ADDRSTRLEN."
485+
Int(address_family) in [AF_INET, AF_INET6], "Address family must be either INET_ADDRSTRLEN or INET6_ADDRSTRLEN."
486486
]()
487487
constrained[
488488
address_length in [INET_ADDRSTRLEN, INET6_ADDRSTRLEN],
@@ -511,7 +511,7 @@ fn inet_ntop[
511511
" address."
512512
)
513513
else:
514-
raise Error("inet_ntop Error: An error occurred while converting the address. Error code: " + str(errno))
514+
raise Error("inet_ntop Error: An error occurred while converting the address. Error code: " + String(errno))
515515

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

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

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

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

680680
return fd
681681

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

771771

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

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

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

940940

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

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

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

11151115

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

11731173

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

12791279
return result
12801280

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

13811381

13821382
fn _recv(
@@ -1442,7 +1442,7 @@ fn recv(
14421442
var result = _recv(socket, buffer, length, flags)
14431443
if result == -1:
14441444
var errno = get_errno()
1445-
if int(errno) in [EAGAIN, EWOULDBLOCK]:
1445+
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
14461446
raise Error(
14471447
"ReceiveError: The socket is marked nonblocking and the receive operation would block, or a receive"
14481448
" timeout had been set and the timeout expired before data was received."
@@ -1467,7 +1467,7 @@ fn recv(
14671467
else:
14681468
raise Error(
14691469
"ReceiveError: An error occurred while attempting to receive data from the socket. Error code: "
1470-
+ str(errno)
1470+
+ String(errno)
14711471
)
14721472

14731473
return result
@@ -1560,7 +1560,7 @@ fn recvfrom(
15601560
var result = _recvfrom(socket, buffer, length, flags, address, Pointer[socklen_t].address_of(sizeof[sockaddr]()))
15611561
if result == -1:
15621562
var errno = get_errno()
1563-
if int(errno) in [EAGAIN, EWOULDBLOCK]:
1563+
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
15641564
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."
15651565
elif errno == EBADF:
15661566
raise "ReceiveError: The socket argument is not a valid file descriptor."
@@ -1585,7 +1585,7 @@ fn recvfrom(
15851585
elif errno == ENOMEM:
15861586
raise "ReceiveError: Insufficient memory was available to fulfill the request."
15871587
else:
1588-
raise "ReceiveError: An error occurred while attempting to receive data from the socket. Error code: " + str(
1588+
raise "ReceiveError: An error occurred while attempting to receive data from the socket. Error code: " + String(
15891589
errno
15901590
)
15911591

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

17171717
return result
@@ -1816,7 +1816,7 @@ fn sendto(
18161816
var errno = get_errno()
18171817
if errno == EAFNOSUPPORT:
18181818
raise "SendToError (EAFNOSUPPORT): Addresses in the specified address family cannot be used with this socket."
1819-
elif int(errno) in [EAGAIN, EWOULDBLOCK]:
1819+
elif Int(errno) in [EAGAIN, EWOULDBLOCK]:
18201820
raise "SendToError (EAGAIN/EWOULDBLOCK): The socket's file descriptor is marked `O_NONBLOCK` and the requested operation would block."
18211821
elif errno == EBADF:
18221822
raise "SendToError (EBADF): The socket argument is not a valid file descriptor."
@@ -1857,7 +1857,7 @@ fn sendto(
18571857
elif errno == ENAMETOOLONG:
18581858
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`."
18591859
else:
1860-
raise "SendToError: An error occurred while attempting to send data to the socket. Error code: " + str(
1860+
raise "SendToError: An error occurred while attempting to send data to the socket. Error code: " + String(
18611861
errno
18621862
)
18631863

@@ -1927,7 +1927,7 @@ fn shutdown(socket: c_int, how: c_int) raises:
19271927
else:
19281928
raise Error(
19291929
"ShutdownError: An error occurred while attempting to receive data from the socket. Error code: "
1930-
+ str(errno)
1930+
+ String(errno)
19311931
)
19321932

19331933

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

20242024

20252025
fn get_errno() -> c_int:

lightbug_http/_owning_list.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
173173
fn __str__[U: RepresentableCollectionElement, //](self: OwningList[U, *_]) -> String:
174174
"""Returns a string representation of a `List`.
175175
176-
When the compiler supports conditional methods, then a simple `str(my_list)` will
176+
When the compiler supports conditional methods, then a simple `String(my_list)` will
177177
be enough.
178178
179179
The elements' type must implement the `__repr__()` method for this to work.

lightbug_http/address.mojo

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,14 @@ struct TCPAddr[network: NetworkType = NetworkType.tcp4](Addr):
185185

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

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

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

197197

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

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

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

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

258258

259259
@value
@@ -424,9 +424,9 @@ fn validate_no_brackets[
424424
var segment: ByteView[origin]
425425

426426
if end_idx is None:
427-
segment = address[int(start_idx) :]
427+
segment = address[Int(start_idx) :]
428428
else:
429-
segment = address[int(start_idx) : int(end_idx.value())]
429+
segment = address[Int(start_idx) : Int(end_idx.value())]
430430

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

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

@@ -527,7 +527,7 @@ fn binary_port_to_int(port: UInt16) -> Int:
527527
Returns:
528528
The port as an integer.
529529
"""
530-
return int(ntohs(port))
530+
return Int(ntohs(port))
531531

532532

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

548548
@parameter

lightbug_http/client.mojo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct Client:
7979
conn = self._connections.take(pool_key)
8080
cached_connection = True
8181
except e:
82-
if str(e) == "PoolManager.take: Key not found.":
82+
if String(e) == "PoolManager.take: Key not found.":
8383
conn = create_connection(request.uri.host, port)
8484
else:
8585
logger.error(e)
@@ -90,7 +90,7 @@ struct Client:
9090
bytes_sent = conn.write(encode(request))
9191
except e:
9292
# Maybe peer reset ungracefully, so try a fresh connection
93-
if str(e) == "SendError: Connection reset by peer.":
93+
if String(e) == "SendError: Connection reset by peer.":
9494
logger.debug("Client.do: Connection reset by peer. Trying a fresh connection.")
9595
conn.teardown()
9696
if cached_connection:
@@ -103,7 +103,7 @@ struct Client:
103103
try:
104104
_ = conn.read(new_buf)
105105
except e:
106-
if str(e) == "EOF":
106+
if String(e) == "EOF":
107107
conn.teardown()
108108
if cached_connection:
109109
return self.do(request^)
@@ -149,7 +149,7 @@ struct Client:
149149
try:
150150
new_uri = URI.parse(new_location)
151151
except e:
152-
raise Error("Client._handle_redirect: Failed to parse the new URI: " + str(e))
152+
raise Error("Client._handle_redirect: Failed to parse the new URI: " + String(e))
153153
original_request.headers[HeaderKey.HOST] = new_uri.host
154154
else:
155155
new_uri = original_request.uri

0 commit comments

Comments
 (0)