Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Deprecate .unsafe_cstr_ptr() #3638

Open
wants to merge 28 commits into
base: nightly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
41e7737
deprecate .unsafe_cstr_ptr()
martinvuyk Oct 10, 2024
0418058
mojo format
martinvuyk Oct 10, 2024
e7fc4d2
fix detail
martinvuyk Oct 10, 2024
0a688e5
fix detail
martinvuyk Oct 10, 2024
bd30f1d
fix detail
martinvuyk Oct 10, 2024
7f52ac2
fix detail
martinvuyk Oct 10, 2024
d4f981f
change deprecated docstring; suggested by @soraros
martinvuyk Oct 10, 2024
98b3e76
Merge branch 'nightly' into deprecate-unsafe-cstr-ptr
martinvuyk Oct 11, 2024
6158151
retry github CI
martinvuyk Oct 11, 2024
86dd0a6
Merge branch 'deprecate-unsafe-cstr-ptr' of github.com:martinvuyk/moj…
martinvuyk Oct 11, 2024
5980efb
retry github CI
martinvuyk Oct 11, 2024
352644a
retry github CI
martinvuyk Oct 11, 2024
fa2a3f2
retry github CI
martinvuyk Oct 11, 2024
5a2c908
retry github CI
martinvuyk Oct 12, 2024
db2f625
retry github CI
martinvuyk Oct 13, 2024
86fa0b0
Merge remote-tracking branch 'upstream/nightly' into deprecate-unsafe…
martinvuyk Oct 13, 2024
6834573
retry github CI
martinvuyk Oct 13, 2024
795f8b3
fix check_licences.mojo reference
martinvuyk Oct 13, 2024
8216402
add fixme comments for unsafe ptr returns
martinvuyk Oct 16, 2024
3f080b5
Merge remote-tracking branch 'upstream/nightly' into deprecate-unsafe…
martinvuyk Oct 16, 2024
f40b368
fix detail
martinvuyk Oct 16, 2024
e6d8b73
retry github CI
martinvuyk Oct 16, 2024
0377e76
Merge branch 'nightly' into deprecate-unsafe-cstr-ptr
martinvuyk Oct 21, 2024
7798069
Merge remote-tracking branch 'upstream/nightly' into deprecate-unsafe…
martinvuyk Nov 6, 2024
b71c044
fix conflict
martinvuyk Nov 6, 2024
2fdc8e8
Merge remote-tracking branch 'upstream/nightly' into deprecate-unsafe…
martinvuyk Nov 12, 2024
5459fd9
fix indentation
martinvuyk Nov 12, 2024
0c8b2c4
Merge remote-tracking branch 'upstream/nightly' into deprecate-unsafe…
martinvuyk Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions stdlib/src/builtin/_pybind.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
err.unsafe_cstr_ptr(),
)
cpython.PyErr_SetString(error_type, c_char_ptr(err))
_ = err^
return PythonObject(PyObjectPtr())

Expand Down Expand Up @@ -119,7 +116,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,
Expand Down
17 changes: 9 additions & 8 deletions stdlib/src/builtin/debug_assert.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -297,12 +297,13 @@ 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]()),
)

Expand Down Expand Up @@ -330,11 +331,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:
Expand Down
1 change: 1 addition & 0 deletions stdlib/src/builtin/error.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 5 additions & 5 deletions stdlib/src/builtin/io.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"""
Expand Down Expand Up @@ -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:
Expand All @@ -180,7 +180,7 @@ fn _printf[
`) -> !pop.scalar<si32>`,
],
_type=Int32,
](fd, fmt.unsafe_cstr_ptr(), loaded_pack)
](fd, c_char_ptr(fmt), loaded_pack)


# ===----------------------------------------------------------------------=== #
Expand Down Expand Up @@ -222,7 +222,7 @@ fn _snprintf[
`) -> !pop.scalar<si32>`,
],
_type=Int32,
](str, size, fmt.unsafe_cstr_ptr(), loaded_pack)
](str, size, c_char_ptr(fmt), loaded_pack)
)


Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ struct StringLiteral(
return ptr.bitcast[UInt8]()

@always_inline
# FIXME(MSTDL-956): This should return a pointer with StaticConstantLifetime.
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
@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.

Expand Down
1 change: 1 addition & 0 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,7 @@ struct String(
"""
return self._buffer.data

@deprecated("Use `sys.ffi::c_char_ptr() -> UnsafePointer[c_char]` instead.")
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]:
"""Retrieves a C-string-compatible pointer to the underlying memory.

Expand Down
14 changes: 4 additions & 10 deletions stdlib/src/python/_bindings.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
e.unsafe_cstr_ptr(),
)
cpython.PyErr_SetString(error_type, c_char_ptr(e))

return -1

Expand Down Expand Up @@ -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,
e.unsafe_cstr_ptr(),
)
cpython.PyErr_SetString(error_type, c_char_ptr(e))

# Return a NULL `PyObject*`.
return PythonObject(PyObjectPtr())
Expand Down
11 changes: 4 additions & 7 deletions stdlib/src/python/_cpython.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -206,10 +206,7 @@ struct PyMethodDef:
# type, similar to `get_linkage_name()`?

return PyMethodDef(
func_name.unsafe_cstr_ptr(),
func,
METH_VARARGS,
docstring.unsafe_cstr_ptr(),
c_char_ptr(func_name), func, METH_VARARGS, c_char_ptr(docstring)
)


Expand Down Expand Up @@ -432,7 +429,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
Expand Down Expand Up @@ -1393,7 +1390,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(
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/python/python.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(),
)

Expand Down
44 changes: 38 additions & 6 deletions stdlib/src/sys/ffi.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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]:
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
"""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]:
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
"""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."""

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -160,8 +193,7 @@ struct DLHandle(CollectionElement, CollectionElementNew, Boolable):
]()

var opaque_function_ptr: OpaquePointer = dlsym(
self.handle,
name.unsafe_cstr_ptr(),
self.handle, c_char_ptr(name)
)

return bool(opaque_function_ptr)
Expand Down Expand Up @@ -203,7 +235,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[
Expand Down Expand Up @@ -244,7 +276,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,
Expand All @@ -261,7 +293,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
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/sys/info.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions stdlib/test/builtin/test_string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"))
Expand Down
Loading