Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing nullptr checks in atomlist #220

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion atom/src/atomlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,16 +785,26 @@ class AtomCListHandler : public AtomListHandler
{
static char *kwlist[] = { "key", "reverse", 0 };
// Get a reference to builtins (borrowed ref hence the incref)
cppy::ptr builtins( cppy::incref( PyImport_AddModule("builtins") ) );
cppy::ptr builtins( cppy::xincref( PyImport_AddModule("builtins") ) );
Copy link
Member

Choose a reason for hiding this comment

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

why are we doingAddModule here instead of Import? If we need a builtin function from globals, that should be pre-fetched on module initialization.

Copy link
Member

Choose a reason for hiding this comment

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

I do not remember the details but I likely followed an example. I would like to fully move away from global static and properly use module state (and we could store super there) but I have no idea if I will find the time to do so.

Copy link
Member

Choose a reason for hiding this comment

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

But hold on here. We seem to have deviated quite a bit from the original code, by calling a builtin and returning before the observer code. What exactly is going on, and why have we deviated so much from the original code?

Copy link
Member

Choose a reason for hiding this comment

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

Once again I do not have the whole history in mind but I think we were relying on a private function to sort the list that disappeared. However I do not remember why I went with super rather than looking up the method directly on list. It may have just been simpler.

if ( !builtins )
return 0;
cppy::ptr super_type( builtins.getattr( "super" ) );
if ( !super_type )
return 0;
// Create super args (tuple steals references)
cppy::ptr super_args( PyTuple_New(2) );
if ( !super_args )
return 0;
PyTuple_SET_ITEM( super_args.get(), 0, cppy::incref( pyobject_cast( Py_TYPE(m_list.get()) ) ) );
PyTuple_SET_ITEM( super_args.get(), 1, cppy::incref( m_list.get() ) );

// Get and call super method
cppy::ptr super( super_type.call( super_args ) );
if ( !super )
return 0;
cppy::ptr meth( super.getattr( "sort" ) );
if ( !meth )
return 0;
cppy::ptr res( meth.call(args, kwargs) );
if( !res )
return 0;
Expand Down
Loading