M
Modular10mo ago
abi

Bytes-like object when using a python module

Hi there, I'm checking out mojo and was going to make a little IRC client using the basic python socket module, but I ran into an issue when trying to call socket.send which was: a bytes-like object is required, not 'str'. So I tried to get the underlying buffer from the string and got a compile error: argument #1 cannot be converted from 'DynamicVector[SIMD[si8, 1]]' to 'PythonObject' I'm new to mojo and my python is very rusty, but I couldn't find a way to get a bytes-like python object, is there a stdlib function somewhere that I'm missing?
6 Replies
sora
sora10mo ago
I think what you are looking for is the Python method on str: "string".encode("utf-8").
abi
abiOP10mo ago
How do I use that method on a mojo String? Since it's a built in python type I'm not sure how to import/use it
sora
sora10mo ago
from python import Python


fn main() raises:
var builtins: PythonObject
try:
builtins = Python.import_module("builtins")
except PythonError:
return print("import error")
var s = String("test")
print(builtins.str(s).encode("utf-8"))
from python import Python


fn main() raises:
var builtins: PythonObject
try:
builtins = Python.import_module("builtins")
except PythonError:
return print("import error")
var s = String("test")
print(builtins.str(s).encode("utf-8"))
abi
abiOP10mo ago
Oh it's an actual module called builtins 🤦‍♀️ This did work! Thanks for sharing your brainpower with me 🙂
sora
sora10mo ago
It's actually the Python module builtins.
abi
abiOP10mo ago
Yeah, I was looking in the python docs but I was on a page talking about the builtins stuff but since it was talking about things that are automatically imported to python I didn't realize that it's in a literal module called builtins
Want results from more Discord servers?
Add your server