From 5aac0150f3e60edb4463cb0450266e7bbb30ccf3 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 26 Nov 2024 11:37:39 -0600 Subject: [PATCH 1/3] Add test for non-dropping of pointer with attribute Closes gh-86 --- test/test_pycparserext.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_pycparserext.py b/test/test_pycparserext.py index 9c0c19c..627d766 100644 --- a/test/test_pycparserext.py +++ b/test/test_pycparserext.py @@ -126,6 +126,12 @@ def test_asm_label(): assert _round_trip_matches(src) +def test_pointer_with_attr(): + # https://github.com/inducer/pycparserext/issues/86 + src = "typedef float * __attribute__((abc)) b;" + assert _round_trip_matches(src) + + def test_funky_header_code(): src = """ extern __inline int __attribute__ ((__nothrow__)) __signbitf (float __x) From fe7e41eb62adc870fdd6663c79d8259d8d6eb563 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 26 Nov 2024 11:41:15 -0600 Subject: [PATCH 2/3] Switch to building with hatchling --- MANIFEST.in | 2 -- pyproject.toml | 54 +++++++++++++++++++++++++++++++++++++++++++------- setup.py | 32 ------------------------------ 3 files changed, 47 insertions(+), 41 deletions(-) delete mode 100644 MANIFEST.in delete mode 100644 setup.py diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index a5021c6..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include README.rst -include LICENSE diff --git a/pyproject.toml b/pyproject.toml index bb15751..d98a05d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,18 +1,55 @@ -[tool.ruff] -target-version = "py38" -line-length = 85 +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "pycparserext" +version = "2024.1" +description = "Extensions for pycparser" +readme = "README.rst" +license = "MIT" +requires-python = "~=3.8" +authors = [ + { name = "Andreas Kloeckner", email = "inform@tiker.net" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "Intended Audience :: Other Audience", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: MIT License", + "Natural Language :: English", + "Programming Language :: Python", + "Topic :: Utilities", +] +dependencies = [ + "ply>=3.4", + "pycparser~=2.21", +] + +[project.urls] +Homepage = "https://github.com/inducer/pycparserext" +[tool.hatch.build.targets.sdist] +include = [ + "/pycparserext", +] +[tool.ruff] preview = true + [tool.ruff.lint] extend-select = [ "B", # flake8-bugbear "C", # flake8-comprehensions "E", # pycodestyle "F", # pyflakes + "G", # flake8-logging-format "I", # flake8-isort "N", # pep8-naming "NPY", # numpy "Q", # flake8-quotes + "UP", # pyupgrade + "RUF", # ruff "W", # pycodestyle ] extend-ignore = [ @@ -20,15 +57,19 @@ extend-ignore = [ "E221", # multiple spaces before operator "E226", # missing whitespace around arithmetic operator "E402", # module-level import not at top of file + "UP006", # updated annotations due to __future__ import + "UP007", # updated annotations due to __future__ import + "UP031", # use f-strings instead of % + "UP032", # use f-strings instead of .format ] [tool.ruff.lint.flake8-quotes] docstring-quotes = "double" inline-quotes = "double" multiline-quotes = "double" -[tool.ruff.lint.per-file-ignores] -"pycparserext/ext_c_generator.py" = ["N802"] -"test/test_pycparserext.py" = ["N802"] +[tool.ruff.lint.pep8-naming] +extend-ignore-names = ["visit_*", "t_*"] + [tool.ruff.lint.isort] combine-as-imports = true @@ -40,4 +81,3 @@ known-local-folder = [ "pycparserext", ] lines-after-imports = 2 - diff --git a/setup.py b/setup.py deleted file mode 100644 index f498f23..0000000 --- a/setup.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python -# -*- coding: latin1 -*- - -from setuptools import setup - - -setup(name="pycparserext", - version="2021.1", - description="Extensions for pycparser", - long_description=open("README.rst", "r").read(), - classifiers=[ - "Development Status :: 4 - Beta", - "Intended Audience :: Developers", - "Intended Audience :: Other Audience", - "Intended Audience :: Science/Research", - "License :: OSI Approved :: MIT License", - "Natural Language :: English", - "Programming Language :: Python", - "Topic :: Utilities", - ], - - python_requires="~=3.8", - install_requires=[ - "ply>=3.4", - "pycparser~=2.21", - ], - - author="Andreas Kloeckner", - url="http://pypi.python.org/pypi/pycparserext", - author_email="inform@tiker.net", - license="MIT", - packages=["pycparserext"]) From 1ed78d649403317ae0e79369749f6ee149e2d7c9 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 26 Nov 2024 11:55:44 -0600 Subject: [PATCH 3/3] Additional ruff fixes --- pycparserext/ext_c_generator.py | 4 ++-- pycparserext/ext_c_lexer.py | 8 ++++---- pycparserext/ext_c_parser.py | 12 +++++------- test/test_pycparserext.py | 1 - 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/pycparserext/ext_c_generator.py b/pycparserext/ext_c_generator.py index a2bc88d..dc958e3 100644 --- a/pycparserext/ext_c_generator.py +++ b/pycparserext/ext_c_generator.py @@ -21,7 +21,7 @@ def visit_UnaryOp(self, n): return "%s %s" % (n.op, operand) -class AsmAndAttributesMixin(object): +class AsmAndAttributesMixin: def visit_Asm(self, n): components = [ n.template, @@ -120,7 +120,7 @@ def _generate_type(self, n, modifiers=None, emit_declname=True): elif typ in (c_ast.ArrayDecl, c_ast.PtrDecl, c_ast.FuncDecl, FuncDeclExt): return self._generate_type( - n.type, modifiers + [n], emit_declname=emit_declname) + n.type, [*modifiers, n], emit_declname=emit_declname) else: return self.visit(n) diff --git a/pycparserext/ext_c_lexer.py b/pycparserext/ext_c_lexer.py index 638d92a..e0c4edb 100644 --- a/pycparserext/ext_c_lexer.py +++ b/pycparserext/ext_c_lexer.py @@ -16,7 +16,7 @@ class GnuCLexer(CLexerBase): + CLexerBase.exponent_part+"))i?[FfLl]?)") @TOKEN(floating_constant) - def t_FLOAT_CONST(self, t): # noqa: N802 + def t_FLOAT_CONST(self, t): return t t_pppragma_ignore = " \t<>.-{}();+-*/$%@&^~!?:,0123456789=" @@ -32,7 +32,7 @@ def __init__(self, *args, **kwargs): class OpenCLCLexer(CLexerBase): - tokens = CLexerBase.tokens + ("LINECOMMENT",) + tokens = (*CLexerBase.tokens, "LINECOMMENT") states = ( # ('comment', 'exclusive'), # ('preproc', 'exclusive'), @@ -40,12 +40,12 @@ class OpenCLCLexer(CLexerBase): ("pppragma", "exclusive"), # unused ) - def t_LINECOMMENT(self, t): # noqa: N802 + def t_LINECOMMENT(self, t): r"\/\/([^\n]+)\n" t.lexer.lineno += t.value.count("\n") # overrides pycparser, must have same name - def t_PPHASH(self, t): # noqa: N802 + def t_PPHASH(self, t): r"[ \t]*\#([^\n]|\\\n)+[^\n\\]\n" t.lexer.lineno += t.value.count("\n") return t diff --git a/pycparserext/ext_c_parser.py b/pycparserext/ext_c_parser.py index 26de7bc..71e7d47 100644 --- a/pycparserext/ext_c_parser.py +++ b/pycparserext/ext_c_parser.py @@ -1,4 +1,3 @@ -from __future__ import division import pycparser.c_ast as c_ast import pycparser.c_parser @@ -51,8 +50,7 @@ def children(self): return tuple(nodelist) def __iter__(self): - for child in (self.types or []): - yield child + yield from (self.types or []) attr_names = () @@ -260,7 +258,7 @@ def __iter__(self): # {{{ attributes -class _AttributesMixin(object): +class _AttributesMixin: def p_attributes_opt_1(self, p): """ attributes_opt : attribute_decl attributes_opt """ @@ -309,7 +307,7 @@ def p_function_specifier_attr(self, p): # {{{ asm -class _AsmMixin(object): +class _AsmMixin: def p_asm_opt_1(self, p): """ asm_opt : empty """ @@ -494,7 +492,7 @@ class GnuCParser(_AsmAndAttributesMixin, CParserBase): from pycparserext.ext_c_lexer import GnuCLexer as lexer_class # noqa - initial_type_symbols = {"__builtin_va_list"} + initial_type_symbols = frozenset({"__builtin_va_list"}) def p_function_specifier_gnu(self, p): """ function_specifier : __INLINE @@ -600,7 +598,7 @@ def p_unified_volatile_gnu(self, p): class OpenCLCParser(_AsmAndAttributesMixin, CParserBase): from pycparserext.ext_c_lexer import OpenCLCLexer as lexer_class # noqa - INT_BIT_COUNTS = [8, 16, 32, 64] + INT_BIT_COUNTS = (8, 16, 32, 64) initial_type_symbols = ( { "%s%d" % (base_name, count) diff --git a/test/test_pycparserext.py b/test/test_pycparserext.py index 627d766..837543a 100644 --- a/test/test_pycparserext.py +++ b/test/test_pycparserext.py @@ -1,4 +1,3 @@ -from __future__ import print_function import pytest