gurudesh/pytracebugs huggingface dataset:
# Buggy code
def __prepare_plot(self):
    try:
        import guiqwt.pyplot  # analysis:ignore

        return True
    except ImportError:
        try:
            if "matplotlib" not in sys.modules:
                import matplotlib

                matplotlib.use("Qt4Agg")
            return True
        except ImportError:
            QMessageBox.warning(
                self,
                _("Import error"),
                _("Please install <b>matplotlib</b> or <b>guiqwt</b>."),
            )


# Traceback (AssertionError)
Traceback (most recent call last):
File "/home/joseph/prog/spyder.git/spyderlib/spyder.py", line 2865, in main
mainwindow = run_spyder(app, options, args)
File "/home/joseph/prog/spyder.git/spyderlib/spyder.py", line 2755, in run_spyder
main.setup()
File "/home/joseph/prog/spyder.git/spyderlib/spyder.py", line 736, in setup
if is_module_installed('guidata'):
File "/home/joseph/prog/spyder.git/spyderlib/utils/programs.py", line 292, in is_module_installed
actver = get_module_version(module_name)
File "/home/joseph/prog/spyder.git/spyderlib/utils/programs.py", line 238, in get_module_version
mod = __import__(module_name)
File "/usr/lib/python2.7/site-packages/guidata/__init__.py", line 539, in <module>
import guidata.config
File "/usr/lib/python2.7/site-packages/guidata/config.py", line 15, in <module>
from guidata.configtools import add_image_module_path, get_translation
File "/usr/lib/python2.7/site-packages/guidata/configtools.py", line 22, in <module>
from guidata.qt.QtGui import (QFont, QLabel, QPixmap, QIcon, QHBoxLayout,
File "/usr/lib/python2.7/site-packages/guidata/qt/__init__.py", line 13, in <module>
assert _modname in ('pyqt', 'pyside')
AssertionError

# Fixed code
def __prepare_plot(self):
    try:
        import guiqwt.pyplot  # analysis:ignore

        return True
    except (ImportError, AssertionError):
        try:
            if "matplotlib" not in sys.modules:
                import matplotlib

                matplotlib.use("Qt4Agg")
            return True
        except ImportError:
            QMessageBox.warning(
                self,
                _("Import error"),
                _("Please install <b>matplotlib</b> or <b>guiqwt</b>."),
            )
# Buggy code
def load_data(self, table_name, path, database=None, overwrite=False, partition=None):
    """
    Wraps the LOAD DATA DDL statement. Loads data into an Impala table by
    physically moving data files.

    Parameters
    ----------
    table_name : string
    database : string, default None (optional)
    """
    table = self.table(table_name, database=database)
    return table.load_data(path, overwrite=overwrite, partition=partition)


# Traceback (AttributeError)
Traceback (most recent call last):
File "winop.py", line 68, in <module>
create_psql_table(psql, df)
File "winop.py", line 44, in create_psql_table
psql.create_table('salary', schema=sch, database='ibis_testing')
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1022, in wrapped
result = f(self, *args, **kwargs)
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1058, in create_table
if database is not None and database != self.engine.url.database:
AttributeError: 'PostgreSQLClient' object has no attribute 'engine'

# Fixed code
def load_data(
    self,
    table_name,
    path,
    database=None,
    overwrite=False,
    partition=None,
):
    """
    Wraps the LOAD DATA DDL statement. Loads data into an Impala table by
    physically moving data files.

    Parameters
    ----------
    table_name : string
    database : string, default None (optional)
    """
    table = self.table(table_name, database=database)
    return table.load_data(path, overwrite=overwrite, partition=partition)
# Buggy code
def load_data(self, table_name, obj, database=None, **kwargs):
    """Load data into a given table.

    Wraps the LOAD DATA DDL statement. Loads data into an OmniSciDB table
    by physically moving data files.

    Parameters
    ----------
    table_name : string
    obj: pandas.DataFrame or pyarrow.Table
    database : string, optional
    """
    _database = self.db_name
    self.set_database(database)
    self.con.load_table(table_name, obj, **kwargs)
    self.set_database(_database)


# Traceback (AttributeError)
Traceback (most recent call last):
File "winop.py", line 68, in <module>
create_psql_table(psql, df)
File "winop.py", line 44, in create_psql_table
psql.create_table('salary', schema=sch, database='ibis_testing')
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1022, in wrapped
result = f(self, *args, **kwargs)
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1058, in create_table
if database is not None and database != self.engine.url.database:
AttributeError: 'PostgreSQLClient' object has no attribute 'engine'

# Fixed code
def load_data(self, table_name, obj, database=None, **kwargs):
    """Load data into a given table.

    Wraps the LOAD DATA DDL statement. Loads data into an OmniSciDB table
    by physically moving data files.

    Parameters
    ----------
    table_name : string
    obj: pandas.DataFrame or pyarrow.Table
    database : string, optional
    """
    _database = self.db_name
    self.set_database(database)
    self.con.load_table(table_name, obj)
    self.set_database(_database)
# Buggy code
def create_table(self, name, expr=None, schema=None, database=None):
    if database is not None and database != self.engine.url.database:
        raise NotImplementedError(
            "Creating tables from a different database is not yet implemented"
        )

    if expr is None and schema is None:
        raise ValueError("You must pass either an expression or a schema")

    if expr is not None and schema is not None:
        if not expr.schema().equals(ibis.schema(schema)):
            raise TypeError(
                "Expression schema is not equal to passed schema. "
                "Try passing the expression without the schema"
            )
    if schema is None:
        schema = expr.schema()

    self._schemas[self._fully_qualified_name(name, database)] = schema
    t = self._table_from_schema(
        name, schema, database=database or self.current_database
    )

    with self.begin() as bind:
        t.create(bind=bind)
        if expr is not None:
            bind.execute(t.insert().from_select(list(expr.columns), expr.compile()))


# Traceback (AttributeError)
Traceback (most recent call last):
File "winop.py", line 68, in <module>
create_psql_table(psql, df)
File "winop.py", line 44, in create_psql_table
psql.create_table('salary', schema=sch, database='ibis_testing')
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1022, in wrapped
result = f(self, *args, **kwargs)
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1058, in create_table
if database is not None and database != self.engine.url.database:
AttributeError: 'PostgreSQLClient' object has no attribute 'engine'

# Fixed code
def create_table(self, name, expr=None, schema=None, database=None):
    if database == self.database_name:
        # avoid fully qualified name
        database = None

    if database is not None:
        raise NotImplementedError(
            "Creating tables from a different database is not yet implemented"
        )

    if expr is None and schema is None:
        raise ValueError("You must pass either an expression or a schema")

    if expr is not None and schema is not None:
        if not expr.schema().equals(ibis.schema(schema)):
            raise TypeError(
                "Expression schema is not equal to passed schema. "
                "Try passing the expression without the schema"
            )
    if schema is None:
        schema = expr.schema()

    self._schemas[self._fully_qualified_name(name, database)] = schema
    t = self._table_from_schema(
        name, schema, database=database or self.current_database
    )

    with self.begin() as bind:
        t.create(bind=bind)
        if expr is not None:
            bind.execute(t.insert().from_select(list(expr.columns), expr.compile()))
# Buggy code
def drop_table(
    self,
    table_name: str,
    database: Optional[str] = None,
    force: bool = False,
) -> None:
    if database is not None and database != self.con.url.database:
        raise NotImplementedError(
            "Dropping tables from a different database is not yet implemented"
        )

    t = self._get_sqla_table(table_name, schema=database, autoload=False)
    t.drop(checkfirst=force)

    assert not t.exists(), "Something went wrong during DROP of table {!r}".format(
        t.name
    )

    self.meta.remove(t)

    qualified_name = self._fully_qualified_name(table_name, database)

    try:
        del self._schemas[qualified_name]
    except KeyError:  # schemas won't be cached if created with raw_sql
        pass


# Traceback (AttributeError)
Traceback (most recent call last):
File "winop.py", line 68, in <module>
create_psql_table(psql, df)
File "winop.py", line 44, in create_psql_table
psql.create_table('salary', schema=sch, database='ibis_testing')
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1022, in wrapped
result = f(self, *args, **kwargs)
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1058, in create_table
if database is not None and database != self.engine.url.database:
AttributeError: 'PostgreSQLClient' object has no attribute 'engine'

# Fixed code
def drop_table(
    self,
    table_name: str,
    database: Optional[str] = None,
    force: bool = False,
) -> None:
    if database == self.database_name:
        # avoid fully qualified name
        database = None

    if database is not None:
        raise NotImplementedError(
            "Dropping tables from a different database is not yet implemented"
        )

    t = self._get_sqla_table(table_name, schema=database, autoload=False)
    t.drop(checkfirst=force)

    assert not t.exists(), "Something went wrong during DROP of table {!r}".format(
        t.name
    )

    self.meta.remove(t)

    qualified_name = self._fully_qualified_name(table_name, database)

    try:
        del self._schemas[qualified_name]
    except KeyError:  # schemas won't be cached if created with raw_sql
        pass
# Buggy code
def attach(self, name, path, create: bool = False) -> None:
    """Connect another SQLite database file

    Parameters
    ----------
    name : string
        Database name within SQLite
    path : string
        Path to sqlite3 file
    create : boolean, optional
        If file does not exist, create file if True otherwise raise an
        Exception

    """
    if not os.path.exists(path) and not create:
        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

    quoted_name = self.con.dialect.identifier_preparer.quote(name)
    self.raw_sql(
        "ATTACH DATABASE {path!r} AS {name}".format(path=path, name=quoted_name)
    )


# Traceback (AttributeError)
Traceback (most recent call last):
File "winop.py", line 68, in <module>
create_psql_table(psql, df)
File "winop.py", line 44, in create_psql_table
psql.create_table('salary', schema=sch, database='ibis_testing')
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1022, in wrapped
result = f(self, *args, **kwargs)
File "/mnt/sda1/dev/quansight/ibis-project/ibis/ibis/sql/alchemy.py", line 1058, in create_table
if database is not None and database != self.engine.url.database:
AttributeError: 'PostgreSQLClient' object has no attribute 'engine'

# Fixed code
def attach(self, name, path, create: bool = False) -> None:
    """Connect another SQLite database file

    Parameters
    ----------
    name : string
        Database name within SQLite
    path : string
        Path to sqlite3 file
    create : boolean, optional
        If file does not exist, create file if True otherwise raise an
        Exception

    """
    if not os.path.exists(path) and not create:
        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

    quoted_name = self.con.dialect.identifier_preparer.quote(name)
    self.raw_sql(
        "ATTACH DATABASE {path!r} AS {name}".format(path=path, name=quoted_name)
    )
    self.has_attachment = True
# Buggy code
def _add_groupby(self, fragment):
    # GROUP BY and HAVING
    if not len(self.group_by):
        return fragment

    group_keys = [self._translate(arg) for arg in self.group_by]
    fragment = fragment.group_by(*group_keys)

    if len(self.having) > 0:
        having_args = [self._translate(arg) for arg in self.having]
        having_clause = _and_all(having_args)
        fragment = fragment.having(having_clause)

    return fragment


# Traceback (ProgrammingError)
-----------------------------------------------------------------------
ProgrammingError                      Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1181                         parameters,
-> 1182                         context)
1183         except BaseException as e:

~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context)
469     def do_execute(self, cursor, statement, parameters, context=None):
--> 470         cursor.execute(statement, parameters)
471

ProgrammingError: Frame clause should not be specified for ranking window functions

# Fixed code
def _add_groupby(self, fragment):
    # GROUP BY and HAVING
    if not len(self.group_by):
        return fragment

    group_keys = [self._translate(arg) for arg in self.group_by]
    fragment = fragment.group_by(*group_keys)

    if len(self.having) > 0:
        having_args = [self._translate(arg) for arg in self.having]
        having_clause = functools.reduce(sql.and_, having_args)
        fragment = fragment.having(having_clause)

    return fragment
# Buggy code
def _add_where(self, fragment):
    if not len(self.where):
        return fragment

    args = [self._translate(pred, permit_subquery=True) for pred in self.where]
    clause = _and_all(args)
    return fragment.where(clause)


# Traceback (ProgrammingError)
-----------------------------------------------------------------------
ProgrammingError                      Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1181                         parameters,
-> 1182                         context)
1183         except BaseException as e:

~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context)
469     def do_execute(self, cursor, statement, parameters, context=None):
--> 470         cursor.execute(statement, parameters)
471

ProgrammingError: Frame clause should not be specified for ranking window functions

# Fixed code
def _add_where(self, fragment):
    if not len(self.where):
        return fragment

    args = [self._translate(pred, permit_subquery=True) for pred in self.where]
    clause = functools.reduce(sql.and_, args)
    return fragment.where(clause)
# Buggy code
def _true_divide(t, expr):
    op = expr.op()
    left, right = op.args

    if util.all_of(op.args, ir.IntegerValue):
        new_expr = left.div(right.cast("double"))
        return t.translate(new_expr)

    return fixed_arity(lambda x, y: x / y, 2)(t, expr)


# Traceback (ProgrammingError)
-----------------------------------------------------------------------
ProgrammingError                      Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1181                         parameters,
-> 1182                         context)
1183         except BaseException as e:

~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context)
469     def do_execute(self, cursor, statement, parameters, context=None):
--> 470         cursor.execute(statement, parameters)
471

ProgrammingError: Frame clause should not be specified for ranking window functions

# Fixed code
def _true_divide(t, expr):
    op = expr.op()
    left, right = args = op.args

    if util.all_of(args, ir.IntegerValue):
        return t.translate(left.div(right.cast("double")))

    return fixed_arity(lambda x, y: x / y, 2)(t, expr)
# Buggy code
def __init__(
    self,
    element,
    order_by=None,
    partition_by=None,
    preceding=None,
    following=None,
):
    super().__init__(element, order_by=order_by, partition_by=partition_by)
    if not isinstance(preceding, _valid_frame_types):
        raise TypeError(
            "preceding must be a string, integer or None, got %r"
            % (type(preceding).__name__)
        )
    if not isinstance(following, _valid_frame_types):
        raise TypeError(
            "following must be a string, integer or None, got %r"
            % (type(following).__name__)
        )
    self.preceding = preceding if preceding is not None else "UNBOUNDED"
    self.following = following if following is not None else "UNBOUNDED"


# Traceback (ProgrammingError)
-----------------------------------------------------------------------
ProgrammingError                      Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1181                         parameters,
-> 1182                         context)
1183         except BaseException as e:

~/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/default.py in do_execute(self, cursor, statement, parameters, context)
469     def do_execute(self, cursor, statement, parameters, context=None):
--> 470         cursor.execute(statement, parameters)
471

ProgrammingError: Frame clause should not be specified for ranking window functions

# Fixed code
def __init__(self, proxy):
    self.proxy = proxy
# Buggy code
def _truncate(kind, units):
    def truncator(translator, expr):
        op = expr.op()
        arg, unit = op.args

        arg = translator.translate(op.args[0])
        try:
            unit = units[unit]
        except KeyError:
            raise com.UnsupportedOperationError(
                "{!r} unit is not supported in timestamp truncate".format(unit)
            )

        return "{}_TRUNC({}, {})".format(kind, arg, unit)

    return truncator


# Traceback (KeyError)
In [11]: ibis.bigquery.compile(ibis.table([('a', 'DATE')]).a.truncate('D'))
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
...
KeyError: 'D'

During handling of the above exception, another exception occurred:

UnsupportedOperationError                 Traceback (most recent call last)
...
UnsupportedOperationError: 'D' unit is not supported in timestamp truncate

# Fixed code
def _truncate(kind, units):
    def truncator(translator, expr):
        arg, unit = expr.op().args
        trans_arg = translator.translate(arg)
        valid_unit = units.get(unit)
        if valid_unit is None:
            raise com.UnsupportedOperationError(
                "BigQuery does not support truncating {} values to unit {!r}".format(
                    arg.type(), unit
                )
            )
        return "{}_TRUNC({}, {})".format(kind, trans_arg, valid_unit)

    return truncator
# Buggy code
def truncator(translator, expr):
    op = expr.op()
    arg, unit = op.args

    arg = translator.translate(op.args[0])
    try:
        unit = units[unit]
    except KeyError:
        raise com.UnsupportedOperationError(
            "{!r} unit is not supported in timestamp truncate".format(unit)
        )

    return "{}_TRUNC({}, {})".format(kind, arg, unit)


# Traceback (KeyError)
In [11]: ibis.bigquery.compile(ibis.table([('a', 'DATE')]).a.truncate('D'))
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
...
KeyError: 'D'

During handling of the above exception, another exception occurred:

UnsupportedOperationError                 Traceback (most recent call last)
...
UnsupportedOperationError: 'D' unit is not supported in timestamp truncate

# Fixed code
def truncator(translator, expr):
    arg, unit = expr.op().args
    trans_arg = translator.translate(arg)
    valid_unit = units.get(unit)
    if valid_unit is None:
        raise com.UnsupportedOperationError(
            "BigQuery does not support truncating {} values to unit {!r}".format(
                arg.type(), unit
            )
        )
    return "{}_TRUNC({}, {})".format(kind, trans_arg, valid_unit)
# Buggy code
def _interval_property(target_unit):
    return property(functools.partial(_to_unit, target_unit=target_unit))


# Traceback (TypeError)
dw_calc = taxi['pickup_datetime'].day_of_week().name('dw_calc')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-139-ff32acdb32c7> in <module>()
1 #%%timeit
2 #hour_calc = taxi['pickup_datetime'].hour().name('hour_calc')
----> 3 dw_calc = taxi['pickup_datetime'].day_of_week().name('dw_calc')
4
5 #t_ = taxi[hour_calc, dw_cal]

TypeError: pipe() missing 1 required positional argument: 'f'

# Fixed code
def _interval_property(target_unit, name):
    return property(
        functools.partial(_to_unit, target_unit=target_unit),
        doc="""Extract the number of {0}s from an IntervalValue expression.

Returns
-------
IntegerValue
    The number of {0}s in the expression
""".format(name),
    )
# Buggy code
def between_time(arg, lower, upper, timezone=None):
    """
    Check if the input expr falls between the lower/upper bounds
    passed. Bounds are inclusive. All arguments must be comparable.

    Parameters
    ----------
    lower : str, datetime.time
    upper : str, datetime.time
    timezone : str, timezone, default None

    Returns
    -------
    is_between : BooleanValue
    """

    if isinstance(arg.op(), ops.Time):
        # Here we pull out the first argument to the underlying Time operation
        # which is by definition (in _timestamp_value_methods) a
        # TimestampValue. We do this so that we can potentially specialize the
        # "between time" operation for timestamp_value_expr.time().between().
        # A similar mechanism is triggered when creating expressions like
        # t.column.distinct().count(), which is turned into t.column.nunique().
        arg = arg.op().args[0]
        if timezone is not None:
            arg = arg.cast(dt.Timestamp(timezone=timezone))
        op = ops.BetweenTime(arg, lower, upper)
    else:
        op = ops.Between(arg, lower, upper)

    return op.to_expr()


# Traceback (TypeError)
dw_calc = taxi['pickup_datetime'].day_of_week().name('dw_calc')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-139-ff32acdb32c7> in <module>()
1 #%%timeit
2 #hour_calc = taxi['pickup_datetime'].hour().name('hour_calc')
----> 3 dw_calc = taxi['pickup_datetime'].day_of_week().name('dw_calc')
4
5 #t_ = taxi[hour_calc, dw_cal]

TypeError: pipe() missing 1 required positional argument: 'f'

# Fixed code
def between_time(arg, lower, upper, timezone=None):
    """Check if the input expr falls between the lower/upper bounds passed.
    Bounds are inclusive. All arguments must be comparable.

    Parameters
    ----------
    lower : str, datetime.time
    upper : str, datetime.time
    timezone : str, timezone, default None

    Returns
    -------
    BooleanValue
    """

    if isinstance(arg.op(), ops.Time):
        # Here we pull out the first argument to the underlying Time operation
        # which is by definition (in _timestamp_value_methods) a
        # TimestampValue. We do this so that we can potentially specialize the
        # "between time" operation for timestamp_value_expr.time().between().
        # A similar mechanism is triggered when creating expressions like
        # t.column.distinct().count(), which is turned into t.column.nunique().
        arg = arg.op().args[0]
        if timezone is not None:
            arg = arg.cast(dt.Timestamp(timezone=timezone))
        op = ops.BetweenTime(arg, lower, upper)
    else:
        op = ops.Between(arg, lower, upper)

    return op.to_expr()
# Buggy code
def _assert_can_compare(self):
    expr, lower, upper = self.args
    if not expr._can_compare(lower) or not expr._can_compare(upper):
        raise TypeError("Arguments are not comparable")


# Traceback (TypeError)
In [1]: import ibis

In [2]: ~ibis.literal(False)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-7ca5f0bed37d> in <module>()
----> 1 ~ibis.literal(False)

TypeError: bad operand type for unary ~: 'BooleanScalar'

# Fixed code
def _assert_can_compare(self):
    expr, lower, upper = self.args
    if not expr._can_compare(lower) or not expr._can_compare(upper):
        raise TypeError("Arguments are not comparable")
# Buggy code
def __init__(self, value, options):
    self.value = as_value_expr(value)
    self.options = as_value_expr(options)
    BooleanValueOp.__init__(self, self.value, self.options)


# Traceback (TypeError)
In [1]: import ibis

In [2]: ~ibis.literal(False)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-7ca5f0bed37d> in <module>()
----> 1 ~ibis.literal(False)

TypeError: bad operand type for unary ~: 'BooleanScalar'

# Fixed code
def __init__(self, value, options):
    self.value = as_value_expr(value)
    self.options = as_value_expr(options)
    super(Contains, self).__init__(self.value, self.options)
# Buggy code
def _try_timestamp(x):
    try:
        return pd.Timestamp(x)
    except (ValueError, TypeError):
        return x


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-115-87d40e40503e> in <module>()
----> 1 repr(db.csv_as_parquet.describe_formatted())

/home/wesm/code/cloudera/ibis/ibis/impala/metadata.py in __repr__(self)
295             data['partition schema'] = self.partitions
296
--> 297         pprint.pprint(data, stream=buf)
298
299         return buf.getvalue()

/home/wesm/anaconda3/lib/python3.5/pprint.py in pprint(object, stream, indent, width, depth, compact)
51         stream=stream, indent=indent, width=width, depth=depth,
52         compact=compact)
---> 53     printer.pprint(object)
54
55 def pformat(object, indent=1, width=80, depth=None, *, compact=False):

# Fixed code
def _try_timestamp(x):
    try:
        ts = pd.Timestamp(x)
        return ts.to_pydatetime()
    except (ValueError, TypeError):
        return x
# Buggy code
def _try_unix_timestamp(x):
    try:
        return pd.Timestamp.fromtimestamp(int(x))
    except (ValueError, TypeError):
        return x


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-115-87d40e40503e> in <module>()
----> 1 repr(db.csv_as_parquet.describe_formatted())

/home/wesm/code/cloudera/ibis/ibis/impala/metadata.py in __repr__(self)
295             data['partition schema'] = self.partitions
296
--> 297         pprint.pprint(data, stream=buf)
298
299         return buf.getvalue()

/home/wesm/anaconda3/lib/python3.5/pprint.py in pprint(object, stream, indent, width, depth, compact)
51         stream=stream, indent=indent, width=width, depth=depth,
52         compact=compact)
---> 53     printer.pprint(object)
54
55 def pformat(object, indent=1, width=80, depth=None, *, compact=False):

# Fixed code
def _try_unix_timestamp(x):
    try:
        ts = pd.Timestamp.fromtimestamp(int(x))
        return ts.to_pydatetime()
    except (ValueError, TypeError):
        return x
# Buggy code
def CopyToDict(self):
    """Copies the attribute container to a dictionary.

    Returns:
      dict[str, object]: attribute values per name.
    """
    return {
        attribute_name: attribute_value
        for attribute_name, attribute_value in self.GetAttributes()
    }


# Traceback (ValueError)
Traceback (most recent call last):
File "plaso/tools/log2timeline.py", line 97, in <module>
if not Main():
File "plaso/tools/log2timeline.py", line 72, in Main
tool.ExtractEventsFromSources()
File "plaso/plaso/cli/log2timeline_tool.py", line 448, in ExtractEventsFromSources
worker_memory_limit=self._worker_memory_limit)
File "plaso/plaso/multi_processing/task_engine.py", line 812, in ProcessSources
self._ProcessSources(source_path_specs, storage_writer)
File "plaso/plaso/multi_processing/task_engine.py", line 343, in _ProcessSources
self._ScheduleTasks(storage_writer)
File "plaso/plaso/multi_processing/task_engine.py", line 449, in _ScheduleTasks
self._MergeTaskStorage(storage_writer)
File "plaso/plaso/multi_processing/task_engine.py", line 256, in _MergeTaskStorage
maximum_number_of_containers=self._MAXIMUM_NUMBER_OF_CONTAINERS)
File "plaso/plaso/storage/sqlite/merge_reader.py", line 278, in MergeAttributeContainers
self._active_container_type, serialized_data)
File "plaso/plaso/storage/interface.py", line 600, in _DeserializeAttributeContainer
attribute_container = self._serializer.ReadSerialized(serialized_string)
File "plaso/plaso/serializer/json_serializer.py", line 404, in ReadSerialized
return cls.ReadSerializedDict(json_dict)
File "plaso/plaso/serializer/json_serializer.py", line 423, in ReadSerializedDict
json_object = cls._ConvertDictToObject(json_dict)
File "plaso/plaso/serializer/json_serializer.py", line 252, in _ConvertDictToObject
'supported.').format(attribute_name))
ValueError: Event data attribute value: return_value of type dict is not supported.

# Fixed code
def CopyToDict(self):
    """Copies the attribute container to a dictionary.

    Returns:
      dict[str, object]: attribute values per name.
    """
    return dict(self.GetAttributes())
