Skip to content

Refactor duplicated codes in exception creations#3402

Merged
youknowone merged 6 commits into
RustPython:mainfrom
Snowapril:refactor_exceptions
Nov 15, 2021
Merged

Refactor duplicated codes in exception creations#3402
youknowone merged 6 commits into
RustPython:mainfrom
Snowapril:refactor_exceptions

Conversation

@Snowapril

@Snowapril Snowapril commented Nov 2, 2021

Copy link
Copy Markdown
Contributor

This revision implements below

  • with procedural-macro, auto-wrapping #[pyattr(once)] attached functions with static_cell! codes
  • refactor duplicated codes in several functions with #[pyattr] attached.
  • add new_exception_type method for creating module-level exceptions.

Comment thread stdlib/src/ssl.rs Outdated
@Snowapril Snowapril force-pushed the refactor_exceptions branch 3 times, most recently from 5abd75d to 04de53d Compare November 6, 2021 08:30
@DimitrisJim DimitrisJim changed the title Refactor duplicated codes in _ssl exception creations Refactor duplicated codes in exception creations Nov 6, 2021
@Snowapril Snowapril force-pushed the refactor_exceptions branch from 04de53d to 5c9c937 Compare November 6, 2021 10:12
@Snowapril

Copy link
Copy Markdown
Contributor Author

Uhm... I have no idea why rusttests have failed 😂

@youknowone

Copy link
Copy Markdown
Member

@Snowapril Look upside to find frozen lib errors

 RuntimeError: Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have access to a consistent external environment (e.g. if you're embedding rustpython in another application), try enabling the freeze-stdlib feature
test dictdatatype::tests::test_abc ... FAILED
test dictdatatype::tests::test_insert ... FAILED
RuntimeError: lost sys.stderr
Traceback (most recent call last):
  File "_frozen_importlib_external", line 1439, in find_spec
AttributeError: 'FileFinder' object has no attribute 'path'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "_frozen_importlib", line 1094, in __import__
  File "_frozen_importlib", line 1015, in _gcd_import
  File "_frozen_importlib", line 992, in _find_and_load
  File "_frozen_importlib", line 992, in _find_and_load
  File "_frozen_importlib", line 972, in _find_and_load_unlocked
  File "_frozen_importlib", line 915, in _find_spec
  File "_frozen_importlib", line 915, in _find_spec
  File "_frozen_importlib_external", line 1342, in find_spec
  File "_frozen_importlib_external", line 1314, in _get_spec
  File "_frozen_importlib_external", line 1440, in find_spec
NameError: name 'OSError' is not defined. Did you mean: 'EOFError'?

The above exception was the direct cause of the following exception:

RuntimeError: Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have access to a consistent external environment (e.g. if you're embedding rustpython in another application), try enabling the freeze-stdlib feature
test dictdatatype::tests::test_x ... FAILED
test format::tests::test_all ... ok
test format::tests::test_fill_and_align ... ok
test format::tests::test_fill_and_width ... ok
test format::tests::test_format_int ... ok
test format::tests::test_format_invalid_specification ... ok
test format::tests::test_format_parse ... ok
test format::tests::test_format_parse_escape ... ok
test format::tests::test_format_parse_fail ... ok
test format::tests::test_parse_field_name ... ok
test format::tests::test_width_only ... ok
test function::tests::test_intonativefunc_noalloc ... ok
test pyobjectrc::tests::miri_test_type_initialization ... ok
test stdlib::io::_io::tests::test_buffered_read ... ok
test stdlib::io::_io::tests::test_buffered_seek ... ok
test stdlib::io::_io::tests::test_buffered_value ... ok
RuntimeError: lost sys.stderr
Traceback (most recent call last):
  File "_frozen_importlib_external", line 1439, in find_spec
AttributeError: 'FileFinder' object has no attribute 'path'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "_frozen_importlib", line 1094, in __import__
  File "_frozen_importlib", line 1015, in _gcd_import
  File "_frozen_importlib", line 992, in _find_and_load
  File "_frozen_importlib", line 992, in _find_and_load
  File "_frozen_importlib", line 972, in _find_and_load_unlocked
  File "_frozen_importlib", line 915, in _find_spec
  File "_frozen_importlib", line 915, in _find_spec
  File "_frozen_importlib_external", line 1342, in find_spec
  File "_frozen_importlib_external", line 1314, in _get_spec
  File "_frozen_importlib_external", line 1440, in find_spec
NameError: name 'OSError' is not defined. Did you mean: 'EOFError'?

@Snowapril Snowapril force-pushed the refactor_exceptions branch from 5c9c937 to 2ca6a94 Compare November 6, 2021 11:19
Comment thread derive/src/pymodule.rs Outdated
// collect to context
for item in items.iter_mut() {
let r = item.try_split_attr_mut(|attrs, item| {
// If attribute is #[pyattr] and item is ItemFn, then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is causing issues someplace but I can't track it down. Removing it from here and moving the static_cell to new_exception_type should be a viable alternative though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moving static_cell to new_exception_type may solve that test failures. But we need to declare it as macro as each functions need its own static_cell. Approach with procedural-macro is best I think as it also solve other stuffs (#[pyattr] attached function in module which is not exception-type)

After more trying, I would mention you. sorry 😢

@DimitrisJim DimitrisJim Nov 7, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, right. Adding an error/exception argument to pyattr then? Have it only applied on attributes that are meant to denote errors.

@Snowapril Snowapril Nov 7, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh . I missed your comment. I add "once" keyword to #[pyattr] like #[pyattr(name = "blahblah", once)] for that exceptions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure this is fit for here, but adding a field to ALLOWED_NAMES allows to read it from ItemMeta.
(with a new AttrItemMeta)
If you are interested in, check how ClassItemMeta handles metaclass. If this is not fit for here, still AttrItemMeta is preferred not to allow other simple items to have once.

@Snowapril Snowapril Nov 11, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for late 😂 I moved static_cell wrapping code to get_module_item of AttributeItem.
Use AttrItemMeta::from_attr for ItemFn and replace original once keyword checking code to attr_meta.inner()._bool("once")? . For the other cases, Item::Const and Item::Use, just use SimpleItemMeta::from_attr as they do not have once keyword ever. Please take a look this commit 7f2fd97.
Thanks 😊

@Snowapril Snowapril force-pushed the refactor_exceptions branch 8 times, most recently from 7f2fd97 to d2fe92f Compare November 12, 2021 12:15
When we have to add #[pyattr] with function, that function must be
called only once when module is imported.

In previous, we did it with add static_cell for each function or skip
it(undesired).

I thought these static_cell stmt for all of each function in modules are
quite duplicated.

Thus, I add wrapping code when the given item is `ItemFn` and have `#[pyattr]`.

Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
At previous, `vm.ctx.new_class` did role for adding exception in
module-level.

This commit add `new_exception_type` for doing that part of role.
As cpython implementation, if no bases is given, pass `exception_type`
by default

Signed-off-by: snowapril <sinjihng@gmail.com>
Replace `new_class` method call for module-level exceptions to newly
added `new_exception_type`

Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
@youknowone youknowone merged commit e7fa32c into RustPython:main Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants