From 41e77373900a84cf97b572e7434d808484b57a26 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 11:54:07 -0300 Subject: [PATCH 01/24] deprecate .unsafe_cstr_ptr() Signed-off-by: martinvuyk --- stdlib/src/builtin/_pybind.mojo | 6 +-- stdlib/src/builtin/debug_assert.mojo | 15 ++++--- stdlib/src/builtin/error.mojo | 1 + stdlib/src/builtin/io.mojo | 10 ++--- stdlib/src/builtin/string_literal.mojo | 2 +- stdlib/src/collections/string.mojo | 1 + stdlib/src/python/_bindings.mojo | 8 ++-- stdlib/src/python/_cpython.mojo | 10 ++--- stdlib/src/python/python.mojo | 4 +- stdlib/src/sys/ffi.mojo | 43 +++++++++++++++++--- stdlib/src/sys/info.mojo | 4 +- stdlib/test/builtin/test_string_literal.mojo | 4 +- 12 files changed, 71 insertions(+), 37 deletions(-) diff --git a/stdlib/src/builtin/_pybind.mojo b/stdlib/src/builtin/_pybind.mojo index 8cf9036056..fc9d2e64f2 100644 --- a/stdlib/src/builtin/_pybind.mojo +++ b/stdlib/src/builtin/_pybind.mojo @@ -14,7 +14,7 @@ from memory import UnsafePointer from sys import sizeof, alignof -from sys.ffi import OpaquePointer +from sys.ffi import OpaquePointer, c_char_ptr import python._cpython as cp from python import TypedPythonObject, Python, PythonObject @@ -51,7 +51,7 @@ fn fail_initialization(owned err: Error) -> PythonObject: cpython.PyErr_SetString( error_type, - err.unsafe_cstr_ptr(), + c_char_ptr(err) ) _ = err^ return PythonObject(PyObjectPtr()) @@ -119,7 +119,7 @@ fn gen_pytype_wrapper[ spec.init_pointee_move( PyType_Spec { # FIXME(MOCO-1306): This should be `T.__name__`. - name: name.unsafe_cstr_ptr(), + name: c_char_ptr(name), basicsize: sizeof[T](), itemsize: 0, flags: cp.Py_TPFLAGS_DEFAULT, diff --git a/stdlib/src/builtin/debug_assert.mojo b/stdlib/src/builtin/debug_assert.mojo index 6a8f442191..6dee9166bb 100644 --- a/stdlib/src/builtin/debug_assert.mojo +++ b/stdlib/src/builtin/debug_assert.mojo @@ -20,7 +20,7 @@ from os import abort from sys import is_defined, triple_is_nvidia_cuda from sys._build import is_debug_build from sys.param_env import env_get_string -from sys.ffi import external_call, c_uint, c_size_t, c_char +from sys.ffi import external_call, c_uint, c_size_t, c_char, c_char_ptr from sys.info import sizeof from memory import UnsafePointer @@ -297,12 +297,11 @@ fn _debug_assert_msg( @parameter if triple_is_nvidia_cuda(): external_call["__assertfail", NoneType]( - "debug_assert message must be a single StringLiteral on GPU" - .unsafe_cstr_ptr(), - loc.file_name.unsafe_cstr_ptr(), + c_char_ptr("debug_assert message must be a single StringLiteral on GPU"), + c_char_ptr(loc.file_name), c_uint(loc.line), # TODO(MSTDL-962) pass through the funciton name here - "kernel".unsafe_cstr_ptr(), + c_char_ptr("kernel"), c_size_t(sizeof[Int8]()), ) @@ -330,11 +329,11 @@ fn _debug_assert_msg_literal(message: StringLiteral, loc: _SourceLocation): @parameter if triple_is_nvidia_cuda(): external_call["__assertfail", NoneType]( - message.unsafe_cstr_ptr(), - loc.file_name.unsafe_cstr_ptr(), + c_char_ptr(message), + c_char_ptr(loc.file_name), c_uint(loc.line), # TODO(MSTDL-962) pass through the funciton name here - "kernel".unsafe_cstr_ptr(), + c_char_ptr("kernel"), c_size_t(sizeof[Int8]()), ) else: diff --git a/stdlib/src/builtin/error.mojo b/stdlib/src/builtin/error.mojo index 00d7ebb439..3e9bd8ebe1 100644 --- a/stdlib/src/builtin/error.mojo +++ b/stdlib/src/builtin/error.mojo @@ -183,6 +183,7 @@ struct Error( # Methods # ===-------------------------------------------------------------------===# + @deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.") fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/stdlib/src/builtin/io.mojo b/stdlib/src/builtin/io.mojo index 9ee0e84e81..d4c9e906e8 100644 --- a/stdlib/src/builtin/io.mojo +++ b/stdlib/src/builtin/io.mojo @@ -23,7 +23,7 @@ from sys import ( _libc as libc, ) from sys._libc import dup, fclose, fdopen, fflush -from sys.ffi import OpaquePointer +from sys.ffi import OpaquePointer, c_char_ptr from builtin.builtin_list import _LITRefPackHelper from builtin.dtype import _get_dtype_printf_format @@ -50,7 +50,7 @@ struct _fdopen[mode: StringLiteral = "a"]: stream_id: The stream id """ - self.handle = fdopen(dup(stream_id.value), mode.unsafe_cstr_ptr()) + self.handle = fdopen(dup(stream_id.value), c_char_ptr(mode)) fn __enter__(self) -> Self: """Open the file handle for use within a context manager""" @@ -167,7 +167,7 @@ fn _printf[ @parameter if triple_is_nvidia_cuda(): _ = external_call["vprintf", Int32]( - fmt.unsafe_cstr_ptr(), Pointer.address_of(loaded_pack) + c_char_ptr(fmt), Pointer.address_of(loaded_pack) ) else: with _fdopen(file) as fd: @@ -180,7 +180,7 @@ fn _printf[ `) -> !pop.scalar`, ], _type=Int32, - ](fd, fmt.unsafe_cstr_ptr(), loaded_pack) + ](fd, c_char_ptr(fmt), loaded_pack) # ===----------------------------------------------------------------------=== # @@ -222,7 +222,7 @@ fn _snprintf[ `) -> !pop.scalar`, ], _type=Int32, - ](str, size, fmt.unsafe_cstr_ptr(), loaded_pack) + ](str, size, c_char_ptr(fmt), loaded_pack) ) diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 11943dfe7b..4cf500e677 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -333,7 +333,7 @@ struct StringLiteral( return ptr.bitcast[UInt8]() @always_inline - # FIXME(MSTDL-956): This should return a pointer with StaticConstantLifetime. + @deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.") fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index dd1db6aba7..5882f58de5 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -1377,6 +1377,7 @@ struct String( """ return self._buffer.data + @deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.") fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/stdlib/src/python/_bindings.mojo b/stdlib/src/python/_bindings.mojo index ae6f76e151..9dd67d7e51 100644 --- a/stdlib/src/python/_bindings.mojo +++ b/stdlib/src/python/_bindings.mojo @@ -13,7 +13,7 @@ from memory import UnsafePointer, Box -from sys.ffi import c_int +from sys.ffi import c_int, c_char_ptr from sys.info import sizeof from os import abort @@ -90,7 +90,7 @@ struct PyMojoObject[T: Pythonable]: ) var type_spec = PyType_Spec { - name: type_name.unsafe_cstr_ptr(), + name: c_char_ptr(type_name), basicsize: sizeof[PyMojoObject[T]](), itemsize: 0, flags: Py_TPFLAGS_DEFAULT, @@ -167,7 +167,7 @@ fn create_empty_init_wrapper[T: Pythonable]() -> Typed_initproc: cpython.PyErr_SetString( error_type, - e.unsafe_cstr_ptr(), + c_char_ptr(e) ) return -1 @@ -268,7 +268,7 @@ fn create_wrapper_function[ cpython.PyErr_SetString( error_type, - e.unsafe_cstr_ptr(), + c_char_ptr(e) ) # Return a NULL `PyObject*`. diff --git a/stdlib/src/python/_cpython.mojo b/stdlib/src/python/_cpython.mojo index 3652d84c5d..5051a9677c 100644 --- a/stdlib/src/python/_cpython.mojo +++ b/stdlib/src/python/_cpython.mojo @@ -23,7 +23,7 @@ from os.path import dirname from pathlib import Path from sys import external_call from sys.arg import argv -from sys.ffi import DLHandle, c_char, c_int, c_uint, OpaquePointer +from sys.ffi import DLHandle, c_char, c_int, c_uint, OpaquePointer, c_char_ptr from python.python import _get_global_python_itf from python._bindings import Typed_initproc @@ -206,10 +206,10 @@ struct PyMethodDef: # type, similar to `get_linkage_name()`? return PyMethodDef( - func_name.unsafe_cstr_ptr(), + c_char_ptr(func_name), func, METH_VARARGS, - docstring.unsafe_cstr_ptr(), + c_char_ptr(docstring) ) @@ -432,7 +432,7 @@ struct PyModuleDef(Stringable, Representable, Formattable): fn __init__(inout self, name: String): self.base = PyModuleDef_Base() - self.name = name.unsafe_cstr_ptr() + self.name = c_char_ptr(name) self.docstring = UnsafePointer[c_char]() # means that the module does not support sub-interpreters self.size = -1 @@ -1393,7 +1393,7 @@ struct CPython: UnsafePointer[c_char], ) -> PyObjectPtr ](StringRef("PyUnicode_DecodeUTF8"))( - strref.data, strref.length, "strict".unsafe_cstr_ptr() + strref.data, strref.length, c_char_ptr("strict") ) self.log( diff --git a/stdlib/src/python/python.mojo b/stdlib/src/python/python.mojo index 0044212d74..446166cf95 100644 --- a/stdlib/src/python/python.mojo +++ b/stdlib/src/python/python.mojo @@ -22,7 +22,7 @@ from python import Python from collections import Dict from os import abort, getenv from sys import external_call, sizeof -from sys.ffi import _get_global, OpaquePointer +from sys.ffi import _get_global, OpaquePointer, c_char_ptr from memory import UnsafePointer @@ -322,7 +322,7 @@ struct Python: var result = cpython.PyModule_AddObjectRef( module.unsafe_as_py_object_ptr(), - name.unsafe_cstr_ptr(), + c_char_ptr(name), value.unsafe_as_py_object_ptr(), ) diff --git a/stdlib/src/sys/ffi.mojo b/stdlib/src/sys/ffi.mojo index 15e1cb13c2..59c21db926 100644 --- a/stdlib/src/sys/ffi.mojo +++ b/stdlib/src/sys/ffi.mojo @@ -22,6 +22,7 @@ from .intrinsics import _mlirtype_is_eq from builtin.builtin_list import _LITRefPackHelper from sys._libc import dlerror, dlopen, dlclose, dlsym +from sys.ffi import c_char_ptr alias c_char = Int8 """C `char` type.""" @@ -86,6 +87,38 @@ fn _c_long_long_dtype() -> DType: return abort[DType]() +@always_inline +fn c_char_ptr[T: _UnsafePtrU8](item: T) -> UnsafePointer[c_char]: + """Get the C.char pointer. + + Parameters: + T: The type. + + Args: + item: The item. + + Returns: + The pointer. + """ + return item.unsafe_ptr().bitcast[c_char]() + + +@always_inline +fn c_char_ptr[T: AnyType](ptr: UnsafePointer[T]) -> UnsafePointer[c_char]: + """Get the C.char pointer. + + Parameters: + T: The type. + + Args: + ptr: The pointer. + + Returns: + The pointer. + """ + return ptr.bitcast[c_char]() + + struct RTLD: """Enumeration of the RTLD flags used during dynamic library loading.""" @@ -129,7 +162,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): @parameter if not os_is_windows(): - var handle = dlopen(path.unsafe_cstr_ptr(), flags) + var handle = dlopen(c_char_ptr(path), flags) if handle == OpaquePointer(): var error_message = dlerror() abort("dlopen failed: " + String(error_message)) @@ -161,7 +194,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): var opaque_function_ptr: OpaquePointer = dlsym( self.handle, - name.unsafe_cstr_ptr(), + c_char_ptr(name) ) return bool(opaque_function_ptr) @@ -203,7 +236,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): A handle to the function. """ - return self._get_function[result_type](name.unsafe_cstr_ptr()) + return self._get_function[result_type](c_char_ptr(name)) @always_inline fn _get_function[ @@ -244,7 +277,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): A handle to the function. """ - return self._get_function[result_type](func_name.unsafe_cstr_ptr()) + return self._get_function[result_type](c_char_ptr(func_name)) fn get_symbol[ result_type: AnyType, @@ -261,7 +294,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): Returns: A pointer to the symbol. """ - return self.get_symbol[result_type](name.unsafe_cstr_ptr()) + return self.get_symbol[result_type](c_char_ptr(name)) fn get_symbol[ result_type: AnyType diff --git a/stdlib/src/sys/info.mojo b/stdlib/src/sys/info.mojo index 6d2d86bb9e..c7bf9d5c3c 100644 --- a/stdlib/src/sys/info.mojo +++ b/stdlib/src/sys/info.mojo @@ -19,7 +19,7 @@ from sys import is_x86 ``` """ -from .ffi import _external_call_const, external_call, OpaquePointer +from .ffi import _external_call_const, external_call, OpaquePointer, c_char_ptr from memory import UnsafePointer @@ -793,7 +793,7 @@ fn _macos_version() raises -> Tuple[Int, Int, Int]: var buf_len = Int(INITIAL_CAPACITY) var err = external_call["sysctlbyname", Int32]( - "kern.osproductversion".unsafe_cstr_ptr(), + c_char_ptr("kern.osproductversion"), buf.data, Pointer.address_of(buf_len), OpaquePointer(), diff --git a/stdlib/test/builtin/test_string_literal.mojo b/stdlib/test/builtin/test_string_literal.mojo index f6b3eaf455..5462056c4b 100644 --- a/stdlib/test/builtin/test_string_literal.mojo +++ b/stdlib/test/builtin/test_string_literal.mojo @@ -12,7 +12,7 @@ # ===----------------------------------------------------------------------=== # # RUN: %mojo %s -from sys.ffi import c_char +from sys.ffi import c_char, c_char_ptr from memory import UnsafePointer from testing import ( @@ -227,7 +227,7 @@ def test_layout(): # assert_equal(empty[0], 0) # Test non-empty StringLiteral C string - var ptr: UnsafePointer[c_char] = "hello".unsafe_cstr_ptr() + var ptr: UnsafePointer[c_char] = c_char_ptr("hello") assert_equal(ptr[0], ord("h")) assert_equal(ptr[1], ord("e")) assert_equal(ptr[2], ord("l")) From 0418058016128d0cb7d73293ce32d1c1ecbefefb Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 11:55:47 -0300 Subject: [PATCH 02/24] mojo format Signed-off-by: martinvuyk --- stdlib/src/builtin/_pybind.mojo | 5 +---- stdlib/src/builtin/debug_assert.mojo | 4 +++- stdlib/src/python/_bindings.mojo | 10 ++-------- stdlib/src/python/_cpython.mojo | 5 +---- stdlib/src/sys/ffi.mojo | 3 +-- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/stdlib/src/builtin/_pybind.mojo b/stdlib/src/builtin/_pybind.mojo index fc9d2e64f2..eed9f66bbf 100644 --- a/stdlib/src/builtin/_pybind.mojo +++ b/stdlib/src/builtin/_pybind.mojo @@ -49,10 +49,7 @@ fn fail_initialization(owned err: Error) -> PythonObject: cpython = get_cpython() error_type = cpython.get_error_global("PyExc_Exception") - cpython.PyErr_SetString( - error_type, - c_char_ptr(err) - ) + cpython.PyErr_SetString(error_type, c_char_ptr(err)) _ = err^ return PythonObject(PyObjectPtr()) diff --git a/stdlib/src/builtin/debug_assert.mojo b/stdlib/src/builtin/debug_assert.mojo index 6dee9166bb..047328962c 100644 --- a/stdlib/src/builtin/debug_assert.mojo +++ b/stdlib/src/builtin/debug_assert.mojo @@ -297,7 +297,9 @@ fn _debug_assert_msg( @parameter if triple_is_nvidia_cuda(): external_call["__assertfail", NoneType]( - c_char_ptr("debug_assert message must be a single StringLiteral on GPU"), + c_char_ptr( + "debug_assert message must be a single StringLiteral on GPU" + ), c_char_ptr(loc.file_name), c_uint(loc.line), # TODO(MSTDL-962) pass through the funciton name here diff --git a/stdlib/src/python/_bindings.mojo b/stdlib/src/python/_bindings.mojo index 9dd67d7e51..f8a5a9b558 100644 --- a/stdlib/src/python/_bindings.mojo +++ b/stdlib/src/python/_bindings.mojo @@ -165,10 +165,7 @@ fn create_empty_init_wrapper[T: Pythonable]() -> Typed_initproc: # TODO(MSTDL-933): Add custom 'MojoError' type, and raise it here. var error_type = cpython.get_error_global("PyExc_ValueError") - cpython.PyErr_SetString( - error_type, - c_char_ptr(e) - ) + cpython.PyErr_SetString(error_type, c_char_ptr(e)) return -1 @@ -266,10 +263,7 @@ fn create_wrapper_function[ # TODO(MSTDL-933): Add custom 'MojoError' type, and raise it here. var error_type = cpython.get_error_global("PyExc_Exception") - cpython.PyErr_SetString( - error_type, - c_char_ptr(e) - ) + cpython.PyErr_SetString(error_type, c_char_ptr(e)) # Return a NULL `PyObject*`. return PythonObject(PyObjectPtr()) diff --git a/stdlib/src/python/_cpython.mojo b/stdlib/src/python/_cpython.mojo index 5051a9677c..e3d33270ef 100644 --- a/stdlib/src/python/_cpython.mojo +++ b/stdlib/src/python/_cpython.mojo @@ -206,10 +206,7 @@ struct PyMethodDef: # type, similar to `get_linkage_name()`? return PyMethodDef( - c_char_ptr(func_name), - func, - METH_VARARGS, - c_char_ptr(docstring) + c_char_ptr(func_name), func, METH_VARARGS, c_char_ptr(docstring) ) diff --git a/stdlib/src/sys/ffi.mojo b/stdlib/src/sys/ffi.mojo index 59c21db926..1eff99afef 100644 --- a/stdlib/src/sys/ffi.mojo +++ b/stdlib/src/sys/ffi.mojo @@ -193,8 +193,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): ]() var opaque_function_ptr: OpaquePointer = dlsym( - self.handle, - c_char_ptr(name) + self.handle, c_char_ptr(name) ) return bool(opaque_function_ptr) From e7fc4d22456e30ee6121cc68fdde3642fd017be3 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 11:57:43 -0300 Subject: [PATCH 03/24] fix detail Signed-off-by: martinvuyk --- stdlib/src/sys/ffi.mojo | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stdlib/src/sys/ffi.mojo b/stdlib/src/sys/ffi.mojo index 1eff99afef..b900d18f8b 100644 --- a/stdlib/src/sys/ffi.mojo +++ b/stdlib/src/sys/ffi.mojo @@ -87,6 +87,11 @@ fn _c_long_long_dtype() -> DType: return abort[DType]() +trait _UnsafePtrU8: + fn unsafe_ptr(self) -> UnsafePointer[UInt8]: + ... + + @always_inline fn c_char_ptr[T: _UnsafePtrU8](item: T) -> UnsafePointer[c_char]: """Get the C.char pointer. From 0a688e595d8d47e4a090b89e6a15bb4869a36a82 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 11:59:14 -0300 Subject: [PATCH 04/24] fix detail Signed-off-by: martinvuyk --- stdlib/src/sys/ffi.mojo | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/src/sys/ffi.mojo b/stdlib/src/sys/ffi.mojo index b900d18f8b..904534d2eb 100644 --- a/stdlib/src/sys/ffi.mojo +++ b/stdlib/src/sys/ffi.mojo @@ -22,7 +22,6 @@ from .intrinsics import _mlirtype_is_eq from builtin.builtin_list import _LITRefPackHelper from sys._libc import dlerror, dlopen, dlclose, dlsym -from sys.ffi import c_char_ptr alias c_char = Int8 """C `char` type.""" From bd30f1d8a31daa67296cafc74e712ebda5c0d767 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 12:02:38 -0300 Subject: [PATCH 05/24] fix detail Signed-off-by: martinvuyk --- stdlib/src/builtin/error.mojo | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/stdlib/src/builtin/error.mojo b/stdlib/src/builtin/error.mojo index 3e9bd8ebe1..e9b15c80a0 100644 --- a/stdlib/src/builtin/error.mojo +++ b/stdlib/src/builtin/error.mojo @@ -194,6 +194,15 @@ struct Error( """ return self.data.bitcast[c_char]() + fn unsafe_ptr(self) -> UnsafePointer[UInt8]: + """Get raw pointer to the underlying data. + + Returns: + The raw pointer to the data. + """ + return self.data + + fn _message(self) -> String: """Converts the Error to string representation. From 7f52ac2635a5ecd0baf0ad80873c0f7186fc3e34 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 12:08:45 -0300 Subject: [PATCH 06/24] fix detail Signed-off-by: martinvuyk --- stdlib/src/builtin/error.mojo | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/src/builtin/error.mojo b/stdlib/src/builtin/error.mojo index e9b15c80a0..f2d6453e7d 100644 --- a/stdlib/src/builtin/error.mojo +++ b/stdlib/src/builtin/error.mojo @@ -202,7 +202,6 @@ struct Error( """ return self.data - fn _message(self) -> String: """Converts the Error to string representation. From d4f981f53afe4aa718a3c18bfdd9110016ceaea9 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 10 Oct 2024 12:23:42 -0300 Subject: [PATCH 07/24] change deprecated docstring; suggested by @soraros Signed-off-by: martinvuyk --- stdlib/src/builtin/error.mojo | 2 +- stdlib/src/builtin/string_literal.mojo | 2 +- stdlib/src/collections/string.mojo | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/src/builtin/error.mojo b/stdlib/src/builtin/error.mojo index f2d6453e7d..df12b2570b 100644 --- a/stdlib/src/builtin/error.mojo +++ b/stdlib/src/builtin/error.mojo @@ -183,7 +183,7 @@ struct Error( # Methods # ===-------------------------------------------------------------------===# - @deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.") + @deprecated("Use `sys.ffi.c_char_ptr()` instead.") fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 4cf500e677..d6839b2540 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -333,7 +333,7 @@ struct StringLiteral( return ptr.bitcast[UInt8]() @always_inline - @deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.") + @deprecated("Use `sys.ffi.c_char_ptr()` instead.") fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 5882f58de5..968bb6461a 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -1377,7 +1377,7 @@ struct String( """ return self._buffer.data - @deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.") + @deprecated("Use `sys.ffi.c_char_ptr()` instead.") fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. From 6158151d91d25f7f886e54fa1b1552d9d98bfea3 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 11 Oct 2024 17:14:42 -0300 Subject: [PATCH 08/24] retry github CI Signed-off-by: martinvuyk From 5980efb457121fa6ce735467087b54e2038d20b3 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 11 Oct 2024 17:38:43 -0300 Subject: [PATCH 09/24] retry github CI Signed-off-by: martinvuyk From 352644a30fa7193b78331a0755744aadad328864 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 11 Oct 2024 19:06:15 -0300 Subject: [PATCH 10/24] retry github CI Signed-off-by: martinvuyk From fa2a3f2214054188ea9a4d661b5a94882788708d Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 11 Oct 2024 19:37:50 -0300 Subject: [PATCH 11/24] retry github CI Signed-off-by: martinvuyk From 5a2c908ec95600816088e847554d6f5e3f3941a9 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Sat, 12 Oct 2024 10:40:05 -0300 Subject: [PATCH 12/24] retry github CI Signed-off-by: martinvuyk From db2f62506e0fc9a93dca343d5dc67cd02843ee95 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Sun, 13 Oct 2024 10:54:28 -0300 Subject: [PATCH 13/24] retry github CI Signed-off-by: martinvuyk From 6834573e5993721775a03fda8a5ea7e36bebd073 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Sun, 13 Oct 2024 15:47:16 -0300 Subject: [PATCH 14/24] retry github CI Signed-off-by: martinvuyk From 795f8b3b756ae966625d27de92b47c4db2d97924 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Sun, 13 Oct 2024 16:45:12 -0300 Subject: [PATCH 15/24] fix check_licences.mojo reference Signed-off-by: martinvuyk --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2fa75b958e..9ca3f26be6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: stages: [commit] - id: check-license name: check-license - entry: mojo stdlib/scripts/check-licenses.mojo + entry: mojo stdlib/scripts/check_licenses.mojo language: system files: '\.(mojo|🔥|py)$' stages: [commit] From 8216402e3500aa3db180bbeb1afafff73ef8b915 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 16 Oct 2024 10:55:36 -0300 Subject: [PATCH 16/24] add fixme comments for unsafe ptr returns Signed-off-by: martinvuyk --- stdlib/src/builtin/error.mojo | 2 ++ stdlib/src/builtin/string_literal.mojo | 3 ++- stdlib/src/collections/string.mojo | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/stdlib/src/builtin/error.mojo b/stdlib/src/builtin/error.mojo index df12b2570b..e242cbb824 100644 --- a/stdlib/src/builtin/error.mojo +++ b/stdlib/src/builtin/error.mojo @@ -184,6 +184,7 @@ struct Error( # ===-------------------------------------------------------------------===# @deprecated("Use `sys.ffi.c_char_ptr()` instead.") + # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. @@ -194,6 +195,7 @@ struct Error( """ return self.data.bitcast[c_char]() + # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. fn unsafe_ptr(self) -> UnsafePointer[UInt8]: """Get raw pointer to the underlying data. diff --git a/stdlib/src/builtin/string_literal.mojo b/stdlib/src/builtin/string_literal.mojo index 0c6ba93938..5f2c2ea5cb 100644 --- a/stdlib/src/builtin/string_literal.mojo +++ b/stdlib/src/builtin/string_literal.mojo @@ -345,8 +345,9 @@ struct StringLiteral( # return type. return ptr.bitcast[UInt8]() - @always_inline @deprecated("Use `sys.ffi.c_char_ptr()` instead.") + @always_inline + # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index dec57a0de6..78ad825572 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -1355,6 +1355,7 @@ struct String( """ pass + # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. fn unsafe_ptr(self) -> UnsafePointer[UInt8]: """Retrieves a pointer to the underlying memory. @@ -1364,6 +1365,7 @@ struct String( return self._buffer.data @deprecated("Use `sys.ffi.c_char_ptr()` instead.") + # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. From f40b36898a983da94c48f86db649577be9156d48 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 16 Oct 2024 11:04:28 -0300 Subject: [PATCH 17/24] fix detail Signed-off-by: martinvuyk --- stdlib/src/python/_bindings.mojo | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/src/python/_bindings.mojo b/stdlib/src/python/_bindings.mojo index 14964d3bc8..1df9318363 100644 --- a/stdlib/src/python/_bindings.mojo +++ b/stdlib/src/python/_bindings.mojo @@ -163,9 +163,7 @@ fn empty_tp_init_wrapper[ except e: # TODO(MSTDL-933): Add custom 'MojoError' type, and raise it here. var error_type = cpython.get_error_global("PyExc_ValueError") - - cpython.PyErr_SetString(error_type, c_char_ptr(e)) - + cpython.PyErr_SetString(error_type, c_char_ptr(e)) return -1 From e6d8b7392ee3b942b01077530c7b0597ef9ed1e7 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 16 Oct 2024 11:05:44 -0300 Subject: [PATCH 18/24] retry github CI Signed-off-by: martinvuyk From b71c044efea5c8f3f865674927dc3f238ab8fd89 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 6 Nov 2024 12:32:29 -0300 Subject: [PATCH 19/24] fix conflict Signed-off-by: martinvuyk --- stdlib/src/python/_cpython.mojo | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/src/python/_cpython.mojo b/stdlib/src/python/_cpython.mojo index 69d1f56f00..fd0665eec1 100644 --- a/stdlib/src/python/_cpython.mojo +++ b/stdlib/src/python/_cpython.mojo @@ -1723,9 +1723,7 @@ struct CPython: https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeUTF8). """ var r = self.lib.call["PyUnicode_DecodeUTF8", PyObjectPtr]( - strslice.unsafe_ptr().bitcast[Int8](), - strslice.byte_length(), - "strict".unsafe_cstr_ptr(), + c_char_ptr(strslice), strslice.byte_length(), c_char_ptr("strict") ) self.log( From 5459fd917688391c9c17f46b9c2aee34fa50c99f Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 12 Nov 2024 16:42:39 -0300 Subject: [PATCH 20/24] fix indentation Signed-off-by: martinvuyk --- stdlib/src/python/_bindings.mojo | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/stdlib/src/python/_bindings.mojo b/stdlib/src/python/_bindings.mojo index 2d829227f8..10bead0b6b 100644 --- a/stdlib/src/python/_bindings.mojo +++ b/stdlib/src/python/_bindings.mojo @@ -114,15 +114,15 @@ fn python_type_object[ # Zeroed item terminator slots.append(PyType_Slot.null()) - var type_spec = PyType_Spec { - # FIXME(MOCO-1306): This should be `T.__name__`. - name: c_char_ptr(type_name), - basicsize: sizeof[PyMojoObject[T]](), - itemsize: 0, - flags: Py_TPFLAGS_DEFAULT, - # Note: This pointer is only "borrowed" by PyType_FromSpec. - slots: slots.unsafe_ptr(), - } + var type_spec = PyType_Spec { + # FIXME(MOCO-1306): This should be `T.__name__`. + name: c_char_ptr(type_name), + basicsize: sizeof[PyMojoObject[T]](), + itemsize: 0, + flags: Py_TPFLAGS_DEFAULT, + # Note: This pointer is only "borrowed" by PyType_FromSpec. + slots: slots.unsafe_ptr(), + } # Construct a Python 'type' object from our type spec. var type_obj = cpython.PyType_FromSpec(UnsafePointer.address_of(type_spec)) From 07609ed1dc64394e30981964428876fcab71338c Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 28 Feb 2025 08:28:23 -0300 Subject: [PATCH 21/24] give an origin to Error.unsafe_ptr() Signed-off-by: martinvuyk --- mojo/stdlib/src/builtin/error.mojo | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/mojo/stdlib/src/builtin/error.mojo b/mojo/stdlib/src/builtin/error.mojo index f261e2d892..3b1d2635ec 100644 --- a/mojo/stdlib/src/builtin/error.mojo +++ b/mojo/stdlib/src/builtin/error.mojo @@ -225,8 +225,13 @@ struct Error( # ===-------------------------------------------------------------------===# @deprecated("Use `sys.ffi.c_char_ptr()` instead.") - # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. - fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: + fn unsafe_cstr_ptr( + ref self, + ) -> UnsafePointer[ + Byte, + mut = Origin(__origin_of(self)).is_mutable, + origin = __origin_of(self), + ]: """Retrieves a C-string-compatible pointer to the underlying memory. The returned pointer is guaranteed to be NUL terminated, and not null. @@ -236,8 +241,14 @@ struct Error( """ return self.data.bitcast[c_char]() - # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. - fn unsafe_ptr(self) -> UnsafePointer[UInt8]: + @always_inline("nodebug") + fn unsafe_ptr( + ref self, + ) -> UnsafePointer[ + Byte, + mut = Origin(__origin_of(self)).is_mutable, + origin = __origin_of(self), + ]: """Get raw pointer to the underlying data. Returns: From b2e4f17bb466b910cc62fb9078e6ed0c917e1282 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 28 Feb 2025 08:45:52 -0300 Subject: [PATCH 22/24] polish and rename to c_str_ptr() Signed-off-by: martinvuyk --- mojo/stdlib/src/builtin/_pybind.mojo | 4 +- mojo/stdlib/src/builtin/error.mojo | 2 +- mojo/stdlib/src/builtin/io.mojo | 10 +-- mojo/stdlib/src/builtin/string_literal.mojo | 2 +- .../stdlib/src/collections/string/string.mojo | 2 +- mojo/stdlib/src/python/_bindings.mojo | 8 +-- mojo/stdlib/src/python/_cpython.mojo | 34 ++++++++-- mojo/stdlib/src/python/python.mojo | 4 +- mojo/stdlib/src/sys/ffi.mojo | 62 ++++++++++++++----- mojo/stdlib/src/sys/info.mojo | 4 +- .../test/builtin/test_string_literal.mojo | 4 +- 11 files changed, 98 insertions(+), 38 deletions(-) diff --git a/mojo/stdlib/src/builtin/_pybind.mojo b/mojo/stdlib/src/builtin/_pybind.mojo index f69c252eea..8df2867e1f 100644 --- a/mojo/stdlib/src/builtin/_pybind.mojo +++ b/mojo/stdlib/src/builtin/_pybind.mojo @@ -13,7 +13,7 @@ from collections import Optional from sys import alignof, sizeof -from sys.ffi import c_char_ptr +from sys.ffi import c_str_ptr import python._cpython as cp from memory import UnsafePointer, stack_allocation @@ -52,7 +52,7 @@ fn fail_initialization(owned err: Error) -> PythonObject: cpython = get_cpython() error_type = cpython.get_error_global("PyExc_Exception") - cpython.PyErr_SetString(error_type, c_char_ptr(err)) + cpython.PyErr_SetString(error_type, c_str_ptr(err)) _ = err^ return PythonObject(PyObjectPtr()) diff --git a/mojo/stdlib/src/builtin/error.mojo b/mojo/stdlib/src/builtin/error.mojo index 3b1d2635ec..a2e10c6044 100644 --- a/mojo/stdlib/src/builtin/error.mojo +++ b/mojo/stdlib/src/builtin/error.mojo @@ -224,7 +224,7 @@ struct Error( # Methods # ===-------------------------------------------------------------------===# - @deprecated("Use `sys.ffi.c_char_ptr()` instead.") + @deprecated("Use `sys.ffi.c_str_ptr()` instead.") fn unsafe_cstr_ptr( ref self, ) -> UnsafePointer[ diff --git a/mojo/stdlib/src/builtin/io.mojo b/mojo/stdlib/src/builtin/io.mojo index b8c4461bab..2ce5956a82 100644 --- a/mojo/stdlib/src/builtin/io.mojo +++ b/mojo/stdlib/src/builtin/io.mojo @@ -28,7 +28,7 @@ from sys import ( ) from sys._amdgpu import printf_append_args, printf_append_string_n, printf_begin from sys._libc import dup, fclose, fdopen, fflush -from sys.ffi import OpaquePointer, c_char, c_char_ptr +from sys.ffi import OpaquePointer, c_char, c_str_ptr from sys.intrinsics import _type_is_eq from builtin.dtype import _get_dtype_printf_format @@ -55,7 +55,7 @@ struct _fdopen[mode: StringLiteral = "a"]: stream_id: The stream id """ - self.handle = fdopen(dup(stream_id.value), c_char_ptr(mode)) + self.handle = fdopen(dup(stream_id.value), c_str_ptr(mode)) fn __enter__(self) -> Self: """Open the file handle for use within a context manager""" @@ -183,7 +183,7 @@ fn _printf[ @parameter if is_nvidia_gpu(): _ = external_call["vprintf", Int32]( - c_char_ptr(fmt), Pointer.address_of(loaded_pack) + c_str_ptr(fmt), Pointer.address_of(loaded_pack) ) elif is_amd_gpu(): # This is adapted from Triton's third party method for lowering @@ -266,7 +266,7 @@ fn _printf[ `) -> !pop.scalar`, ], _type=Int32, - ](fd, c_char_ptr(fmt), loaded_pack) + ](fd, c_str_ptr(fmt), loaded_pack) # ===----------------------------------------------------------------------=== # @@ -310,7 +310,7 @@ fn _snprintf[ `) -> !pop.scalar`, ], _type=Int32, - ](str, size, c_char_ptr(fmt), loaded_pack) + ](str, size, c_str_ptr(fmt), loaded_pack) ) diff --git a/mojo/stdlib/src/builtin/string_literal.mojo b/mojo/stdlib/src/builtin/string_literal.mojo index 412929e0a7..202a132998 100644 --- a/mojo/stdlib/src/builtin/string_literal.mojo +++ b/mojo/stdlib/src/builtin/string_literal.mojo @@ -520,7 +520,7 @@ struct StringLiteral( # return type. return ptr.bitcast[Byte]().origin_cast[False, StaticConstantOrigin]() - @deprecated("Use `sys.ffi.c_char_ptr()` instead.") + @deprecated("Use `sys.ffi.c_str_ptr()` instead.") @always_inline fn unsafe_cstr_ptr( self, diff --git a/mojo/stdlib/src/collections/string/string.mojo b/mojo/stdlib/src/collections/string/string.mojo index b71c18ce8e..2dec72a378 100644 --- a/mojo/stdlib/src/collections/string/string.mojo +++ b/mojo/stdlib/src/collections/string/string.mojo @@ -1285,7 +1285,7 @@ struct String( """ return self._buffer.unsafe_ptr() - @deprecated("Use `sys.ffi.c_char_ptr()` instead.") + @deprecated("Use `sys.ffi.c_str_ptr()` instead.") # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: """Retrieves a C-string-compatible pointer to the underlying memory. diff --git a/mojo/stdlib/src/python/_bindings.mojo b/mojo/stdlib/src/python/_bindings.mojo index 2bb0074443..e7d784075e 100644 --- a/mojo/stdlib/src/python/_bindings.mojo +++ b/mojo/stdlib/src/python/_bindings.mojo @@ -12,7 +12,7 @@ # ===----------------------------------------------------------------------=== # -from sys.ffi import c_int, c_char_ptr +from sys.ffi import c_int, c_str_ptr from collections import Optional from os import abort from sys.ffi import c_int @@ -111,7 +111,7 @@ fn python_type_object[ var type_spec = PyType_Spec( # FIXME(MOCO-1306): This should be `T.__name__`. - c_char_ptr(type_name), + c_str_ptr(type_name), sizeof[PyMojoObject[T]](), 0, Py_TPFLAGS_DEFAULT, @@ -182,7 +182,7 @@ fn empty_tp_init_wrapper[ except e: # TODO(MSTDL-933): Add custom 'MojoError' type, and raise it here. var error_type = cpython.get_error_global("PyExc_ValueError") - cpython.PyErr_SetString(error_type, c_char_ptr(e)) + cpython.PyErr_SetString(error_type, c_str_ptr(e)) return -1 @@ -277,7 +277,7 @@ fn py_c_function_wrapper[ # TODO(MSTDL-933): Add custom 'MojoError' type, and raise it here. var error_type = cpython.get_error_global("PyExc_Exception") - cpython.PyErr_SetString(error_type, c_char_ptr(e)) + cpython.PyErr_SetString(error_type, c_str_ptr(e)) # Return a NULL `PyObject*`. return PythonObject(PyObjectPtr()) diff --git a/mojo/stdlib/src/python/_cpython.mojo b/mojo/stdlib/src/python/_cpython.mojo index 563da3b78c..022cb91872 100644 --- a/mojo/stdlib/src/python/_cpython.mojo +++ b/mojo/stdlib/src/python/_cpython.mojo @@ -33,7 +33,7 @@ from sys.ffi import ( c_size_t, c_ssize_t, c_uint, - c_char_ptr, + c_str_ptr, ) from memory import UnsafePointer @@ -372,7 +372,7 @@ struct PyMethodDef: # type, similar to `get_linkage_name()`? return PyMethodDef( - c_char_ptr(func_name), func, METH_VARARGS, c_char_ptr(docstring) + c_str_ptr(func_name), func, METH_VARARGS, c_str_ptr(docstring) ) @@ -650,7 +650,7 @@ struct PyModuleDef(Stringable, Representable, Writable): @implicit fn __init__(out self, name: String): self.base = PyModuleDef_Base() - self.name = c_char_ptr(name) + self.name = c_str_ptr(name) self.docstring = UnsafePointer[c_char]() # means that the module does not support sub-interpreters self.size = -1 @@ -1716,12 +1716,38 @@ struct CPython: # Unicode Objects # ===-------------------------------------------------------------------===# +<<<<<<< HEAD:stdlib/src/python/_cpython.mojo + fn PyUnicode_DecodeUTF8(inout self, strref: StringRef) -> PyObjectPtr: + """[Reference]( + https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeUTF8). + """ + + var r = self.lib.call["PyUnicode_DecodeUTF8", PyObjectPtr]( + strref.unsafe_ptr().bitcast[Int8](), + strref.length, + c_str_ptr("strict"), + ) + + self.log( + r._get_ptr_as_int(), + " NEWREF PyUnicode_DecodeUTF8, refcnt:", + self._Py_REFCNT(r), + ", str:", + strref, + ) + + self._inc_total_rc() + return r + + fn PyUnicode_DecodeUTF8(inout self, strslice: StringSlice) -> PyObjectPtr: +======= fn PyUnicode_DecodeUTF8(mut self, strslice: StringSlice) -> PyObjectPtr: +>>>>>>> upstream/main:mojo/stdlib/src/python/_cpython.mojo """[Reference]( https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeUTF8). """ var r = self.lib.call["PyUnicode_DecodeUTF8", PyObjectPtr]( - c_char_ptr(strslice), strslice.byte_length(), c_char_ptr("strict") + c_str_ptr(strslice), strslice.byte_length(), c_str_ptr("strict") ) self.log( diff --git a/mojo/stdlib/src/python/python.mojo b/mojo/stdlib/src/python/python.mojo index 1c9233b400..ab44efc716 100644 --- a/mojo/stdlib/src/python/python.mojo +++ b/mojo/stdlib/src/python/python.mojo @@ -23,7 +23,7 @@ from collections import Dict from collections.string import StringSlice from os import abort, getenv from sys import external_call, sizeof -from sys.ffi import _Global, c_char_ptr +from sys.ffi import _Global, c_str_ptr from memory import UnsafePointer @@ -338,7 +338,7 @@ struct Python: var result = cpython.PyModule_AddObjectRef( module.unsafe_as_py_object_ptr(), - c_char_ptr(name), + c_str_ptr(name), value.unsafe_as_py_object_ptr(), ) diff --git a/mojo/stdlib/src/sys/ffi.mojo b/mojo/stdlib/src/sys/ffi.mojo index 41d2adb985..3d4bdffc42 100644 --- a/mojo/stdlib/src/sys/ffi.mojo +++ b/mojo/stdlib/src/sys/ffi.mojo @@ -103,14 +103,48 @@ fn _c_long_long_dtype() -> DType: return abort[DType]() -trait _UnsafePtrU8: - fn unsafe_ptr(self) -> UnsafePointer[UInt8]: - ... +@always_inline +fn c_str_ptr(item: StringLiteral) -> UnsafePointer[c_char]: + """Get the `c_char` pointer. + + Args: + item: The item. + + Returns: + The pointer. + """ + return item.unsafe_ptr().bitcast[c_char]() @always_inline -fn c_char_ptr[T: _UnsafePtrU8](item: T) -> UnsafePointer[c_char]: - """Get the C.char pointer. +fn c_str_ptr(item: String) -> UnsafePointer[c_char]: + """Get the `c_char` pointer. + + Args: + item: The item. + + Returns: + The pointer. + """ + return item.unsafe_ptr().bitcast[c_char]() + + +@always_inline +fn c_str_ptr(item: StringSlice) -> UnsafePointer[c_char]: + """Get the `c_char` pointer. + + Args: + item: The item. + + Returns: + The pointer. + """ + return item.unsafe_ptr().bitcast[c_char]() + + +@always_inline +fn c_str_ptr[T: CollectionElement](item: List[T]) -> UnsafePointer[c_char]: + """Get the `c_char` pointer. Parameters: T: The type. @@ -125,19 +159,19 @@ fn c_char_ptr[T: _UnsafePtrU8](item: T) -> UnsafePointer[c_char]: @always_inline -fn c_char_ptr[T: AnyType](ptr: UnsafePointer[T]) -> UnsafePointer[c_char]: - """Get the C.char pointer. +fn c_str_ptr[T: CollectionElement](item: Span[T]) -> UnsafePointer[c_char]: + """Get the `c_char` pointer. Parameters: T: The type. Args: - ptr: The pointer. + item: The item. Returns: The pointer. """ - return ptr.bitcast[c_char]() + return item.unsafe_ptr().bitcast[c_char]() # ===-----------------------------------------------------------------------===# @@ -222,7 +256,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): @parameter if not os_is_windows(): - var handle = dlopen(c_char_ptr(path), flags) + var handle = dlopen(c_str_ptr(path), flags) if handle == OpaquePointer(): var error_message = dlerror() abort( @@ -260,7 +294,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): ]() var opaque_function_ptr: OpaquePointer = dlsym( - self.handle, c_char_ptr(name) + self.handle, c_str_ptr(name) ) return Bool(opaque_function_ptr) @@ -302,7 +336,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): A handle to the function. """ - return self._get_function[result_type](c_char_ptr(name)) + return self._get_function[result_type](c_str_ptr(name)) @always_inline fn _get_function[ @@ -343,7 +377,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): A handle to the function. """ - return self._get_function[result_type](c_char_ptr(func_name)) + return self._get_function[result_type](c_str_ptr(func_name)) fn get_symbol[ result_type: AnyType, @@ -360,7 +394,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable): Returns: A pointer to the symbol. """ - return self.get_symbol[result_type](c_char_ptr(name)) + return self.get_symbol[result_type](c_str_ptr(name)) fn get_symbol[ result_type: AnyType diff --git a/mojo/stdlib/src/sys/info.mojo b/mojo/stdlib/src/sys/info.mojo index 00b08cddd2..06185f1cee 100644 --- a/mojo/stdlib/src/sys/info.mojo +++ b/mojo/stdlib/src/sys/info.mojo @@ -21,7 +21,7 @@ from sys import is_x86 from memory import UnsafePointer -from .ffi import OpaquePointer, _external_call_const, external_call, c_char_ptr +from .ffi import OpaquePointer, _external_call_const, external_call, c_str_ptr @always_inline("nodebug") @@ -844,7 +844,7 @@ fn _macos_version() raises -> Tuple[Int, Int, Int]: var buf_len = Int(INITIAL_CAPACITY) var err = external_call["sysctlbyname", Int32]( - c_char_ptr("kern.osproductversion"), + c_str_ptr("kern.osproductversion"), buf.data, Pointer.address_of(buf_len), OpaquePointer(), diff --git a/mojo/stdlib/test/builtin/test_string_literal.mojo b/mojo/stdlib/test/builtin/test_string_literal.mojo index 50b16429eb..1f2694e4d3 100644 --- a/mojo/stdlib/test/builtin/test_string_literal.mojo +++ b/mojo/stdlib/test/builtin/test_string_literal.mojo @@ -12,7 +12,7 @@ # ===----------------------------------------------------------------------=== # # RUN: %mojo %s -from sys.ffi import c_char, c_char_ptr +from sys.ffi import c_char, c_str_ptr from memory import UnsafePointer from testing import ( assert_equal, @@ -326,7 +326,7 @@ def test_layout(): # assert_equal(empty[0], 0) # Test non-empty StringLiteral C string - var ptr: UnsafePointer[c_char] = c_char_ptr("hello") + var ptr: UnsafePointer[c_char] = c_str_ptr("hello") assert_equal(ptr[0], ord("h")) assert_equal(ptr[1], ord("e")) assert_equal(ptr[2], ord("l")) From ec3fa4345214f7d293f9aac7d779cfc1549cf4a2 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 28 Feb 2025 08:55:01 -0300 Subject: [PATCH 23/24] fix details Signed-off-by: martinvuyk --- mojo/stdlib/src/builtin/error.mojo | 9 ++++--- mojo/stdlib/src/os/env.mojo | 12 ++++----- mojo/stdlib/src/os/os.mojo | 6 ++--- mojo/stdlib/src/python/_cpython.mojo | 26 ------------------- mojo/stdlib/src/sys/ffi.mojo | 15 ++++++++++- .../collections/string/test_string_slice.mojo | 5 ++-- 6 files changed, 28 insertions(+), 45 deletions(-) diff --git a/mojo/stdlib/src/builtin/error.mojo b/mojo/stdlib/src/builtin/error.mojo index a2e10c6044..b1823fe5c0 100644 --- a/mojo/stdlib/src/builtin/error.mojo +++ b/mojo/stdlib/src/builtin/error.mojo @@ -17,7 +17,7 @@ These are Mojo built-ins, so you don't need to import them. from collections.string import StringSlice from sys import alignof, sizeof -from sys.ffi import c_char +from sys.ffi import c_char, c_str_ptr from memory import UnsafePointer, memcpy from memory.memory import _free @@ -197,7 +197,7 @@ struct Error( return writer.write( StringSlice[__origin_of(self)]( - unsafe_from_utf8_cstr_ptr=self.unsafe_cstr_ptr() + unsafe_from_utf8_cstr_ptr=c_str_ptr(self) ) ) @@ -213,7 +213,7 @@ struct Error( repr( String( StringSlice[__origin_of(self)]( - unsafe_from_utf8_cstr_ptr=self.unsafe_cstr_ptr() + unsafe_from_utf8_cstr_ptr=c_str_ptr(self) ) ) ), @@ -228,7 +228,7 @@ struct Error( fn unsafe_cstr_ptr( ref self, ) -> UnsafePointer[ - Byte, + c_char, mut = Origin(__origin_of(self)).is_mutable, origin = __origin_of(self), ]: @@ -256,6 +256,7 @@ struct Error( """ return self.data + @doc_private fn __mojo_debugger_raise_hook(): """This function is used internally by the Mojo Debugger.""" diff --git a/mojo/stdlib/src/os/env.mojo b/mojo/stdlib/src/os/env.mojo index 027bc44be8..c072af91a6 100644 --- a/mojo/stdlib/src/os/env.mojo +++ b/mojo/stdlib/src/os/env.mojo @@ -21,7 +21,7 @@ from os import setenv from collections.string import StringSlice from sys import external_call, os_is_linux, os_is_macos, os_is_windows -from sys.ffi import c_int +from sys.ffi import c_int, c_str_ptr from memory import UnsafePointer @@ -47,8 +47,8 @@ fn setenv(name: String, value: String, overwrite: Bool = True) -> Bool: return False var status = external_call["setenv", Int32]( - name.unsafe_cstr_ptr(), - value.unsafe_cstr_ptr(), + c_str_ptr(name), + c_str_ptr(value), Int32(1 if overwrite else 0), ) return status == 0 @@ -67,7 +67,7 @@ fn unsetenv(name: String) -> Bool: not os_is_windows(), "operating system must be Linux or macOS" ]() - return external_call["unsetenv", c_int](name.unsafe_cstr_ptr()) == 0 + return external_call["unsetenv", c_int](c_str_ptr(name)) == 0 fn getenv(name: String, default: String = "") -> String: @@ -90,9 +90,7 @@ fn getenv(name: String, default: String = "") -> String: if not os_is_supported: return default - var ptr = external_call["getenv", UnsafePointer[UInt8]]( - name.unsafe_cstr_ptr() - ) + var ptr = external_call["getenv", UnsafePointer[UInt8]](c_str_ptr(name)) if not ptr: return default return String(StringSlice[ptr.origin](unsafe_from_utf8_ptr=ptr)) diff --git a/mojo/stdlib/src/os/os.mojo b/mojo/stdlib/src/os/os.mojo index 4bb9d30583..282dfa4f8f 100644 --- a/mojo/stdlib/src/os/os.mojo +++ b/mojo/stdlib/src/os/os.mojo @@ -22,7 +22,7 @@ from os import listdir from collections import InlineArray, List from collections.string import StringSlice from sys import external_call, is_gpu, os_is_linux, os_is_windows -from sys.ffi import OpaquePointer, c_char +from sys.ffi import OpaquePointer, c_char, c_str_ptr from memory import UnsafePointer @@ -108,9 +108,7 @@ struct _DirHandle: if not isdir(path): raise "the directory '" + path + "' does not exist" - self._handle = external_call["opendir", OpaquePointer]( - path.unsafe_cstr_ptr() - ) + self._handle = external_call["opendir", OpaquePointer](c_str_ptr(path)) if not self._handle: raise "unable to open the directory '" + path + "'" diff --git a/mojo/stdlib/src/python/_cpython.mojo b/mojo/stdlib/src/python/_cpython.mojo index 022cb91872..68801afaa7 100644 --- a/mojo/stdlib/src/python/_cpython.mojo +++ b/mojo/stdlib/src/python/_cpython.mojo @@ -1716,33 +1716,7 @@ struct CPython: # Unicode Objects # ===-------------------------------------------------------------------===# -<<<<<<< HEAD:stdlib/src/python/_cpython.mojo - fn PyUnicode_DecodeUTF8(inout self, strref: StringRef) -> PyObjectPtr: - """[Reference]( - https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeUTF8). - """ - - var r = self.lib.call["PyUnicode_DecodeUTF8", PyObjectPtr]( - strref.unsafe_ptr().bitcast[Int8](), - strref.length, - c_str_ptr("strict"), - ) - - self.log( - r._get_ptr_as_int(), - " NEWREF PyUnicode_DecodeUTF8, refcnt:", - self._Py_REFCNT(r), - ", str:", - strref, - ) - - self._inc_total_rc() - return r - - fn PyUnicode_DecodeUTF8(inout self, strslice: StringSlice) -> PyObjectPtr: -======= fn PyUnicode_DecodeUTF8(mut self, strslice: StringSlice) -> PyObjectPtr: ->>>>>>> upstream/main:mojo/stdlib/src/python/_cpython.mojo """[Reference]( https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeUTF8). """ diff --git a/mojo/stdlib/src/sys/ffi.mojo b/mojo/stdlib/src/sys/ffi.mojo index 3d4bdffc42..f8461c9728 100644 --- a/mojo/stdlib/src/sys/ffi.mojo +++ b/mojo/stdlib/src/sys/ffi.mojo @@ -16,7 +16,7 @@ from collections.string import StringSlice from os import abort from sys._libc import dlclose, dlerror, dlopen, dlsym -from memory import UnsafePointer +from memory import UnsafePointer, Span from .info import is_64bit, os_is_linux, os_is_macos, os_is_windows from .intrinsics import _mlirtype_is_eq @@ -116,6 +116,19 @@ fn c_str_ptr(item: StringLiteral) -> UnsafePointer[c_char]: return item.unsafe_ptr().bitcast[c_char]() +@always_inline +fn c_str_ptr(item: Error) -> UnsafePointer[c_char]: + """Get the `c_char` pointer. + + Args: + item: The item. + + Returns: + The pointer. + """ + return item.unsafe_ptr().bitcast[c_char]() + + @always_inline fn c_str_ptr(item: String) -> UnsafePointer[c_char]: """Get the `c_char` pointer. diff --git a/mojo/stdlib/test/collections/string/test_string_slice.mojo b/mojo/stdlib/test/collections/string/test_string_slice.mojo index 97a4df03bd..d1df768f2c 100644 --- a/mojo/stdlib/test/collections/string/test_string_slice.mojo +++ b/mojo/stdlib/test/collections/string/test_string_slice.mojo @@ -18,6 +18,7 @@ from collections.string.string_slice import ( _count_utf8_continuation_bytes, ) from sys.info import alignof, sizeof +from sys.ffi import c_str_ptr from memory import Span, UnsafePointer from testing import assert_equal, assert_false, assert_raises, assert_true @@ -1214,9 +1215,7 @@ def test_string_slice_from_pointer(): assert_equal(3, len(a)) assert_equal(3, len(b)) var c = String("ABCD") - var d = StringSlice[__origin_of(c)]( - unsafe_from_utf8_cstr_ptr=c.unsafe_cstr_ptr() - ) + var d = StringSlice[__origin_of(c)](unsafe_from_utf8_cstr_ptr=c_str_ptr(c)) var e = StringSlice[__origin_of(c)](unsafe_from_utf8_ptr=c.unsafe_ptr()) assert_equal(4, len(c)) assert_equal(4, len(d)) From 5c6d9ccde88d667c425bd6978695b0d97dc8f5ed Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 28 Feb 2025 09:07:46 -0300 Subject: [PATCH 24/24] polish Signed-off-by: martinvuyk --- mojo/stdlib/src/builtin/error.mojo | 17 ------- mojo/stdlib/src/builtin/string_literal.mojo | 14 ------ .../stdlib/src/collections/string/string.mojo | 12 ----- mojo/stdlib/src/sys/ffi.mojo | 44 ++++++++++++++++--- 4 files changed, 38 insertions(+), 49 deletions(-) diff --git a/mojo/stdlib/src/builtin/error.mojo b/mojo/stdlib/src/builtin/error.mojo index b1823fe5c0..17027e3340 100644 --- a/mojo/stdlib/src/builtin/error.mojo +++ b/mojo/stdlib/src/builtin/error.mojo @@ -224,23 +224,6 @@ struct Error( # Methods # ===-------------------------------------------------------------------===# - @deprecated("Use `sys.ffi.c_str_ptr()` instead.") - fn unsafe_cstr_ptr( - ref self, - ) -> UnsafePointer[ - c_char, - mut = Origin(__origin_of(self)).is_mutable, - origin = __origin_of(self), - ]: - """Retrieves a C-string-compatible pointer to the underlying memory. - - The returned pointer is guaranteed to be NUL terminated, and not null. - - Returns: - The pointer to the underlying memory. - """ - return self.data.bitcast[c_char]() - @always_inline("nodebug") fn unsafe_ptr( ref self, diff --git a/mojo/stdlib/src/builtin/string_literal.mojo b/mojo/stdlib/src/builtin/string_literal.mojo index 202a132998..0f2be4e0c8 100644 --- a/mojo/stdlib/src/builtin/string_literal.mojo +++ b/mojo/stdlib/src/builtin/string_literal.mojo @@ -520,20 +520,6 @@ struct StringLiteral( # return type. return ptr.bitcast[Byte]().origin_cast[False, StaticConstantOrigin]() - @deprecated("Use `sys.ffi.c_str_ptr()` instead.") - @always_inline - fn unsafe_cstr_ptr( - self, - ) -> UnsafePointer[c_char, mut=False, origin=StaticConstantOrigin]: - """Retrieves a C-string-compatible pointer to the underlying memory. - - The returned pointer is guaranteed to be NUL terminated, and not null. - - Returns: - The pointer to the underlying memory. - """ - return self.unsafe_ptr().bitcast[c_char]() - @always_inline fn as_string_slice(self) -> StaticString: """Returns a string slice of this static string literal. diff --git a/mojo/stdlib/src/collections/string/string.mojo b/mojo/stdlib/src/collections/string/string.mojo index 2dec72a378..3735a3c108 100644 --- a/mojo/stdlib/src/collections/string/string.mojo +++ b/mojo/stdlib/src/collections/string/string.mojo @@ -1285,18 +1285,6 @@ struct String( """ return self._buffer.unsafe_ptr() - @deprecated("Use `sys.ffi.c_str_ptr()` instead.") - # FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin. - fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]: - """Retrieves a C-string-compatible pointer to the underlying memory. - - The returned pointer is guaranteed to be null, or NUL terminated. - - Returns: - The pointer to the underlying memory. - """ - return self.unsafe_ptr().bitcast[c_char]() - @always_inline fn as_bytes(ref self) -> Span[Byte, __origin_of(self)]: """Returns a contiguous slice of the bytes owned by this string. diff --git a/mojo/stdlib/src/sys/ffi.mojo b/mojo/stdlib/src/sys/ffi.mojo index f8461c9728..cce0d00d47 100644 --- a/mojo/stdlib/src/sys/ffi.mojo +++ b/mojo/stdlib/src/sys/ffi.mojo @@ -104,7 +104,9 @@ fn _c_long_long_dtype() -> DType: @always_inline -fn c_str_ptr(item: StringLiteral) -> UnsafePointer[c_char]: +fn c_str_ptr( + item: StringLiteral, +) -> UnsafePointer[c_char, mut=False, origin=StaticConstantOrigin]: """Get the `c_char` pointer. Args: @@ -117,7 +119,13 @@ fn c_str_ptr(item: StringLiteral) -> UnsafePointer[c_char]: @always_inline -fn c_str_ptr(item: Error) -> UnsafePointer[c_char]: +fn c_str_ptr( + ref item: Error, +) -> UnsafePointer[ + c_char, + mut = Origin(__origin_of(item)).is_mutable, + origin = __origin_of(item), +]: """Get the `c_char` pointer. Args: @@ -130,7 +138,13 @@ fn c_str_ptr(item: Error) -> UnsafePointer[c_char]: @always_inline -fn c_str_ptr(item: String) -> UnsafePointer[c_char]: +fn c_str_ptr( + ref item: String, +) -> UnsafePointer[ + c_char, + mut = Origin(__origin_of(item)).is_mutable, + origin = __origin_of(item), +]: """Get the `c_char` pointer. Args: @@ -143,7 +157,11 @@ fn c_str_ptr(item: String) -> UnsafePointer[c_char]: @always_inline -fn c_str_ptr(item: StringSlice) -> UnsafePointer[c_char]: +fn c_str_ptr( + item: StringSlice, +) -> UnsafePointer[ + c_char, mut = __type_of(item).mut, origin = __type_of(item).origin +]: """Get the `c_char` pointer. Args: @@ -156,7 +174,13 @@ fn c_str_ptr(item: StringSlice) -> UnsafePointer[c_char]: @always_inline -fn c_str_ptr[T: CollectionElement](item: List[T]) -> UnsafePointer[c_char]: +fn c_str_ptr[ + T: CollectionElement +](item: List[T]) -> UnsafePointer[ + c_char, + mut = Origin(__origin_of(item)).is_mutable, + origin = __origin_of(item), +]: """Get the `c_char` pointer. Parameters: @@ -172,7 +196,15 @@ fn c_str_ptr[T: CollectionElement](item: List[T]) -> UnsafePointer[c_char]: @always_inline -fn c_str_ptr[T: CollectionElement](item: Span[T]) -> UnsafePointer[c_char]: +fn c_str_ptr[ + T: CollectionElement +](item: Span[T]) -> UnsafePointer[ + c_char, + mut = __type_of(item).mut, + origin = __type_of(item).origin, + address_space = __type_of(item).address_space, + alignment = __type_of(item).alignment, +]: """Get the `c_char` pointer. Parameters: