56 Commits

Author SHA1 Message Date
Khaled Hosny
232b2ccbc4 Move the rest of py23 module to textTools
Change all imports to use textTools module, except the test_py23.py test
which is kept until we decide to remove the module (if ever).
2021-08-20 01:29:45 +02:00
Behdad Esfahbod
80069e9608 [py23] Make tobytes() always return bytes even for bytearray 2021-04-08 11:13:15 -06:00
Behdad Esfahbod
3a80b56829 [py23] Change tobytes() to accept bytearray 2021-04-08 09:41:45 -06:00
Chris Simpkins
3e252d8c87
[py23] change deprecation warning from "next release" to "future release" 2021-03-20 11:52:12 -04:00
Nikolaus Waxweiler
ab45b3b0c1 Pare down py23 module to Python 3 symbols only, deprecate 2019-08-09 11:30:39 +01:00
justvanrossum
20c93b9fdb use RecursionError, and provide an alias for RuntimeError for Py2 2019-03-17 18:18:10 +01:00
Cosimo Lupo
2150ef875f
py23: add backport for math.isfinite (python >= 3.2) 2019-01-22 15:42:54 +00:00
Cosimo Lupo
d9d30c819a
loggingTools: move here the LastResortLogger from py23
and add a small test
2018-06-19 16:54:50 +01:00
Cosimo Lupo
e6a5db543d
py23: don't automatically set logging.lastResort handler on py27
To avoid the 'No handlers could be fonud' message, from now on, one should always
configure logging in one's scripts or applications.

logging.basicConfig, or fontTools.configLogger (with some predefined defaults
useful for fonttools scripts) can be used to quickly configure logging.
Read the python docs for more advanced logging usage.
2018-06-19 15:35:28 +01:00
Cosimo Lupo
c4950ecc8b
py23: add 'zip' (alias to izip in py2) to __all__ 2018-06-12 17:29:10 +01:00
Cosimo Lupo
a84c097c10
py23: alias itertools.izip as 'zip'
so we consistently use python3 zip iterator on both py2 and py3
2018-06-06 18:21:15 +01:00
Andy Clymer
a3307324ae Includes a suggested fix from @anthrotype 2018-04-03 15:40:54 -04:00
Andy Clymer
241a4474b4 Fixes the eq comparison of the Tag object to work with a None type 2018-04-03 14:54:39 -04:00
Behdad Esfahbod
524b62a1b2 Add fontTools.ttLib.sfnt.readTTCHeader() 2018-01-23 13:44:56 -08:00
Cosimo Lupo
350b73363b [py23] must define redirect_stderr for py3.4 2017-11-07 12:08:04 +00:00
Cosimo Lupo
d3251241be [py23] Add backports for redirect_stdout/stderr context managers 2017-11-07 11:54:28 +00:00
Cosimo Lupo
a8442490dd
[py23] add backport for python 3's SimpleNamespace class
This can be useful sometimes. The code is taken from the docs:

https://docs.python.org/3/library/types.html#types.SimpleNamespace
2017-04-10 19:47:48 +01:00
Cosimo Lupo
972b3e6a15
[py23] handle exception at shutdown in 'lastResort' logging handler
On python2.7, the fonttools py23 module registers a 'lastResort' StreamHandler
similar to the one found in python3's logging module, that always writes
to the current value `sys.stderr`.

This also applies to any python library that imports from fontTools.misc.py23
under python2.7.

The logging module has a 'shutdown' atexit handler that flushes all the
logging handlers' streams just before the python interpreter exits.

Sometimes (e.g. when calling `python setup.py test` as in MutatorMath's test
suite), the interpreter termination ends with a traceback, which is
triggered by the atexit handler failing to flush the lastResort handler's
stream, sys.stderr

AttributeError: None has no attribute 'stderr'

This is because during module teardown, the globals (in this case 'sys')
are set to None, and the order in which modules are deleted is not
guaranteed.

See 58531934a8
2017-01-28 14:18:29 +00:00
Cosimo Lupo
1a9389653c
py23: minor typo in docstring 2016-12-14 10:03:49 +00:00
Cosimo Lupo
e7867529ad
py23: update round3 docstring to indicate that None is an allowable argument 2016-12-02 12:15:47 +00:00
Cosimo Lupo
f38cde159b
py23: in Python3 < 3.6, round3 wraps built-in round() to workaround inconsistent behaviour between floats/ints and ndigits=None 2016-12-02 12:15:47 +00:00
Cosimo Lupo
eb8654dc4e
py23: move import decimal to top-level scope 2016-12-01 17:43:35 +00:00
Cosimo Lupo
b09501fd8b
py23: only use the slow round3 implementation for half-way, even cases; add backport of py35 math.isclose() 2016-12-01 17:12:20 +00:00
Cosimo Lupo
f742ea07e8
py23: in round3, avoid rounding if number is already int; return the same type when called with two args
We only define 'round3' for PY2 and 'round2' for PY3, and also make sure 'round3' is always an alias of
the built-in 'round' on Python 3; and similarly 'round2' is an alias of built-in 'round' on Python 2.

Thus, for clarity, one can do:

    from fontTools.misc.py23 import round3 as round

or

    from fontTools.misc.py23 import round2 as round

