Skip to content

Commit fcb6f4c

Browse files
Nickatakilevkivskyi
authored andcommitted
Fix for issue #524 (#527)
Check whether Protocol.__doc__ exists before modifying it. Fixes the crash in typing_extensions when run with -OO flag.
1 parent 7d7ffcd commit fcb6f4c

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

python2/test_typing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import collections
44
import contextlib
5+
import os
56
import pickle
67
import re
8+
import subprocess
79
import sys
810
from unittest import TestCase, main, SkipTest
911
from copy import copy, deepcopy
@@ -1963,6 +1965,16 @@ def foo(x):
19631965

19641966
self.assertIsNone(typing.get_type_hints(foo))
19651967

1968+
def test_typing_compiles_with_opt(self):
1969+
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
1970+
'typing.py')
1971+
try:
1972+
subprocess.check_output('python -OO {}'.format(file_path),
1973+
stderr=subprocess.STDOUT,
1974+
shell=True)
1975+
except subprocess.CalledProcessError:
1976+
self.fail('Module does not compile with optimize=2 (-OO flag).')
1977+
19661978

19671979
if __name__ == '__main__':
19681980
main()

src/test_typing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import contextlib
22
import collections
3+
import os
34
import pickle
45
import re
6+
import subprocess
57
import sys
68
from unittest import TestCase, main, skipUnless, SkipTest, expectedFailure
79
from copy import copy, deepcopy
@@ -2638,6 +2640,16 @@ def test_all(self):
26382640
self.assertIn('SupportsBytes', a)
26392641
self.assertIn('SupportsComplex', a)
26402642

2643+
def test_typing_compiles_with_opt(self):
2644+
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
2645+
'typing.py')
2646+
try:
2647+
subprocess.check_output('python -OO {}'.format(file_path),
2648+
stderr=subprocess.STDOUT,
2649+
shell=True)
2650+
except subprocess.CalledProcessError:
2651+
self.fail('Module does not compile with optimize=2 (-OO flag).')
2652+
26412653

26422654
if __name__ == '__main__':
26432655
main()

typing_extensions/src_py2/test_typing_extensions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import contextlib
55
import collections
66
import pickle
7+
import subprocess
78
from unittest import TestCase, main, skipUnless
89

910
from typing_extensions import NoReturn, ClassVar
@@ -740,6 +741,16 @@ def test_typing_extensions_defers_when_possible(self):
740741
getattr(typing_extensions, item),
741742
getattr(typing, item))
742743

744+
def test_typing_extensions_compiles_with_opt(self):
745+
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
746+
'typing_extensions.py')
747+
try:
748+
subprocess.check_output('python -OO {}'.format(file_path),
749+
stderr=subprocess.STDOUT,
750+
shell=True)
751+
except subprocess.CalledProcessError:
752+
self.fail('Module does not compile with optimize=2 (-OO flag).')
753+
743754

744755
if __name__ == '__main__':
745756
main()

typing_extensions/src_py3/test_typing_extensions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import contextlib
55
import collections
66
import pickle
7+
import subprocess
78
from unittest import TestCase, main, skipUnless
89
from typing import TypeVar, Optional
910
from typing import T, KT, VT # Not in __all__.
@@ -1208,6 +1209,7 @@ class E:
12081209

12091210

12101211
class AllTests(BaseTestCase):
1212+
12111213
def test_typing_extensions_includes_standard(self):
12121214
a = typing_extensions.__all__
12131215
self.assertIn('ClassVar', a)
@@ -1244,6 +1246,16 @@ def test_typing_extensions_defers_when_possible(self):
12441246
getattr(typing_extensions, item),
12451247
getattr(typing, item))
12461248

1249+
def test_typing_extensions_compiles_with_opt(self):
1250+
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
1251+
'typing_extensions.py')
1252+
try:
1253+
subprocess.check_output('python -OO {}'.format(file_path),
1254+
stderr=subprocess.STDOUT,
1255+
shell=True)
1256+
except subprocess.CalledProcessError:
1257+
self.fail('Module does not compile with optimize=2 (-OO flag).')
1258+
12471259

12481260
if __name__ == '__main__':
12491261
main()

typing_extensions/src_py3/typing_extensions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,9 @@ def __new__(cls, *args, **kwds):
981981
if OLD_GENERICS:
982982
return _generic_new(_next_in_mro(cls), cls, *args, **kwds)
983983
return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
984-
985-
Protocol.__doc__ = Protocol.__doc__.format(bases="Protocol, Generic[T]" if
986-
OLD_GENERICS else "Protocol[T]")
984+
if Protocol.__doc__ is not None:
985+
Protocol.__doc__ = Protocol.__doc__.format(bases="Protocol, Generic[T]" if
986+
OLD_GENERICS else "Protocol[T]")
987987

988988
def runtime(cls):
989989
"""Mark a protocol class as a runtime protocol, so that it

0 commit comments

Comments
 (0)