question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError: 'post' on some fonts that have the 'post' table

See original GitHub issue

I initially reported this as https://github.com/rosettatype/hyperglot/issues/24, but it seems to be a fonttools bug:

I’m seeing KeyError: 'post' on some fonts that have the ‘post’ table.

The attached ZIP contains two test fonts and a simple test script to reproduce the problem.

Here’s the script:

from fontTools.ttLib import TTFont

def process_font(path):
    print('processing: %s' % path)
    font = TTFont(path, lazy=False)
    cmap = font["cmap"]
    font.close()
    return [chr(c) for c in cmap.getBestCmap().keys()]

filename = 'NotoSansDevanagari-Regular.ttf'
c = process_font(filename)
print(len(c))

filename = 'NotoSansMalayalam-Regular.ttf'
c = process_font(filename)
print(len(c))

…and here’s what I get:

$ python3 fonttools_test.py 
processing: NotoSansDevanagari-Regular.ttf
270
processing: NotoSansMalayalam-Regular.ttf
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/fontTools/ttLib/ttFont.py", line 372, in __getitem__
    return self.tables[tag]
KeyError: 'post'

fonttools_test_with_fonts.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
justvanrossumcommented, Mar 31, 2021

The error message I got was: “ValueError: I/O operation on a closed file”. But it was reported as an error occurring during the handling of KeyError: ‘post’, which was confusing. (#2251 takes away the KeyError in this case, so it’s more obvious that there’s an I/O problem happening.)

0reactions
justvanrossumcommented, Mar 31, 2021

FWIW, I figured out why the one font failed and the other succeeded:

  • cmap subtables are loaded lazily, and usually a subtable get decompiled only once you access its subtable.cmap
  • When there are multiple subtables, it is checked whether there are subtables that have identical data offsets, and if so, it is ensured that only one subtable is decompiled
  • The way this is implemented prevents to also do lazy loading, so the shared subtable’s data is decompiled immediately.

Ergo, in the hyperglot scenario / test script in this issue, if the source font has two unicode cmap subtables that share their data (which is common: (0, 3) and (3, 1) subtables are frequently both present, sharing their data) the cmap is loaded eagerly, and the premature font.close() doesn’t cause any trouble. But if the font does not share subtable data, cmap subtables are indeed loaded lazily, in which case the premature font.close() does cause trouble, as the glyph order has then not yet been loaded, and for that, the post table is needed.

Working font: two unicode cmap subtables Failing font: one unicode cmap subtable

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyError: 'post' on some fonts that have the 'post' table #24
I see crashes on some fonts (e.g. some of the Noto Fonts) with KeyError: post, e.g: $ hyperglot NotoSansMalayalam-Regular.ttf Traceback ...
Read more >
KeyError: -1 when appending new tag to soup in bs4
Each file I iterate over adds a new column to the table in the template. This works fine for the first file. I...
Read more >
Python KeyError Exceptions and How to Handle Them
In this tutorial, you'll learn how to handle Python KeyError exceptions. They are often caused by a bad key lookup in a dictionary,...
Read more >
KeyError in Python Code - Refinitiv Developer Community
When I run the code, I get the error message: KeyError: 'daily_return_AZN.L'. Fairly new to using Python and Refinitiv ...
Read more >
"KeyError: 'body'" Error sending email using "New ... - Odoo
Hi, I am running a Odoo10 community. I get an error when I try to send emails to contacts. Sending online proposals work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found