8000 new_from_source keeps a ref to the source · libvips/pyvips@3eff2c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3eff2c7

Browse files
committed
new_from_source keeps a ref to the source
Since it might be a custom source with callabcks, and the python wrapper for the source class must stay alive. Added another example of custom sources.
1 parent 4d5dfa9 commit 3eff2c7

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
- ensure compatibility with a single shared libvips library [kleisauke]
44
- add flags_dict(), enum_dict() for better flags introspection
5-
- improve generation of enums.py
5+
- improve generation of `enums.py`
6+
- add `stream.py` example
7+
- fix a missing reference issue with custom sources
68

79
## Version 2.2.2 (released 4 Jan 2023)
810

examples/stream.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import gc
5+
import requests
6+
import pyvips
7+
8+
URL = "https://cdn.filestackcontent.com/bnTGtQw5ShqMPpxH2tMw"
9+
URLS = [URL] * int(sys.argv[1])
10+
11+
session = requests.Session()
12+
13+
image = pyvips.Image.black(1500, 1500)
14+
15+
for i, url in enumerate(URLS):
16+
print(f"loading {url} ...")
17+
stream = session.get(url, stream=True).raw
18+
19+
source = pyvips.SourceCustom()
20+
source.on_read((lambda stream: stream.read)(stream))
21+
22+
tile = pyvips.Image.new_from_source(source, "", access="sequential")
23+
image = image.composite2(tile, "over", x= 50 * (i + 1), y= 50 * (i + 1))
24+
25+
print(f"writing output.jpg ...")
26+
image.write_to_file("output.jpg")
27+

pyvips/vimage.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,15 @@ def new_from_source(source, options, **kwargs):
664664
raise Error('unable to load from source')
665665
name = _to_string(pointer)
666666

667-
return pyvips.Operation.call(name, source,
668-
string_options=options, **kwargs)
667+
image = pyvips.Operation.call(name, source,
668+
string_options=options, **kwargs)
669+
670+
# keep a secret ref to the source object .. we need that to stay
671+
# alive, since it might be a custom source that triggers a python
672+
# callback
673+
image._references.append(source)
674+
675+
return image
669676

670677
@staticmethod
671678
def new_temp_file(format):

0 commit comments

Comments
 (0)
0