[designspaceLib] [t1Lib] [misc.macRes] fix some cases where pathlib.Path objects were not accepted (#1421)

* fix two cases where pathlib.Path objects were not accepted

* make macRes reader accept os.PathLike objects

* use __fspath__ explicitly to support os.PathLike paths

* use __fspath__ explicitly to support os.PathLike paths

* convert tmpdir to str

* only test pathlib.Path on Python 3.6 and up
This commit is contained in:
Just van Rossum 2018-12-30 12:07:05 +01:00 committed by GitHub
parent 8e080f2a82
commit 327b05d8c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 2 deletions

View File

@ -1053,6 +1053,8 @@ class DesignSpaceDocument(LogMixin, AsDictMixin):
return f.getvalue()
def read(self, path):
if hasattr(path, "__fspath__"): # support os.PathLike objects
path = path.__fspath__()
self.path = path
self.filename = os.path.basename(path)
reader = self.readerClass(path, self)
@ -1061,6 +1063,8 @@ class DesignSpaceDocument(LogMixin, AsDictMixin):
self.findDefault()
def write(self, path):
if hasattr(path, "__fspath__"): # support os.PathLike objects
path = path.__fspath__()
self.path = path
self.filename = os.path.basename(path)
self.updatePaths()

View File

@ -33,6 +33,8 @@ class ResourceReader(MutableMapping):
@staticmethod
def openResourceFork(path):
if hasattr(path, "__fspath__"): # support os.PathLike objects
path = path.__fspath__()
with open(path + '/..namedfork/rsrc', 'rb') as resfork:
data = resfork.read()
infile = BytesIO(data)

View File

@ -108,11 +108,12 @@ class T1Font(object):
def read(path, onlyHeader=False):
"""reads any Type 1 font file, returns raw data"""
normpath = path.lower()
_, ext = os.path.splitext(path)
ext = ext.lower()
creator, typ = getMacCreatorAndType(path)
if typ == 'LWFN':
return readLWFN(path, onlyHeader), 'LWFN'
if normpath[-4:] == '.pfb':
if ext == '.pfb':
return readPFB(path, onlyHeader), 'PFB'
else:
return readOther(path), 'OTHER'

View File

@ -4,6 +4,7 @@ from __future__ import (print_function, division, absolute_import,
unicode_literals)
import os
import sys
import pytest
import warnings
@ -785,3 +786,19 @@ def test_documentLib(tmpdir):
assert dummyKey in new.lib
assert new.lib[dummyKey] == dummyData
@pytest.mark.skipif(sys.version_info[:2] < (3, 6), reason="pathlib is only tested on 3.6 and up")
def test_read_with_path_object():
import pathlib
source = (pathlib.Path(__file__) / "../data/test.designspace").resolve()
assert source.exists()
doc = DesignSpaceDocument()
doc.read(source)
@pytest.mark.skipif(sys.version_info[:2] < (3, 6), reason="pathlib is only tested on 3.6 and up")
def test_with_with_path_object(tmpdir):
import pathlib
tmpdir = str(tmpdir)
dest = pathlib.Path(tmpdir) / "test.designspace"
doc = DesignSpaceDocument()
doc.write(dest)
assert dest.exists()

View File

@ -2,6 +2,7 @@ from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
import unittest
import os
import sys
from fontTools import t1Lib
from fontTools.pens.basePen import NullPen
import random
@ -50,6 +51,11 @@ class ReadWriteTest(unittest.TestCase):
data = self.write(font, 'OTHER', dohex=True)
self.assertEqual(font.getData(), data)
@unittest.skipIf(sys.version_info[:2] < (3, 6), "pathlib is only tested on 3.6 and up")
def test_read_with_path(self):
import pathlib
font = t1Lib.T1Font(pathlib.Path(PFB))
@staticmethod
def write(font, outtype, dohex=False):
temp = os.path.join(DATADIR, 'temp.' + outtype.lower())