PATH: //proc/thread-self/root/opt/alt/python311/lib64/python3.11/test
FILE_BARU
CREATE
FOLDER_BARU
MKDIR
UPLOAD_FILE
GO
[ .. KEMBALI ]
๐ Sine-1000Hz-300ms.aif
โ
X
๐ __init__.py
โ
X
๐ __main__.py
โ
X
๐ __pycache__/
X
๐ _test_atexit.py
โ
X
๐ _test_eintr.py
โ
X
๐ _test_embed_set_config.py
โ
X
๐ _test_embed_structseq.py
โ
X
๐ _test_multiprocessing.py
โ
X
๐ _test_venv_multiprocessing.py
โ
X
๐ archiver_tests.py
โ
X
๐ audiodata/
X
๐ audiotest.au
โ
X
๐ audiotests.py
โ
X
๐ audit-tests.py
โ
X
๐ autotest.py
โ
X
๐ bisect_cmd.py
โ
X
๐ certdata/
X
๐ cjkencodings/
X
๐ clinic.test.c
โ
X
๐ cmath_testcases.txt
โ
X
๐ configdata/
X
๐ crashers/
X
๐ curses_tests.py
โ
X
๐ data/
X
๐ datetimetester.py
โ
X
๐ decimaltestdata/
X
๐ dis_module.py
โ
X
๐ dtracedata/
X
๐ empty.vbs
โ
X
๐ encoded_modules/
X
๐ exception_hierarchy.txt
โ
X
๐ floating_points.txt
โ
X
๐ fork_wait.py
โ
X
๐ formatfloat_testcases.txt
โ
X
๐ ieee754.txt
โ
X
๐ imghdrdata/
X
๐ imp_dummy.py
โ
X
๐ leakers/
X
๐ libregrtest/
X
๐ list_tests.py
โ
X
๐ lock_tests.py
โ
X
๐ mailcap.txt
โ
X
๐ mapping_tests.py
โ
X
๐ math_testcases.txt
โ
X
๐ memory_watchdog.py
โ
X
๐ mime.types
โ
X
๐ mock_socket.py
โ
X
๐ mp_fork_bomb.py
โ
X
๐ mp_preload.py
โ
X
๐ multibytecodec_support.py
โ
X
๐ pickletester.py
โ
X
๐ profilee.py
โ
X
๐ pstats.pck
โ
X
๐ pyclbr_input.py
โ
X
๐ pythoninfo.py
โ
X
๐ randv2_32.pck
โ
X
๐ randv2_64.pck
โ
X
๐ randv3.pck
โ
X
๐ re_tests.py
โ
X
๐ recursion.tar
โ
X
๐ regrtest.py
โ
X
๐ regrtestdata/
X
๐ relimport.py
โ
X
๐ reperf.py
โ
X
๐ seq_tests.py
โ
X
๐ signalinterproctester.py
โ
X
๐ sndhdrdata/
X
๐ ssl_servers.py
โ
X
๐ ssltests.py
โ
X
๐ string_tests.py
โ
X
๐ subprocessdata/
X
๐ support/
X
๐ test___all__.py
โ
X
๐ test__locale.py
โ
X
๐ test__opcode.py
โ
X
๐ test__osx_support.py
โ
X
๐ test__xxsubinterpreters.py
โ
X
๐ test_abc.py
โ
X
๐ test_abstract_numbers.py
โ
X
๐ test_aifc.py
โ
X
๐ test_argparse.py
โ
X
๐ test_array.py
โ
X
๐ test_asdl_parser.py
โ
X
๐ test_ast.py
โ
X
๐ test_asyncgen.py
โ
X
๐ test_asynchat.py
โ
X
๐ test_asyncio/
X
๐ test_asyncore.py
โ
X
๐ test_atexit.py
โ
X
๐ test_audioop.py
โ
X
๐ test_audit.py
โ
X
๐ test_augassign.py
โ
X
๐ test_base64.py
โ
X
๐ test_baseexception.py
โ
X
๐ test_bdb.py
โ
X
๐ test_bigaddrspace.py
โ
X
๐ test_bigmem.py
โ
X
๐ test_binascii.py
โ
X
๐ test_binop.py
โ
X
๐ test_bisect.py
โ
X
๐ test_bool.py
โ
X
๐ test_buffer.py
โ
X
๐ test_bufio.py
โ
X
๐ test_builtin.py
โ
X
๐ test_bytes.py
โ
X
๐ test_bz2.py
โ
X
๐ test_c_locale_coercion.py
โ
X
๐ test_calendar.py
โ
X
๐ test_call.py
โ
X
๐ test_capi/
X
๐ test_cgi.py
โ
X
๐ test_cgitb.py
โ
X
๐ test_charmapcodec.py
โ
X
๐ test_check_c_globals.py
โ
X
๐ test_class.py
โ
X
๐ test_clinic.py
โ
X
๐ test_cmath.py
โ
X
๐ test_cmd.py
โ
X
๐ test_cmd_line.py
โ
X
๐ test_cmd_line_script.py
โ
X
๐ test_code.py
โ
X
๐ test_code_module.py
โ
X
๐ test_codeccallbacks.py
โ
X
๐ test_codecencodings_cn.py
โ
X
๐ test_codecencodings_hk.py
โ
X
๐ test_codecencodings_iso2022.py
โ
X
๐ test_codecencodings_jp.py
โ
X
๐ test_codecencodings_kr.py
โ
X
๐ test_codecencodings_tw.py
โ
X
๐ test_codecmaps_cn.py
โ
X
๐ test_codecmaps_hk.py
โ
X
๐ test_codecmaps_jp.py
โ
X
๐ test_codecmaps_kr.py
โ
X
๐ test_codecmaps_tw.py
โ
X
๐ test_codecs.py
โ
X
๐ test_codeop.py
โ
X
๐ test_collections.py
โ
X
๐ test_colorsys.py
โ
X
๐ test_compare.py
โ
X
๐ test_compile.py
โ
X
๐ test_compileall.py
โ
X
๐ test_complex.py
โ
X
๐ test_concurrent_futures/
X
๐ test_configparser.py
โ
X
๐ test_contains.py
โ
X
๐ test_context.py
โ
X
๐ test_contextlib.py
โ
X
๐ test_contextlib_async.py
โ
X
๐ test_copy.py
โ
X
๐ test_copyreg.py
โ
X
๐ test_coroutines.py
โ
X
๐ test_cppext/
X
๐ test_cprofile.py
โ
X
๐ test_crashers.py
โ
X
๐ test_crypt.py
โ
X
๐ test_csv.py
โ
X
๐ test_ctypes.py
โ
X
๐ test_curses.py
โ
X
๐ test_dataclasses/
X
๐ test_datetime.py
โ
X
๐ test_dbm.py
โ
X
๐ test_dbm_dumb.py
โ
X
๐ test_dbm_gnu.py
โ
X
๐ test_dbm_ndbm.py
โ
X
๐ test_decimal.py
โ
X
๐ test_decorators.py
โ
X
๐ test_defaultdict.py
โ
X
๐ test_deque.py
โ
X
๐ test_descr.py
โ
X
๐ test_descrtut.py
โ
X
๐ test_devpoll.py
โ
X
๐ test_dict.py
โ
X
๐ test_dict_version.py
โ
X
๐ test_dictcomps.py
โ
X
๐ test_dictviews.py
โ
X
๐ test_difflib.py
โ
X
๐ test_difflib_expect.html
โ
X
๐ test_dis.py
โ
X
๐ test_distutils.py
โ
X
๐ test_doctest/
X
๐ test_docxmlrpc.py
โ
X
๐ test_dtrace.py
โ
X
๐ test_dynamic.py
โ
X
๐ test_dynamicclassattribute.py
โ
X
๐ test_eintr.py
โ
X
๐ test_email/
X
๐ test_embed.py
โ
X
๐ test_ensurepip.py
โ
X
๐ test_enum.py
โ
X
๐ test_enumerate.py
โ
X
๐ test_eof.py
โ
X
๐ test_epoll.py
โ
X
๐ test_errno.py
โ
X
๐ test_except_star.py
โ
X
๐ test_exception_group.py
โ
X
๐ test_exception_hierarchy.py
โ
X
๐ test_exception_variations.py
โ
X
๐ test_exceptions.py
โ
X
๐ test_extcall.py
โ
X
๐ test_faulthandler.py
โ
X
๐ test_fcntl.py
โ
X
๐ test_file.py
โ
X
๐ test_file_eintr.py
โ
X
๐ test_filecmp.py
โ
X
๐ test_fileinput.py
โ
X
๐ test_fileio.py
โ
X
๐ test_fileutils.py
โ
X
๐ test_finalization.py
โ
X
๐ test_float.py
โ
X
๐ test_flufl.py
โ
X
๐ test_fnmatch.py
โ
X
๐ test_fork1.py
โ
X
๐ test_format.py
โ
X
๐ test_fractions.py
โ
X
๐ test_frame.py
โ
X
๐ test_frozen.py
โ
X
๐ test_fstring.py
โ
X
๐ test_ftplib.py
โ
X
๐ test_funcattrs.py
โ
X
๐ test_functools.py
โ
X
๐ test_future_stmt/
X
๐ test_gc.py
โ
X
๐ test_gdb/
X
๐ test_generator_stop.py
โ
X
๐ test_generators.py
โ
X
๐ test_genericalias.py
โ
X
๐ test_genericclass.py
โ
X
๐ test_genericpath.py
โ
X
๐ test_genexps.py
โ
X
๐ test_getopt.py
โ
X
๐ test_getpass.py
โ
X
๐ test_getpath.py
โ
X
๐ test_gettext.py
โ
X
๐ test_glob.py
โ
X
๐ test_global.py
โ
X
๐ test_grammar.py
โ
X
๐ test_graphlib.py
โ
X
๐ test_grp.py
โ
X
๐ test_gzip.py
โ
X
๐ test_hash.py
โ
X
๐ test_hashlib.py
โ
X
๐ test_heapq.py
โ
X
๐ test_hmac.py
โ
X
๐ test_html.py
โ
X
๐ test_htmlparser.py
โ
X
๐ test_http_cookiejar.py
โ
X
๐ test_http_cookies.py
โ
X
๐ test_httplib.py
โ
X
๐ test_httpservers.py
โ
X
๐ test_idle.py
โ
X
๐ test_imaplib.py
โ
X
๐ test_imghdr.py
โ
X
๐ test_imp.py
โ
X
๐ test_import/
X
๐ test_importlib/
X
๐ test_index.py
โ
X
๐ test_inspect/
X
๐ test_int.py
โ
X
๐ test_int_literal.py
โ
X
๐ test_interpreters.py
โ
X
๐ test_io.py
โ
X
๐ test_ioctl.py
โ
X
๐ test_ipaddress.py
โ
X
๐ test_isinstance.py
โ
X
๐ test_iter.py
โ
X
๐ test_iterlen.py
โ
X
๐ test_itertools.py
โ
X
๐ test_json/
X
๐ test_keyword.py
โ
X
๐ test_keywordonlyarg.py
โ
X
๐ test_kqueue.py
โ
X
๐ test_largefile.py
โ
X
๐ test_launcher.py
โ
X
๐ test_lib2to3.py
โ
X
๐ test_linecache.py
โ
X
๐ test_list.py
โ
X
๐ test_listcomps.py
โ
X
๐ test_lltrace.py
โ
X
๐ test_locale.py
โ
X
๐ test_logging.py
โ
X
๐ test_long.py
โ
X
๐ test_longexp.py
โ
X
๐ test_lzma.py
โ
X
๐ test_mailbox.py
โ
X
๐ test_mailcap.py
โ
X
๐ test_marshal.py
โ
X
๐ test_math.py
โ
X
๐ test_memoryio.py
โ
X
๐ test_memoryview.py
โ
X
๐ test_metaclass.py
โ
X
๐ test_mimetypes.py
โ
X
๐ test_minidom.py
โ
X
๐ test_mmap.py
โ
X
๐ test_module/
X
๐ test_modulefinder.py
โ
X
๐ test_msilib.py
โ
X
๐ test_multibytecodec.py
โ
X
๐ test_multiprocessing_fork/
X
๐ test_multiprocessing_forkserver/
X
๐ test_multiprocessing_main_handling.py
โ
X
๐ test_multiprocessing_spawn/
X
๐ test_named_expressions.py
โ
X
๐ test_netrc.py
โ
X
๐ test_nis.py
โ
X
๐ test_nntplib.py
โ
X
๐ test_ntpath.py
โ
X
๐ test_numeric_tower.py
โ
X
๐ test_opcache.py
โ
X
๐ test_opcodes.py
โ
X
๐ test_openpty.py
โ
X
๐ test_operator.py
โ
X
๐ test_optparse.py
โ
X
๐ test_ordered_dict.py
โ
X
๐ test_os.py
โ
X
๐ test_ossaudiodev.py
โ
X
๐ test_osx_env.py
โ
X
๐ test_pathlib.py
โ
X
๐ test_patma.py
โ
X
๐ test_pdb.py
โ
X
๐ test_peepholer.py
โ
X
๐ test_peg_generator/
X
๐ test_pep646_syntax.py
โ
X
๐ test_pickle.py
โ
X
๐ test_picklebuffer.py
โ
X
๐ test_pickletools.py
โ
X
๐ test_pipes.py
โ
X
๐ test_pkg.py
โ
X
๐ test_pkgutil.py
โ
X
๐ test_platform.py
โ
X
๐ test_plistlib.py
โ
X
๐ test_poll.py
โ
X
๐ test_popen.py
โ
X
๐ test_poplib.py
โ
X
๐ test_positional_only_arg.py
โ
X
๐ test_posix.py
โ
X
๐ test_posixpath.py
โ
X
๐ test_pow.py
โ
X
๐ test_pprint.py
โ
X
๐ test_print.py
โ
X
๐ test_profile.py
โ
X
๐ test_property.py
โ
X
๐ test_pstats.py
โ
X
๐ test_pty.py
โ
X
๐ test_pulldom.py
โ
X
๐ test_pwd.py
โ
X
๐ test_py_compile.py
โ
X
๐ test_pyclbr.py
โ
X
๐ test_pydoc/
X
๐ test_pyexpat.py
โ
X
๐ test_queue.py
โ
X
๐ test_quopri.py
โ
X
๐ test_raise.py
โ
X
๐ test_random.py
โ
X
๐ test_range.py
โ
X
๐ test_re.py
โ
X
๐ test_readline.py
โ
X
๐ test_regrtest.py
โ
X
๐ test_repl.py
โ
X
๐ test_reprlib.py
โ
X
๐ test_resource.py
โ
X
๐ test_richcmp.py
โ
X
๐ test_rlcompleter.py
โ
X
๐ test_robotparser.py
โ
X
๐ test_runpy.py
โ
X
๐ test_sax.py
โ
X
๐ test_sched.py
โ
X
๐ test_scope.py
โ
X
๐ test_script_helper.py
โ
X
๐ test_secrets.py
โ
X
๐ test_select.py
โ
X
๐ test_selectors.py
โ
X
๐ test_set.py
โ
X
๐ test_setcomps.py
โ
X
๐ test_shelve.py
โ
X
๐ test_shlex.py
โ
X
๐ test_shutil.py
โ
X
๐ test_signal.py
โ
X
๐ test_site.py
โ
X
๐ test_slice.py
โ
X
๐ test_smtpd.py
โ
X
๐ test_smtplib.py
โ
X
๐ test_smtpnet.py
โ
X
๐ test_sndhdr.py
โ
X
๐ test_socket.py
โ
X
๐ test_socketserver.py
โ
X
๐ test_sort.py
โ
X
๐ test_source_encoding.py
โ
X
๐ test_spwd.py
โ
X
๐ test_sqlite3/
X
๐ test_ssl.py
โ
X
๐ test_stable_abi_ctypes.py
โ
X
๐ test_startfile.py
โ
X
๐ test_stat.py
โ
X
๐ test_statistics.py
โ
X
๐ test_strftime.py
โ
X
๐ test_string.py
โ
X
๐ test_string_literals.py
โ
X
๐ test_stringprep.py
โ
X
๐ test_strptime.py
โ
X
๐ test_strtod.py
โ
X
๐ test_struct.py
โ
X
๐ test_structseq.py
โ
X
๐ test_subclassinit.py
โ
X
๐ test_subprocess.py
โ
X
๐ test_sunau.py
โ
X
๐ test_sundry.py
โ
X
๐ test_super.py
โ
X
๐ test_support.py
โ
X
๐ test_symtable.py
โ
X
๐ test_syntax.py
โ
X
๐ test_sys.py
โ
X
๐ test_sys_setprofile.py
โ
X
๐ test_sys_settrace.py
โ
X
๐ test_sysconfig.py
โ
X
๐ test_syslog.py
โ
X
๐ test_tabnanny.py
โ
X
๐ test_tarfile.py
โ
X
๐ test_tcl.py
โ
X
๐ test_telnetlib.py
โ
X
๐ test_tempfile.py
โ
X
๐ test_termios.py
โ
X
๐ test_textwrap.py
โ
X
๐ test_thread.py
โ
X
๐ test_threadedtempfile.py
โ
X
๐ test_threading.py
โ
X
๐ test_threading_local.py
โ
X
๐ test_threadsignals.py
โ
X
๐ test_time.py
โ
X
๐ test_timeit.py
โ
X
๐ test_timeout.py
โ
X
๐ test_tix.py
โ
X
๐ test_tk.py
โ
X
๐ test_tokenize.py
โ
X
๐ test_tomllib/
X
๐ test_tools/
X
๐ test_trace.py
โ
X
๐ test_traceback.py
โ
X
๐ test_tracemalloc.py
โ
X
๐ test_ttk_guionly.py
โ
X
๐ test_ttk_textonly.py
โ
X
๐ test_tty.py
โ
X
๐ test_tuple.py
โ
X
๐ test_turtle.py
โ
X
๐ test_type_annotations.py
โ
X
๐ test_type_cache.py
โ
X
๐ test_type_comments.py
โ
X
๐ test_typechecks.py
โ
X
๐ test_types.py
โ
X
๐ test_typing.py
โ
X
๐ test_ucn.py
โ
X
๐ test_unary.py
โ
X
๐ test_unicode.py
โ
X
๐ test_unicode_file.py
โ
X
๐ test_unicode_file_functions.py
โ
X
๐ test_unicode_identifiers.py
โ
X
๐ test_unicodedata.py
โ
X
๐ test_unittest.py
โ
X
๐ test_univnewlines.py
โ
X
๐ test_unpack.py
โ
X
๐ test_unpack_ex.py
โ
X
๐ test_unparse.py
โ
X
๐ test_urllib.py
โ
X
๐ test_urllib2.py
โ
X
๐ test_urllib2_localnet.py
โ
X
๐ test_urllib2net.py
โ
X
๐ test_urllib_response.py
โ
X
๐ test_urllibnet.py
โ
X
๐ test_urlparse.py
โ
X
๐ test_userdict.py
โ
X
๐ test_userlist.py
โ
X
๐ test_userstring.py
โ
X
๐ test_utf8_mode.py
โ
X
๐ test_utf8source.py
โ
X
๐ test_uu.py
โ
X
๐ test_uuid.py
โ
X
๐ test_venv.py
โ
X
๐ test_wait3.py
โ
X
๐ test_wait4.py
โ
X
๐ test_warnings/
X
๐ test_wave.py
โ
X
๐ test_weakref.py
โ
X
๐ test_weakset.py
โ
X
๐ test_webbrowser.py
โ
X
๐ test_winconsoleio.py
โ
X
๐ test_winreg.py
โ
X
๐ test_winsound.py
โ
X
๐ test_with.py
โ
X
๐ test_wsgiref.py
โ
X
๐ test_xdrlib.py
โ
X
๐ test_xml_dom_minicompat.py
โ
X
๐ test_xml_etree.py
โ
X
๐ test_xml_etree_c.py
โ
X
๐ test_xmlrpc.py
โ
X
๐ test_xmlrpc_net.py
โ
X
๐ test_xxlimited.py
โ
X
๐ test_xxtestfuzz.py
โ
X
๐ test_yield_from.py
โ
X
๐ test_zipapp.py
โ
X
๐ test_zipfile.py
โ
X
๐ test_zipfile64.py
โ
X
๐ test_zipimport.py
โ
X
๐ test_zipimport_support.py
โ
X
๐ test_zlib.py
โ
X
๐ test_zoneinfo/
X
๐ testcodec.py
โ
X
๐ testtar.tar
โ
X
๐ testtar.tar.xz
โ
X
๐ tf_inherit_check.py
โ
X
๐ time_hashlib.py
โ
X
๐ tokenizedata/
X
๐ tracedmodules/
X
๐ typinganndata/
X
๐ win_console_handler.py
โ
X
๐ xmltestdata/
X
๐ xmltests.py
โ
X
๐ zip_cp437_header.zip
โ
X
๐ zipdir.zip
โ
X
๐ zipdir_backslash.zip
โ
X
๐ ziptestdata/
X
SAVING...
BERHASIL DIUBAH!
EDITING: test_linecache.py
""" Tests for the linecache module """ import linecache import unittest import os.path import tempfile import tokenize from importlib.machinery import ModuleSpec from test import support from test.support import os_helper FILENAME = linecache.__file__ NONEXISTENT_FILENAME = FILENAME + '.missing' INVALID_NAME = '!@$)(!@#_1' EMPTY = '' TEST_PATH = os.path.dirname(__file__) MODULES = "linecache abc".split() MODULE_PATH = os.path.dirname(FILENAME) SOURCE_1 = ''' " Docstring " def function(): return result ''' SOURCE_2 = ''' def f(): return 1 + 1 a = f() ''' SOURCE_3 = ''' def f(): return 3''' # No ending newline class TempFile: def setUp(self): super().setUp() with tempfile.NamedTemporaryFile(delete=False) as fp: self.file_name = fp.name fp.write(self.file_byte_string) self.addCleanup(os_helper.unlink, self.file_name) class GetLineTestsGoodData(TempFile): # file_list = ['list\n', 'of\n', 'good\n', 'strings\n'] def setUp(self): self.file_byte_string = ''.join(self.file_list).encode('utf-8') super().setUp() def test_getline(self): with tokenize.open(self.file_name) as fp: for index, line in enumerate(fp): if not line.endswith('\n'): line += '\n' cached_line = linecache.getline(self.file_name, index + 1) self.assertEqual(line, cached_line) def test_getlines(self): lines = linecache.getlines(self.file_name) self.assertEqual(lines, self.file_list) class GetLineTestsBadData(TempFile): # file_byte_string = b'Bad data goes here' def test_getline(self): self.assertEqual(linecache.getline(self.file_name, 1), '') def test_getlines(self): self.assertEqual(linecache.getlines(self.file_name), []) class EmptyFile(GetLineTestsGoodData, unittest.TestCase): file_list = [] class SingleEmptyLine(GetLineTestsGoodData, unittest.TestCase): file_list = ['\n'] class GoodUnicode(GetLineTestsGoodData, unittest.TestCase): file_list = ['รก\n', 'b\n', 'abcdef\n', 'รกรกรกรกรก\n'] class BadUnicode_NoDeclaration(GetLineTestsBadData, unittest.TestCase): file_byte_string = b'\n\x80abc' class BadUnicode_WithDeclaration(GetLineTestsBadData, unittest.TestCase): file_byte_string = b'# coding=utf-8\n\x80abc' class FakeLoader: def get_source(self, fullname): return f'source for {fullname}' class NoSourceLoader: def get_source(self, fullname): return None class LineCacheTests(unittest.TestCase): def test_getline(self): getline = linecache.getline # Bad values for line number should return an empty string self.assertEqual(getline(FILENAME, 2**15), EMPTY) self.assertEqual(getline(FILENAME, -1), EMPTY) # Float values currently raise TypeError, should it? self.assertRaises(TypeError, getline, FILENAME, 1.1) # Bad filenames should return an empty string self.assertEqual(getline(EMPTY, 1), EMPTY) self.assertEqual(getline(INVALID_NAME, 1), EMPTY) # Check module loading for entry in MODULES: filename = os.path.join(MODULE_PATH, entry) + '.py' with open(filename, encoding='utf-8') as file: for index, line in enumerate(file): self.assertEqual(line, getline(filename, index + 1)) # Check that bogus data isn't returned (issue #1309567) empty = linecache.getlines('a/b/c/__init__.py') self.assertEqual(empty, []) def test_no_ending_newline(self): self.addCleanup(os_helper.unlink, os_helper.TESTFN) with open(os_helper.TESTFN, "w", encoding='utf-8') as fp: fp.write(SOURCE_3) lines = linecache.getlines(os_helper.TESTFN) self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"]) def test_clearcache(self): cached = [] for entry in MODULES: filename = os.path.join(MODULE_PATH, entry) + '.py' cached.append(filename) linecache.getline(filename, 1) # Are all files cached? self.assertNotEqual(cached, []) cached_empty = [fn for fn in cached if fn not in linecache.cache] self.assertEqual(cached_empty, []) # Can we clear the cache? linecache.clearcache() cached_empty = [fn for fn in cached if fn in linecache.cache] self.assertEqual(cached_empty, []) def test_checkcache(self): getline = linecache.getline # Create a source file and cache its contents source_name = os_helper.TESTFN + '.py' self.addCleanup(os_helper.unlink, source_name) with open(source_name, 'w', encoding='utf-8') as source: source.write(SOURCE_1) getline(source_name, 1) # Keep a copy of the old contents source_list = [] with open(source_name, encoding='utf-8') as source: for index, line in enumerate(source): self.assertEqual(line, getline(source_name, index + 1)) source_list.append(line) with open(source_name, 'w', encoding='utf-8') as source: source.write(SOURCE_2) # Try to update a bogus cache entry linecache.checkcache('dummy') # Check that the cache matches the old contents for index, line in enumerate(source_list): self.assertEqual(line, getline(source_name, index + 1)) # Update the cache and check whether it matches the new source file linecache.checkcache(source_name) with open(source_name, encoding='utf-8') as source: for index, line in enumerate(source): self.assertEqual(line, getline(source_name, index + 1)) source_list.append(line) def test_lazycache_no_globals(self): lines = linecache.getlines(FILENAME) linecache.clearcache() self.assertEqual(False, linecache.lazycache(FILENAME, None)) self.assertEqual(lines, linecache.getlines(FILENAME)) def test_lazycache_smoke(self): lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() self.assertEqual( True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) # Note here that we're looking up a nonexistent filename with no # globals: this would error if the lazy value wasn't resolved. self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME)) def test_lazycache_provide_after_failed_lookup(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() linecache.getlines(NONEXISTENT_FILENAME) linecache.lazycache(NONEXISTENT_FILENAME, globals()) self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME)) def test_lazycache_check(self): linecache.clearcache() linecache.lazycache(NONEXISTENT_FILENAME, globals()) linecache.checkcache() def test_lazycache_bad_filename(self): linecache.clearcache() self.assertEqual(False, linecache.lazycache('', globals())) self.assertEqual(False, linecache.lazycache('<foo>', globals())) def test_lazycache_already_cached(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) self.assertEqual( False, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME])) def test_memoryerror(self): lines = linecache.getlines(FILENAME) self.assertTrue(lines) def raise_memoryerror(*args, **kwargs): raise MemoryError with support.swap_attr(linecache, 'updatecache', raise_memoryerror): lines2 = linecache.getlines(FILENAME) self.assertEqual(lines2, lines) linecache.clearcache() with support.swap_attr(linecache, 'updatecache', raise_memoryerror): lines3 = linecache.getlines(FILENAME) self.assertEqual(lines3, []) self.assertEqual(linecache.getlines(FILENAME), lines) def test_loader(self): filename = 'scheme://path' for loader in (None, object(), NoSourceLoader()): linecache.clearcache() module_globals = {'__name__': 'a.b.c', '__loader__': loader} self.assertEqual(linecache.getlines(filename, module_globals), []) linecache.clearcache() module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader()} self.assertEqual(linecache.getlines(filename, module_globals), ['source for a.b.c\n']) for spec in (None, object(), ModuleSpec('', FakeLoader())): linecache.clearcache() module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader(), '__spec__': spec} self.assertEqual(linecache.getlines(filename, module_globals), ['source for a.b.c\n']) linecache.clearcache() spec = ModuleSpec('x.y.z', FakeLoader()) module_globals = {'__name__': 'a.b.c', '__loader__': spec.loader, '__spec__': spec} self.assertEqual(linecache.getlines(filename, module_globals), ['source for x.y.z\n']) class LineCacheInvalidationTests(unittest.TestCase): def setUp(self): super().setUp() linecache.clearcache() self.deleted_file = os_helper.TESTFN + '.1' self.modified_file = os_helper.TESTFN + '.2' self.unchanged_file = os_helper.TESTFN + '.3' for fname in (self.deleted_file, self.modified_file, self.unchanged_file): self.addCleanup(os_helper.unlink, fname) with open(fname, 'w', encoding='utf-8') as source: source.write(f'print("I am {fname}")') self.assertNotIn(fname, linecache.cache) linecache.getlines(fname) self.assertIn(fname, linecache.cache) os.remove(self.deleted_file) with open(self.modified_file, 'w', encoding='utf-8') as source: source.write('print("was modified")') def test_checkcache_for_deleted_file(self): linecache.checkcache(self.deleted_file) self.assertNotIn(self.deleted_file, linecache.cache) self.assertIn(self.modified_file, linecache.cache) self.assertIn(self.unchanged_file, linecache.cache) def test_checkcache_for_modified_file(self): linecache.checkcache(self.modified_file) self.assertIn(self.deleted_file, linecache.cache) self.assertNotIn(self.modified_file, linecache.cache) self.assertIn(self.unchanged_file, linecache.cache) def test_checkcache_with_no_parameter(self): linecache.checkcache() self.assertNotIn(self.deleted_file, linecache.cache) self.assertNotIn(self.modified_file, linecache.cache) self.assertIn(self.unchanged_file, linecache.cache) if __name__ == "__main__": unittest.main()
SIMPAN PERUBAHAN