Add error checking on width and height fields:

This commit is contained in:
Dean Rispin 2016-03-01 16:25:38 -08:00
parent 8d00e63b87
commit e48755f7d0
2 changed files with 22 additions and 2 deletions

View File

@ -492,17 +492,21 @@ class Escpos(object):
self._raw(TXT_2WIDTH) self._raw(TXT_2WIDTH)
elif width == 1 and height == 1: elif width == 1 and height == 1:
self._raw(TXT_NORMAL) self._raw(TXT_NORMAL)
else: elif width >= 1 and width <= 8 and height >= 1 and height <= 8 and isinstance(width, int) and isinstance(height, int):
self._raw(TXT_SIZE + chr(TXT_WIDTH[width] + TXT_HEIGHT[height])) self._raw(TXT_SIZE + chr(TXT_WIDTH[width] + TXT_HEIGHT[height]))
# Type else:
raise SetVariableError()
# Upside down
if flip == True: if flip == True:
self._raw(TXT_FLIP_ON) self._raw(TXT_FLIP_ON)
else: else:
self._raw(TXT_FLIP_OFF) self._raw(TXT_FLIP_OFF)
# Smoothing
if smooth == True: if smooth == True:
self._raw(TXT_SMOOTH_ON) self._raw(TXT_SMOOTH_ON)
else: else:
self._raw(TXT_SMOOTH_OFF) self._raw(TXT_SMOOTH_OFF)
# Type
if text_type.upper() == "B": if text_type.upper() == "B":
self._raw(TXT_BOLD_ON) self._raw(TXT_BOLD_ON)
self._raw(TXT_UNDERL_OFF) self._raw(TXT_UNDERL_OFF)

View File

@ -12,6 +12,7 @@ Result/Exit codes:
- `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError` - `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError`
- `80` = Invalid char code :py:exc:`~escpos.exceptions.CharCodeError` - `80` = Invalid char code :py:exc:`~escpos.exceptions.CharCodeError`
- `90` = USB device not found :py:exc:`~escpos.exceptions.USBNotFoundError` - `90` = USB device not found :py:exc:`~escpos.exceptions.USBNotFoundError`
- `100` = Set variable out of range :py:exc:`~escpos.exceptions.SetVariableError`
:author: `Manuel F Martinez <manpaz@bashlinux.com>`_ and others :author: `Manuel F Martinez <manpaz@bashlinux.com>`_ and others
:organization: Bashlinux and `python-escpos <https://github.com/python-escpos>`_ :organization: Bashlinux and `python-escpos <https://github.com/python-escpos>`_
@ -167,3 +168,18 @@ class USBNotFoundError(Error):
def __str__(self): def __str__(self):
return "USB device not found" return "USB device not found"
class SetVariableError(Error):
""" A set method variable was out of range
Check set variables against minimum and maximum values
Ths returncode for this exception is `100`.
"""
def __init__(self, msg=""):
Error.__init__(self, msg)
self.msg = msg
self.resultcode = 100
def __str__(self):
return "Set variable out of range"