This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ElementTree.parse gives error message about missing1 required positional argument: 'source' but it is there
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: f.de.kruijf, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-09-11 09:22 by f.de.kruijf, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg376706 - (view) Author: Freek de Kruijf (f.de.kruijf) * Date: 2020-09-11 09:22
In a function definition I have the following piece of code:

    try:
        with open(requests,'rt') as f:
            tree = ElementTree.parse(f)

On execution I got:

Traceback (most recent call last):
   File "/srv/www/bin/web.py", line 362, in <module>
     build_db()
   File "/srv/www/bin/web.py", line 58, in build_db
     db_builder.build_DB()
   File "/srv/www/bin/db_builder.py", line 190, in build_DB
     tree = ElementTree.parse(f)
 TypeError: parse() missing 1 required positional argument: 'source'

There is an assignment for requests like
requests = '..' + os.path.sep + "/etc/signatures.xml"
This gives requests the value ..//etc/signatures.xml while the program runs in /srv/www/bin/

When I run the small python script like:

import xml.etree.ElementTree as ElementTree
with open('/srv/www/etc/signatures.xml','rt') as f:
    tree = ElementTree.parse(f)

all is well.
Apparently open(requests,'rt') does not raise an error.
msg376709 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-09-11 09:31
parse() is an instance method. It should be called as tree.parse(f) (where tree is an instance of ElementTree), not ElementTree.parse(f).
msg376727 - (view) Author: Freek de Kruijf (f.de.kruijf) * Date: 2020-09-11 12:13
Op vrijdag 11 september 2020 11:31:29 CEST schreef u:
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
> 
> parse() is an instance method. It should be called as tree.parse(f) (where
> tree is an instance of ElementTree), not ElementTree.parse(f).
> 
> ----------
I have no idea how to make tree an instance.
Reading the documentation,
https://docs.python.org/2/library/xml.etree.elementtree.html,
it shows as an example:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')

which is exactly what the program looks like and what my small python script 
does without an error.

-- 
fr.gr.

Freek de Kruijf
msg376732 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-09-11 15:06
It was not clear what you do without code and full traceback.

The example uses *function* parse() from *module* ElementTree. Your code seems uses *method* parse() of *class* ElementTree.

https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.parse
https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.parse
msg376738 - (view) Author: Freek de Kruijf (f.de.kruijf) * Date: 2020-09-11 21:09
Op vrijdag 11 september 2020 17:06:43 CEST schreef u:
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
> 
> It was not clear what you do without code and full traceback.
> 
> The example uses *function* parse() from *module* ElementTree. Your code
> seems uses *method* parse() of *class* ElementTree.
> 
> https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.Eleme
> ntTree.parse
> https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.Elem
> entTree.ElementTree.parse

I have very little knowledge about class, instance and the other specific 
terms used in Python.

I just try to use a Python script, I found and need, and ran into this 
problem. What it does looks simple to me. As simple as the 3 line script I 
showed, which works.
Why does the larger script give this confusing error about a missing argument, 
which is present? It is a file name or file object as it should.
What I understand is that tree becomes an instance. Of what?

Is the problem caused by using ElementTree in "import xml.etree.ElementTree as 
ElementTree". Should I use "import xml.etree.ElementTree as ET" and "tree = 
ET.parse(f)"?

-- 
fr.gr.

Freek de Kruijf
History
Date User Action Args
2022-04-11 14:59:35adminsetgithub: 85925
2020-09-11 21:09:28f.de.kruijfsetmessages: + msg376738
2020-09-11 15:06:43serhiy.storchakasetmessages: + msg376732
2020-09-11 12:13:42f.de.kruijfsetmessages: + msg376727
2020-09-11 09:31:29serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg376709

resolution: not a bug
stage: resolved
2020-09-11 09:22:51f.de.kruijfcreate