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 23 commits into
base: nightly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 2 additions & 5 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 @@ -53,10 +53,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
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
11 changes: 11 additions & 0 deletions stdlib/src/builtin/error.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ struct Error(
# Methods
# ===-------------------------------------------------------------------===#

@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.

Expand All @@ -196,6 +198,15 @@ 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.

Returns:
The raw pointer to the data.
"""
return self.data

fn _message(self) -> String:
"""Converts the Error to string representation.

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 utils import Span, write_buffered, write_args
from collections import InlineArray
Expand Down Expand Up @@ -51,7 +51,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 @@ -168,7 +168,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 @@ -181,7 +181,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 @@ -223,7 +223,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
1 change: 1 addition & 0 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ struct StringLiteral(
# return type.
return ptr.bitcast[UInt8]()

@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]:
Expand Down
3 changes: 3 additions & 0 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,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.

Expand All @@ -1516,6 +1517,8 @@ 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.

Expand Down
16 changes: 4 additions & 12 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 {
# FIXME(MOCO-1306): This should be `T.__name__`.
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 @@ -163,12 +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,
e.unsafe_cstr_ptr(),
)

cpython.PyErr_SetString(error_type, c_char_ptr(e))
return -1


Expand Down Expand Up @@ -266,10 +261,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
10 changes: 4 additions & 6 deletions stdlib/src/python/_cpython.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ from sys.ffi import (
c_size_t,
c_ssize_t,
c_uint,
c_char_ptr,
)

from python.python import _get_global_python_itf
Expand Down Expand Up @@ -371,10 +372,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 @@ -638,7 +636,7 @@ struct PyModuleDef(Stringable, Representable, Writable):

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 @@ -1750,7 +1748,7 @@ struct CPython:
]("PyUnicode_DecodeUTF8")(
strref.data.bitcast[Int8](),
strref.length,
"strict".unsafe_cstr_ptr(),
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
48 changes: 42 additions & 6 deletions stdlib/src/sys/ffi.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,43 @@ 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]:
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 +166,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 +197,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 +239,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 +280,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 +297,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