Skip to content

Conversation

@jopemachine
Copy link
Contributor

@jopemachine jopemachine commented Sep 21, 2022

Outline

Add co_names to PyCode

CPython document says co_names is a tuple of names other than arguments and function locals

Reference

https://github.com/python/cpython/blob/main/Doc/library/inspect.rst#types-and-members

Result

>>>>> a = 3
>>>>> b = 5
>>>>> c = 7
>>>>> def f(k, l, m):
.....   d = 9
.....   return a + k
..... 
>>>>> f.__code__.co_names
1
('a',)
>>>>> 

@youknowone
Copy link
Member

if there is no related unittest in Lib/tests, could you add the test you used in this issue to somewhere in extra_tests/snippets?

@Snowapril
Copy link
Contributor

Snowapril commented Oct 3, 2022

Oh, It seems we missed Lib/test/code.py from cpython. We'd better use it
https://github.com/python/cpython/blob/main/Lib/test/test_code.py

@youknowone
Copy link
Member

@Snowapril is it related to this changes?

@Snowapril
Copy link
Contributor

I'm sorry. I uploaded a different link 😂 Please take a look again @jopemachine

@jopemachine jopemachine force-pushed the add-co-names branch 3 times, most recently from 79c64e7 to cf0ee0c Compare October 4, 2022 01:26
@jopemachine jopemachine marked this pull request as ready for review October 4, 2022 03:45
@jopemachine
Copy link
Contributor Author

jopemachine commented Oct 4, 2022

I updated the test_code.py, but there are some points that I'd like to mention.

  • Updated opcode.py for updating test_code.py. but I didn't update test_opcode.py because the test code is not changed.
  • There are some dump function calls in the above comment of the test_code.py. Because this raised test failure, I commented it out and added TODO: RUSTPYTHON comment on top of each function call.
  • I checked the added co_names manually in the dump calls, the first dump call's actual result is different from the expected result. The actual result is ('__doc__') while the expected result is empty tuple, the remaining test results are correct.
  • The updated opcode.py contains def_op('YIELD_FROM', 72) line which not exist in the cpython anymore. I included the line because it is used in coroutines.py. This needs to be removed after the coroutines.py file is updated.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! thank you!

@youknowone youknowone merged commit 46ff239 into RustPython:main Oct 4, 2022
@youknowone youknowone added the z-ca-2022 Tag to track contrubution-academy 2022 label Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2022 Tag to track contrubution-academy 2022

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants