Here are the examples of the python api sys._getframe.f_lineno taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
12 Examples
3
Example 1
def test_future_source_traceback(self):
self.loop.set_debug(True)
future = asyncio.Future(loop=self.loop)
lineno = sys._getframe().f_lineno - 1
self.assertIsInstance(future._source_traceback, list)
self.assertEqual(future._source_traceback[-1][:3],
(__file__,
lineno,
'test_future_source_traceback'))
3
Example 2
Project: qdb Source File: test_tracer.py
def test_conditional_breakpoint(self):
"""
Tests valid conditional breakpoints.
WARNING: This test relies on the relative line numbers inside the test.
"""
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
loop_counter = 0
cmd_manager.enqueue(lambda t: self.assertEqual(loop_counter, 5))
line_offset = 5
db.set_break(
self.filename,
sys._getframe().f_lineno + line_offset,
cond='loop_counter == 5'
)
db.set_trace(stop=False)
while loop_counter < 10:
loop_counter += 1
3
Example 3
def test_future_source_traceback(self):
self.loop.set_debug(True)
future = self.create_future()
lineno = sys._getframe().f_lineno - 1
self.assertIsInstance(future._source_traceback, list)
self.assertEqual(future._source_traceback[-2][:3],
(__file__,
lineno,
'test_future_source_traceback'))
0
Example 4
Project: qdb Source File: test_tracer.py
def test_set_continue_with_breaks(self):
"""
Tests the behavior of continue when there are breakpoints in the mix.
WARNING: This test relies on the relative line numbers inside the test.
"""
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
line_offset = 8 # The difference in the set_break call and line_2.
db.set_break(
self.filename,
sys._getframe().f_lineno + line_offset,
)
cmd_manager.enqueue(lambda t: t.set_continue())
cmd_manager.user_wait(0.2)
line_1 = line_2 = line_3 = False
with Timeout(0.1, False):
db.set_trace()
line_1 = True
line_2 = True
line_3 = True
# Assert we only got to line_1 because of the breakpoint.
# These are split up to give more helpful messages if the test fails.
self.assertTrue(line_1)
self.assertFalse(line_2)
self.assertFalse(line_3)
# We are still in stepping mode so we should be reporting the stack.
self.assertEqual(
db.get_line(self.filename, db.curframe.f_lineno),
' db.get_line(self.filename, db.curframe.f_lineno),'
)
sys.settrace(None)
db.disable()
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
line_2_offset = 13 # The difference in the set_break call and line_2.
line_3_offset = 10 # THe difference in the set_break call and line_3.
db.set_break(
self.filename,
sys._getframe().f_lineno + line_2_offset,
)
db.set_break(
self.filename,
sys._getframe().f_lineno + line_3_offset,
)
cmd_manager.enqueue(lambda t: t.set_continue())
cmd_manager.enqueue(lambda t: t.set_continue())
cmd_manager.user_wait(0.2)
line_1 = line_2 = line_3 = False
with Timeout(0.1, False):
db.set_trace()
line_1 = True
line_2 = True
line_3 = True
self.assertTrue(line_1)
self.assertTrue(line_2)
self.assertFalse(line_3)
self.assertEqual(
db.get_line(self.filename, db.curframe.f_lineno),
' db.get_line(self.filename, db.curframe.f_lineno),'
)
0
Example 5
Project: qdb Source File: test_tracer.py
def test_set_trace_without_stop(self):
"""
Asserts that calling set_trace with stop=False will start tracing
but not stop.
WARNING: This test relies on the relative line numbers inside the test.
"""
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
line_offset = 8 # The difference in the set_break call and line_3.
db.set_break(
self.filename,
sys._getframe().f_lineno + line_offset,
)
cmd_manager.user_wait(0.2)
line_1 = line_2 = line_3 = False
with Timeout(0.1, False):
db.set_trace(stop=False) # Should not stop us here.
line_1 = True
line_2 = True
line_3 = True
# Since we are stepping, we should not hit this line.
self.assertTrue(line_1)
self.assertTrue(line_2)
# We should have still stopped at this breakpoint if we are tracing.
self.assertFalse(line_3)
db.disable()
db = Qdb(cmd_manager=NopCommandManager())
line_1 = False
with Timeout(0.1, False):
db.set_trace(stop=False)
line_1 = True
self.assertTrue(line_1)
0
Example 6
Project: qdb Source File: test_tracer.py
def test_conditional_breakpoint_raises(self):
"""
Tests conditional breakpoints that raise an exception.
WARNING: This test relies on the relative line numbers inside the test.
"""
line = None
exc = ValueError('lol wut r u doing?')
cond = 'raiser()'
stopped = [False]
def stop():
stopped[0] = True
return True # Execute the assertion.
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
cmd_manager.enqueue(lambda t: stop() and self.assertEqual(line, 1))
line_offset = 9
# Set a condition that will raise a ValueError.
db.set_break(
self.filename,
sys._getframe().f_lineno + line_offset,
cond=cond,
)
db.set_trace(stop=False)
def raiser():
raise exc
line = 1
line = 2 # This line number is used in the data assertion.
line = 3
db.disable()
errors = [e['p'] for e in cmd_manager.sent if e['e'] == 'error']
self.assertEqual(len(errors), 1)
error = errors[0]
self.assertEqual(error['type'], 'condition')
negative_line_offset = 13
self.assertEqual(
error['data'], {
'line': sys._getframe().f_lineno - negative_line_offset,
'cond': cond,
'exc': type(exc).__name__,
'output': db.exception_serializer(exc),
}
)
# Make sure we stopped when we raised the exception.
self.assertTrue(stopped[0])
0
Example 7
Project: qdb Source File: test_tracer.py
def test_conditional_breakpoint_timeout(self):
"""
Tests conditional breakpoints that cause timeouts.
WARNING: This test relies on the relative line numbers inside the test.
"""
stopped = [False]
def stop():
stopped[0] = True
return True # Execute the assertion.
line = None
cond = 'g()'
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager, execution_timeout=1)
cmd_manager.enqueue(lambda t: stop() and self.assertEqual(line, 1))
line_offset = 10
# Set a condition that will time out.
db.set_break(
self.filename,
sys._getframe().f_lineno + line_offset,
cond='g()',
)
db.set_trace(stop=False)
def g():
while True:
pass
line = 1
line = 2
line = 3
db.disable()
errors = [e['p'] for e in cmd_manager.sent if e['e'] == 'error']
self.assertEqual(len(errors), 1)
error = errors[0]
self.assertEqual(error['type'], 'condition')
negative_line_offset = 14
exc = QdbExecutionTimeout(cond, db.execution_timeout)
self.assertEqual(
error['data'], {
'line': sys._getframe().f_lineno - negative_line_offset,
'cond': cond,
'exc': type(exc).__name__,
'output': db.exception_serializer(exc),
}
)
# Make sure we stopped when we raised the exception.
self.assertTrue(stopped[0])
0
Example 8
Project: qdb Source File: test_tracer.py
def test_temporary_breakpoint(self):
"""
Tests conditional breakpoints.
WARNING: This test relies on the relative line numbers inside the test.
"""
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
cmd_manager.enqueue(lambda t: t.set_continue())
cmd_manager.user_wait(0.2)
loop_counter = 0
line_offset = 6
db.set_break(
self.filename,
sys._getframe().f_lineno + line_offset,
temporary=True,
)
with Timeout(0.1, False):
db.set_trace(stop=False)
while loop_counter < 10:
loop_counter += 1
# By hitting it the first time, we cleared the breakpoint and did not
# stop there again.
self.assertEqual(loop_counter, 10)
0
Example 9
Project: qdb Source File: test_tracer.py
def test_clear_break(self):
"""
Tests clearing a breakpoint.
WARNING: This test relies on the relative line numbers inside the test.
"""
cmd_manager = QueueCommandManager()
db = Qdb(cmd_manager=cmd_manager)
clear_break_offset = 14
set_break_offset = 8
cmd_manager.enqueue(lambda t: t.clear_break(
self.filename,
sys._getframe().f_lineno + clear_break_offset
))
cmd_manager.enqueue(lambda t: t.set_continue())
cmd_manager.user_wait(0.2)
db.set_break(
self.filename,
sys._getframe().f_lineno + set_break_offset
)
db.set_trace(stop=False)
continued = False
with Timeout(0.1):
db.set_trace(stop=False)
for n in range(2):
pass
continued = True
self.assertTrue(continued)
0
Example 10
def _get_lineno(self):
return sys._getframe(1).f_lineno
0
Example 11
Project: raven-python Source File: tests.py
def test_log_location(self):
out = StringIO()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(out)
handler.setFormatter(logging.Formatter(
u'%(name)s|%(filename)s|%(funcName)s|%(lineno)d|'
u'%(levelname)s|%(message)s'))
logger.addHandler(handler)
client = Client('http://foo:[email protected]/0')
with client.context:
logger.info('Hello World!')
lineno = sys._getframe().f_lineno - 1
items = out.getvalue().strip().split('|')
assert items[0] == 'tests.breadcrumbs.tests'
assert items[1].rstrip('co') == 'tests.py'
assert items[2] == 'test_log_location'
assert int(items[3]) == lineno
assert items[4] == 'INFO'
assert items[5] == 'Hello World!'
0
Example 12
Project: oslo.messaging Source File: test_logger.py
@mock.patch('oslo_utils.timeutils.utcnow')
def test_logging_conf(self, mock_utcnow):
with mock.patch('oslo_messaging.transport.get_transport',
return_value=test_notifier._FakeTransport(self.conf)):
logging.config.dictConfig({
'version': 1,
'handlers': {
'notification': {
'class': 'oslo_messaging.LoggingNotificationHandler',
'level': self.priority.upper(),
'url': 'test://',
},
},
'loggers': {
'default': {
'handlers': ['notification'],
'level': self.priority.upper(),
},
},
})
mock_utcnow.return_value = datetime.datetime.utcnow()
levelno = getattr(logging, self.priority.upper())
logger = logging.getLogger('default')
lineno = sys._getframe().f_lineno + 1
logger.log(levelno, 'foobar')
n = oslo_messaging.notify._impl_test.NOTIFICATIONS[0][1]
self.assertEqual(getattr(self, 'queue', self.priority.upper()),
n['priority'])
self.assertEqual('logrecord', n['event_type'])
self.assertEqual(str(timeutils.utcnow()), n['timestamp'])
self.assertIsNone(n['publisher_id'])
pathname = __file__
if pathname.endswith(('.pyc', '.pyo')):
pathname = pathname[:-1]
self.assertDictEqual(
n['payload'],
{'process': os.getpid(),
'funcName': 'test_logging_conf',
'name': 'default',
'thread': None,
'levelno': levelno,
'processName': 'MainProcess',
'pathname': pathname,
'lineno': lineno,
'msg': 'foobar',
'exc_info': None,
'levelname': logging.getLevelName(levelno),
'extra': None})