8000 tiny polish · libvips/pyvips@f41eff3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f41eff3

Browse files
committed
tiny polish
1 parent 29d244c commit f41eff3

File tree

8 files changed

+54
-52
lines changed

8 files changed

+54
-52
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@ Local user install::
7474

7575
Run test suite::
7676

77-
$ nosetests |& more
7877
$ nosetests --logging-level=WARNING
78+
$ python3 -m "nose" --logging-level=WARNING
79+

pyvips/base.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@
1919
logger.debug('Loaded lib {0}'.format(vips_lib))
2020
logger.debug('Loaded lib {0}'.format(gobject_lib))
2121

22-
is_PY3 = sys.version_info[0] == 3
22+
_is_PY3 = sys.version_info[0] == 3
2323

24-
if is_PY3:
24+
if _is_PY3:
2525
text_type = str
2626
else:
2727
text_type = unicode
2828

29+
def to_bytes(x):
30+
if isinstance(x, text_type):
31+
x = x.encode()
32+
return x
33+
34+
def to_string(x):
35+
if _is_PY3 and isinstance(x, bytes):
36+
x = x.decode('utf-8')
37+
return x
38+
2939
# apparently the best way to find out
30-
is_64bits = sys.maxsize > 2 ** 32
40+
_is_64bits = sys.maxsize > 2 ** 32
3141

3242
# GType is an int the size of a pointer ... I don't think we can just use
3343
# size_t, sadly
34-
if is_64bits:
44+
if _is_64bits:
3545
ffi.cdef('''
3646
typedef uint64_t GType;
3747
''')
@@ -81,12 +91,8 @@ def __init__(self, message, detail = None):
8191
def __str__(self):
8292
return '{0}\n {1}'.format(self.message, self.detail)
8393

84-
argv = sys.argv[0]
85-
if isinstance(argv, text_type):
86-
argv = argv.encode()
87-
88-
if vips_lib.vips_init(argv) != 0:
89-
raise Error('unable to init Vips')
94+
if vips_lib.vips_init(to_bytes(sys.argv[0])) != 0:
95+
raise Error('unable to init libvips')
9096

9197
def shutdown():
9298
logger.debug('Shutting down libvips')
@@ -98,16 +104,6 @@ def shutdown():
98104
def leak_set(leak):
99105
return vips_lib.vips_leak_set(leak)
100106

101-
def to_bytes(x):
102-
if isinstance(x, text_type):
103-
x = x.encode()
104-
return x
105-
106-
def to_string(x):
107-
if is_PY3 and isinstance(x, bytes):
108-
x = x.decode('utf-8')
109-
return x
110-
111107
def path_filename7(filename):
112108
return to_string(ffi.string(vips_lib.vips_path_filename7(to_bytes(filename))))
113109

pyvips/enums.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
logger = logging.getLogger(__name__)
88

9-
# you can supply enum values as strings or ints ... these classes give the ints
10-
# for each string, so pyvips.BandFormat.SHORT is equivalent to 'short'
11-
129
class BandFormat(object):
1310
UCHAR = 'uchar'
1411
CHAR = 'char'

pyvips/gobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class GObject(object):
4141
def __init__(self, pointer):
4242
# record the pointer we were given to manage
4343
self.pointer = pointer
44-
logger.debug('GObject.__init__: pointer = {0}'.format(self.pointer))
44+
# logger.debug('GObject.__init__: pointer = {0}'.format(self.pointer))
4545

4646
# on GC, unref
4747
self.gobject = ffi.gc(self.pointer, gobject_lib.g_object_unref)
48-
logger.debug('GObject.__init__: gobject = {0}'.format(self.gobject))
48+
# logger.debug('GObject.__init__: gobject = {0}'.format(self.gobject))
4949

5050
@staticmethod
5151
def print_all(msg):

pyvips/gvalue.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ def set(self, value):
105105
gobject_lib.g_value_set_double(self.gvalue, value)
106106
elif fundamental == GValue.genum_type:
107107
if isinstance(value, basestring if _is_PY2 else str):
108-
enum_value = vips_lib.vips_enum_from_nick(b'pyvips', gtype, to_bytes(value))
108+
enum_value = vips_lib.vips_enum_from_nick(b'pyvips', gtype,
109+
to_bytes(value))
109110

110111
if enum_value < 0:
111112
raise Error('no such enum {0}')
@@ -229,7 +230,7 @@ def get(self):
229230
result = ffi.unpack(buf, psize[0])
230231
else:
231232
raise Error('unsupported gtype for get {0}'.
232-
format(type_name(gtype)))
233+
format(type_name(gtype)))
233234

234235
return result
235236

pyvips/vimage.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ def _is_2D(array):
6262
return True
6363

6464
# https://stackoverflow.com/a/22409540/1480019
65-
def with_metaclass(mcls):
65+
def _with_metaclass(mcls):
6666
def decorator(cls):
6767
body = vars(cls).copy()
6868
# clean out class body
6969
body.pop('__dict__', None)
7070
body.pop('__weakref__', None)
71+
7172
return mcls(cls.__name__, cls.__bases__, body)
73+
7274
return decorator
7375

7476
# apply a function to a thing, or map over a list
7577
# we often need to do something like (1.0 / other) and need to work for lists
7678
# as well as scalars
77-
def smap(func, x):
79+
def _smap(func, x):
7880
if isinstance(x, list):
7981
return list(map(func, x))
8082
else:
@@ -130,7 +132,7 @@ def call_function(*args, **kwargs):
130132

131133
return call_function
132134

133-
@with_metaclass(ImageType)
135+
@_with_metaclass(ImageType)
134136
class Image(VipsObject):
135137
# private static
136138

@@ -159,8 +161,10 @@ def new_from_file(vips_filename, **kwargs):
159161
if name == ffi.NULL:
160162
raise Error('unable to load from file {0}'.format(vips_filename))
161163

162-
return Operation.call(to_string(ffi.string(name)), to_string(ffi.string(filename)),
163-
string_options = to_string(ffi.string(options)), **kwargs)
164+
return Operation.call(to_string(ffi.string(name)),
165+
to_string(ffi.string(filename)),
166+
string_options = to_string(ffi.string(options)),
167+
**kwargs)
164168

165169
@staticmethod
166170
def new_from_buffer(data, options, **kwargs):
@@ -206,7 +210,7 @@ def new_temp_file(format):
206210
def new_from_image(self, value):
207211
pixel = (Image.black(1, 1) + value).cast(self.format)
208212
image = pixel.embed(0, 0, self.width, self.height,
209-
extend = "copy")
213+
extend = "copy")
210214
image = image.copy(interpretation = self.interpretation,
211215
xres = self.xres,
212216
yres = self.yres,
@@ -233,7 +237,8 @@ def write_to_file(self, vips_filename, **kwargs):
233237
raise Error('unable to write to file {0}'.format(vips_filename))
234238

235239
return Operation.call(to_string(ffi.string(name)), self, filename,
236-
string_options = to_string(ffi.string(options)), **kwargs)
240+
string_options = to_string(ffi.string(options)),
241+
**kwargs)
237242

238243
def write_to_buffer(self, format_string, **kwargs):
239244
format_string = to_bytes(format_string)
@@ -243,7 +248,8 @@ def write_to_buffer(self, format_string, **kwargs):
243248
raise Error('unable to write to buffer')
244249

245250
return Operation.call(to_string(ffi.string(name)), self,
246-
string_options = to_string(ffi.string(options)), **kwargs)
251+
string_options = to_string(ffi.string(options)),
252+
**kwargs)
247253

248254
def write(self, other):
249255
result = vips_lib.vips_image_write(self.pointer, other.pointer)
@@ -257,7 +263,8 @@ def get_typeof(self, name):
257263

258264
def get(self, name):
259265
gv = GValue()
260-
result = vips_lib.vips_image_get(self.pointer, to_bytes(name), gv.pointer)
266+
result = vips_lib.vips_image_get(self.pointer, to_bytes(name),
267+
gv.pointer)
261268
if result != 0:
262269
raise Error('unable to get {0}'.format(name))
263270

@@ -359,7 +366,7 @@ def __sub__(self, other):
359366
if isinstance(other, package_index['Image']):
360367
return self.subtract(other)
361368
else:
362-
return self.linear(1, smap(lambda x: -1 * x, other))
369+
return self.linear(1, _smap(lambda x: -1 * x, other))
363370

