Skip to content

Commit

Permalink
Ignore attributes starting with dunder in _TokenType (fixes #672).
Browse files Browse the repository at this point in the history
This issue came up, when trying to deepcopy a parsed statement.
deepcopy uses getattr(obj, '__deepcopy__', None) to get a method
for copying an object. Before this change a new attribute
'__deepcopy__' was created as a new instance of _TokenType (a tuple).
  • Loading branch information
andialbrecht committed Sep 19, 2023
1 parent 8ce446e commit fac38cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Notable Changes
* Drop support for Python 3.5.
* Python 3.12 is now supported (pr725, by hugovk).

Bug Fixes

* Ignore dunder attributes when creating Tokens (issue672).


Release 0.4.4 (Apr 18, 2023)
----------------------------
Expand Down
3 changes: 3 additions & 0 deletions sqlparse/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def __contains__(self, item):
return item is not None and (self is item or item[:len(self)] == self)

def __getattr__(self, name):
# don't mess with dunder
if name.startswith('__'):
return super().__getattr__(self, name)
new = _TokenType(self + (name,))
setattr(self, name, new)
new.parent = self
Expand Down
8 changes: 8 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

import pytest

import sqlparse
Expand Down Expand Up @@ -436,3 +438,9 @@ def test_comment_between_cte_clauses_issue632():
baz AS ()
SELECT * FROM baz;""")
assert p.get_type() == "SELECT"


def test_copy_issue672():
p = sqlparse.parse('select * from foo')[0]
copied = copy.deepcopy(p)
assert str(p) == str(copied)

0 comments on commit fac38cd

Please sign in to comment.