Skip to content

Commit 4fc9393

Browse files
authored
Merge pull request #1254 from gpotter2/patch-7
ContextManagerSubprocess fix
2 parents eaf9db4 + f668222 commit 4fc9393

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

scapy/arch/windows/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ def __init__(self):
401401
self._reload()
402402

403403
def _reload(self):
404+
self.pdfreader = None
405+
self.psreader = None
404406
# We try some magic to find the appropriate executables
405-
self.pdfreader = win_find_exe("AcroRd32")
406-
self.psreader = win_find_exe("gsview32")
407407
self.dot = win_find_exe("dot")
408408
self.tcpdump = win_find_exe("windump")
409409
self.tshark = win_find_exe("tshark")

scapy/packet.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from scapy.fields import StrField, ConditionalField, Emph, PacketListField, BitField, \
1818
MultiEnumField, EnumField, FlagsField
1919
from scapy.config import conf
20+
from scapy.consts import WINDOWS
2021
from scapy.compat import *
2122
from scapy.base_classes import BasePacket, Gen, SetGen, Packet_metaclass
2223
from scapy.volatile import VolatileValue
@@ -502,12 +503,16 @@ def psdump(self, filename=None, **kargs):
502503
"""
503504
canvas = self.canvas_dump(**kargs)
504505
if filename is None:
505-
fname = get_temp_file(autoext=".eps")
506+
fname = WINDOWS and get_temp_file(autoext=".eps")
506507
canvas.writeEPSfile(fname)
507-
with ContextManagerSubprocess("psdump()", conf.prog.psreader):
508-
subprocess.Popen([conf.prog.psreader, fname])
508+
if conf.prog.psreader is None:
509+
os.startfile(fname)
510+
else:
511+
with ContextManagerSubprocess("psdump()", conf.prog.psreader):
512+
subprocess.Popen([conf.prog.psreader, fname])
509513
else:
510514
canvas.writeEPSfile(filename)
515+
print()
511516

512517
def pdfdump(self, filename=None, **kargs):
513518
"""
@@ -522,10 +527,14 @@ def pdfdump(self, filename=None, **kargs):
522527
if filename is None:
523528
fname = get_temp_file(autoext=".pdf")
524529
canvas.writePDFfile(fname)
525-
with ContextManagerSubprocess("pdfdump()", conf.prog.pdfreader):
526-
subprocess.Popen([conf.prog.pdfreader, fname])
530+
if WINDOWS and conf.prog.pdfreader is None:
531+
os.startfile(fname)
532+
else:
533+
with ContextManagerSubprocess("pdfdump()", conf.prog.pdfreader):
534+
subprocess.Popen([conf.prog.pdfreader, fname])
527535
else:
528536
canvas.writePDFfile(filename)
537+
print()
529538

530539

531540
def canvas_dump(self, layer_shift=0, rebuild=1):

scapy/plist.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from collections import defaultdict
1515

1616
from scapy.config import conf
17+
from scapy.consts import WINDOWS
1718
from scapy.base_classes import BasePacket,BasePacketList
1819
from scapy.utils import do_graph,hexdump,make_table,make_lined_table,make_tex_table, \
1920
get_temp_file, issubtype
@@ -435,8 +436,11 @@ def psdump(self, filename = None, **kargs):
435436
if filename is None:
436437
filename = get_temp_file(autoext=".ps")
437438
d.writePSfile(filename)
438-
with ContextManagerSubprocess("psdump()"):
439-
subprocess.Popen([conf.prog.psreader, filename+".ps"])
439+
if WINDOWS and conf.prog.psreader is None:
440+
os.startfile(filename)
441+
else:
442+
with ContextManagerSubprocess("psdump()", conf.prog.psreader):
443+
subprocess.Popen([conf.prog.psreader, filename])
440444
else:
441445
d.writePSfile(filename)
442446
print()
@@ -449,8 +453,11 @@ def pdfdump(self, filename = None, **kargs):
449453
if filename is None:
450454
filename = get_temp_file(autoext=".pdf")
451455
d.writePDFfile(filename)
452-
with ContextManagerSubprocess("psdump()"):
453-
subprocess.Popen([conf.prog.pdfreader, filename+".pdf"])
456+
if WINDOWS and conf.prog.pdfreader is None:
457+
os.startfile(filename)
458+
else:
459+
with ContextManagerSubprocess("pdfdump()", conf.prog.pdfreader):
460+
subprocess.Popen([conf.prog.pdfreader, filename])
454461
else:
455462
d.writePDFfile(filename)
456463
print()

scapy/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ class ContextManagerSubprocess(object):
435435
Context manager that eases checking for unknown command.
436436
437437
Example:
438-
>>> with ContextManagerSubprocess("my custom message"):
438+
>>> with ContextManagerSubprocess("my custom message", "unknown_command"):
439439
>>> subprocess.Popen(["unknown_command"])
440440
441441
"""

0 commit comments

Comments
 (0)