Skip to content

Commit 8495170

Browse files
committed
Python bytes
1 parent de3644c commit 8495170

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdio.h>
2+
#include <Python.h>
3+
4+
/**
5+
* print_python_bytes - a func that prints bytes information.
6+
* @p: The Python Object
7+
* Return: Just Nothing.
8+
*/
9+
10+
void print_python_bytes(PyObject *p)
11+
{
12+
char *string;
13+
long int size, i, limit;
14+
15+
printf("[.] bytes object info\n");
16+
17+
if (!PyBytes_Check(p))
18+
19+
{
20+
printf(" [ERROR] Invalid Bytes Object\n");
21+
return;
22+
}
23+
24+
size = ((PyVarObject *)(p))->ob_size;
25+
string = ((PyBytesObject *)p)->ob_sval;
26+
27+
printf(" size: %ld\n", size);
28+
printf(" trying string: %s\n", string);
29+
30+
if (size >= 10)
31+
{
32+
limit = 10;
33+
}
34+
35+
else
36+
{
37+
limit = size + 1;
38+
}
39+
40+
41+
printf(" first %ld bytes:", limit);
42+
43+
for (i = 0; i < limit; i++)
44+
if (string[i] >= 0)
45+
printf(" %02x", string[i]);
46+
else
47+
printf(" %02x", 256 + string[i]);
48+
49+
printf("\n");
50+
}
51+
52+
/**
53+
* print_python_list - A func that prints list information
54+
* @p: the Python obj to pass in.
55+
* Return: Just Nothing.
56+
*/
57+
58+
void print_python_list(PyObject *p)
59+
{
60+
long int size, i;
61+
PyListObject *list;
62+
PyObject *obj;
63+
64+
size = ((PyVarObject *)(p))->ob_size;
65+
list = (PyListObject *)p;
66+
67+
printf("[*] Python list info\n");
68+
printf("[*] Size of the Python List = %ld\n", size);
69+
printf("[*] Allocated = %ld\n", list->allocated);
70+
71+
for (i = 0; i < size; i++)
72+
{
73+
obj = ((PyListObject *)p)->ob_item[i];
74+
printf("Element %ld: %s\n", i, ((obj)->ob_type)->tp_name);
75+
if (PyBytes_Check(obj))
76+
{
77+
print_python_bytes(obj);
78+
}
79+
80+
}
81+
}

0 commit comments

Comments
 (0)