Skip to content

Commit

Permalink
Merge pull request #69 from Schnouki/fix-ci
Browse files Browse the repository at this point in the history
fix: Fix CI and compatibility with Python < 3.8
  • Loading branch information
skorokithakis authored Nov 23, 2023
2 parents cd9d392 + 61b1c99 commit 38b7eb0
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 38 deletions.
49 changes: 36 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,43 @@ on: [push]

jobs:
tests:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
fail-fast: true
matrix:
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, pypy3]
python-version:
- "2.7"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
- pypy3.7
- pypy3.8
- pypy3.9

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel pycodestyle
- name: Run tests
run: make pycodestyle test
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
if: matrix.python-version != '2.7'
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Set up Python 2.7
if: matrix.python-version == '2.7'
run: |
sudo apt-get update
sudo apt-get install -y python2.7 python2.7-dev
sudo ln -sf python2.7 /usr/bin/python
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py
rm get-pip.py
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel pycodestyle
- name: Run tests
run: make pycodestyle test
77 changes: 56 additions & 21 deletions q.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,29 @@ class Q(object):

@property
def short(self):
__class__.TEXT_REPR = __class__.pydoc.TextRepr()
cls = self.__class__
cls.TEXT_REPR = cls.pydoc.TextRepr()

@property
def long(self):
__class__.TEXT_REPR = __class__.pydoc.TextRepr()
__class__.TEXT_REPR.maxarray = __class__.q_max_length
__class__.TEXT_REPR.maxdeque = __class__.q_max_length
__class__.TEXT_REPR.maxdict = __class__.q_max_length
__class__.TEXT_REPR.maxfrozenset = __class__.q_max_length
__class__.TEXT_REPR.maxlevel = __class__.q_max_length
__class__.TEXT_REPR.maxlist = __class__.q_max_length
__class__.TEXT_REPR.maxlong = __class__.q_max_length
__class__.TEXT_REPR.maxother = __class__.q_max_length
__class__.TEXT_REPR.maxset = __class__.q_max_length
__class__.TEXT_REPR.maxstring = __class__.q_max_length
__class__.TEXT_REPR.maxtuple = __class__.q_max_length
cls = self.__class__
cls.TEXT_REPR = cls.pydoc.TextRepr()
cls.TEXT_REPR.maxarray = cls.q_max_length
cls.TEXT_REPR.maxdeque = cls.q_max_length
cls.TEXT_REPR.maxdict = cls.q_max_length
cls.TEXT_REPR.maxfrozenset = cls.q_max_length
cls.TEXT_REPR.maxlevel = cls.q_max_length
cls.TEXT_REPR.maxlist = cls.q_max_length
cls.TEXT_REPR.maxlong = cls.q_max_length
cls.TEXT_REPR.maxother = cls.q_max_length
cls.TEXT_REPR.maxset = cls.q_max_length
cls.TEXT_REPR.maxstring = cls.q_max_length
cls.TEXT_REPR.maxtuple = cls.q_max_length

@long.setter
def long(self, value):
__class__.q_max_length = value
cls = self.__class__
cls.q_max_length = value
self.long

# For portably converting strings between python2 and python3
Expand Down Expand Up @@ -260,6 +263,44 @@ def get_call_exprs(self, caller_frame, line):
except SyntaxError:
return None

if self.sys.version_info >= (3, 8):
return self._get_accurate_call_exprs(caller_frame, line, tree)

return self._get_basic_call_exprs(caller_frame, line, tree)

def _get_basic_call_exprs(self, caller_frame, line, tree):
"""Gets the argument expressions from the source of a function call.
Slightly buggy (multiple calls to q() on a single line cause garbled
output, #67), but works on all Python versions.
"""
for node in self.ast.walk(tree):
if isinstance(node, self.ast.Call):
offsets = []
for arg in node.args:
# In Python 3.4 the col_offset is calculated wrong. See
# https://bugs.python.org/issue21295
if isinstance(arg, self.ast.Attribute) and (
(3, 4, 0) <= self.sys.version_info <= (3, 4, 3)):
offsets.append(arg.col_offset - len(arg.value.id) - 1)
else:
offsets.append(arg.col_offset)
if node.keywords:
line = line[:node.keywords[0].value.col_offset]
line = self.re.sub(r'\w+\s*=\s*$', '', line)
else:
line = self.re.sub(r'\s*\)\s*$', '', line)
offsets.append(len(line))
args = []
for i in range(len(node.args)):
args.append(line[offsets[i]:offsets[i + 1]].rstrip(', '))
return args

def _get_accurate_call_exprs(self, caller_frame, line, tree):
"""Gets the argument expressions from the source of a function call.
Accurate, but depends on Python 3.8+.
"""
# There can be multiple function calls on a line
# (for example: q(1) + q(2)), so in order to show
# correct output, we need to identify what function call we
Expand Down Expand Up @@ -300,13 +341,7 @@ def get_call_exprs(self, caller_frame, line):

offsets = []
for arg in node.args:
# In Python 3.4 the col_offset is calculated wrong. See
# https://bugs.python.org/issue21295
if isinstance(arg, self.ast.Attribute) and (
(3, 4, 0) <= self.sys.version_info <= (3, 4, 3)):
offsets.append(arg.col_offset - len(arg.value.id) - 1)
else:
offsets.append(arg.col_offset)
offsets.append(arg.col_offset)
if node.keywords:
line = line[:node.keywords[0].value.col_offset]
line = self.re.sub(r'\w+\s*=\s*$', '', line)
Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: Implementation :: Jython",
"Intended Audience :: Developers",
Expand Down
9 changes: 5 additions & 4 deletions test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def __init__(s, two, three, four):
"s.attrib2='Attrib2'",
]))

@unittest.skipIf(sys.version_info < (3, 8), "requires Python 3.8+")
def test_q_multiple_calls_on_line(self):
import q
q.writer.color = False
Expand Down Expand Up @@ -192,8 +193,8 @@ def decorated_log_bad(msg='default'):
return msg

decorated_log_bad('decorated bad message')
self.assertInQLog("do_nothing\\((?:\n\s*)?'"
"decorated bad message'\\)")
self.assertInQLog(r"do_nothing\((?:\n\s*)?'"
r"decorated bad message'\)")
self.assertInQLog("-> 'decorated bad message'")

def test_q_nested_good_wrappers(self):
Expand All @@ -214,8 +215,8 @@ def decorated_log_good(msg='default'):
return msg

decorated_log_good('decorated good message')
self.assertInQLog("decorated_log_good\\((?:\n\s*)?'"
"decorated good message'\\)")
self.assertInQLog(r"decorated_log_good\((?:\n\s*)?'"
r"decorated good message'\)")
self.assertInQLog("-> 'decorated good message'")

@unittest.skipIf(sys.version_info < (3, 3), "requires Python 3.3+")
Expand Down

0 comments on commit 38b7eb0

Please sign in to comment.