π΄ unaiverse.networking.p2p.lib_types
What this module does π΄
Provides the TypeInterface marshalling layer that converts Python values to and from Go ctypes representations (strings, ints, floats, bools, bytes, JSON) and manages freeing of Go-allocated pointers.
lib_types
¶
βββββ βββββ ββββββ βββββ βββββ βββββ βββββ ββββββββββ βββββββββββ βββββββββ ββββββββββ
βββββ βββββ ββββββββ βββββ βββββ βββββ βββββ ββββββββββββββββββββββββ ββββββββββββββββββββββ
ββββ ββββ ββββββββ ββββ ββββββ ββββ ββββ ββββ ββββ β β ββββ ββββ ββββ βββ ββββ β β
ββββ ββββ βββββββββββββ ββββββββ ββββ ββββ ββββ βββββββ βββββββββββ βββββββββββ βββββββ
ββββ ββββ ββββ ββββββββ βββββββ ββββ βββββ βββ βββββββ ββββββββββββ βββββββββββ βββββββ
ββββ ββββ ββββ βββββββ ββββββββ ββββ βββββββββ ββββ β β ββββ ββββ βββ ββββ ββββ β β
ββββββββββ βββββ βββββββββββββββββ βββββ βββββ ββββββββββ βββββ ββββββββββββββββ ββββββββββ
ββββββββ βββββ βββββ ββββββββ βββββ βββ ββββββββββ βββββ βββββ βββββββββ ββββββββββ
A Collectionless AI Project (https://collectionless.ai)
Registration/Login: https://unaiverse.io
Code Repositories: https://github.com/collectionlessai/
Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
TypeInterface
¶
TypeInterface(libp2p_instance: GoLibP2P)
Low-level bridge for converting between Python types and Go/C types via ctypes.
TypeInterface centralises all marshalling and unmarshalling needed when calling
functions exported by a Go shared library through ctypes. Every method either
converts a Python value into a C-compatible representation ready to pass into a Go
function, or converts a value returned by Go back into a native Python type.
A particular concern is memory safety: Go functions that return heap-allocated C
strings transfer ownership of that memory to the caller. from_go_ptr_to_json
therefore calls FreeString on the underlying GoLibP2P instance to release
the memory, and maintains an internal freed-pointer registry protected by a
threading.Lock to guard against accidental double-free errors.
Attributes:
| Name | Type | Description |
|---|---|---|
libp2p |
GoLibP2P
|
The |
Initialize the type bridge with a loaded Go shared-library instance.
An internal freed-pointer registry (a set guarded by a threading.Lock)
is created to detect and prevent double-free errors across concurrent calls
to from_go_ptr_to_json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
libp2p_instance
|
GoLibP2P
|
A fully loaded |
required |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_string
¶
Convert a Python string to a UTF-8 encoded bytes object for Go/C interop.
The returned bytes value is suitable for direct use with ctypes when passing
to a C function that expects a char* (ctypes.c_char_p): ctypes
automatically passes a pointer to the byte string's internal data buffer.
If s is None it is treated as an empty string, so callers do not need
to guard against None before invoking this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The Python string to encode. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_string
¶
Convert a null-terminated C string returned by Go into a Python str.
The argument is expected to be the raw value obtained from a ctypes.c_char_p
result type, which ctypes exposes as a bytes object (or None for a NULL
pointer). A falsy value (None or empty bytes) is mapped to an empty string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cstr
|
bytes
|
The raw bytes value from a |
required |
Returns:
| Type | Description |
|---|---|
str
|
The UTF-8 decoded Python string, or an empty string when |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_int
¶
Convert a Python integer to a Go-compatible ctypes.c_int.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
int
|
The Python integer to convert. |
required |
Returns:
| Type | Description |
|---|---|
c_int
|
A |
c_int
|
function that expects a C |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_int
¶
Convert a ctypes.c_int returned by Go into a Python int.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_int
|
The |
required |
Returns:
| Type | Description |
|---|---|
int
|
The corresponding Python |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_float
¶
Convert a Python float to a Go-compatible ctypes.c_float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
float
|
The Python float to convert. |
required |
Returns:
| Type | Description |
|---|---|
c_float
|
A |
c_float
|
function that expects a C |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_float
¶
Convert a ctypes.c_float returned by Go into a Python float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_float
|
The |
required |
Returns:
| Type | Description |
|---|---|
float
|
The corresponding Python |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_bool
¶
Convert a Python boolean to a Go-compatible integer (1 for True, 0 for False).
Go does not export a dedicated bool type across the C boundary; booleans are
conventionally represented as int values where non-zero means true.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
bool
|
The Python boolean to convert. |
required |
Returns:
| Type | Description |
|---|---|
c_int
|
A |
c_int
|
when |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_bool
¶
Convert a Go-compatible integer (ctypes.c_int) to a Python boolean.
The convention is that a value of exactly 1 means True; any other
value (including negative integers) is treated as False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_int
|
The |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in unaiverse/networking/p2p/lib_types.py
to_go_bytes
¶
Convert a Python bytes object to a Go-compatible ctypes.c_char_p.
A fixed-size C buffer is allocated via ctypes.create_string_buffer and
then cast to ctypes.c_char_p so that Go receives a stable pointer to the
byte data. If b is None, an empty byte string is used instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
bytes
|
The Python |
required |
Returns:
| Type | Description |
|---|---|
c_char_p
|
A |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_bytes
¶
Convert a raw C byte buffer returned by Go into a Python bytes object.
ctypes.string_at is used to copy exactly length bytes starting at
the address given by cptr. If either cptr is a NULL pointer or
length is not positive, an empty bytes object is returned immediately
without attempting a memory read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cptr
|
c_char_p
|
The |
required |
length
|
int
|
The number of bytes to read from |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A |
bytes
|
when |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_ptr_to_json
¶
Convert a Go-allocated C string containing JSON into a Python object, then free the memory.
The full lifecycle is: validate the pointer is not NULL and has not already
been freed; cast the integer address to ctypes.c_char_p and read the
null-terminated byte string; decode it as UTF-8; parse the resulting string
as JSON; and - in a finally block that executes even on error - call
libp2p.FreeString to release the C heap memory allocated by Go.
A freed-pointer registry (protected by an internal threading.Lock) guards
against double-free errors. If the same pointer address is passed a second
time before the registry is cleared, the method raises instead of calling
FreeString again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c_void_ptr_val
|
int
|
The integer address of the C string allocated by Go.
A value of |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The Python object produced by parsing the JSON string (typically a |
Any
|
|
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Exception
|
If the pointer was already freed (detected via the internal registry), indicating a double-free logic error. |
Exception
|
If reading or UTF-8 decoding the C string fails. |
Exception
|
If the string is not valid JSON ( |
Note
FreeString is always called in the finally block regardless of
whether JSON parsing succeeds or fails, to avoid memory leaks. If
FreeString itself raises, the error is logged at CRITICAL level
but is not re-raised so that the original exception (if any) propagates
unchanged.
Source code in unaiverse/networking/p2p/lib_types.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
to_go_json
¶
Encode a Python object to a JSON string and return it as UTF-8 bytes for Go/C interop.
The object is serialised with json.dumps and the resulting string is then
passed through to_go_string to produce a UTF-8 encoded bytes value.
The returned value is suitable for direct use with ctypes when passing to a C
function that expects a char* (ctypes.c_char_p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The Python object to serialise (for example a |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A |
bytes
|
|
Source code in unaiverse/networking/p2p/lib_types.py
from_go_string_to_list
¶
Decode a JSON-encoded list from a Go C string into a Python list.
The C string is first converted to a Python str via from_go_string, and
the resulting JSON text is then parsed with json.loads. The Go-exported
function is expected to produce a JSON array; if the content is not a valid JSON
list the call will raise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cstr
|
c_char_p
|
The raw |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A Python |
Raises:
| Type | Description |
|---|---|
JSONDecodeError
|
If |
UnicodeDecodeError
|
If the byte sequence in |