Skip to content

Commit dff9dd7

Browse files
committed
Introduced bpython._py3compat: Defines some useful attributes.
1 parent 700fa46 commit dff9dd7

File tree

7 files changed

+52
-15
lines changed

7 files changed

+52
-15
lines changed

bpython/_py3compat.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# encoding: utf-8
2+
3+
# The MIT License
4+
#
5+
# Copyright (c) 2012 the bpython authors.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
26+
27+
"""
28+
Helper module for Python 3 compatibility.
29+
30+
Defines the following attributes:
31+
32+
- PythonLexer: Pygment's Python lexer matching the hosting runtime's
33+
Python version.
34+
- py3: True if the hosting Python runtime is of Python version 3 or later
35+
"""
36+
37+
import sys
38+
39+
py3 = (sys.version_info[0] == 3)
40+
41+
if py3:
42+
from pygments.lexers import Python3Lexer as PythonLexer
43+
else:
44+
from pygments.lexers import PythonLexer

bpython/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@
8181
from bpython.translations import _
8282

8383
from bpython import repl
84+
from bpython._py3compat import py3
8485
from bpython.pager import page
8586
from bpython import autocomplete
8687
import bpython.args
8788

88-
py3 = sys.version_info[0] == 3
8989
if not py3:
9090
import inspect
9191

bpython/gtk_.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@
3737
import gobject
3838
import gtk
3939
import pango
40-
from pygments.lexers import PythonLexer
4140

4241
from bpython import importcompletion, repl, translations
42+
from bpython._py3compat import PythonLexer, py3
4343
from bpython.formatter import theme_map
4444
from bpython.translations import _
4545
import bpython.args
4646

4747

48-
py3 = sys.version_info[0] == 3
49-
5048
_COLORS = dict(b='blue', c='cyan', g='green', m='magenta', r='red',
5149
w='white', y='yellow', k='black', d='black')
5250

bpython/importcompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def catch_warnings():
4141
finally:
4242
warnings.filters = filters
4343

44-
py3 = sys.version_info[:2] >= (3, 0)
44+
from bpython._py3compat import py3
4545

4646
# The cached list of all known modules
4747
modules = set()

bpython/inspection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
import re
3030
import sys
3131
import types
32+
from itertools import dropwhile
3233

33-
from pygments.lexers import PythonLexer
3434
from pygments.token import Token
3535

36+
from bpython._py3compat import PythonLexer, py3
37+
3638
try:
3739
collections.Callable
3840
has_collections_callable = True
@@ -44,8 +46,6 @@
4446
except AttributeError:
4547
has_instance_type = False
4648

47-
py3 = sys.version_info[0] == 3
48-
4949
if not py3:
5050
_name = re.compile(r'[a-zA-Z_]\w*$')
5151

bpython/repl.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,10 @@
4242
from urlparse import urlparse
4343
from xmlrpclib import ServerProxy, Error as XMLRPCError
4444

45-
py3 = sys.version_info[0] == 3
46-
4745
from pygments.token import Token
48-
if py3:
49-
from pygments.lexers import Python3Lexer as PythonLexer
50-
else:
51-
from pygments.lexers import PythonLexer
5246

5347
from bpython import importcompletion, inspection
48+
from bpython._py3compat import PythonLexer, py3
5449
from bpython.formatter import Parenthesis
5550
from bpython.translations import _
5651
from bpython.autocomplete import Autocomplete

bpython/urwid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from pygments.token import Token
4747

4848
from bpython import args as bpargs, repl, translations
49+
from bpython._py3compat import py3
4950
from bpython.formatter import theme_map
5051
from bpython.importcompletion import find_coroutine
5152
from bpython.translations import _
@@ -54,7 +55,6 @@
5455

5556
import urwid
5657

57-
py3 = sys.version_info[0] == 3
5858
if not py3:
5959
import inspect
6060

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy