Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31e9730

Browse files
task 2
1 parent 541065f commit 31e9730

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

‎0x0A_CPython/2-python.c‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <Python.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
void print_python_list(PyObject *p);
6+
void print_python_bytes(PyObject *p);
7+
8+
/**
9+
* print_python_bytes - print some basic info about Python bytes objects
10+
* @p: python object
11+
* Return: nothing
12+
**/
13+
void print_python_bytes(PyObject *p)
14+
{
15+
char *s;
16+
Py_ssize_t l, i;
17+
18+
printf("[.] bytes object info\n");
19+
if (!PyBytes_Check(p))
20+
printf(" [ERROR] Invalid Bytes Object\n");
21+
else
22+
{
23+
l = ((PyVarObject *) p)->ob_size;
24+
printf(" size: %lu\n", l);
25+
s = ((PyBytesObject *) p)->ob_sval;
26+
printf(" trying string: %s\n", s);
27+
if (l > 10)
28+
l = 10;
29+
else
30+
l++;
31+
printf(" first %lu bytes: ", l);
32+
for (i = 0; i < l - 1; i++)
33+
printf("%02x ", s[i] & 0xff);
34+
printf("%02x\n", s[l - 1] & 0xff);
35+
}
36+
}
37+
38+
/**
39+
* print_python_list - print some basic info about Python lists
40+
* @p: python object
41+
**/
42+
void print_python_list(PyObject *p)
43+
{
44+
Py_ssize_t listsize, i;
45+
PyObject *item;
46+
47+
printf("[*] Python list info\n");
48+
if (!PyList_Check(p))
49+
printf(" [ERROR] Invalid List Object\n");
50+
else
51+
{
52+
listsize = ((PyVarObject *) p)->ob_size;
53+
printf("[*] Size of the Python List = %lu\n", listsize);
54+
printf("[*] Allocated = %lu\n",
55+
((PyListObject *) p)->allocated);
56+
for (i = 0; i < listsize; i++)
57+
{
58+
item = ((PyListObject *) p)->ob_item[i];
59+
printf("Element %lu: %s\n", i, item->ob_type->tp_name);
60+
if (strcmp(item->ob_type->tp_name, "bytes") == 0)
61+
print_python_bytes(item);
62+
}
63+
}
64+
}

‎0x0A_CPython/tests/2-tests.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import ctypes
2+
3+
lib = ctypes.CDLL('./libPython.so')
4+
lib.print_python_list.argtypes = [ctypes.py_object]
5+
lib.print_python_bytes.argtypes = [ctypes.py_object]
6+
s = b"Hello"
7+
lib.print_python_bytes(s);
8+
b = b'\xff\xf8\x00\x00\x00\x00\x00\x00';
9+
lib.print_python_bytes(b);
10+
b = b'What does the \'b\' character do in front of a string literal?';
11+
lib.print_python_bytes(b);
12+
l = [b'Hello', b'World']
13+
lib.print_python_list(l)
14+
del l[1]
15+
lib.print_python_list(l)
16+
l = l + [4, 5, 6.0, (9, 8), [9, 8, 1024], b"Holberton", "Betty"]
17+
lib.print_python_list(l)
18+
l = []
19+
lib.print_python_list(l)
20+
l.append(0)
21+
lib.print_python_list(l)
22+
l.append(1)
23+
l.append(2)
24+
l.append(3)
25+
l.append(4)
26+
lib.print_python_list(l)
27+
l.pop()
28+
lib.print_python_list(l)
29+
l = ["Holberton"]
30+
lib.print_python_list(l)
31+
lib.print_python_bytes(l);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /