我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sqlalchemy.exc.SAWarning()。
def emits_warning_on(db, *messages): """Mark a test as emitting a warning on a specific dialect. With no arguments, squelches all SAWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). Note that emits_warning_on does **not** assert that the warnings were in fact seen. """ @decorator def decorate(fn, *args, **kw): with expect_warnings_on(db, assert_=False, *messages): return fn(*args, **kw) return decorate
def emits_warning_on(db, *messages): """Mark a test as emitting a warning on a specific dialect. With no arguments, squelches all SAWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). Note that emits_warning_on does **not** assert that the warnings were in fact seen. """ @decorator def decorate(fn, *args, **kw): with expect_warnings_on(db, *messages): return fn(*args, **kw) return decorate
def expect_warnings(*messages): """Context manager to expect warnings with the given messages.""" filters = [dict(action='ignore', category=sa_exc.SAPendingDeprecationWarning)] if not messages: filters.append(dict(action='ignore', category=sa_exc.SAWarning)) else: filters.extend(dict(action='ignore', message=message, category=sa_exc.SAWarning) for message in messages) for f in filters: warnings.filterwarnings(**f) try: yield finally: resetwarnings()
def emits_warning_on(db, *warnings): """Mark a test as emitting a warning on a specific dialect. With no arguments, squelches all SAWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). """ spec = db_spec(db) @decorator def decorate(fn, *args, **kw): if isinstance(db, util.string_types): if not spec(config._current): return fn(*args, **kw) else: wrapped = emits_warning(*warnings)(fn) return wrapped(*args, **kw) else: if not _is_excluded(*db): return fn(*args, **kw) else: wrapped = emits_warning(*warnings)(fn) return wrapped(*args, **kw) return decorate
def upgrade(migrate_engine): # ignore reflection warnings with warnings.catch_warnings(): warnings.simplefilter("ignore", category=sa_exc.SAWarning) metadata = MetaData() metadata.bind = migrate_engine harvest_source_table = Table('harvest_source', metadata, autoload=True) package_table = Table('package', metadata, autoload=True) harvested_document_table = Table('harvested_document', metadata, Column('url', UnicodeText, nullable=False), Column('guid', UnicodeText, default=u''), Column('source_id', UnicodeText, ForeignKey('harvest_source.id')), Column('package_id', UnicodeText, ForeignKey('package.id')), ) harvested_document_table.c.url.drop() harvested_document_table.c.guid.create(harvested_document_table) harvested_document_table.c.source_id.create(harvested_document_table) harvested_document_table.c.package_id.create(harvested_document_table)
def warn(msg, stacklevel=3): """Issue a warning. If msg is a string, :class:`.exc.SAWarning` is used as the category. .. note:: This function is swapped out when the test suite runs, with a compatible version that uses warnings.warn_explicit, so that the warnings registry can be controlled. """ if isinstance(msg, basestring): warnings.warn(msg, exc.SAWarning, stacklevel=stacklevel) else: warnings.warn(msg, stacklevel=stacklevel)
def emits_warning_on(db, *warnings): """Mark a test as emitting a warning on a specific dialect. With no arguments, squelches all SAWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). """ spec = db_spec(db) @decorator def decorate(fn, *args, **kw): if isinstance(db, basestring): if not spec(config.db): return fn(*args, **kw) else: wrapped = emits_warning(*warnings)(fn) return wrapped(*args, **kw) else: if not _is_excluded(*db): return fn(*args, **kw) else: wrapped = emits_warning(*warnings)(fn) return wrapped(*args, **kw) return decorate
def create(doc_path, output_path="sqlite://"): ''' Parse the relational table-like XML file provided by http://www.unimod.org/downloads.html and convert each <tag>_row into an equivalent database entry. By default the table will be held in memory. ''' tree = preprocess_xml(doc_path) engine = create_engine(output_path) Base.metadata.create_all(engine) session = sessionmaker(bind=engine, autoflush=False)() with warnings.catch_warnings(): warnings.simplefilter("ignore", category=sa_exc.SAWarning) for model in Base._decl_class_registry.values(): if hasattr(model, "_tag_name") and hasattr(model, "from_tag"): for tag in tree.iterfind(".//" + model._tag_name): session.add(model.from_tag(tag)) session.commit() return session
def expect_warnings(*messages, **kw): """Context manager which expects one or more warnings. With no arguments, squelches all SAWarnings emitted via sqlalchemy.util.warn and sqlalchemy.util.warn_limited. Otherwise pass string expressions that will match selected warnings via regex; all non-matching warnings are sent through. The expect version **asserts** that the warnings were in fact seen. Note that the test suite sets SAWarning warnings to raise exceptions. """ return _expect_warnings(sa_exc.SAWarning, messages, **kw)
def setup_filters(): """Set global warning behavior for the test suite.""" warnings.filterwarnings('ignore', category=sa_exc.SAPendingDeprecationWarning) warnings.filterwarnings('error', category=sa_exc.SADeprecationWarning) warnings.filterwarnings('error', category=sa_exc.SAWarning)
def emits_warning(*messages): """Mark a test as emitting a warning. With no arguments, squelches all SAWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). """ @decorator def decorate(fn, *args, **kw): with expect_warnings(*messages): return fn(*args, **kw) return decorate
def upgrade(migrate_engine): # ignore reflection warnings with warnings.catch_warnings(): warnings.simplefilter("ignore", category=sa_exc.SAWarning) metadata = MetaData() metadata.bind = migrate_engine user_table = Table('user', metadata, autoload=True) # name_column = user_table.c.name unique_name_constraint = UniqueConstraint('name', table=user_table) unique_name_constraint.create()