# Buggy code
def __init__(self):
    """Initializes event data."""
    super(CupsIppEventData, self).__init__(data_type=self.DATA_TYPE)
    self.application = None
    self.computer_name = None
    self.copies = None
    # TODO: remove data_dict.
    self.data_dict = None
    self.doc_type = None
    self.job_id = None
    self.job_name = None
    self.owner = None
    self.printer_id = None
    self.uri = None
    self.user = None


# Traceback (ValueError)
Traceback (most recent call last):
File "plaso/tools/log2timeline.py", line 97, in <module>
if not Main():
File "plaso/tools/log2timeline.py", line 72, in Main
tool.ExtractEventsFromSources()
File "plaso/plaso/cli/log2timeline_tool.py", line 448, in ExtractEventsFromSources
worker_memory_limit=self._worker_memory_limit)
File "plaso/plaso/multi_processing/task_engine.py", line 812, in ProcessSources
self._ProcessSources(source_path_specs, storage_writer)
File "plaso/plaso/multi_processing/task_engine.py", line 343, in _ProcessSources
self._ScheduleTasks(storage_writer)
File "plaso/plaso/multi_processing/task_engine.py", line 449, in _ScheduleTasks
self._MergeTaskStorage(storage_writer)
File "plaso/plaso/multi_processing/task_engine.py", line 256, in _MergeTaskStorage
maximum_number_of_containers=self._MAXIMUM_NUMBER_OF_CONTAINERS)
File "plaso/plaso/storage/sqlite/merge_reader.py", line 278, in MergeAttributeContainers
self._active_container_type, serialized_data)
File "plaso/plaso/storage/interface.py", line 600, in _DeserializeAttributeContainer
attribute_container = self._serializer.ReadSerialized(serialized_string)
File "plaso/plaso/serializer/json_serializer.py", line 404, in ReadSerialized
return cls.ReadSerializedDict(json_dict)
File "plaso/plaso/serializer/json_serializer.py", line 423, in ReadSerializedDict
json_object = cls._ConvertDictToObject(json_dict)
File "plaso/plaso/serializer/json_serializer.py", line 252, in _ConvertDictToObject
'supported.').format(attribute_name))
ValueError: Event data attribute value: return_value of type dict is not supported.

# Fixed code
def __init__(self):
    """Initializes event data."""
    super(CupsIppEventData, self).__init__(data_type=self.DATA_TYPE)
    self.application = None
    self.computer_name = None
    self.copies = None
    self.doc_type = None
    self.job_id = None
    self.job_name = None
    self.owner = None
    self.printer_id = None
    self.uri = None
    self.user = None
# Buggy code
def _ProcessEvent(self, mediator, event, event_data, event_data_stream):
    """Processes an event.

    Args:
      mediator (AnalysisMediator): mediates interactions between
          analysis plugins and other components, such as storage and dfvfs.
      event (EventObject): event.
      event_data (EventData): event data.
      event_data_stream (EventDataStream): event data stream.
    """
    try:
        self._analysis_plugin.ExamineEvent(
            mediator, event, event_data, event_data_stream
        )

    except Exception as exception:  # pylint: disable=broad-except
        self.SignalAbort()

        # TODO: write analysis error.

        if self._debug_output:
            logger.warning("Unhandled exception while processing event object.")
            logger.exception(exception)


# Traceback (plaso.lib.error)
2020-07-13 13:03:56,615 [INFO] (MainProcess) PID:4081 <data_location> Determined data location: /tmp/workspace/psort-linux-nsrlsvr/data
Traceback (most recent call last):
File "./tools/psort.py", line 99, in <module>
if not Main():
File "./tools/psort.py", line 76, in Main
tool.ProcessStorage()
File "/tmp/workspace/psort-linux-nsrlsvr/plaso/cli/psort_tool.py", line 558, in ProcessStorage
worker_memory_limit=self._worker_memory_limit)
File "/tmp/workspace/psort-linux-nsrlsvr/plaso/multi_processing/psort.py", line 944, in AnalyzeEvents
storage_writer, analysis_plugins, event_filter=event_filter)
File "/tmp/workspace/psort-linux-nsrlsvr/plaso/multi_processing/psort.py", line 306, in _AnalyzeEvents
event_queue.PushItem((event, event_data, event_data_stream))
File "/tmp/workspace/psort-linux-nsrlsvr/plaso/engine/zeromq_queue.py", line 457, in PushItem
raise errors.QueueFull
plaso.lib.errors.QueueFull

# Fixed code
def _ProcessEvent(self, mediator, event, event_data, event_data_stream):
    """Processes an event.

    Args:
      mediator (AnalysisMediator): mediates interactions between
          analysis plugins and other components, such as storage and dfvfs.
      event (EventObject): event.
      event_data (EventData): event data.
      event_data_stream (EventDataStream): event data stream.
    """
    try:
        self._analysis_plugin.ExamineEvent(
            mediator, event, event_data, event_data_stream
        )

    except Exception as exception:  # pylint: disable=broad-except
        # TODO: write analysis error and change logger to debug only.

        logger.warning("Unhandled exception while processing event object.")
        logger.exception(exception)
# Buggy code
def ExamineEvent(self, mediator, event, event_data):
    """Analyzes an event and creates Windows Services as required.

    At present, this method only handles events extracted from the Registry.

    Args:
      mediator (AnalysisMediator): mediates interactions between analysis
          plugins and other components, such as storage and dfvfs.
      event (EventObject): event to examine.
      event_data (EventData): event data.
    """
    if event_data.data_type not in self._SUPPORTED_EVENT_DATA_TYPES:
        return

    # TODO: Handle event log entries here also (ie, event id 4697).
    service = WindowsService.FromEventData(event_data)
    self._service_collection.AddService(service)


