networking.p2p.lib_types
lib_types
¶
Classes:
| Name | Description |
|---|---|
TypeInterface |
Helper class for converting between Python types and Go types using ctypes. |
Classes¶
TypeInterface
¶
Helper class for converting between Python types and Go types using ctypes.
Methods:
| Name | Description |
|---|---|
to_go_string |
Converts a Python string to a UTF-8 encoded Python 'bytes' object. |
from_go_string |
Converts a C char pointer (Go string) to a Python string. |
to_go_int |
Converts a Python integer to a Go-compatible ctypes integer. |
from_go_int |
Converts a ctypes.c_int from Go to a Python integer. |
to_go_float |
Converts a Python float to a Go-compatible ctypes float. |
from_go_float |
Converts a ctypes.c_float from Go to a Python float. |
to_go_bool |
Converts a Python boolean to a Go-compatible integer (1 if True, 0 if False). |
from_go_bool |
Converts a Go-compatible integer (ctypes.c_int) to a Python boolean. |
to_go_bytes |
Converts a Python bytes object to a Go-compatible C char pointer. |
from_go_bytes |
Converts a Go pointer representing a byte array to a Python bytes object. |
from_go_ptr_to_json |
Converts a C void* pointer (returned by Go as int) pointing to a |
to_go_json |
Encodes a Python object to a JSON string, returning a UTF-8 encoded |
from_go_string_to_list |
Decodes a JSON-encoded list from a Go C char pointer into a Python list. |
Source code in unaiverse/networking/p2p/lib_types.py
Methods:¶
to_go_string
¶
Converts a Python string to a UTF-8 encoded Python 'bytes' object.
This 'bytes' object is suitable for direct use with ctypes when passing to a C function expecting a 'char*' (ctypes.c_char_p), as ctypes will automatically pass a pointer to the byte string's data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The Python string. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A Python 'bytes' object containing the UTF-8 encoded string. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_string
¶
Converts a C char pointer (Go string) to a Python string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cstr
|
bytes
|
The C char pointer. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The decoded Python string. |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_int
¶
Converts a Python integer to a Go-compatible ctypes integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
int
|
The Python integer. |
required |
Returns:
| Type | Description |
|---|---|
c_int
|
A ctypes.c_int equivalent. |
from_go_int
¶
Converts a ctypes.c_int from Go to a Python integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_int
|
The ctypes.c_int value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The corresponding Python integer. |
to_go_float
¶
Converts a Python float to a Go-compatible ctypes float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
float
|
The Python float. |
required |
Returns:
| Type | Description |
|---|---|
c_float
|
A ctypes.c_float equivalent. |
from_go_float
¶
Converts a ctypes.c_float from Go to a Python float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_float
|
The ctypes.c_float value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The corresponding Python float. |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_bool
¶
Converts a Python boolean to a Go-compatible integer (1 if True, 0 if False).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
bool
|
The Python boolean. |
required |
Returns:
| Type | Description |
|---|---|
c_int
|
A ctypes.c_int (1 or 0). |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_bool
¶
Converts a Go-compatible integer (ctypes.c_int) to a Python boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_int
|
The ctypes.c_int value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the value equals 1, False otherwise. |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_bytes
¶
Converts a Python bytes object to a Go-compatible C char pointer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
bytes
|
The Python bytes. |
required |
Returns:
| Type | Description |
|---|---|
c_char_p
|
A ctypes.c_char_p pointing to the byte data. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_bytes
¶
Converts a Go pointer representing a byte array to a Python bytes object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cptr
|
c_char_p
|
The C pointer to the byte array. |
required |
length
|
int
|
The number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A Python bytes object containing the read data. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_ptr_to_json
¶
Converts a C void* pointer (returned by Go as int) pointing to a null-terminated C string containing JSON into a Python object.
It reads the string, parses it as JSON, and crucially frees the C memory using the provided FreeString function from the Go library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c_void_ptr_val
|
int
|
The integer value representing the C pointer address. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The parsed Python object from the JSON string. |
Raises:
| Type | Description |
|---|---|
GoLibError
|
If the pointer is NULL, reading/decoding fails, or JSON parsing fails. |
TypeError
|
When go_lib is not a valid ctypes library object. |
Source code in unaiverse/networking/p2p/lib_types.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 | |
to_go_json
¶
Encodes a Python object to a JSON string, returning a UTF-8 encoded Python 'bytes' object.
This 'bytes' object is suitable for direct use with ctypes when passing to a C function expecting a 'char*' (ctypes.c_char_p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The Python object (e.g., dict, list) to encode. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A Python 'bytes' object containing the JSON string, UTF-8 encoded. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_string_to_list
¶
Decodes a JSON-encoded list from a Go C char pointer into a Python list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cstr
|
c_char_p
|
The Go string (C char pointer) containing a JSON list. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A Python list representing the JSON data. |