amueller / word_cloud

A little word cloud generator in Python
https://amueller.github.io/word_cloud
MIT License
10.1k stars 2.31k forks source link

Unexplainable errors when generating SVG #626

Open pieman2201 opened 3 years ago

pieman2201 commented 3 years ago

Description

Segfaults / memory errors seemingly at random? I ran the same code 4 times: each trial produced a different result. I restarted the Python interpreter between trials.

Steps/Code to Reproduce

import wordcloud
wordcloud.WordCloud(width = 800, height = 800).generate("hi hi hi hi hi hi hi hi hi hi hi h ih i hvfjdksnfdjkl f dsg fdsg fdsg").to_svg(embed_font = True)

Expected Results

A wordcloud svg as a string output

Actual Results

Trial 1
Python 3.9.1 (default, Feb  6 2021, 06:49:13) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wordcloud
>>> wordcloud.WordCloud(width = 800, height = 800).generate("hi hi hi hi hi hi hi hi hi hi hi h ih i hvfjdksnfdjkl f dsg fdsg fdsg").to_svg(embed_font = True)
munmap_chunk(): invalid pointer
Aborted (core dumped)
Trial 2

Of the 4 trials with the exact same input, this was the only one to actually return a string.

Python 3.9.1 (default, Feb  6 2021, 06:49:13) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wordcloud
>>> wordcloud.WordCloud(width = 800, height = 800).generate("hi hi hi hi hi hi hi hi hi hi hi h ih i hvfjdksnfdjkl f dsg fdsg fdsg").to_svg(embed_font = True)
'<svg xmlns="http://www.w3.org/2000/svg" width="800" height="800">\n<style>@font-face{font-family:\'Droid Sans Mono\';font-weight:normal;font-style:normal;src:url("data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAAYoAAsAAAAABxQAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABPUy8yAAABeAAAAFYAAABgfZx2OWNtYXAAAAHsAAAAQx.... essentially a valid SVG ...</svg>'
Trial 3
Python 3.9.1 (default, Feb  6 2021, 06:49:13) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wordcloud
>>> wordcloud.WordCloud(width = 800, height = 800).generate("hi hi hi hi hi hi hi hi hi hi hi h ih i hvfjdksnfdjkl f dsg fdsg fdsg").to_svg(embed_font = True)
Bus error (core dumped)
Trial 4
Python 3.9.1 (default, Feb  6 2021, 06:49:13) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wordcloud
>>> wordcloud.WordCloud(width = 800, height = 800).generate("hi hi hi hi hi hi hi hi hi hi hi h ih i hvfjdksnfdjkl f dsg fdsg fdsg").to_svg(embed_font = True)
Segmentation fault (core dumped)

Versions

Python 3.9.1 (default, Feb  6 2021, 06:49:13) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform; print(platform.platform())
Linux-5.10.16-arch1-1-x86_64-with-glibc2.33
>>> import sys; print("Python", sys.version)
Python 3.9.1 (default, Feb  6 2021, 06:49:13) 
[GCC 10.2.0]
>>> import numpy; print("NumPy", numpy.__version__)
NumPy 1.20.1
>>> import matplotlib; print("matplotlib", matplotlib.__version__)
matplotlib 3.3.4
>>> import wordcloud; print("wordcoud", wordcloud.__version__)
wordcoud 1.8.1
pieman2201 commented 3 years ago

I may (?) have further insight into the issue. Upon trying the same code multiple more times, I ran into the following Python error and segfault:

>>> import wordcloud
>>> wordcloud.WordCloud(width = 800, height = 800).generate("hi hi hi hi hi hi hi hi hi hi hi h ih i hvfjdksnfdjkl f dsg fdsg fdsg").to_svg(embed_font = True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 632, in generate
    return self.generate_from_text(text)
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 614, in generate_from_text
    self.generate_from_frequencies(words)
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 446, in generate_from_frequencies
    self.generate_from_frequencies(dict(frequencies[:2]),
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 501, in generate_from_frequencies
    box_size = draw.textsize(word, font=transposed_font)
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/PIL/ImageDraw.py", line 479, in textsize
    return font.getsize(text, direction, features, language, stroke_width)
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/PIL/ImageFont.py", line 769, in getsize
    w, h = self.font.getsize(text)
  File "/home/ishan/docs/code/html/personal-site/venv/lib/python3.9/site-packages/PIL/ImageFont.py", line 430, in getsize
    size, offset = self.font.getsize(text, "L", direction, features, language)
OSError: invalid face handle
>>> 
Segmentation fault (core dumped)
amueller commented 3 years ago

This might be related to #516? Whta version of pil do you have?