# Traceback (plaso.lib.error)
2020-06-04 09:30:46,985 [INFO] (MainProcess) PID:4370 <data_location> Determined data location: /tmp/workspace/psort-linux-services/data
Traceback (most recent call last):
File "./tools/psort.py", line 95, in <module>
if not Main():
File "./tools/psort.py", line 72, in Main
tool.ProcessStorage()
File "/tmp/workspace/psort-linux-services/plaso/cli/psort_tool.py", line 554, in ProcessStorage
worker_memory_limit=self._worker_memory_limit)
File "/tmp/workspace/psort-linux-services/plaso/multi_processing/psort.py", line 909, in AnalyzeEvents
storage_writer, analysis_plugins, event_filter=event_filter)
File "/tmp/workspace/psort-linux-services/plaso/multi_processing/psort.py", line 281, in _AnalyzeEvents
event_queue.PushItem((event, event_data))
File "/tmp/workspace/psort-linux-services/plaso/engine/zeromq_queue.py", line 457, in PushItem
raise errors.QueueFull
plaso.lib.errors.QueueFull

# Fixed code
def ExamineEvent(self, mediator, event, event_data):
    """Analyzes an event and creates Windows Services as required.

    At present, this method only handles events extracted from the Registry.

    Args:
      mediator (AnalysisMediator): mediates interactions between analysis
          plugins and other components, such as storage and dfvfs.
      event (EventObject): event to examine.
      event_data (EventData): event data.
    """
    # TODO: Handle event log entries here also (ie, event id 4697).
    if event_data.data_type != "windows:registry:service":
        return

    event_data_attributes = event_data.CopyToDict()
    service_event_data = services.WindowsRegistryServiceEventData()
    service_event_data.CopyFromDict(event_data_attributes)

    service = WindowsService.FromEventData(service_event_data)
    self._service_collection.AddService(service)
# Buggy code
def SetLabel(self, label):
    """

    Args:
      label (str): label to apply to events extracted from files that are
          present in nsrlsvr.
    """
    self._label = label


# Traceback (ValueError)
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.7/dist-packages/plaso-20171118-py2.7.egg/plaso/analysis/interface.py", line 438, in run
hash_analyses = self.Analyze(hashes)
File "/usr/local/lib/python2.7/dist-packages/plaso-20171118-py2.7.egg/plaso/analysis/nsrlsvr.py", line 99, in Analyze
'Opening connection to {0:s}:{1:d}'.format(self._host, self._port))
ValueError: Unknown format code 'd' for object of type 'unicode'

# Fixed code
def SetLabel(self, label):
    """Sets the tagging label.

    Args:
      label (str): label to apply to events extracted from files that are
          present in nsrlsvr.
    """
    self._label = label
# Buggy code
def __init__(self, output_mediator):
    """Initializes the output module object.

    Args:
      output_mediator: The output mediator object (instance of OutputMediator).
    """
    super(ElasticSearchOutputModule, self).__init__(output_mediator)

    self._doc_type = None
    self._elastic = None
    self._flush_interval = None
    self._host = None
    self._index_name = None
    self._mapping = None
    self._output_mediator = output_mediator
    self._port = None
    self._raw_fields = False
    self._elastic_user = None
    self._elastic_password = None


# Traceback (TypeError)
2017-03-27 09:18:34,497 [INFO] (MainProcess) PID:5106 <timesketch_out> Timeline name: test

2017-03-27 09:18:34,497 [INFO] (MainProcess) PID:5106 <timesketch_out> Owner of the timeline: None
Traceback (most recent call last):
File "/usr/local/bin/psort.py", line 4, in <module>
__import__('pkg_resources').run_script('plaso==1.5.2.post20170221', 'psort.py')
File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 719, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1504, in run_script
exec(code, namespace, namespace)
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/EGG-INFO/scripts/psort.py", line 910, in <module>
if not Main():
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/EGG-INFO/scripts/psort.py", line 896, in Main
tool.ProcessStorage()
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/EGG-INFO/scripts/psort.py", line 840, in ProcessStorage
time_slice=self._time_slice, use_time_slicer=self._use_time_slicer)
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/plaso/frontend/psort.py", line 225, in ExportEvents
use_time_slicer=use_time_slicer)
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/plaso/multi_processing/psort.py", line 661, in ExportEvents
output_module, deduplicate_events)
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/plaso/output/event_buffer.py", line 92, in __init__
self._output_module.WriteHeader()
File "/usr/local/lib/python2.7/dist-packages/plaso-1.5.2.post20170221-py2.7.egg/plaso/output/timesketch_out.py", line 158, in WriteHeader
self._index_name, _document_mapping, self._doc_type)
TypeError: __init__() takes exactly 10 arguments (8 given)

# Fixed code
def __init__(self, output_mediator):
    """Initializes the output module object.

    Args:
      output_mediator: The output mediator object (instance of OutputMediator).
    """
    super(ElasticSearchOutputModule, self).__init__(output_mediator)
    self._doc_type = None
    self._elastic = None
    self._elastic_password = None
    self._elastic_user = None
    self._flush_interval = None
    self._host = None
    self._index_name = None
    self._mapping = None
    self._output_mediator = output_mediator
    self._port = None
    self._raw_fields = False