364371
def __rsub__(self, other):
365372
return self.linear(-1, other)
@@ -379,7 +386,7 @@ def __div__(self, other):
379386
if isinstance(other, package_index['Image']):
380387
return self.divide(other)
381388
else:
382-
return self.linear(smap(lambda x: 1.0 / x, other), 0)
389+
return self.linear(_smap(lambda x: 1.0 / x, other), 0)
383390

384391
def __rdiv__(self, other):
385392
return (self ** -1) * other
@@ -394,7 +401,7 @@ def __floordiv__(self, other):
394401
if isinstance(other, package_index['Image']):
395402
return self.divide(other).floor()
396403
else:
397-
return self.linear(smap(lambda x: 1.0 / x, other), 0).floor()
404+
return self.linear(_smap(lambda x: 1.0 / x, other), 0).floor()
398405

399406
def __rfloordiv__(self, other):
400407
return ((self ** -1) * other).floor()
@@ -508,7 +515,7 @@ def bandjoin(self, other):
508515

509516
# if [other] is all numbers, we can use bandjoin_const
510517
non_number = next((x for x in other
511-
if not isinstance(x, numbers.Number)),
518+
if not isinstance(x, numbers.Number)),
512519
None)
513520

514521
if non_number == None:

pyvips/vobject.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_typeof(self, name):
9292
pspec, argument_class, argument_instance)
9393

9494
if result != 0:
95-
# need to clear any error
95+
# need to clear any error, this is horrible
9696
Error('')
9797
return 0
9898

@@ -125,6 +125,9 @@ def set(self, name, value):
125125
# set a series of options using a string, perhaps 'fred=12, tile'
126126
def set_string(self, string_options):
127127
vo = ffi.cast('VipsObject *', self.pointer)
128-
return vips_lib.vips_object_set_from_string(vo, to_bytes(string_options)) == 0
128+
result = vips_lib.vips_object_set_from_string(vo,
129+
to_bytes(string_options))
130+
131+
return result == 0
129132

130133
__all__ = ['VipsObject']

pyvips/voperation.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _find_inside(pred, thing):
5959
class Operation(VipsObject):
6060

6161
def __init__(self, pointer):
62-
logger.debug('Operation.__init__: pointer = {0}'.format(pointer))
62+
# logger.debug('Operation.__init__: pointer = {0}'.format(pointer))
6363
super(Operation, self).__init__(pointer)
6464

6565
def set(self, name, flags, match_image, value):
@@ -78,7 +78,7 @@ def set(self, name, flags, match_image, value):
7878

7979
# MODIFY args need to be copied before they are set
8080
if (flags & _MODIFY) != 0:
81-
logger.debug('copying MODIFY arg {0}'.format(name))
81+
# logger.debug('copying MODIFY arg {0}'.format(name))
8282
# make sure we have a unique copy
8383
value = value.copy().copy_memory()
8484

@@ -146,7 +146,7 @@ def call(operation_name, *args, **kwargs):
146146
if n_required != len(args):
147147
raise Error(('unable to call {0}: {1} arguments given, ' +
148148
'but {2} required').
149-
format(operation_name, len(args), n_required))
149+
format(operation_name, len(args), n_required))
150150

151151
# the first image argument is the thing we expand constants to
152152
# match ... look inside tables for images, since we may be passing
@@ -175,13 +175,10 @@ def call(operation_name, *args, **kwargs):
175175
op.set(name, flags_from_name[name], match_image, value)
176176

177177
# build operation
178-
vop2 = vips_lib.vips_cache_operation_build(op.pointer)
179-
if vop2 == ffi.NULL:
178+
vop = vips_lib.vips_cache_operation_build(op.pointer)
179+
if vop == ffi.NULL:
180180
raise Error('unable to call {0}'.format(operation_name))
181-
op2 = Operation(vop2)
182-
op = op2
183-
op2 = None
184-
vop2 = None
181+
op = Operation(vop)
185182

186183
# fetch required output args, plus modified input images
187184
result = []

0 commit comments

Comments
 (0)
0