summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Smith <jules@op59.net>2020-07-30 16:18:44 +0100
committerJulian Smith <jules@op59.net>2020-07-30 21:51:39 +0100
commita1441168f346cefd28e6336f6fbb3b310540dbff (patch)
tree1705c82010025added4c2ed97842d211beacf341
parent868200752e41adacc9d027092eba7be2db572b00 (diff)
downloadghostpdl-a1441168f346cefd28e6336f6fbb3b310540dbff.tar.gz
Changed gsapi.py to raise exceptions instead of returning error number
gsapi.py: Generate exceptions instead of returning error codes. Added python versions of gs_error_* codes. examples.py: Updated to match new API. Re-raise import error if 'import gsapi' fails. Patched up calls of run_gpdl() to pass in_filename because passing None caused underlying C to return an error. Marked as executable. Look for input files relative to top of ghospdl checkout (found using __file__).
-rwxr-xr-x[-rw-r--r--]demos/python/examples.py181
-rwxr-xr-xdemos/python/gsapi.py259
2 files changed, 219 insertions, 221 deletions
diff --git a/demos/python/examples.py b/demos/python/examples.py
index 296a0805a..d5c8b6adb 100644..100755
--- a/demos/python/examples.py
+++ b/demos/python/examples.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 21 10:05:07 2020
@@ -8,93 +9,35 @@ Example use of gsapi for various tasks.
try:
import gsapi
-except Exception as err:
+except Exception:
print('Failure to import gsapi. Check shared library path')
- print(err.args)
+ raise
-def run_gpdl(params, path):
-
- try:
- e, instance = gsapi.gsapi_new_instance(0)
- if e:
- raise Warning('gsapi_new_instance failure')
- except Exception as err:
- print(err.args)
- return
-
- try:
- e = gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
- if e:
- raise Warning('gsapi_set_arg_encoding failure')
-
- if path != None:
- e = gsapi.gsapi_add_control_path(instance, gsapi.GS_PERMIT_FILE_READING, path)
- if e:
- raise Warning('gsapi_add_control_path failure')
+import os
- e = gsapi.gsapi_init_with_args(instance, params)
- if e:
- raise Warning('gsapi_init_with_args failure')
-
- except Exception as err:
- print(err.args)
- return
+ghostpdl_root = os.path.abspath('%s/../../..' % __file__)
+print('ghostpdl_root=%s' % ghostpdl_root)
+def run_gpdl(params, path):
+ instance = gsapi.gsapi_new_instance(0)
+ gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
+ gsapi.gsapi_add_control_path(instance, gsapi.GS_PERMIT_FILE_READING, path)
+ gsapi.gsapi_init_with_args(instance, params)
end_gpdl(instance)
- return
def init_gpdl(params):
-
- try:
- e, instance = gsapi.gsapi_new_instance(0)
- if e:
- raise Warning('gsapi_new_instance failure')
- except Exception as err:
- print(err.args)
- return None
-
- try:
- e = gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
- if e:
- raise Warning('gsapi_set_arg_encoding failure')
-
- e = gsapi.gsapi_init_with_args(instance, params)
- if e:
- raise Warning('gsapi_init_with_args failure')
-
- except Exception as err:
- print(err.args)
- return None
-
+ instance = gsapi.gsapi_new_instance(0)
+ gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
+ gsapi.gsapi_init_with_args(instance, params)
return instance
def run_file(instance, filename):
-
- try:
- (e, exitcode) = gsapi.gsapi_run_file(instance, filename, None)
- if e:
- raise Warning('gsapi_run_file failure')
-
- except Exception as err:
- print(err.args)
-
- return
+ exitcode = gsapi.gsapi_run_file(instance, filename, None)
+ return exitcode
def end_gpdl(instance):
-
- try:
- e = gsapi.gsapi_exit(instance)
- if e:
- raise Warning('gsapi_exit failure')
-
- e = gsapi.gsapi_delete_instance(instance)
- if e:
- raise Warning('gsapi_delete_instance failure')
-
- except Exception as err:
- print(err.args)
-
- return
+ gsapi.gsapi_exit(instance)
+ gsapi.gsapi_delete_instance(instance)
# run multiple files through same instance
def multiple_files():
@@ -104,34 +47,32 @@ def multiple_files():
params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pngalpha',
'-r72', '-o', out_filename]
instance = init_gpdl(params)
- run_file(instance, '../../examples/tiger.eps')
- run_file(instance, '../../examples/snowflak.ps')
- run_file(instance, '../../examples/annots.pdf')
+ run_file(instance, '%s/examples/tiger.eps' % ghostpdl_root)
+ run_file(instance, '%s/examples/snowflak.ps' % ghostpdl_root)
+ run_file(instance, '%s/examples/annots.pdf' % ghostpdl_root)
end_gpdl(instance)
# Extract text from source file
def extract_text():
- in_filename = '../../examples/alphabet.ps'
+ in_filename = '%s/examples/alphabet.ps' % ghostpdl_root
out_filename = 'alphabet.txt'
print('Extracting text from %s to %s' % (in_filename, out_filename))
params =['gs', '-dNOPAUSE', '-dBATCH','-sDEVICE=txtwrite',
'-dTextFormat=3','-o', out_filename, '-f', in_filename]
- run_gpdl(params, None)
-
- return
+ run_gpdl(params, in_filename)
# Perform different color conversions on text, graphic, and image content
# through the use of different destination ICC profiles
def object_dependent_color_conversion():
- in_filename = '../../examples/text_graph_image_cmyk_rgb.pdf'
+ in_filename = '%s/examples/text_graph_image_cmyk_rgb.pdf' % ghostpdl_root
out_filename = 'rendered_profile.tif'
- image_icc = '../../toolbin/color/icc_creator/effects/cyan_output.icc'
- graphic_icc = '../../toolbin/color/icc_creator/effects/magenta_output.icc'
- text_icc = '../../toolbin/color/icc_creator/effects/yellow_output.icc'
+ image_icc = '%s/toolbin/color/icc_creator/effects/cyan_output.icc' % ghostpdl_root
+ graphic_icc = '%s/toolbin/color/icc_creator/effects/magenta_output.icc' % ghostpdl_root
+ text_icc = '%s/toolbin/color/icc_creator/effects/yellow_output.icc' % ghostpdl_root
print('Object dependent color conversion on %s to %s' % (in_filename, out_filename))
params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=tiff32nc',
@@ -143,15 +84,13 @@ def object_dependent_color_conversion():
# Include ICC profile location to readable path
run_gpdl(params, '../../toolbin/color/icc_creator/effects/')
- return
-
# Perform different color conversions on text, graphic, and image content
# through the use of different rendering intents
def object_dependent_rendering_intent():
- in_filename = '../../examples/text_graph_image_cmyk_rgb.pdf'
+ in_filename = '%s/examples/text_graph_image_cmyk_rgb.pdf' % ghostpdl_root
out_filename = 'rendered_intent.tif'
- output_icc_profile = '../../toolbin/color/src_color/cmyk_des_renderintent.icc'
+ output_icc_profile = '%s/toolbin/color/src_color/cmyk_des_renderintent.icc' % ghostpdl_root
print('Object dependent rendering intents on %s to %s' % (in_filename, out_filename))
params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=tiff32nc',
@@ -163,85 +102,55 @@ def object_dependent_rendering_intent():
# Include ICC profile location to readable path
run_gpdl(params, '../../toolbin/color/src_color/')
- return
-
# Distill
def distill():
- in_filename = '../../examples/tiger.eps'
+ in_filename = '%s/examples/tiger.eps' % ghostpdl_root
out_filename = 'tiger.pdf'
print('Distilling %s to %s' % (in_filename, out_filename))
params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite',
'-o', out_filename, '-f', in_filename]
- run_gpdl(params, None)
-
- return
+ run_gpdl(params, in_filename)
# Transparency in Postscript
def trans_ps():
- in_filename = '../../examples/transparency_example.ps'
+ in_filename = '%s/examples/transparency_example.ps' % ghostpdl_root
out_filename = 'transparency.png'
print('Rendering Transparency PS file %s to %s' % (in_filename, out_filename))
params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pngalpha',
'-dALLOWPSTRANSPARENCY', '-o', out_filename, '-f', in_filename]
- run_gpdl(params, None)
-
- return
+ run_gpdl(params, in_filename)
# Run string to feed chunks
def run_string():
f = None
size = 1024;
- in_filename = '../../examples/tiger.eps'
+ in_filename = '%s/examples/tiger.eps' % ghostpdl_root
out_filename = 'tiger_byte_fed.png'
params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pngalpha',
'-o', out_filename]
- try:
- e, instance = gsapi.gsapi_new_instance(0)
- if e:
- raise Warning('gsapi_new_instance failure')
- except Exception as err:
- print(err.args)
- return
-
- try:
- e = gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
- if e:
- raise Warning('gsapi_set_arg_encoding failure')
-
- e = gsapi.gsapi_init_with_args(instance, params)
- if e:
- raise Warning('gsapi_init_with_args failure')
-
- [e, exitcode] = gsapi.gsapi_run_string_begin(instance, 0)
- if e:
- raise Warning('gsapi_run_string_begin failure')
-
- f = open(in_filename,"rb")
+ instance = gsapi.gsapi_new_instance(0)
+
+ gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
+ gsapi.gsapi_init_with_args(instance, params)
+
+ exitcode = gsapi.gsapi_run_string_begin(instance, 0)
+
+ with open(in_filename,"rb") as f:
while True:
data = f.read(size)
if not data:
break
- [e, exitcode] = gsapi.gsapi_run_string_continue(instance, data, 0)
- if e:
- raise Warning('gsapi_run_string_continue failure')
-
- [e, exitcode] = gsapi.gsapi_run_string_end(instance, 0)
- if e:
- raise Warning('gsapi_run_string_end failure')
+ exitcode = gsapi.gsapi_run_string_continue(instance, data, 0)
- except Exception as err:
- print(err.args)
+ exitcode = gsapi.gsapi_run_string_end(instance, 0)
end_gpdl(instance)
- if f != None:
- f.close()
- return
# Examples
@@ -259,4 +168,4 @@ print('***********Multiple files********')
multiple_files()
print('***********Run string********')
run_string()
-wait = input("press enter to exit") \ No newline at end of file
+wait = input("press enter to exit")
diff --git a/demos/python/gsapi.py b/demos/python/gsapi.py
index 5d656606c..6feff2461 100755
--- a/demos/python/gsapi.py
+++ b/demos/python/gsapi.py
@@ -7,9 +7,11 @@ Overview:
All functions have the same name as the C function that they wrap.
- All functions return an integer error code unless otherwise stated. [The
- exceptions are usually for out-parameters, which are returned directly as
- part of a tuple.]
+ Functions raise a GSError exception if the underlying function returned a
+ negative error code.
+
+ Functions that don't have out-params return None. Out-params are returned
+ directly (using tuples if there are more than one).
See examples.py for sample usage.
@@ -18,8 +20,7 @@ Usage:
make sodebug
LD_LIBRARY_PATH=sodebugbin ./demos/python/gsapi.py
- On Windows perform Release build (x64 or Win32). Select
- appropriate DLL to use below.
+ On Windows perform Release build (x64 or Win32).
Requirements:
@@ -61,6 +62,15 @@ else:
raise Exception('Unrecognised platform.system()=%s' % platform.system())
+class GSError(Exception):
+ def __init__(self, gs_error):
+ self.gs_error = gs_error
+ def __str__(self):
+ return 'Ghostscript exception %i: %s' % (
+ self.gs_error,
+ _gs_error_text(self.gs_error),
+ )
+
class gsapi_revision_t:
def __init__(self, product, copyright, revision, revisiondate):
self.product = product
@@ -83,15 +93,15 @@ def gsapi_revision():
# strings.]
_r = _gsapi_revision_t()
e = _libgs.gsapi_revision(ctypes.byref(_r), ctypes.sizeof(_r))
- if e:
- return e, None
+ if e < 0:
+ raise GSError(e)
r = gsapi_revision_t(
_r.product.decode('utf-8'),
_r.copyright.decode('utf-8'),
_r.revision,
_r.revisiondate,
)
- return e, r
+ return r
def gsapi_new_instance(caller_handle):
@@ -99,13 +109,19 @@ def gsapi_new_instance(caller_handle):
Returns (e, instance).
'''
instance = ctypes.c_void_p()
- e = _libgs.gsapi_new_instance(ctypes.byref(instance), ctypes.c_void_p(caller_handle))
- return e, instance
+ e = _libgs.gsapi_new_instance(
+ ctypes.byref(instance),
+ ctypes.c_void_p(caller_handle),
+ )
+ if e < 0:
+ raise GSError(e)
+ return instance
def gsapi_delete_instance(instance):
e = _libgs.gsapi_delete_instance(instance)
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_set_stdio(instance, stdin_fn, stdout_fn, stderr_fn):
@@ -146,20 +162,20 @@ def gsapi_set_stdio(instance, stdin_fn, stdout_fn, stderr_fn):
stderr_fn2 = make_out(stderr_fn)
stdin_fn2 = make_in(stdin_fn)
e = _libgs.gsapi_set_stdio(instance, stdout_fn2, stdout_fn2, stdout_fn2)
- if not e:
- # Need to keep references to call-back functions.
- global _gsapi_set_stdio_refs
- _gsapi_set_stdio_refs = stdin_fn2, stdout_fn2, stderr_fn2
- return e
+ if e < 0:
+ raise GSError(e)
+ # Need to keep references to call-back functions.
+ global _gsapi_set_stdio_refs
+ _gsapi_set_stdio_refs = stdin_fn2, stdout_fn2, stderr_fn2
def gsapi_set_poll(instance, poll_fn):
poll_fn2 = _poll_fn(poll_fn)
e = _libgs.gsapi_set_poll(instance, poll_fn2)
- if not e:
- global _gsapi_set_poll_refs
- _gsapi_set_poll_refs = poll_fn2
- return e
+ if e < 0:
+ raise GSError(e)
+ global _gsapi_set_poll_refs
+ _gsapi_set_poll_refs = poll_fn2
class display_callback:
@@ -208,11 +224,11 @@ def gsapi_set_display_callback(instance, callback):
setattr(callback2, name, value2)
e = _libgs.gsapi_set_display_callback(instance, ctypes.byref(callback2))
- if not e:
- # Ensure that we keep references to callbacks.
- global _gsapi_set_display_callback_refs
- _gsapi_set_display_callback_refs = callback2
- return e
+ if e < 0:
+ raise GSError(e)
+ # Ensure that we keep references to callbacks.
+ global _gsapi_set_display_callback_refs
+ _gsapi_set_display_callback_refs = callback2
def gsapi_set_default_device_list(instance, list_):
@@ -221,7 +237,8 @@ def gsapi_set_default_device_list(instance, list_):
assert isinstance(list_, str)
list_2 = list_.encode('utf-8')
e = _libgs.gsapi_set_default_device_list(instance, list_2, len(list_))
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_get_default_device_list(instance):
@@ -237,9 +254,9 @@ def gsapi_get_default_device_list(instance):
ctypes.byref(list_),
ctypes.byref(len_),
)
- if e:
- return e, ''
- return e, list_[:len_.value].decode('latin-1')
+ if e < 0:
+ raise GSError(e)
+ return list_[:len_.value].decode('latin-1')
GS_ARG_ENCODING_LOCAL = 0
@@ -254,15 +271,15 @@ def gsapi_set_arg_encoding(instance, encoding):
GS_ARG_ENCODING_UTF16LE,
)
e = _libgs.gsapi_set_arg_encoding(instance, encoding)
- if not e:
- if encoding == GS_ARG_ENCODING_LOCAL:
- # This is probably wrong on Windows.
- _encoding = 'utf-8'
- elif encoding == GS_ARG_ENCODING_UTF8:
- _encoding = 'utf-8'
- elif encoding == GS_ARG_ENCODING_UTF16LE:
- _encoding = 'utf-16-le'
- return e
+ if e < 0:
+ raise GSError(e)
+ if encoding == GS_ARG_ENCODING_LOCAL:
+ # This is probably wrong on Windows.
+ _encoding = 'utf-8'
+ elif encoding == GS_ARG_ENCODING_UTF8:
+ _encoding = 'utf-8'
+ elif encoding == GS_ARG_ENCODING_UTF16LE:
+ _encoding = 'utf-16-le'
def gsapi_init_with_args(instance, args):
@@ -279,7 +296,8 @@ def gsapi_init_with_args(instance, args):
argv[argc] = None
e = _libgs.gsapi_init_with_args(instance, argc, argv)
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_run_string_begin(instance, user_errors):
@@ -288,7 +306,9 @@ def gsapi_run_string_begin(instance, user_errors):
'''
pexit_code = ctypes.c_int()
e = _libgs.gsapi_run_string_begin(instance, user_errors, ctypes.byref(pexit_code))
- return e, pexit_code.value
+ if e < 0:
+ raise GSError(e)
+ return pexit_code.value
def gsapi_run_string_continue(instance, str_, user_errors):
@@ -296,7 +316,9 @@ def gsapi_run_string_continue(instance, str_, user_errors):
<str_> should be either a python string or a bytes object. If the former,
it is converted into a bytes object using utf-8 encoding.
- Returns (e, exit_code).
+ We don't raise exception for gs_error_NeedInput.
+
+ Returns exit_code.
'''
if isinstance(str_, str):
str_ = str_.encode('utf-8')
@@ -309,7 +331,12 @@ def gsapi_run_string_continue(instance, str_, user_errors):
user_errors,
ctypes.byref(pexit_code),
)
- return e, pexit_code.value
+ if e == gs_error_NeedInput.num:
+ # This is normal, so we don't raise.
+ pass
+ elif e < 0:
+ raise GSError(e)
+ return pexit_code.value
def gsapi_run_string_end(instance, user_errors):
@@ -322,7 +349,9 @@ def gsapi_run_string_end(instance, user_errors):
user_errors,
ctypes.byref(pexit_code),
)
- return e, pexit_code.value
+ if e < 0:
+ raise GSError(e)
+ return pexit_code.value
def gsapi_run_string_with_length(instance, str_, length, user_errors):
@@ -332,7 +361,9 @@ def gsapi_run_string_with_length(instance, str_, length, user_errors):
Returns (e, exit_code).
'''
- return gsapi_run_string(instance, str_[:length], user_errors)
+ e = gsapi_run_string(instance, str_[:length], user_errors)
+ if e < 0:
+ raise GSError(e)
def gsapi_run_string(instance, str_, user_errors):
@@ -354,7 +385,9 @@ def gsapi_run_string(instance, str_, user_errors):
user_errors,
ctypes.byref(pexit_code),
)
- return e, pexit_code.value
+ if e < 0:
+ raise GSError(e)
+ return pexit_code.value
def gsapi_run_file(instance, filename, user_errors):
@@ -366,12 +399,15 @@ def gsapi_run_file(instance, filename, user_errors):
pexit_code = ctypes.c_int()
filename2 = filename.encode(_encoding)
e = _libgs.gsapi_run_file(instance, filename2, user_errors, ctypes.byref(pexit_code))
- return e, pexit_code.value
+ if e < 0:
+ raise GSError(e)
+ return pexit_code.value
def gsapi_exit(instance):
e = _libgs.gsapi_exit(instance)
- return e
+ if e < 0:
+ raise GSError(e)
gs_spt_invalid = -1
@@ -408,7 +444,8 @@ def gsapi_set_param(instance, param, value):
else:
assert 0, 'unrecognised type: %s' % type(value)
e = _libgs.gsapi_set_param(instance, type2, param2, value2)
- return e
+ if e < 0:
+ raise GSError(e)
GS_PERMIT_FILE_READING = 0
@@ -421,7 +458,8 @@ def gsapi_add_control_path(instance, type_, path):
# <path> to be encoded in encoding set by gsapi_set_arg_encoding().]
path2 = path.encode(_encoding)
e = _libgs.gsapi_add_control_path(instance, type_, path2)
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_remove_control_path(instance, type_, path):
@@ -429,28 +467,93 @@ def gsapi_remove_control_path(instance, type_, path):
# <path> to be encoded in encoding set by gsapi_set_arg_encoding().]
path2 = path.encode(_encoding)
e = _libgs.gsapi_remove_control_path(instance, type_, path2)
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_purge_control_paths(instance, type_):
e = _libgs.gsapi_purge_control_paths(instance, type_)
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_activate_path_control(instance, enable):
e = _libgs.gsapi_activate_path_control(instance, enable)
- return e
+ if e < 0:
+ raise GSError(e)
def gsapi_is_path_control_active(instance):
e = _libgs.gsapi_is_path_control_active(instance)
- return e
+ if e < 0:
+ raise GSError(e)
# Implementation details.
#
+_Error_num_to_error = dict()
+class _Error:
+ def __init__(self, num, desc):
+ self.num = num
+ self.desc = desc
+ _Error_num_to_error[self.num] = self
+
+gs_error_ok = _Error( 0, 'ok')
+gs_error_unknownerror = _Error( -1, 'unknown error')
+gs_error_dictfull = _Error( -2, 'dict full')
+gs_error_dictstackoverflow = _Error( -3, 'dict stack overflow')
+gs_error_dictstackunderflow = _Error( -4, 'dict stack underflow')
+gs_error_execstackoverflow = _Error( -5, 'exec stack overflow')
+gs_error_interrupt = _Error( -6, 'interrupt')
+gs_error_invalidaccess = _Error( -7, 'invalid access')
+gs_error_invalidexit = _Error( -8, 'invalid exit')
+gs_error_invalidfileaccess = _Error( -9, 'invalid fileaccess')
+gs_error_invalidfont = _Error( -10, 'invalid font')
+gs_error_invalidrestore = _Error( -11, 'invalid restore')
+gs_error_ioerror = _Error( -12, 'ioerror')
+gs_error_limitcheck = _Error( -13, 'limit check')
+gs_error_nocurrentpoint = _Error( -14, 'no current point')
+gs_error_rangecheck = _Error( -15, 'range check')
+gs_error_stackoverflow = _Error( -16, 'stack overflow')
+gs_error_stackunderflow = _Error( -17, 'stack underflow')
+gs_error_syntaxerror = _Error( -18, 'syntax error')
+gs_error_timeout = _Error( -19, 'timeout')
+gs_error_typecheck = _Error( -20, 'type check')
+gs_error_undefined = _Error( -21, 'undefined')
+gs_error_undefinedfilename = _Error( -22, 'undefined filename')
+gs_error_undefinedresult = _Error( -23, 'undefined result')
+gs_error_unmatchedmark = _Error( -24, 'unmatched mark')
+gs_error_VMerror = _Error( -25, 'VMerror')
+
+gs_error_configurationerror = _Error( -26, 'configuration error')
+gs_error_undefinedresource = _Error( -27, 'undefined resource')
+gs_error_unregistered = _Error( -28, 'unregistered')
+gs_error_invalidcontext = _Error( -29, 'invalid context')
+gs_error_invalidid = _Error( -30, 'invalid id')
+
+gs_error_hit_detected = _Error( -99, 'hit detected')
+gs_error_Fatal = _Error(-100, 'Fatal')
+gs_error_Quit = _Error(-101, 'Quit')
+gs_error_InterpreterExit = _Error(-102, 'Interpreter Exit')
+gs_error_Remap_Color = _Error(-103, 'Remap Color')
+gs_error_ExecStackUnderflow = _Error(-104, 'Exec Stack Underflow')
+gs_error_VMreclaim = _Error(-105, 'VM reclaim')
+gs_error_NeedInput = _Error(-106, 'Need Input')
+gs_error_NeedFile = _Error(-107, 'Need File')
+gs_error_Info = _Error(-110, 'Info')
+gs_error_handled = _Error(-111, 'handled')
+
+def _gs_error_text(gs_error):
+ '''
+ Returns text description of <gs_error>. See base/gserrors.h.
+ '''
+ e = _Error_num_to_error.get(gs_error)
+ if e:
+ return e.desc
+ return 'no error'
+
# The encoding that we use when passing strings to the underlying gsapi_*() C
# functions. Changed by gsapi_set_arg_encoding().
@@ -626,47 +729,33 @@ if __name__ == '__main__':
print('libgs: %s' % _libgs)
- e, revision = gsapi_revision()
- print('libgs.gsapi_revision => e=%s revision=%s' % (e, revision))
- assert not e
-
- e, revision = gsapi_revision()
- print('gsapi_revision() => e=%s: %s' % (e, revision))
- assert not e
-
+ revision = gsapi_revision()
+ print('libgs.gsapi_revision() ok: %s' % revision)
- e, instance = gsapi_new_instance(1)
- print('gsapi_new_instance => e=%s: %s' % (e, instance))
- assert not e
+ instance = gsapi_new_instance(1)
+ print('gsapi_new_instance() ok: %s' % instance)
- e = gsapi_set_arg_encoding(instance, GS_ARG_ENCODING_UTF8)
- print('gsapi_set_arg_encoding => e=%s' % e)
- assert not e
+ gsapi_set_arg_encoding(instance, GS_ARG_ENCODING_UTF8)
+ print('gsapi_set_arg_encoding() ok.')
def stdout_fn(caller_handle, bytes_):
sys.stdout.write(bytes_.decode('latin-1'))
- e = gsapi_set_stdio(instance, None, stdout_fn, None)
- print('gsapi_set_stdio => e=%s' % e)
- assert not e
+ gsapi_set_stdio(instance, None, stdout_fn, None)
+ print('gsapi_set_stdio() ok.')
d = display_callback()
- e = gsapi_set_display_callback(instance, d)
- print('gsapi_set_display_callback => e=%s' % e)
- assert not e
+ gsapi_set_display_callback(instance, d)
+ print('gsapi_set_display_callback() ok.')
- e = gsapi_set_default_device_list(instance, 'bmp256 bmp32b bmpgray bmpmono bmpsep1 bmpsep8 ccr cdeskjet cdj1600 cdj500')
- print('gsapi_set_default_device_list => e=%s' % e)
- assert not e
+ gsapi_set_default_device_list(instance, 'bmp256 bmp32b bmpgray bmpmono bmpsep1 bmpsep8 ccr cdeskjet cdj1600 cdj500')
+ print('gsapi_set_default_device_list() ok.')
- e, l = gsapi_get_default_device_list(instance)
- print('gsapi_get_default_device_list => e=%s l=%s' % (e, l))
- assert not e
+ l = gsapi_get_default_device_list(instance)
+ print('gsapi_get_default_device_list() ok: l=%s' % l)
- e = gsapi_init_with_args(instance, ['gs',])
- print('gsapi_init_with_args => e=%s' % e)
- assert not e
+ gsapi_init_with_args(instance, ['gs',])
+ print('gsapi_init_with_args() ok')
for value in 32, True, 3.14, 'hello world':
- e = gsapi_set_param(instance, "foo", value);
- print('gsapi_set_param %s => e=%s' % (value, e))
- assert not e
+ gsapi_set_param(instance, "foo", value);
+ print('gsapi_set_param() %s ok.' % value)