__init__.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. """
  2. Pyperclip
  3. A cross-platform clipboard module for Python, with copy & paste functions for plain text.
  4. By Al Sweigart al@inventwithpython.com
  5. BSD License
  6. Usage:
  7. import pyperclip
  8. pyperclip.copy('The text to be copied to the clipboard.')
  9. spam = pyperclip.paste()
  10. if not pyperclip.is_available():
  11. print("Copy functionality unavailable!")
  12. On Windows, no additional modules are needed.
  13. On Mac, the pyobjc module is used, falling back to the pbcopy and pbpaste cli
  14. commands. (These commands should come with OS X.).
  15. On Linux, install xclip, xsel, or wl-clipboard (for "wayland" sessions) via package manager.
  16. For example, in Debian:
  17. sudo apt-get install xclip
  18. sudo apt-get install xsel
  19. sudo apt-get install wl-clipboard
  20. Otherwise on Linux, you will need the qtpy or PyQt5 modules installed.
  21. This module does not work with PyGObject yet.
  22. Cygwin is currently not supported.
  23. Security Note: This module runs programs with these names:
  24. - which
  25. - pbcopy
  26. - pbpaste
  27. - xclip
  28. - xsel
  29. - wl-copy/wl-paste
  30. - klipper
  31. - qdbus
  32. A malicious user could rename or add programs with these names, tricking
  33. Pyperclip into running them with whatever permissions the Python process has.
  34. """
  35. __version__ = '1.11.0'
  36. import base64
  37. import contextlib
  38. import ctypes
  39. import os
  40. import platform
  41. import subprocess
  42. import sys
  43. import time
  44. import warnings
  45. from ctypes import c_size_t, sizeof, c_wchar_p, get_errno, c_wchar
  46. from typing import Union, Optional
  47. _IS_RUNNING_PYTHON_2 = sys.version_info[0] == 2 # type: bool
  48. # For paste(): Python 3 uses str, Python 2 uses unicode.
  49. if _IS_RUNNING_PYTHON_2:
  50. # mypy complains about `unicode` for Python 2, so we ignore the type error:
  51. _PYTHON_STR_TYPE = unicode # type: ignore
  52. else:
  53. _PYTHON_STR_TYPE = str
  54. ENCODING = 'utf-8' # type: str
  55. try:
  56. # Use shutil.which() for Python 3+
  57. from shutil import which
  58. def _py3_executable_exists(name): # type: (str) -> bool
  59. return bool(which(name))
  60. _executable_exists = _py3_executable_exists
  61. except ImportError:
  62. # Use the "which" unix command for Python 2.7 and prior.
  63. def _py2_executable_exists(name): # type: (str) -> bool
  64. return subprocess.call(['which', name],
  65. stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
  66. _executable_exists = _py2_executable_exists
  67. # Exceptions
  68. class PyperclipException(RuntimeError):
  69. pass
  70. class PyperclipWindowsException(PyperclipException):
  71. def __init__(self, message):
  72. message += " (%s)" % ctypes.WinError()
  73. super(PyperclipWindowsException, self).__init__(message)
  74. class PyperclipTimeoutException(PyperclipException):
  75. pass
  76. def init_osx_pbcopy_clipboard():
  77. def copy_osx_pbcopy(text):
  78. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  79. p = subprocess.Popen(['pbcopy', 'w'],
  80. stdin=subprocess.PIPE, close_fds=True)
  81. p.communicate(input=text.encode(ENCODING))
  82. def paste_osx_pbcopy():
  83. p = subprocess.Popen(['pbpaste', 'r'],
  84. stdout=subprocess.PIPE, close_fds=True)
  85. stdout, stderr = p.communicate()
  86. return stdout.decode(ENCODING)
  87. return copy_osx_pbcopy, paste_osx_pbcopy
  88. def init_osx_pyobjc_clipboard():
  89. def copy_osx_pyobjc(text):
  90. '''Copy string argument to clipboard'''
  91. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  92. newStr = Foundation.NSString.stringWithString_(text).nsstring()
  93. newData = newStr.dataUsingEncoding_(Foundation.NSUTF8StringEncoding)
  94. board = AppKit.NSPasteboard.generalPasteboard()
  95. board.declareTypes_owner_([AppKit.NSStringPboardType], None)
  96. board.setData_forType_(newData, AppKit.NSStringPboardType)
  97. def paste_osx_pyobjc():
  98. "Returns contents of clipboard"
  99. board = AppKit.NSPasteboard.generalPasteboard()
  100. content = board.stringForType_(AppKit.NSStringPboardType)
  101. return content
  102. return copy_osx_pyobjc, paste_osx_pyobjc
  103. def init_qt_clipboard():
  104. global QApplication
  105. # $DISPLAY should exist
  106. # Try to import from qtpy, but if that fails try PyQt5
  107. try:
  108. from qtpy.QtWidgets import QApplication
  109. except:
  110. from PyQt5.QtWidgets import QApplication
  111. app = QApplication.instance()
  112. if app is None:
  113. app = QApplication([])
  114. def copy_qt(text):
  115. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  116. cb = app.clipboard()
  117. cb.setText(text)
  118. def paste_qt():
  119. cb = app.clipboard()
  120. return _PYTHON_STR_TYPE(cb.text())
  121. return copy_qt, paste_qt
  122. def init_xclip_clipboard():
  123. DEFAULT_SELECTION='c'
  124. PRIMARY_SELECTION='p'
  125. def copy_xclip(text, primary=False):
  126. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  127. selection=DEFAULT_SELECTION
  128. if primary:
  129. selection=PRIMARY_SELECTION
  130. p = subprocess.Popen(['xclip', '-selection', selection],
  131. stdin=subprocess.PIPE, close_fds=True)
  132. p.communicate(input=text.encode(ENCODING))
  133. def paste_xclip(primary=False):
  134. selection=DEFAULT_SELECTION
  135. if primary:
  136. selection=PRIMARY_SELECTION
  137. p = subprocess.Popen(['xclip', '-selection', selection, '-o'],
  138. stdout=subprocess.PIPE,
  139. stderr=subprocess.PIPE,
  140. close_fds=True)
  141. stdout, stderr = p.communicate()
  142. # Intentionally ignore extraneous output on stderr when clipboard is empty
  143. return stdout.decode(ENCODING)
  144. return copy_xclip, paste_xclip
  145. def init_xsel_clipboard():
  146. DEFAULT_SELECTION='-b'
  147. PRIMARY_SELECTION='-p'
  148. def copy_xsel(text, primary=False):
  149. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  150. selection_flag = DEFAULT_SELECTION
  151. if primary:
  152. selection_flag = PRIMARY_SELECTION
  153. p = subprocess.Popen(['xsel', selection_flag, '-i'],
  154. stdin=subprocess.PIPE, close_fds=True)
  155. p.communicate(input=text.encode(ENCODING))
  156. def paste_xsel(primary=False):
  157. selection_flag = DEFAULT_SELECTION
  158. if primary:
  159. selection_flag = PRIMARY_SELECTION
  160. p = subprocess.Popen(['xsel', selection_flag, '-o'],
  161. stdout=subprocess.PIPE, close_fds=True)
  162. stdout, stderr = p.communicate()
  163. return stdout.decode(ENCODING)
  164. return copy_xsel, paste_xsel
  165. def init_wl_clipboard():
  166. PRIMARY_SELECTION = "-p"
  167. def copy_wl(text, primary=False):
  168. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  169. args = ["wl-copy"]
  170. if primary:
  171. args.append(PRIMARY_SELECTION)
  172. if not text:
  173. args.append('--clear')
  174. subprocess.check_call(args, close_fds=True)
  175. else:
  176. pass
  177. p = subprocess.Popen(args, stdin=subprocess.PIPE, close_fds=True)
  178. p.communicate(input=text.encode(ENCODING))
  179. def paste_wl(primary=False):
  180. args = ["wl-paste", "-n", "-t", "text"]
  181. if primary:
  182. args.append(PRIMARY_SELECTION)
  183. p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
  184. stdout, _stderr = p.communicate()
  185. return stdout.decode(ENCODING)
  186. return copy_wl, paste_wl
  187. def init_klipper_clipboard():
  188. def copy_klipper(text):
  189. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  190. p = subprocess.Popen(
  191. ['qdbus', 'org.kde.klipper', '/klipper', 'setClipboardContents',
  192. text.encode(ENCODING)],
  193. stdin=subprocess.PIPE, close_fds=True)
  194. p.communicate(input=None)
  195. def paste_klipper():
  196. p = subprocess.Popen(
  197. ['qdbus', 'org.kde.klipper', '/klipper', 'getClipboardContents'],
  198. stdout=subprocess.PIPE, close_fds=True)
  199. stdout, stderr = p.communicate()
  200. # Workaround for https://bugs.kde.org/show_bug.cgi?id=342874
  201. # TODO: https://github.com/asweigart/pyperclip/issues/43
  202. clipboardContents = stdout.decode(ENCODING)
  203. # even if blank, Klipper will append a newline at the end
  204. assert len(clipboardContents) > 0
  205. # make sure that newline is there
  206. assert clipboardContents.endswith('\n')
  207. if clipboardContents.endswith('\n'):
  208. clipboardContents = clipboardContents[:-1]
  209. return clipboardContents
  210. return copy_klipper, paste_klipper
  211. def init_dev_clipboard_clipboard():
  212. def copy_dev_clipboard(text):
  213. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  214. if text == '':
  215. warnings.warn('Pyperclip cannot copy a blank string to the clipboard on Cygwin. This is effectively a no-op.')
  216. if '\r' in text:
  217. warnings.warn('Pyperclip cannot handle \\r characters on Cygwin.')
  218. fo = open('/dev/clipboard', 'wt')
  219. fo.write(text)
  220. fo.close()
  221. def paste_dev_clipboard():
  222. fo = open('/dev/clipboard', 'rt')
  223. content = fo.read()
  224. fo.close()
  225. return content
  226. return copy_dev_clipboard, paste_dev_clipboard
  227. def init_no_clipboard():
  228. class ClipboardUnavailable(object):
  229. def __call__(self, *args, **kwargs):
  230. additionalInfo = ''
  231. if sys.platform == 'linux':
  232. additionalInfo = '\nOn Linux, you can run `sudo apt-get install xclip`, `sudo apt-get install xselect` (on X11) or `sudo apt-get install wl-clipboard` (on Wayland) to install a copy/paste mechanism.'
  233. raise PyperclipException('Pyperclip could not find a copy/paste mechanism for your system. For more information, please visit https://pyperclip.readthedocs.io/en/latest/index.html#not-implemented-error' + additionalInfo)
  234. if _IS_RUNNING_PYTHON_2:
  235. def __nonzero__(self):
  236. return False
  237. else:
  238. def __bool__(self):
  239. return False
  240. return ClipboardUnavailable(), ClipboardUnavailable()
  241. # Windows-related clipboard functions:
  242. class CheckedCall(object):
  243. def __init__(self, f):
  244. super(CheckedCall, self).__setattr__("f", f)
  245. def __call__(self, *args):
  246. ret = self.f(*args)
  247. if not ret and get_errno():
  248. raise PyperclipWindowsException("Error calling " + self.f.__name__)
  249. return ret
  250. def __setattr__(self, key, value):
  251. setattr(self.f, key, value)
  252. def init_windows_clipboard():
  253. global HGLOBAL, LPVOID, DWORD, LPCSTR, INT, HWND, HINSTANCE, HMENU, BOOL, UINT, HANDLE
  254. from ctypes.wintypes import (HGLOBAL, LPVOID, DWORD, LPCSTR, INT, HWND,
  255. HINSTANCE, HMENU, BOOL, UINT, HANDLE)
  256. windll = ctypes.windll
  257. msvcrt = ctypes.CDLL('msvcrt')
  258. safeCreateWindowExA = CheckedCall(windll.user32.CreateWindowExA)
  259. safeCreateWindowExA.argtypes = [DWORD, LPCSTR, LPCSTR, DWORD, INT, INT,
  260. INT, INT, HWND, HMENU, HINSTANCE, LPVOID]
  261. safeCreateWindowExA.restype = HWND
  262. safeDestroyWindow = CheckedCall(windll.user32.DestroyWindow)
  263. safeDestroyWindow.argtypes = [HWND]
  264. safeDestroyWindow.restype = BOOL
  265. OpenClipboard = windll.user32.OpenClipboard
  266. OpenClipboard.argtypes = [HWND]
  267. OpenClipboard.restype = BOOL
  268. safeCloseClipboard = CheckedCall(windll.user32.CloseClipboard)
  269. safeCloseClipboard.argtypes = []
  270. safeCloseClipboard.restype = BOOL
  271. safeEmptyClipboard = CheckedCall(windll.user32.EmptyClipboard)
  272. safeEmptyClipboard.argtypes = []
  273. safeEmptyClipboard.restype = BOOL
  274. safeGetClipboardData = CheckedCall(windll.user32.GetClipboardData)
  275. safeGetClipboardData.argtypes = [UINT]
  276. safeGetClipboardData.restype = HANDLE
  277. safeSetClipboardData = CheckedCall(windll.user32.SetClipboardData)
  278. safeSetClipboardData.argtypes = [UINT, HANDLE]
  279. safeSetClipboardData.restype = HANDLE
  280. safeGlobalAlloc = CheckedCall(windll.kernel32.GlobalAlloc)
  281. safeGlobalAlloc.argtypes = [UINT, c_size_t]
  282. safeGlobalAlloc.restype = HGLOBAL
  283. safeGlobalLock = CheckedCall(windll.kernel32.GlobalLock)
  284. safeGlobalLock.argtypes = [HGLOBAL]
  285. safeGlobalLock.restype = LPVOID
  286. safeGlobalUnlock = CheckedCall(windll.kernel32.GlobalUnlock)
  287. safeGlobalUnlock.argtypes = [HGLOBAL]
  288. safeGlobalUnlock.restype = BOOL
  289. wcslen = CheckedCall(msvcrt.wcslen)
  290. wcslen.argtypes = [c_wchar_p]
  291. wcslen.restype = UINT
  292. GMEM_MOVEABLE = 0x0002
  293. CF_UNICODETEXT = 13
  294. @contextlib.contextmanager
  295. def window():
  296. """
  297. Context that provides a valid Windows hwnd.
  298. """
  299. # we really just need the hwnd, so setting "STATIC"
  300. # as predefined lpClass is just fine.
  301. hwnd = safeCreateWindowExA(0, b"STATIC", None, 0, 0, 0, 0, 0,
  302. None, None, None, None)
  303. try:
  304. yield hwnd
  305. finally:
  306. safeDestroyWindow(hwnd)
  307. @contextlib.contextmanager
  308. def clipboard(hwnd):
  309. """
  310. Context manager that opens the clipboard and prevents
  311. other applications from modifying the clipboard content.
  312. """
  313. # We may not get the clipboard handle immediately because
  314. # some other application is accessing it (?)
  315. # We try for at least 500ms to get the clipboard.
  316. t = time.time() + 0.5
  317. success = False
  318. while time.time() < t:
  319. success = OpenClipboard(hwnd)
  320. if success:
  321. break
  322. time.sleep(0.01)
  323. if not success:
  324. raise PyperclipWindowsException("Error calling OpenClipboard")
  325. try:
  326. yield
  327. finally:
  328. safeCloseClipboard()
  329. def copy_windows(text):
  330. # This function is heavily based on
  331. # http://msdn.com/ms649016#_win32_Copying_Information_to_the_Clipboard
  332. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  333. with window() as hwnd:
  334. # http://msdn.com/ms649048
  335. # If an application calls OpenClipboard with hwnd set to NULL,
  336. # EmptyClipboard sets the clipboard owner to NULL;
  337. # this causes SetClipboardData to fail.
  338. # => We need a valid hwnd to copy something.
  339. with clipboard(hwnd):
  340. safeEmptyClipboard()
  341. if text:
  342. # http://msdn.com/ms649051
  343. # If the hMem parameter identifies a memory object,
  344. # the object must have been allocated using the
  345. # function with the GMEM_MOVEABLE flag.
  346. count = wcslen(text) + 1
  347. handle = safeGlobalAlloc(GMEM_MOVEABLE,
  348. count * sizeof(c_wchar))
  349. locked_handle = safeGlobalLock(handle)
  350. ctypes.memmove(c_wchar_p(locked_handle), c_wchar_p(text), count * sizeof(c_wchar))
  351. safeGlobalUnlock(handle)
  352. safeSetClipboardData(CF_UNICODETEXT, handle)
  353. def paste_windows():
  354. with clipboard(None):
  355. handle = safeGetClipboardData(CF_UNICODETEXT)
  356. if not handle:
  357. # GetClipboardData may return NULL with errno == NO_ERROR
  358. # if the clipboard is empty.
  359. # (Also, it may return a handle to an empty buffer,
  360. # but technically that's not empty)
  361. return ""
  362. locked_handle = safeGlobalLock(handle)
  363. return_value = c_wchar_p(locked_handle).value
  364. safeGlobalUnlock(handle)
  365. return return_value
  366. return copy_windows, paste_windows
  367. def init_wsl_clipboard():
  368. def copy_wsl(text):
  369. text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
  370. p = subprocess.Popen(['clip.exe'],
  371. stdin=subprocess.PIPE, close_fds=True)
  372. p.communicate(input=text.encode('utf-16le'))
  373. def paste_wsl():
  374. ps_script = '[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((Get-Clipboard -Raw)))'
  375. # '-noprofile' speeds up load time
  376. p = subprocess.Popen(['powershell.exe', '-noprofile', '-command', ps_script],
  377. stdout=subprocess.PIPE,
  378. stderr=subprocess.PIPE,
  379. close_fds=True)
  380. stdout, stderr = p.communicate()
  381. if stderr:
  382. raise Exception(f"Error pasting from clipboard: {stderr}")
  383. try:
  384. base64_encoded = stdout.decode('utf-8').strip()
  385. decoded_bytes = base64.b64decode(base64_encoded)
  386. return decoded_bytes.decode('utf-8')
  387. except Exception as e:
  388. raise RuntimeError(f"Decoding error: {e}")
  389. return copy_wsl, paste_wsl
  390. # Automatic detection of clipboard mechanisms and importing is done in determine_clipboard():
  391. def determine_clipboard():
  392. '''
  393. Determine the OS/platform and set the copy() and paste() functions
  394. accordingly.
  395. '''
  396. global Foundation, AppKit, qtpy, PyQt5
  397. # Setup for the CYGWIN platform:
  398. if 'cygwin' in platform.system().lower(): # Cygwin has a variety of values returned by platform.system(), such as 'CYGWIN_NT-6.1'
  399. # FIXME: pyperclip currently does not support Cygwin,
  400. # see https://github.com/asweigart/pyperclip/issues/55
  401. if os.path.exists('/dev/clipboard'):
  402. warnings.warn('Pyperclip\'s support for Cygwin is not perfect, see https://github.com/asweigart/pyperclip/issues/55')
  403. return init_dev_clipboard_clipboard()
  404. # Setup for the WINDOWS platform:
  405. elif os.name == 'nt' or platform.system() == 'Windows':
  406. return init_windows_clipboard()
  407. if platform.system() == 'Linux' and os.path.isfile('/proc/version'):
  408. with open('/proc/version', 'r') as f:
  409. if "microsoft" in f.read().lower():
  410. return init_wsl_clipboard()
  411. # Setup for the MAC OS X platform:
  412. if os.name == 'mac' or platform.system() == 'Darwin':
  413. try:
  414. import Foundation # check if pyobjc is installed
  415. import AppKit
  416. except ImportError:
  417. return init_osx_pbcopy_clipboard()
  418. else:
  419. return init_osx_pyobjc_clipboard()
  420. # Setup for the LINUX platform:
  421. if os.getenv("WAYLAND_DISPLAY") and _executable_exists("wl-copy") and _executable_exists("wl-paste"):
  422. return init_wl_clipboard()
  423. # `import PyQt4` sys.exit()s if DISPLAY is not in the environment.
  424. # Thus, we need to detect the presence of $DISPLAY manually
  425. # and not load PyQt4 if it is absent.
  426. elif os.getenv("DISPLAY"):
  427. if _executable_exists("xclip"):
  428. # Note: 2024/06/18 Google Trends shows xclip as more popular than xsel.
  429. return init_xclip_clipboard()
  430. if _executable_exists("xsel"):
  431. return init_xsel_clipboard()
  432. if _executable_exists("klipper") and _executable_exists("qdbus"):
  433. return init_klipper_clipboard()
  434. try:
  435. # qtpy is a small abstraction layer that lets you write
  436. # applications using a single api call to either PyQt or PySide.
  437. # https://pypi.python.org/pypi/QtPy
  438. import qtpy # check if qtpy is installed
  439. return init_qt_clipboard()
  440. except ImportError:
  441. pass
  442. # If qtpy isn't installed, fall back on importing PyQt5
  443. try:
  444. import PyQt5 # check if PyQt5 is installed
  445. return init_qt_clipboard()
  446. except ImportError:
  447. pass
  448. return init_no_clipboard()
  449. def set_clipboard(clipboard):
  450. '''
  451. Explicitly sets the clipboard mechanism. The "clipboard mechanism" is how
  452. the copy() and paste() functions interact with the operating system to
  453. implement the copy/paste feature. The clipboard parameter must be one of:
  454. - pbcopy
  455. - pbobjc (default on Mac OS X)
  456. - qt
  457. - xclip
  458. - xsel
  459. - klipper
  460. - windows (default on Windows)
  461. - no (this is what is set when no clipboard mechanism can be found)
  462. '''
  463. global copy, paste
  464. clipboard_types = {
  465. "pbcopy": init_osx_pbcopy_clipboard,
  466. "pyobjc": init_osx_pyobjc_clipboard,
  467. "qt": init_qt_clipboard, # TODO - split this into 'qtpy' and 'pyqt5'
  468. "xclip": init_xclip_clipboard,
  469. "xsel": init_xsel_clipboard,
  470. "wl-clipboard": init_wl_clipboard,
  471. "klipper": init_klipper_clipboard,
  472. "windows": init_windows_clipboard,
  473. "no": init_no_clipboard,
  474. }
  475. if clipboard not in clipboard_types:
  476. raise ValueError('Argument must be one of %s' % (', '.join([repr(_) for _ in clipboard_types.keys()])))
  477. # Sets pyperclip's copy() and paste() functions:
  478. copy, paste = clipboard_types[clipboard]()
  479. def lazy_load_stub_copy(text):
  480. '''
  481. A stub function for copy(), which will load the real copy() function when
  482. called so that the real copy() function is used for later calls.
  483. This allows users to import pyperclip without having determine_clipboard()
  484. automatically run, which will automatically select a clipboard mechanism.
  485. This could be a problem if it selects, say, the memory-heavy PyQt5 module
  486. but the user was just going to immediately call set_clipboard() to use a
  487. different clipboard mechanism.
  488. The lazy loading this stub function implements gives the user a chance to
  489. call set_clipboard() to pick another clipboard mechanism. Or, if the user
  490. simply calls copy() or paste() without calling set_clipboard() first,
  491. will fall back on whatever clipboard mechanism that determine_clipboard()
  492. automatically chooses.
  493. '''
  494. global copy, paste
  495. copy, paste = determine_clipboard()
  496. return copy(text)
  497. def lazy_load_stub_paste():
  498. '''
  499. A stub function for paste(), which will load the real paste() function when
  500. called so that the real paste() function is used for later calls.
  501. This allows users to import pyperclip without having determine_clipboard()
  502. automatically run, which will automatically select a clipboard mechanism.
  503. This could be a problem if it selects, say, the memory-heavy PyQt5 module
  504. but the user was just going to immediately call set_clipboard() to use a
  505. different clipboard mechanism.
  506. The lazy loading this stub function implements gives the user a chance to
  507. call set_clipboard() to pick another clipboard mechanism. Or, if the user
  508. simply calls copy() or paste() without calling set_clipboard() first,
  509. will fall back on whatever clipboard mechanism that determine_clipboard()
  510. automatically chooses.
  511. '''
  512. global copy, paste
  513. copy, paste = determine_clipboard()
  514. return paste()
  515. def is_available():
  516. return copy != lazy_load_stub_copy and paste != lazy_load_stub_paste
  517. # Initially, copy() and paste() are set to lazy loading wrappers which will
  518. # set `copy` and `paste` to real functions the first time they're used, unless
  519. # set_clipboard() or determine_clipboard() is called first.
  520. copy, paste = lazy_load_stub_copy, lazy_load_stub_paste
  521. __all__ = ['copy', 'paste', 'set_clipboard', 'determine_clipboard']