Commit b098b018 authored by Julien Muchembled's avatar Julien Muchembled

Make some dependencies optional: pandas, wendelin

parent 59e7ec0f
Pipeline #17867 failed with stage
in 0 seconds
...@@ -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)
'read_excel', 'read_html', 'read_msgpack', allow_type(pd.DatetimeIndex)
'read_gbq', 'read_sas', 'read_stata'] # XXX: pd.DataFrame has its own security thus disable
ModuleSecurityInfo('pandas').declarePrivate(*pandas_black_list) # until we can fully integrate it
#allow_type(pd.DataFrame)
dataframe_black_list = ['to_csv', 'to_json', 'to_pickle', 'to_hdf', allow_type(pd.MultiIndex)
'to_excel', 'to_html', 'to_sql', 'to_msgpack', allow_type(pd.indexes.range.RangeIndex)
'to_latex', 'to_gbq', 'to_stata'] allow_type(pd.indexes.numeric.Int64Index)
dataframe_black_list_dict = {m: restrictedMethod for m in dataframe_black_list} allow_type(pd.core.groupby.DataFrameGroupBy)
ContainerAssertions[pd.DataFrame] = _check_access_wrapper( allow_type(pd.core.groupby.SeriesGroupBy)
pd.DataFrame, dataframe_black_list_dict) allow_class(pd.DataFrame)
# Modify 'safetype' dict in full_write_guard function # Note: These black_list methods are for pandas 0.19.2
# of RestrictedPython (closure) directly to allow ContainerAssertions[pd.Series] = _check_access_wrapper(
# write access to ndarray and pandas DataFrame. pd.Series, dict.fromkeys((
from RestrictedPython.Guards import full_write_guard 'to_csv', 'to_json', 'to_pickle', 'to_hdf', 'to_sql', 'to_msgpack',
full_write_guard.func_closure[1].cell_contents.__self__[np.ndarray] = True ), restrictedMethod))
full_write_guard.func_closure[1].cell_contents.__self__[np.core.records.recarray] = True
full_write_guard.func_closure[1].cell_contents.__self__[np.core.records.record] = True ModuleSecurityInfo('pandas').declarePrivate(
full_write_guard.func_closure[1].cell_contents.__self__[pd.DataFrame] = True 'read_csv', 'read_json', 'read_pickle', 'read_hdf', 'read_fwf',
full_write_guard.func_closure[1].cell_contents.__self__[pd.Series] = True 'read_excel', 'read_html', 'read_msgpack',
full_write_guard.func_closure[1].cell_contents.__self__[pd.tseries.index.DatetimeIndex] = True 'read_gbq', 'read_sas', 'read_stata',
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 ContainerAssertions[pd.DataFrame] = _check_access_wrapper(
full_write_guard.func_closure[1].cell_contents.__self__[pd.Index] = True pd.DataFrame, dict.fromkeys((
'to_csv', 'to_json', 'to_pickle', 'to_hdf', 'to_excel', 'to_html',
'to_sql', 'to_msgpack', 'to_latex', 'to_gbq', 'to_stata',
), restrictedMethod))
safetype.update(dict.fromkeys((
pd.DataFrame,
pd.Series,
pd.tseries.index.DatetimeIndex,
pd.core.indexing._iLocIndexer,
pd.core.indexing._LocIndexer,
pd.MultiIndex,
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
m = AstroidBuilder(MANAGER).string_build('__path__ = %r' % wendelin.__path__) except ImportError:
m.package = True pass
return m else:
MANAGER.register_transform(Module, wendelin_transform, lambda node: node.name == 'wendelin') def wendelin_transform(node):
m = AstroidBuilder(MANAGER).string_build('__path__ = %r' % wendelin.__path__)
m.package = True
return m
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