Skip to content

Commit d1857a0

Browse files
committed
windows printing: give process time to finish when exiting pcbasic
1 parent 11863b9 commit d1857a0

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

pcbasic

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import logging
1414

1515
import plat
1616
import ansipipe
17+
import printer
1718

1819
# declare to avoid pylint errors
1920
config = None
@@ -50,6 +51,7 @@ def main():
5051
start_basic()
5152
finally:
5253
try:
54+
printer.wait()
5355
# clean up our temp dir if we made one
5456
if plat.temp_dir and plat.system != 'Android':
5557
shutil.rmtree(plat.temp_dir)

printer.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,48 @@ def flush(self):
3535
utf8buf = unicodepage.UTF8Converter(preserve_control=True).to_utf8(printbuf)
3636
line_print(utf8buf, self.printer_name)
3737

38+
# only needed on Windows
39+
wait = lambda x: None
3840

3941
if plat.system == 'Windows':
40-
import tempfile
41-
import threading
4242
import os
4343
import win32print
4444
import win32api
4545
import win32com
4646
import win32com.shell.shell
4747
import win32event
48+
# temp file in temp dir
49+
printfile = os.path.join(plat.temp_dir, 'pcbasic_print.txt')
50+
# handle for last printing process
51+
handle = -1
4852

4953
def line_print(printbuf, printer_name):
5054
""" Print the buffer to a Windows printer. """
55+
global handle
5156
if printer_name == '' or printer_name=='default':
5257
printer_name = win32print.GetDefaultPrinter()
53-
with tempfile.NamedTemporaryFile(mode='wb', prefix='pcbasic_',
54-
suffix='.txt', delete=False) as f:
58+
# open a file in our PC-BASIC temporary directory
59+
# this will get cleaned up on exit
60+
with open(printfile, 'wb') as f:
5561
# write UTF-8 Byte Order mark to ensure Notepad recognises encoding
5662
f.write('\xef\xbb\xbf')
5763
f.write(printbuf)
58-
# flush buffer to ensure it all actually gets printed
59-
f.flush()
60-
# fMask = SEE_MASK_NOASYNC(0x00000100) + SEE_MASK_NOCLOSEPROCESS
64+
# fMask = SEE_MASK_NOASYNC(0x00000100) + SEE_MASK_NOCLOSEPROCESS
65+
try:
6166
resdict = win32com.shell.shell.ShellExecuteEx(fMask=256+64,
62-
lpVerb='printto', lpFile=f.name,
67+
lpVerb='printto', lpFile=printfile,
6368
lpParameters='"%s"' % printer_name)
6469
handle = resdict['hProcess']
65-
# spin off a thread as the WIndows AI timeout doesn't work
66-
# all this fluff just to print a bit of plain text on Windows...
67-
outp = threading.Thread(target=wait_printer, args=(handle, f.name))
68-
outp.daemon = True
69-
outp.start()
70-
71-
def wait_printer(handle, filename):
72-
""" Wait for the print to finish, then delete temp file. """
73-
# note that this fails to delete the temp file for print jobs on exit
74-
if win32event.WaitForSingleObject(handle, -1) != win32event.WAIT_OBJECT_0:
75-
logging.warning('Printing process failed')
76-
try:
77-
os.remove(filename)
78-
except EnvironmentError as e:
70+
except WindowsError as e:
7971
logging.warning('Error while printing: %s', str(e))
72+
handle = -1
8073

74+
def wait():
75+
""" Give printing process some time to complete. """
76+
try:
77+
win32event.WaitForSingleObject(handle, 1000)
78+
except WindowsError:
79+
pass
8180

8281
elif plat.system == 'Android':
8382

0 commit comments

Comments
 (0)