Re: win32clipboard writing to clipboard on Windows 11
On Mon, Jun 17, 2024 at 8:36 PM MRAB via Python-list
wrote:
> On 2024-06-17 20:27, Rob Cliffe via Python-list wrote:
>
> > SetClipboardData(CF_UNICODETEXT, "0")
> > CloseClipboard()
win32clipboard.SetClipboardData() first tries to covert the second
argument as an integer handle to global memory, which gets passed to
WinAPI SetClipboardData(). The integer conversion is basically via
int(). With int("0"), it's passing a NULL handle value, which
instructs the window manager to query the data from the window that
was associated via OpenClipboard(), if any. Since no memory handle is
passed in this case, SetClipboardData() returns NULL.
win32clipboard.SetClipboardData() misinterprets this as failure and
raises an exception for whatever random error code is currently set in
the thread's last error value. On the other hand, for a numeric text
string with a nonzero value, such as "123",
win32clipboard.SetClipboardData() will raise an exception for the
error code ERROR_INVALID_HANDLE (6), unless the integer value happens
to be a valid global memory handle value.
I recommend just using win32clipboard.SetClipboardText(). Otherwise I
don't see an easy workaround given the peculiar design of
win32clipboard.SetClipboardData(). You'd have to manually allocate a
block of global memory, copy the numeric text string into it, and pass
the global memory handle to win32clipboard.SetClipboardData(). For
example:
import ctypes
import win32clipboard
from ctypes import wintypes
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
GMEM_MOVEABLE = 0x0002
kernel32.GlobalAlloc.restype = wintypes.HGLOBAL
kernel32.GlobalFree.argtypes = (wintypes.HGLOBAL,)
kernel32.GlobalLock.restype = wintypes.LPVOID
kernel32.GlobalLock.argtypes = (wintypes.HGLOBAL,)
kernel32.GlobalUnlock.argtypes = (wintypes.HGLOBAL,)
def global_alloc_text(text):
array_t = ctypes.c_wchar * (len(text) + 1)
hMem = kernel32.GlobalAlloc(GMEM_MOVEABLE, ctypes.sizeof(array_t))
if not hMem:
raise ctypes.WinError(ctypes.get_last_error())
pMem = kernel32.GlobalLock(hMem)
try:
try:
array_t.from_address(pMem).value = text
finally:
kernel32.GlobalUnlock(hMem)
except:
kernel32.GlobalFree(hMem)
raise
return hMem
def set_clipboard_text(text):
hMem = global_alloc_text(text)
try:
win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT,
hMem)
# Now the system owns the global memory.
except:
kernel32.GlobalFree(hMem)
--
https://mail.python.org/mailman/listinfo/python-list
Re: win32clipboard writing to clipboard on Windows 11
On Tue, Jun 18, 2024 at 2:19 AM Eryk Sun wrote: > > > def set_clipboard_text(text): > hMem = global_alloc_text(text) > try: > win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, > hMem) > # Now the system owns the global memory. > except: > kernel32.GlobalFree(hMem) Oops, that suppresses the exception. Fixed: def set_clipboard_text(text): hMem = global_alloc_from_text(text) try: win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, hMem) # Now the system owns the global memory. except: kernel32.GlobalFree(hMem) raise -- https://mail.python.org/mailman/listinfo/python-list
Re: Anonymous email users
On 6/17/24 17:51, dn via Python-list wrote: +1 The "public" part is not to embarrass posters, but recognition that there are likely other people 'out there' (or arriving in-future if they care to read the archives) experiencing a similar problem. (hence need for descriptive Subject lines - isn't the most difficult task in programming 'choosing names'?) well, one of two, along with cache invalidation and off-by-one errors (according to the wags). I do agree with this, but mailman (2) archives aren't particularly useful for searching, as they're organized in monthly chunks and you have to keep clicking around - this list doesn't have a search engine as it's not converted to be one of the mailman 3 lists. There are supposed to be some search engine incantations to make this better. I find this one works, though I can never actually remember it and have to go hunting again each time... picking a random-ish subject line from this list in the past: site:mail.python.org inurl:Python-list multiplication I don't know that we publicise such methods (there are probably others). -- https://mail.python.org/mailman/listinfo/python-list
Re: Anonymous email users
On 2024-06-18, Mats Wichmann via Python-list wrote: > On 6/17/24 17:51, dn via Python-list wrote: > >> +1 >> >> The "public" part is not to embarrass posters, but recognition that >> there are likely other people 'out there' (or arriving in-future if they >> care to read the archives) experiencing a similar problem. (hence need >> for descriptive Subject lines - isn't the most difficult task in >> programming 'choosing names'?) > > well, one of two, along with cache invalidation and off-by-one errors > (according to the wags). > > I do agree with this, but mailman (2) archives aren't particularly > useful for searching, as they're organized in monthly chunks and you > have to keep clicking around - this list doesn't have a search engine as > it's not converted to be one of the mailman 3 lists. Gmane used to have a usable search feature (along with a decent threaded web UI to read the arhives), but that got lost during the great gmane server/domain upheaval of 2016 (or whenever that was). I still miss it. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: in Python: (101 102 103 201 202 203 301 302 303 401 402 403 )
On 2024-06-14 06:10:06 -, candycanearter07 via Python-list wrote:
> Phil Carmody wrote at 12:01 this Thursday (GMT):
> > I'd say you can't beat the verbosity, or lack thereof of just plain
> > zsh/bash:
> > $ echo {1,2,3,4}0{1,2,3}
> > 101 102 103 201 202 203 301 302 303 401 402 403
>
>
> I /think/ you can replace it with {1...4} and {1...3}? I know there is
> some syntax for "range of numbers" but I can't remember it exactly.
Only two dots, not three:
% echo {1..4}0{1..3}
101 102 103 201 202 203 301 302 303 401 402 403
hp
--
_ | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| | | [email protected] |-- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
signature.asc
Description: PGP signature
--
https://mail.python.org/mailman/listinfo/python-list
Timezone in HH:MM Format
Hello,
How can I convert a date, usually datetime.now(), into a format where
the timezone is in hours:minutes format. I was able to get that format
in shell:
$ date +%Y-%m-%dT%H:%M:%S%:z
2024-06-18T19:24:09-04:00
The closest I got in python is
from datetime import datetime
from zoneinfo import ZoneInfo
s = datetime.strftime(datetime.now(ZoneInfo("America/New_York")),
"%Y-%m-%dT%H:%M:%S%z")
print(s)
This prints the same as the shell command above except the last column:
2024-06-18T19:28:56-0400
Any help will be appreciated.
Regards
Ivan
--
Tangra Mega Rock: http://www.radiotangra.com
--
https://mail.python.org/mailman/listinfo/python-list
Re: Timezone in HH:MM Format
On 2024-06-19 00:32, Ivan "Rambius" Ivanov via Python-list wrote:
Hello,
How can I convert a date, usually datetime.now(), into a format where
the timezone is in hours:minutes format. I was able to get that format
in shell:
$ date +%Y-%m-%dT%H:%M:%S%:z
2024-06-18T19:24:09-04:00
The closest I got in python is
from datetime import datetime
from zoneinfo import ZoneInfo
s = datetime.strftime(datetime.now(ZoneInfo("America/New_York")),
"%Y-%m-%dT%H:%M:%S%z")
print(s)
This prints the same as the shell command above except the last column:
2024-06-18T19:28:56-0400
Starting from Python 3.12, you can use "%:z" in the format string. For
earlier versions of Python, you need to do some string slicing.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Timezone in HH:MM Format
On 2024-06-18, Ivan "Rambius" Ivanov wrote:
> Hello,
>
> How can I convert a date, usually datetime.now(), into a format where
> the timezone is in hours:minutes format. I was able to get that format
> in shell:
>
> $ date +%Y-%m-%dT%H:%M:%S%:z
> 2024-06-18T19:24:09-04:00
>
> The closest I got in python is
>
> from datetime import datetime
> from zoneinfo import ZoneInfo
>
> s = datetime.strftime(datetime.now(ZoneInfo("America/New_York")),
> "%Y-%m-%dT%H:%M:%S%z")
> print(s)
>
> This prints the same as the shell command above except the last column:
> 2024-06-18T19:28:56-0400
>
> Any help will be appreciated.
datetime.now(ZoneInfo("America/New_York")).isoformat()
--
https://mail.python.org/mailman/listinfo/python-list
Re: Timezone in HH:MM Format
Thank you all for your responses!
On Tue, Jun 18, 2024 at 9:54 PM Jon Ribbens via Python-list
wrote:
>
> datetime.now(ZoneInfo("America/New_York")).isoformat()
Both .isoformat() and "%:z" work.
--
Tangra Mega Rock: http://www.radiotangra.com
--
https://mail.python.org/mailman/listinfo/python-list
