<commit_before>import contextlib
import ctypes

user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

# Types
BOOL = ctypes.c_uint32
HWND = ctypes.c_uint32
NULL = 0

GMEM_MOVEABLE = 0x0002
CF_TEXT = 1


class ClipboardError(Exception):
    pass


def OpenClipboard(hWndNewOwner=NULL):
    hWndNewOwner = HWND(hWndNewOwner)
    success = user32.OpenClipboard(hWndNewOwner)
    if not success:
        raise ClipboardError("Cannot Open Clipboard")


@contextlib.contextmanager
def clipboard(hWndNewOwner=NULL):
    try:
        OpenClipboard(hWndNewOwner)
        yield
    finally:
        CloseClipboard()

def CloseClipboard():
    success = user32.CloseClipboard()
    if not success:
        raise ClipboardError("Cannot close clipboard")

def EmptyClipboard():
    return user32.EmptyClipboard()


def global_lock(memory_handle):
    memory = kernel32.GlobalLock(memory_handle)
    if not memory:
        raise MemoryError("Failed locking global memory")
    return ctypes.c_char_p(memory)


def global_unlock(hGlobalMemory):
    is_locked = kernel32.GlobalUnlock(hGlobalMemory)
    if is_locked:
        raise MemoryError("memory is still locked.")


@contextlib.contextmanager
def global_memory(hGlobalMemory):
    memory = global_lock(hGlobalMemory)
    yield memory
    global_unlock(hGlobalMemory)


def Copy(text):
    with clipboard():
        EmptyClipboard()
        # Assuming we are handling ascii
        data = ctypes.create_string_buffer(text)
        memory_handle = kernel32.GlobalAlloc(GMEM_MOVEABLE, ctypes.sizeof(data))
        if not memory_handle:
            #TODO: use GetLastError() here.
            raise MemoryError("Allocation failed.")

        with global_memory(memory_handle) as memory:
            ctypes.memmove(memory, data, ctypes.sizeof(data))

        user32.SetClipboardData(CF_TEXT, memory_handle)


if __name__ == '__main__':
    Copy("what is this?")<commit_msg>Added implementation for `paste` as well.<commit_after>import contextlib
import ctypes

user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

# Types
BOOL = ctypes.c_uint32
HWND = ctypes.c_uint32
NULL = 0

GMEM_MOVEABLE = 0x0002
CF_TEXT = 1


class ClipboardError(Exception):
    pass


def OpenClipboard(hWndNewOwner=NULL):
    hWndNewOwner = HWND(hWndNewOwner)
    success = user32.OpenClipboard(hWndNewOwner)
    if not success:
        raise ClipboardError("Cannot Open Clipboard")


@contextlib.contextmanager
def clipboard(hWndNewOwner=NULL):
    try:
        OpenClipboard(hWndNewOwner)
        yield
    finally:
        CloseClipboard()

def CloseClipboard():
    success = user32.CloseClipboard()
    if not success:
        raise ClipboardError("Cannot close clipboard")

def EmptyClipboard():
    return user32.EmptyClipboard()


def global_lock(memory_handle):
    memory = kernel32.GlobalLock(memory_handle)
    if not memory:
        raise MemoryError("Failed locking global memory")
    return ctypes.c_char_p(memory)


def global_unlock(hGlobalMemory):
    is_locked = kernel32.GlobalUnlock(hGlobalMemory)
    # No need to check return value??
    # if is_locked:
    #     raise MemoryError("memory is still locked.")


@contextlib.contextmanager
def global_memory(hGlobalMemory):
    memory = global_lock(hGlobalMemory)
    yield memory
    global_unlock(hGlobalMemory)


def Copy(text):
    with clipboard():
        EmptyClipboard()
        # Assuming we are handling ascii
        data = ctypes.create_string_buffer(text)
        memory_handle = kernel32.GlobalAlloc(GMEM_MOVEABLE, ctypes.sizeof(data))
        if not memory_handle:
            #TODO: use GetLastError() here.
            raise MemoryError("Allocation failed.")

        with global_memory(memory_handle) as memory:
            ctypes.memmove(memory, data, ctypes.sizeof(data))

        user32.SetClipboardData(CF_TEXT, memory_handle)

        kernel32.GlobalFree(memory_handle)


def GetClipboardData():
    hglb = user32.GetClipboardData(CF_TEXT)
    if not hglb:
        raise ClipboardError("Cannot get data.")
    return hglb

def Paste():
    with clipboard():
        hglb = GetClipboardData()
        with global_memory(hglb) as memory:
            return memory.value

if __name__ == '__main__':
    print Paste()
    Copy("what is this?")
    print Paste()
{'fraction_non_alphanumeric': 0.08471411901983664, 'fraction_numerical': 0.01633605600933489, 'mean_word_length': 3.45067497403946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2426294', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1322, 'n_words': 302}