Skip to content

Commit

Permalink
[External] [stdlib] Use UnsafePointer in _cpython.mojo (#38798)
Browse files Browse the repository at this point in the history
[External] [stdlib] Use `UnsafePointer` in `_cpython.mojo`

Co-authored-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
Closes #2405
MODULAR_ORIG_COMMIT_REV_ID: 44ed3e851362efb44f26ef7fd44cacfa0a137a80
  • Loading branch information
gabrieldemarmiesse authored and JoeLoser committed Apr 30, 2024
1 parent 48b4859 commit ad5b435
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions stdlib/src/python/_cpython.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from os import getenv
from sys import external_call
from sys.ffi import DLHandle

from memory import DTypePointer, LegacyPointer
from memory import DTypePointer, UnsafePointer

from utils import StringRef, StaticIntTuple

Expand Down Expand Up @@ -156,7 +156,7 @@ struct CPython:
var dict_type: PyObjectPtr
var logging_enabled: Bool
var version: PythonVersion
var total_ref_count: LegacyPointer[Int]
var total_ref_count: UnsafePointer[Int]

fn __init__(inout self: CPython):
var logging_enabled = getenv("MODULAR_CPYTHON_LOGGING") == "ON"
Expand All @@ -172,7 +172,7 @@ struct CPython:
var null_pointer = DTypePointer[DType.int8].get_null()

self.lib = DLHandle(python_lib)
self.total_ref_count = LegacyPointer[Int].alloc(1)
self.total_ref_count = UnsafePointer[Int].alloc(1)
self.none_value = PyObjectPtr(null_pointer)
self.dict_type = PyObjectPtr(null_pointer)
self.logging_enabled = logging_enabled
Expand All @@ -187,7 +187,12 @@ struct CPython:
existing.Py_DecRef(existing.none_value)
if existing.logging_enabled:
print("CPython destroy")
print("Number of remaining refs:", existing.total_ref_count.load())
var remaining_refs = move_from_pointee(existing.total_ref_count)
print("Number of remaining refs:", remaining_refs)
# Technically not necessary since we're working with register
# passable types, by it's good practice to re-initialize the
# pointer after a consuming move.
initialize_pointee_move(existing.total_ref_count, remaining_refs)
_py_finalize(existing.lib)
existing.lib.close()
existing.total_ref_count.free()
Expand Down Expand Up @@ -217,12 +222,12 @@ struct CPython:
self.total_ref_count = existing.total_ref_count

fn _inc_total_rc(inout self):
var v = self.total_ref_count.load()
self.total_ref_count[0] = v + 1
var v = move_from_pointee(self.total_ref_count)
initialize_pointee_move(self.total_ref_count, v + 1)

fn _dec_total_rc(inout self):
var v = self.total_ref_count.load()
self.total_ref_count[0] = v - 1
var v = move_from_pointee(self.total_ref_count)
initialize_pointee_move(self.total_ref_count, v - 1)

fn Py_IncRef(inout self, ptr: PyObjectPtr):
if self.logging_enabled:
Expand Down Expand Up @@ -772,15 +777,15 @@ struct CPython:
var key = DTypePointer[DType.int8].get_null()
var value = DTypePointer[DType.int8].get_null()
var v = p
var position = LegacyPointer[Int].address_of(v)
var position = UnsafePointer[Int].address_of(v)
var value_ptr = UnsafePointer[DTypePointer[DType.int8]].address_of(
value
)
var key_ptr = UnsafePointer[DTypePointer[DType.int8]].address_of(key)
var result = self.lib.get_function[
fn (
PyObjectPtr,
LegacyPointer[Int],
UnsafePointer[Int],
UnsafePointer[DTypePointer[DType.int8]],
UnsafePointer[DTypePointer[DType.int8]],
) -> Int
Expand Down Expand Up @@ -809,6 +814,6 @@ struct CPython:
return PyKeyValuePair {
key: key,
value: value,
position: position.load(),
position: move_from_pointee(position),
success: result == 1,
}

0 comments on commit ad5b435

Please sign in to comment.