Skip to content

Commit 6ebd51e

Browse files
committed
core:clipboard. Minor optimisation.
1 parent 347dbab commit 6ebd51e

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

kivy/core/clipboard/clipboard_winctypes.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,47 @@
1010
if platform != 'win':
1111
raise SystemError('unsupported platform for Windows clipboard')
1212

13+
user32 = ctypes.windll.user32
14+
kernel32 = ctypes.windll.kernel32
15+
msvcrt = ctypes.cdll.msvcrt
16+
c_char_p = ctypes.c_char_p
17+
1318

1419
class ClipboardWindows(ClipboardBase):
1520

1621
def get(self, mimetype='text/plain'):
17-
ctypes.windll.user32.OpenClipboard(0)
22+
user32.OpenClipboard(0)
1823
# 1 is CF_TEXT
19-
pcontents = ctypes.windll.user32.GetClipboardData(1)
20-
data = ctypes.c_char_p(pcontents).value
24+
pcontents = user32.GetClipboardData(1)
25+
data = c_char_p(pcontents).value
2126
#ctypes.windll.kernel32.GlobalUnlock(pcontents)
22-
ctypes.windll.user32.CloseClipboard()
27+
user32.CloseClipboard()
2328
return data
2429

2530
def put(self, data, mimetype='text/plain'):
2631
GMEM_DDESHARE = 0x2000
27-
ctypes.windll.user32.OpenClipboard(0)
28-
ctypes.windll.user32.EmptyClipboard()
32+
user32.OpenClipboard(0)
33+
user32.EmptyClipboard()
2934
try:
3035
# works on Python 2 (bytes() only takes one argument)
31-
hCd = ctypes.windll.kernel32.GlobalAlloc(
36+
hCd = kernel32.GlobalAlloc(
3237
GMEM_DDESHARE, len(bytes(text)) + 1)
3338
except TypeError:
3439
# works on Python 3 (bytes() requires an encoding)
35-
hCd = ctypes.windll.kernel32.GlobalAlloc(
40+
hCd = kernel32.GlobalAlloc(
3641
GMEM_DDESHARE, len(bytes(text, 'ascii')) + 1)
37-
pchData = ctypes.windll.kernel32.GlobalLock(hCd)
42+
pchData = kernel32.GlobalLock(hCd)
3843
try:
3944
# works on Python 2 (bytes() only takes one argument)
40-
ctypes.cdll.msvcrt.strcpy(
41-
ctypes.c_char_p(pchData), bytes(text))
45+
msvcrt.strcpy(
46+
c_char_p(pchData), bytes(text))
4247
except TypeError:
4348
# works on Python 3 (bytes() requires an encoding)
44-
ctypes.cdll.msvcrt.strcpy(
45-
ctypes.c_char_p(pchData), bytes(text, 'ascii'))
46-
ctypes.windll.kernel32.GlobalUnlock(hCd)
47-
ctypes.windll.user32.SetClipboardData(1, hCd)
48-
ctypes.windll.user32.CloseClipboard()
49+
msvcrt.strcpy(
50+
c_char_p(pchData), bytes(text, 'ascii'))
51+
kernel32.GlobalUnlock(hCd)
52+
user32.SetClipboardData(1, hCd)
53+
user32.CloseClipboard()
4954

5055
def get_types(self):
5156
return list('text/plain',)

0 commit comments

Comments
 (0)