# Buggy code
def __init__(self, analyzer_class):
    """Initializes a hash tagging analysis plugin.

    Args:
      analyzer_class (type): A subclass of HashAnalyzer that will be
          instantiated by the plugin.
    """
    super(HashTaggingAnalysisPlugin, self).__init__()
    self._analysis_queue_timeout = self.DEFAULT_QUEUE_TIMEOUT
    self._analyzer_started = False
    self._event_uuids_by_pathspec = defaultdict(list)
    self._hash_pathspecs = defaultdict(list)
    self._requester_class = None
    self._time_of_last_status_log = time.time()
    self.hash_analysis_queue = Queue.Queue()
    self.hash_queue = Queue.Queue()

    self._analyzer = analyzer_class(self.hash_queue, self.hash_analysis_queue)


# Traceback (KeyError)
[ERROR] (nsrlsvr   ) PID:XXXXX <analysis_process> <dfvfs.path.os_path_spec.OSPathSpec object at 0xXXXXXXXXXXXX>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/plaso/multi_processing/analysis_process.py", line 138, in _Main
self._analysis_mediator.ProduceAnalysisReport(self._analysis_plugin)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/mediator.py", line 95, in ProduceAnalysisReport
analysis_report = plugin.CompileReport(self)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/interface.py", line 280, in CompileReport
hash_analysis)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/interface.py", line 187, in _HandleHashAnalysis
event_uuids = self._event_uuids_by_pathspec.pop(pathspec)
KeyError: <dfvfs.path.os_path_spec.OSPathSpec object at 0xXXXXXXXXXXXX>

# Fixed code
def __init__(self, analyzer_class):
    """Initializes a hash tagging analysis plugin.

    Args:
      analyzer_class (type): A subclass of HashAnalyzer that will be
          instantiated by the plugin.
    """
    super(HashTaggingAnalysisPlugin, self).__init__()
    self._analysis_queue_timeout = self.DEFAULT_QUEUE_TIMEOUT
    self._analyzer_started = False
    self._event_uuids_by_pathspec = collections.defaultdict(list)
    self._hash_pathspecs = collections.defaultdict(list)
    self._requester_class = None
    self._time_of_last_status_log = time.time()
    self.hash_analysis_queue = Queue.Queue()
    self.hash_queue = Queue.Queue()

    self._analyzer = analyzer_class(self.hash_queue, self.hash_analysis_queue)
# Buggy code
def _HandleHashAnalysis(self, hash_analysis):
    """Deals with the results of the analysis of a hash.

    This method ensures that labels are generated for the hash,
    then tags all events derived from files with that hash.

    Args:
      hash_analysis (HashAnalysis): hash analysis plugin's results for a given
          hash.

    Returns:
      tuple: containing:

        list[dfvfs.PathSpec]: pathspecs that had the hash value looked up.
        list[str]: labels that corresponds to the hash value that was looked up.
        list[EventTag]: event tags for all events that were extracted from the
            path specifications.
    """
    tags = []
    labels = self.GenerateLabels(hash_analysis.hash_information)
    pathspecs = self._hash_pathspecs.pop(hash_analysis.subject_hash)
    for pathspec in pathspecs:
        event_uuids = self._event_uuids_by_pathspec.pop(pathspec)
        if labels:
            for event_uuid in event_uuids:
                tag = self._CreateTag(event_uuid, labels)
                tags.append(tag)
    return pathspecs, labels, tags


# Traceback (KeyError)
[ERROR] (nsrlsvr   ) PID:XXXXX <analysis_process> <dfvfs.path.os_path_spec.OSPathSpec object at 0xXXXXXXXXXXXX>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/plaso/multi_processing/analysis_process.py", line 138, in _Main
self._analysis_mediator.ProduceAnalysisReport(self._analysis_plugin)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/mediator.py", line 95, in ProduceAnalysisReport
analysis_report = plugin.CompileReport(self)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/interface.py", line 280, in CompileReport
hash_analysis)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/interface.py", line 187, in _HandleHashAnalysis
event_uuids = self._event_uuids_by_pathspec.pop(pathspec)
KeyError: <dfvfs.path.os_path_spec.OSPathSpec object at 0xXXXXXXXXXXXX>

# Fixed code
def _HandleHashAnalysis(self, hash_analysis):
    """Deals with the results of the analysis of a hash.

    This method ensures that labels are generated for the hash,
    then tags all events derived from files with that hash.

    Args:
      hash_analysis (HashAnalysis): hash analysis plugin's results for a given
          hash.

    Returns:
      tuple: containing:

        list[dfvfs.PathSpec]: pathspecs that had the hash value looked up.
        list[str]: labels that corresponds to the hash value that was looked up.
        list[EventTag]: event tags for all events that were extracted from the
            path specifications.
    """
    tags = []
    labels = self.GenerateLabels(hash_analysis.hash_information)
    pathspecs = self._hash_pathspecs.pop(hash_analysis.subject_hash)
    for pathspec in pathspecs:
        event_uuids = self._event_uuids_by_pathspec.pop(pathspec, [])
        if labels:
            for event_uuid in event_uuids:
                tag = self._CreateTag(event_uuid, labels)
                tags.append(tag)
    return pathspecs, labels, tags