and be certain that the fast built-in implementation will be used on the
respective python major version.
2016-11-30 12:58:58 +00:00
Cosimo Lupo
8de2f44b31 [py23] don't export 'round2' and 'round3' in __all__, only 'round'
No need to pollute the namespace. If one needs one or the other, one can just import that explicitly.
2016-09-13 18:44:30 +02:00
Cosimo Lupo
f07c29c8bc [py23] export 'round' in __all__
(let's see if some of our tests that import * breaks now...)
2016-09-13 07:16:12 +01:00
Cosimo Lupo
b22f8c7310 [py23] in PY2 shadow built-in round with round3; in PY3 simply use built-in round 2016-09-13 07:13:32 +01:00
Cosimo Lupo
ffa03f6566 [py23] add round2 and round3 function for simulating Python 2 and Python 3 built-in round
The implementation is inspired by https://github.com/PythonCharmers/python-future/blob/master/src/future/builtins/newround.py

It adds support for the old Python 2 round, and for negative 'ndigits'
2016-09-13 07:13:32 +01:00
Cosimo Lupo
a796b03366 py23: raise Py23Error when using 'xrange'
see https://github.com/behdad/fonttools/pull/600#issuecomment-216935319
2016-05-04 18:31:13 +01:00
Cosimo Lupo
7efb32b2de py23: always use iterator whether one uses 'range' (py3) or 'xrange' (py2) 2016-05-04 17:16:39 +01:00
Cosimo Lupo
da04ab2909 [py23] define __all__ variable
specifying the list of names that are imported when `from py23 import *` is used
2016-04-07 09:21:05 +01:00
Cosimo Lupo
52620787d8 [py23] set the lastResort handler and custom logger class only for python 2
move _StderrHandler and _Logger classes here (previously defined in loggingTools)
2016-02-01 13:17:15 +00:00
Cosimo Lupo
47bde21b86 [py23] add wrapper around io.open bridging py23 differences 2016-01-25 17:52:45 +00:00
Cosimo Lupo
2eda1785f6 [py23] fix ImportError when trying to import unichr, basestring or unicode when already defined
When one does `from fontTools.misc.py23 import *`, everything seems to work fine.

However, linters will complain when one uses the asterisk to import all names from a module, since they can't detect when names are left undefined -- asterisks are greedy and will eat all names.

If one avoids the asterik and attempts to import explicitly, like in `from fontTools.misc.py23 import basestring`, the problem then is that, if `py23` does not re-define the name -- e.g. under python2 `basestring` or `unicode` are built-ins -- then the import statement raises `ImportError`.

The same happens for the `unichr` function on a "wide" Python 2 build (in which `sys.maxunicode == 0x10FFFF`).

Now, to work around this, we need to re-assign those built-ins to their very same names. This may look silly, but at least it works.
2015-11-23 12:02:12 +00:00
Cosimo Lupo
2e058808fe [py23] define BytesIO, StringIO and 'UnicodeIO' to disambiguate bytes vs unicode in-memory streams 2015-08-07 17:24:42 +01:00
Cosimo Lupo
a29383eb9c [py23] minor formatting fix 2015-05-08 19:53:15 +01:00
Cosimo Lupo
8ff416f2c5 [py23] simulate "wide" unichr and ord on "narrow" UCS2-only Python 2 2015-05-08 19:28:42 +01:00
Behdad Esfahbod
ba0a3b9abb Revamp name table Unicode handling some more
Part of https://github.com/behdad/fonttools/issues/236

Now we fallback to ASCII for unknown encodings.  Not sure if this might be a bad idea.
The main user-visible difference is that if there's an ASCII-only text in an unknown
encoding, we still "decode" it and use unicode="True" instead of unicode="False".

Or is assuming that any unsupported encoding is ASCII-compatible too intrusive?
2015-04-16 17:09:49 -07:00
Behdad Esfahbod
8b0152f0a1 Fix XMLWriter to take sinks that accept both bytes() and unicodes()
Fix xmlWriter_test with python3 as well.
2015-04-14 19:07:34 -07:00
Behdad Esfahbod
4af65b0f3d [py23] Minor refactoring 2014-07-21 13:19:53 -04:00
Behdad Esfahbod
1ae29591ef from __future__ import absolute_import
Such that our Python 2 is closer to Python 3.

Part of https://github.com/behdad/fonttools/issues/77
2014-01-14 15:07:50 +08:00
Behdad Esfahbod
8ea6439d3b Implement __ne__ when __eq__ is defined 2013-12-06 22:25:48 -05:00
Behdad Esfahbod
cc13b7889d Simplify 2013-12-04 04:12:57 -05:00
Behdad Esfahbod
94968f3151 Use pure-Python StringIO, not cStringIO
misc.psLib tries to subclass StringIO.  It doesn't work with
the cStringIO version.

Change doesn't seem to affect performance of CFF, which is the
biggest StringIO user.

7279302238 (commitcomment-4767054)
2013-12-04 04:11:06 -05:00
Denis Jacquerye
db08ee2c19 missing from __future__ import print_function in setup.py
python3: sum() can't sum bytes, using b''.join(seq) instead
2013-11-29 14:15:48 -05:00
Behdad Esfahbod
c076261598 s/latin-1/latin1/g 2013-11-28 17:32:44 -05:00
Behdad Esfahbod
18316aa769 ps23 More bytes fixes. All ''join()'s fixed 2013-11-28 17:32:43 -05:00
Behdad Esfahbod
d3689390a6 py23 Fix up byteord() implementation 2013-11-28 17:32:43 -05:00
Behdad Esfahbod
3e8795d99a py23 Use io.BytesIO to replace StringIO.StringIO 2013-11-28 17:32:43 -05:00
Behdad Esfahbod
821572c9a9 py23 Add bytesjoin() 2013-11-28 17:32:43 -05:00