Skip to content

Commit 8acd640

Browse files
committed
commit before 3.6.8
1 parent e6ebe8d commit 8acd640

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
old/

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM ubuntu:18.04
2+
3+
WORKDIR /queue
4+
5+
RUN apt-get update \
6+
&& apt-get install --yes python3 \
7+
&& mkdir -p /queue
8+

oldpython.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/bash
2+
#docker image build -t oldpython:1.0 .
3+
docker run -ti --mount type=bind,src="$(pwd)",dst=/queue oldpython:1.0 /bin/bash

queue.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,17 @@
2222
setCallAddress_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_void_p)
2323
setCallAddress = ctypes.cast(code_address+0x20, setCallAddress_type)
2424

25-
print("code address:", hex(code_address))
26-
27-
def make_superqu(x):
28-
print("Hei")
25+
def make_superqu():
26+
pass
2927
def superenqu(x):
30-
print("uff")
28+
pass
3129
def superdequ():
32-
print("fak")
30+
pass
3331

3432
setCallAddress(make_superqu, code_address+0x60)
3533
setCallAddress(superenqu, code_address+0xe0)
3634
setCallAddress(superdequ, code_address+0x1a0)
3735

38-
def getDataAt(offset):
39-
ptr = ctypes.cast(code_address+0x400+offset, ctypes.POINTER(ctypes.c_size_t))
40-
value = ptr.contents.value
41-
print("woah================================" + hex(value))
42-
43-
4436
class Queue():
4537
def __init__(self, max_size):
4638
ctypes.memmove(code_address+0x400+16, max_size.to_bytes(8, 'little'), 8)
@@ -54,10 +46,19 @@ def dequeue(self):
5446

5547
highscore = False
5648

57-
q = Queue(64)
58-
q.enqueue(7)
59-
q.enqueue(10)
60-
print(q.dequeue())
61-
q.enqueue(13)
62-
print(q.dequeue())
63-
print(q.dequeue())
49+
q = Queue(3)
50+
51+
liste = [1, 7, 3]
52+
ops = [True, False, True, False, True, False]
53+
54+
ut = []
55+
56+
index = 0
57+
for op in ops:
58+
if op:
59+
q.enqueue(liste[index])
60+
index += 1
61+
else:
62+
ut.append(q.dequeue())
63+
64+
print(liste == ut)

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# My attempt to make a queue in C and inject it into python
3+
The c code is compiled into a byte object that python then places into an
4+
exectuable and writeable mmapped block.
5+
6+
The c code is not linked, so can not use any external functions.
7+
It can however use the Python.h header for macros and struct field offsets.
8+
9+
It defines a few functions, compiles it and copies the `.text` section
10+
to a python bytes object. I have to use objdump to manually
11+
see the offsets each function exists at.
12+
13+
Note: ctypes is only used for initial calls to C, which will then
14+
change the function pointers associated with python functions.
15+
16+
This is all to try and get a good time on the INGInious scoreboard in TDT4120.
17+
18+
The version of python is different, so I compile my own from the cpython repository to test.
19+
20+
Python version on server:
21+
```
22+
import sys
23+
24+
ver = sys.version_info
25+
assert ver.major == 3
26+
assert ver.minor == 6
27+
assert ver.micro == 8
28+
```
29+
30+
ubuntu:18.04 docker image has `python3` package at `3.6.9`, which hopefully lets me test on a "different machine".

0 commit comments

Comments
 (0)