From: Felix Schwarz <felix.schwarz@oss.schwarz.eu>
https://github.com/edgewall/genshi/pull/49

--- a/genshi/util.py
+++ b/genshi/util.py
@@ -119,7 +119,6 @@ def _insert_item(self, item):
 
     def _manage_size(self):
         while len(self._dict) > self.capacity:
-            olditem = self._dict[self.tail.key]
             del self._dict[self.tail.key]
             if self.tail != self.head:
                 self.tail = self.tail.prv

--- a/genshi/core.py
+++ b/genshi/core.py
@@ -20,7 +20,8 @@
 
 import six
 
-from genshi.util import plaintext, stripentities, striptags, stringrepr
+from genshi.compat import stringrepr
+from genshi.util import stripentities, striptags
 
 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace',
            'QName']
--- a/genshi/filters/tests/i18n.py
+++ b/genshi/filters/tests/i18n.py
@@ -12,7 +12,6 @@
 # history and logs, available at http://genshi.edgewall.org/log/.
 
 from datetime import datetime
-import doctest
 from gettext import NullTranslations
 import unittest
 
--- a/genshi/filters/tests/test_html.py
+++ b/genshi/filters/tests/test_html.py
@@ -11,7 +11,6 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-import doctest
 import unittest
 
 import six
--- a/genshi/filters/tests/transform.py
+++ b/genshi/filters/tests/transform.py
@@ -12,7 +12,6 @@
 # history and logs, available at http://genshi.edgewall.org/log/.
 
 import doctest
-from pprint import pprint
 import unittest
 
 import six
--- a/genshi/input.py
+++ b/genshi/input.py
@@ -22,8 +22,6 @@
 import six
 from six.moves import html_entities as entities, html_parser as html
 
-import six
-
 from genshi.core import Attrs, QName, Stream, stripentities
 from genshi.core import START, END, XML_DECL, DOCTYPE, TEXT, START_NS, \
                         END_NS, START_CDATA, END_CDATA, PI, COMMENT
--- a/genshi/output.py
+++ b/genshi/output.py
@@ -20,7 +20,7 @@
 
 import six
 
-from genshi.core import escape, Attrs, Markup, Namespace, QName, StreamEventKind
+from genshi.core import escape, Attrs, Markup, QName, StreamEventKind
 from genshi.core import START, END, TEXT, XML_DECL, DOCTYPE, START_NS, END_NS, \
                         START_CDATA, END_CDATA, PI, COMMENT, XML_NAMESPACE
 
--- a/genshi/template/base.py
+++ b/genshi/template/base.py
@@ -15,7 +15,6 @@
 
 from collections import deque
 import os
-import sys
 
 import six
 
--- a/genshi/template/directives.py
+++ b/genshi/template/directives.py
@@ -19,8 +19,7 @@
 from genshi.path import Path
 from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \
                                  EXPR, _apply_directives, _eval_expr
-from genshi.template.eval import Expression, ExpressionASTTransformer, \
-                                 _ast, _parse
+from genshi.template.eval import Expression, _ast, _parse
 
 __all__ = ['AttrsDirective', 'ChooseDirective', 'ContentDirective',
            'DefDirective', 'ForDirective', 'IfDirective', 'MatchDirective',
--- a/genshi/template/interpolation.py
+++ b/genshi/template/interpolation.py
@@ -16,7 +16,6 @@
 """
 
 from itertools import chain
-import os
 import re
 from tokenize import PseudoToken
 
--- a/genshi/template/markup.py
+++ b/genshi/template/markup.py
@@ -15,7 +15,7 @@
 
 from itertools import chain
 
-from genshi.core import Attrs, Markup, Namespace, Stream, StreamEventKind
+from genshi.core import Attrs, Markup, Namespace, Stream
 from genshi.core import START, END, START_NS, END_NS, TEXT, PI, COMMENT
 from genshi.input import XMLParser
 from genshi.template.base import BadDirectiveError, Template, \
--- a/genshi/template/text.py
+++ b/genshi/template/text.py
@@ -35,7 +35,6 @@
                                  TemplateSyntaxError, EXEC, INCLUDE, SUB
 from genshi.template.eval import Suite
 from genshi.template.directives import *
-from genshi.template.directives import Directive
 from genshi.template.interpolation import interpolate
 
 __all__ = ['NewTextTemplate', 'OldTextTemplate', 'TextTemplate']
--- a/genshi/tests/core.py
+++ b/genshi/tests/core.py
@@ -11,13 +11,12 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-import doctest
 import pickle
 import unittest
 
 from genshi import core
 from genshi.core import Markup, Attrs, Namespace, QName, escape, unescape
-from genshi.input import XML, ParseError
+from genshi.input import XML
 from genshi.compat import StringIO, BytesIO, IS_PYTHON2
 from genshi.tests.test_utils import doctest_suite
 
--- a/genshi/tests/input.py
+++ b/genshi/tests/input.py
@@ -11,8 +11,6 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-import doctest
-import sys
 import unittest
 
 from genshi.core import Attrs, Stream
--- a/genshi/tests/output.py
+++ b/genshi/tests/output.py
@@ -11,9 +11,7 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-import doctest
 import unittest
-import sys
 
 from genshi.core import Attrs, Markup, QName, Stream
 from genshi.input import HTML, XML
--- a/genshi/tests/path.py
+++ b/genshi/tests/path.py
@@ -11,7 +11,6 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-import doctest
 import unittest
 
 from genshi.core import Attrs, QName
--- a/genshi/tests/util.py
+++ b/genshi/tests/util.py
@@ -11,7 +11,6 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-import doctest
 import unittest
 
 from genshi import util
--- a/genshi/util.py
+++ b/genshi/util.py
@@ -19,8 +19,6 @@
 
 import six
 
-from .compat import stringrepr
-
 __docformat__ = 'restructuredtext en'
 
 
--- a/setup.py
+++ b/setup.py
@@ -12,11 +12,8 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://genshi.edgewall.org/log/.
 
-from distutils.cmd import Command
 from distutils.command.build_ext import build_ext
 from distutils.errors import CCompilerError, DistutilsPlatformError
-import doctest
-from glob import glob
 import os
 try:
     from setuptools import setup, Extension

--- a/genshi/compat.py
+++ b/genshi/compat.py
@@ -99,6 +99,13 @@ def get_code_params(code):
 
 
     def build_code_chunk(code, filename, name, lineno):
+        if hasattr(code, 'replace'):
+            # Python 3.8+
+            return code.replace(
+                co_filename=filename,
+                co_name=name,
+                co_firstlineno=lineno,
+            )
         params =  [0, code.co_nlocals, code.co_kwonlyargcount,
                   code.co_stacksize, code.co_flags | 0x0040,
                   code.co_code, code.co_consts, code.co_names,
