Commit db633b6f authored by Julien Muchembled's avatar Julien Muchembled

Make some dependencies optional: pandas, wendelin

parent 4426de1b
...@@ -402,8 +402,7 @@ ModuleSecurityInfo('os').declarePublic('urandom') ...@@ -402,8 +402,7 @@ ModuleSecurityInfo('os').declarePublic('urandom')
# #
# backport from wendelin # backport from wendelin
# #
# we neeed to allow access to numpy's internal types # we need to allow access to numpy's internal types
import pandas as pd
import numpy as np import numpy as np
allow_module('numpy') allow_module('numpy')
allow_module('numpy.lib.recfunctions') allow_module('numpy.lib.recfunctions')
...@@ -427,55 +426,66 @@ allow_type(np.timedelta64) ...@@ -427,55 +426,66 @@ allow_type(np.timedelta64)
allow_type(type(np.c_)) allow_type(type(np.c_))
allow_type(type(np.dtype('int16'))) allow_type(type(np.dtype('int16')))
allow_module('pandas') # Modify 'safetype' dict in full_write_guard function of RestrictedPython
# (closure) directly to allow write access to ndarray
# (and pandas DataFrame below).
allow_type(pd.Series) from RestrictedPython.Guards import full_write_guard
allow_type(pd.Timestamp) safetype = full_write_guard.func_closure[1].cell_contents.__self__
allow_type(pd.DatetimeIndex) safetype.update(dict.fromkeys((
# XXX: pd.DataFrame has its own security thus disable until we can fully integrate it np.ndarray,
#allow_type(pd.DataFrame) np.core.records.recarray,
allow_type(pd.MultiIndex) np.core.records.record,
allow_type(pd.indexes.range.RangeIndex) ), True))
allow_type(pd.indexes.numeric.Int64Index)
allow_type(pd.core.groupby.DataFrameGroupBy)
allow_type(pd.core.groupby.SeriesGroupBy)
allow_class(pd.DataFrame)
def restrictedMethod(s,name): def restrictedMethod(s,name):
def dummyMethod(*args, **kw): def dummyMethod(*args, **kw):
raise Unauthorized(name) raise Unauthorized(name)
return dummyMethod return dummyMethod
# Note: These black_list methods are for pandas 0.19.2 try:
series_black_list = ['to_csv', 'to_json', 'to_pickle', 'to_hdf', import pandas as pd
'to_sql', 'to_msgpack'] except ImportError:
series_black_list_dict = {m: restrictedMethod for m in series_black_list} pass
ContainerAssertions[pd.Series] = _check_access_wrapper(pd.Series, else:
series_black_list_dict) allow_module('pandas')
allow_type(pd.Series)
pandas_black_list = ['read_csv', 'read_json', 'read_pickle', 'read_hdf', 'read_fwf', allow_type(pd.Timestamp)
allow_type(pd.DatetimeIndex)
# XXX: pd.DataFrame has its own security thus disable
# until we can fully integrate it
#allow_type(pd.DataFrame)
allow_type(pd.MultiIndex)
allow_type(pd.indexes.range.RangeIndex)
allow_type(pd.indexes.numeric.Int64Index)
allow_type(pd.core.groupby.DataFrameGroupBy)
allow_type(pd.core.groupby.SeriesGroupBy)
allow_class(pd.DataFrame)
# Note: These black_list methods are for pandas 0.19.2
ContainerAssertions[pd.Series] = _check_access_wrapper(
pd.Series, dict.fromkeys((
'to_csv', 'to_json', 'to_pickle', 'to_hdf', 'to_sql', 'to_msgpack',
), restrictedMethod))
ModuleSecurityInfo('pandas').declarePrivate(
'read_csv', 'read_json', 'read_pickle', 'read_hdf', 'read_fwf',
'read_excel', 'read_html', 'read_msgpack', 'read_excel', 'read_html', 'read_msgpack',
'read_gbq', 'read_sas', 'read_stata'] 'read_gbq', 'read_sas', 'read_stata',
ModuleSecurityInfo('pandas').declarePrivate(*pandas_black_list) )
dataframe_black_list = ['to_csv', 'to_json', 'to_pickle', 'to_hdf', ContainerAssertions[pd.DataFrame] = _check_access_wrapper(
'to_excel', 'to_html', 'to_sql', 'to_msgpack', pd.DataFrame, dict.fromkeys((
'to_latex', 'to_gbq', 'to_stata'] 'to_csv', 'to_json', 'to_pickle', 'to_hdf', 'to_excel', 'to_html',
dataframe_black_list_dict = {m: restrictedMethod for m in dataframe_black_list} 'to_sql', 'to_msgpack', 'to_latex', 'to_gbq', 'to_stata',
ContainerAssertions[pd.DataFrame] = _check_access_wrapper( ), restrictedMethod))
pd.DataFrame, dataframe_black_list_dict)
safetype.update(dict.fromkeys((
# Modify 'safetype' dict in full_write_guard function pd.DataFrame,
# of RestrictedPython (closure) directly to allow pd.Series,
# write access to ndarray and pandas DataFrame. pd.tseries.index.DatetimeIndex,
from RestrictedPython.Guards import full_write_guard pd.core.indexing._iLocIndexer,
full_write_guard.func_closure[1].cell_contents.__self__[np.ndarray] = True pd.core.indexing._LocIndexer,
full_write_guard.func_closure[1].cell_contents.__self__[np.core.records.recarray] = True pd.MultiIndex,
full_write_guard.func_closure[1].cell_contents.__self__[np.core.records.record] = True pd.Index,
full_write_guard.func_closure[1].cell_contents.__self__[pd.DataFrame] = True ), True))
full_write_guard.func_closure[1].cell_contents.__self__[pd.Series] = True
full_write_guard.func_closure[1].cell_contents.__self__[pd.tseries.index.DatetimeIndex] = True
full_write_guard.func_closure[1].cell_contents.__self__[pd.core.indexing._iLocIndexer] = True
full_write_guard.func_closure[1].cell_contents.__self__[pd.core.indexing._LocIndexer] = True
full_write_guard.func_closure[1].cell_contents.__self__[pd.MultiIndex] = True
full_write_guard.func_closure[1].cell_contents.__self__[pd.Index] = True
...@@ -335,12 +335,16 @@ for filename in os.listdir(os.path.dirname(lxml.__file__)): ...@@ -335,12 +335,16 @@ for filename in os.listdir(os.path.dirname(lxml.__file__)):
# with `No name 'bigarray' in module 'wendelin' (no-name-in-module)`. # with `No name 'bigarray' in module 'wendelin' (no-name-in-module)`.
# #
# -> Teach pylint to properly understand wendelin package nature. # -> Teach pylint to properly understand wendelin package nature.
import wendelin try:
def wendelin_transform(node): import wendelin
except ImportError:
pass
else:
def wendelin_transform(node):
m = AstroidBuilder(MANAGER).string_build('__path__ = %r' % wendelin.__path__) m = AstroidBuilder(MANAGER).string_build('__path__ = %r' % wendelin.__path__)
m.package = True m.package = True
return m return m
MANAGER.register_transform(Module, wendelin_transform, lambda node: node.name == 'wendelin') MANAGER.register_transform(Module, wendelin_transform, lambda node: node.name == 'wendelin')
# Properly search for namespace packages: original astroid (as of 1.3.8) only # Properly search for namespace packages: original astroid (as of 1.3.8) only
# checks at top-level and it doesn't work for Shared.DC.ZRDB (defined in # checks at top-level and it doesn't work for Shared.DC.ZRDB (defined in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment