🌐 AI搜索 & 代理 主页
Skip to content

ParseElement.copy() result refers to previous instance #612

@waqasilyas

Description

@waqasilyas

Hi

I have come across a problem that seems to be caused by ParseElement.copy(). Not sure though if this should be a bug or if there is an easy way to fix it.

Here are the steps to reproduce

import pyparsing as pp
custom_id = pp.pyparsing_common.identifier.copy().set_name("custom identifer")
custom_id.parse_string("1")

This produces exception:

Traceback (most recent call last):
  File "d:\misc\practice\dsl\halo-test\pyparsing\bug.prompt.py", line 7, in <module>
    custom_id.parse_string("1")
  File "D:\programs\miniconda3\envs\python310\lib\site-packages\pyparsing\core.py", line 1216, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected identifier, found '1'  (at char 0), (line:1, col:1)

Note that it should say "Expected custom identifier". However the following code behaves correctly:

import pyparsing as pp
custom_id = pp.Word(pp.identchars, pp.identbodychars).set_name("custom identifer")
custom_id.parse_string("1")

The little debugging I could do seems to show that the parseImpl overwritten in the constructor remains the same after copy(). You can test this by:

print(custom_id.parseImpl.__self__)

This prints identifier.

Due to this if I use set_results_name, it will also produce unexpected results.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions