An open API service indexing awesome lists of open source software.

https://github.com/bontail/pystrector

Package for displaying and changing core Python structures
https://github.com/bontail/pystrector

c python3 structures

Last synced: 10 months ago
JSON representation

Package for displaying and changing core Python structures

Awesome Lists containing this project

README

          

# Pystrector

### The **Py**_(thon)_ **Str**_(uct)_ _(Refl)_**ector**

![icon](docs/pystrector_icon.png)

---

The package for displaying and modifying Python's internal structures.
Do you want to see how Python objects are arranged inside?
Then this package is for you.

```python
from pystrector import Binder
binder = Binder()
some_object = 1
reflector = binder.bind(some_object)
print(reflector.ob_base.ob_refcnt.pretty_value)
```

---

### Python

To install pystrector, enter the command.

```shell
python3 -m pip install pystrector
```

---

### Git

```shell
git clone https://github.com/bontail/pystrector.git
```

---

### Documentation

[Russian](./docs/README-RU.md)

To access the representation of the basic structures, you need to create an anchor object

```python
from pystrector import Binder
binder = Binder()
```

Now you can call the binding method to get a class object representing the structure

```python
some_object = 1
reflector = binder.bind(some_object)
```

The display object has all the same fields as the internal structure

```c
// core structure
struct _longobject {
PyObject ob_base;
_PyLongValue long_value;
};
```

```python
class _longobject(DataType, is_union=False):
ob_base = _object()
long_value = _PyLongValue()
```

If an object contains an anonymous_var, then you can go straight to the fields of this object

```python
class anonymous_1(DataType, is_union=True):
ob_refcnt = LongLong()
ob_refcnt_split = UnsignedInt[2]


class _object(DataType, is_union=False):
anonymous_var_1 = anonymous_1()
ob_type = Pointer(datatype="_typeobject")

some_object = 1
reflector = binder.bind(some_object).cast_to(_object)
# print(reflector.anonymous_var_1.ob_refcnt.pretty_value)
print(reflector.ob_refcnt.pretty_value)
```

For each type, you can call pretty_value and bytes_value

**pretty_value** - will result in the most similar type in Python

**bytes_value** - always returns bytearray

```python
print(reflector.long_value.lv_tag.pretty_value)
print(reflector.long_value.lv_tag.bytes_value)
```

You can also set values

**bytes_value** - accepts only bytearray

**pretty_value** - accepts a similar Python type

**without parameters** - takes another object from the mapper

```python
reflector.ob_base.ob_refcnt.bytes_value = bytearray(1)
reflector.ob_base.ob_refcnt.pretty_value = 1000
reflector.ob_base.ob_refcnt = binder.bind(7).ob_base.ob_refcnt
```

There is also work with pointers and arrays as in C

```python
x = [1, 2, 3]
print(binder.bind(x).ob_item[j][1])
print(+(binder.bind(x).ob_item[j]))
print(+(binder.bind(x).ob_item[j] + 1))
```

You can convert mappers of some data types to others

```python
from pystrector.core_datatypes import _longobject

x = [1, 2, 3]

binder.bind(x).ob_item[0][0].cast_to(_longobject) # need to cast because list saves PyObjects
```

Or use auto cast (works only with PyObject)

```python
x = [1, 2, 3]

binder.bind(x).ob_item[0][0].cast()
```

More examples can be seen in [tests](./tests)