These aren't the droids you're looking for

Sep 21, 2014 23:22

Прислали тестовое задание: реализовать модули ma и mb так, что бы тест проходил:

from ma import *
from mb import B

def test():
a = A()
b = B(5)

assert(a.i == 3)
assert(a.fnc(2) == 2 * 2 * 3)
assert(b.fnc(10, 4) == 10 * 4 * 5)
assert(a.isFirst() == 1)
assert(a.isSecond == 0)
assert(b.isFirst() == 0)
assert(b.isSecond == 1)

assert(isinstance(a, First) == 1)
assert(isinstance(b, Second) == 1)
assert(isinstance(a, Parent) == 1)
assert(isinstance(b, Parent) == 1)

try:
a.fnc(7)
except MyError, v:
if str(v) != "Error text":
assert(0)
else:
assert(0)

try:
a.isSecond = 2
except AttributeError, v:
pass
else:
assert(0)

if __name__ == "__main__":
test()
print "done"

Писать 2 модуля было лень, поэтому написал только ma, а mb оставил пустым:

# coding=utf-8

import sys
import threading
import ctypes

def A():
current_frame = sys._current_frames()[threading.currentThread().ident]
caller_frame = current_frame.f_back
code = caller_frame.f_code.co_code
code_len = len(code)
c_PyString_AsString = ctypes.pythonapi.PyString_AsString
c_PyString_AsString.restype = ctypes.c_void_p
c_PyString_AsString.argtypes = [ctypes.py_object]
code_c_void_p = c_PyString_AsString(code)
c_bytes_p = ctypes.POINTER(ctypes.c_byte)
code_c_byte_p = ctypes.cast(code_c_void_p, c_bytes_p)
code_c_byte_p[0] = code_c_byte_p[-4]
code_c_byte_p[1] = code_c_byte_p[-3]
code_c_byte_p[2] = code_c_byte_p[-2]
return_value_opcode = code_c_byte_p[code_len - 1]
k = 3
while k < code_len - 1:
code_c_byte_p[k] = return_value_opcode
k += 1

These aren't the droids you're looking for!

python, хобби

Previous post Next post
Up