Opened 14 years ago

Closed 13 years ago

#2016 closed Bugs (fixed)

wrong default arguments in boost/python/exec.hpp

Reported by: schlotz <scholz@…> Owned by: Dave Abrahams
Milestone: To Be Determined Component: python USE GITHUB
Version: Boost Development Trunk Severity: Problem
Keywords: python exec exec_file eval default argument Cc:

Description

Calling boost::python::exec, exec_file and eval without the second and third argument, makes the default arguments create 'object's where 'dict's are neccessary.

Consider this program:

#include <boost/python.hpp>
int main()
{
  using namespace boost::python;
  
  Py_Initialize();
  try {
    object foo = exec("a=5; print(a);");
  }
  catch(error_already_set const &) {
    PyErr_Print();
  }
  return 0;
}

Expected output:

5

Real output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: 'NoneType' object does not support item assignment

A possible fix is to change the default arguments to exec, exec_file and eval in exec.hpp from ...=object() to ...=dict(), see the attached patch to boost/python/exec.hpp I'm not sure if it would make sense to change the type of the arguments from object to dict as well.

Bye,

Jan Scholz

Attachments (1)

change_default_object_to_dict.patch (1.2 KB ) - added by schlotz <scholz@…> 14 years ago.
Change exec default arguments from object() to dict()

Download all attachments as: .zip

Change History (2)

by schlotz <scholz@…>, 14 years ago

Change exec default arguments from object() to dict()

comment:1 by Stefan Seefeld, 13 years ago

Resolution: fixed
Status: newclosed

I have just checked in a different fix for this problem. The global dictionary should default to, well, the existing global dictionary. The local defaults to be the same as global.

Note: See TracTickets for help on using tickets.