Skip to content

Commit

Permalink
Python bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
major9999 committed Jun 15, 2023
1 parent de3644c commit 8495170
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions 0x04-python-more_data_structures/103-python.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
#include <Python.h>

/**
* print_python_bytes - a func that prints bytes information.
* @p: The Python Object
* Return: Just Nothing.
*/

void print_python_bytes(PyObject *p)
{
char *string;
long int size, i, limit;

printf("[.] bytes object info\n");

if (!PyBytes_Check(p))

{
printf(" [ERROR] Invalid Bytes Object\n");
return;
}

size = ((PyVarObject *)(p))->ob_size;
string = ((PyBytesObject *)p)->ob_sval;

printf(" size: %ld\n", size);
printf(" trying string: %s\n", string);

if (size >= 10)
{
limit = 10;
}

else
{
limit = size + 1;
}


printf(" first %ld bytes:", limit);

for (i = 0; i < limit; i++)
if (string[i] >= 0)
printf(" %02x", string[i]);
else
printf(" %02x", 256 + string[i]);

printf("\n");
}

/**
* print_python_list - A func that prints list information
* @p: the Python obj to pass in.
* Return: Just Nothing.
*/

void print_python_list(PyObject *p)
{
long int size, i;
PyListObject *list;
PyObject *obj;

size = ((PyVarObject *)(p))->ob_size;
list = (PyListObject *)p;

printf("[*] Python list info\n");
printf("[*] Size of the Python List = %ld\n", size);
printf("[*] Allocated = %ld\n", list->allocated);

for (i = 0; i < size; i++)
{
obj = ((PyListObject *)p)->ob_item[i];
printf("Element %ld: %s\n", i, ((obj)->ob_type)->tp_name);
if (PyBytes_Check(obj))
{
print_python_bytes(obj);
}

}
}

0 comments on commit 8495170

Please sign in to comment.