Repository: cython/cython

Issue:
[BUG] Unable to encrypt Chinese function
### Describe the bug

```
Compiling 测试.py because it changed.
[1/1] Cythonizing 测试.py
D:\Python\Python311\Lib\site-packages\Cython\Compiler\Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: D:\MYpython\测试.py
  tree = Parsing.p_module(s, pxd, full_module_name)
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\Python\Python311\Scripts\easycython.exe\__main__.py", line 7, in <module>
  File "D:\Python\Python311\Lib\site-packages\begin\main.py", line 52, in start
    cmdline.apply_options(self.__wrapped__, opts,
  File "D:\Python\Python311\Lib\site-packages\begin\cmdline.py", line 253, in apply_options
    return_value = call_function(func, signature(ext), opts)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\begin\cmdline.py", line 236, in call_function
    return func(*pargs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\easycython\easycython.py", line 77, in main
    ext_modules = cythonize(ext_modules),
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\Cython\Build\Dependencies.py", line 1154, in cythonize
    cythonize_one(*args)
  File "D:\Python\Python311\Lib\site-packages\Cython\Build\Dependencies.py", line 1300, in cythonize_one
    result = compile_single(pyx_file, options, full_module_name=full_module_name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\Main.py", line 615, in compile_single 
    return run_pipeline(source, options, full_module_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\Main.py", line 539, in run_pipeline   
    err, enddata = Pipeline.run_pipeline(pipeline, source)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\Pipeline.py", line 398, in run_pipeline
    data = run(phase, data)
           ^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\Pipeline.py", line 375, in run        
    return phase(data)
           ^^^^^^^^^^^
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\Pipeline.py", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\ModuleNode.py", line 222, in process_implementation
    self.generate_c_code(env, options, result)
  File "D:\Python\Python311\Lib\site-packages\Cython\Compiler\ModuleNode.py", line 550, in generate_c_code
    rootwriter.copyto(f)
  File "Cython\\Compiler\\Code.py", line 1846, in Cython.Compiler.Code.CCodeWriter.copyto
  File "Cython\\StringIOTree.py", line 81, in Cython.StringIOTree.StringIOTree.copyto
  File "Cython\\StringIOTree.py", line 81, in Cython.StringIOTree.StringIOTree.copyto
  File "Cython\\StringIOTree.py", line 84, in Cython.StringIOTree.StringIOTree.copyto
  File "<frozen codecs>", line 721, in write
  File "<frozen codecs>", line 377, in write
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 993-994: ordinal not in range(256)
```

### Code to reproduce the behaviour:

```python
def 测试():
    print("测试")
    
测试()
```

#Failed to build pyd on this simple Chinese function

### Expected behaviour

_No response_

### OS

windows

### Python version

3.11.3

### Cython version

_No response_

### Additional context

_No response_


Discussion:
I think the problem is the file name, rather than the the file contents.

It does look like we're restrictive about the file name

https://github.com/cython/cython/blob/06f155984d80652f2b26997b09aa42a50da7c3a2/Cython/Utils.py#L135-L141

I don't think we need to be.

Resolving patch:
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -3584,7 +3584,7 @@ def generate_module_creation_code(self, env, code):
         code.putln("int add_module_result = PyState_AddModule(%s, &%s);" % (
             module_temp, Naming.pymoduledef_cname))
         code.putln("%s = 0; /* transfer ownership from %s to %s pseudovariable */" % (
-            module_temp, module_temp, env.module_name
+            module_temp, module_temp, env.module_name.as_c_string_literal()
         ))
         # At this stage the module likely has a refcount of 2 - one owned by the list
         # inside PyState_AddModule and one owned by "__pyx_m" (and returned from this


Test patch:
diff --git a/tests/run/unicode_imports.srctree b/tests/run/unicode_imports.srctree
--- a/tests/run/unicode_imports.srctree
+++ b/tests/run/unicode_imports.srctree
@@ -22,7 +22,7 @@ from __future__ import unicode_literals
 
 from Cython.Build import cythonize
 
-files = ["mymoð.pyx", "from_cy.pyx"]
+files = ["mymoð.pyx", "from_cy.pyx", "测试.py" ]
 
 modules = cythonize(files)
 
@@ -46,6 +46,11 @@ cdef struct S:
     int x
     
 cdef public api void cdef_func() # just to test generation of headers
+
+############ 测试.py #############
+
+def g():
+    return 1
     
 ############ from_py.py #########
 
@@ -53,12 +58,15 @@ cdef public api void cdef_func() # just to test generation of headers
 
 import mymoð
 from mymoð import f
+import 测试
 
 __doc__ = """
 >>> mymoð.f()
 True
 >>> f()
 True
+>>> 测试.g()
+1
 """
 
 ######### from_cy.pyx ##########
@@ -68,6 +76,7 @@ True
 import mymoð
 
 from mymoð import f
+import 测试
 
 cimport pxd_moð
 from pxd_moð cimport S
@@ -79,6 +88,14 @@ def test_imported():
     True
     """
     return mymoð.f() and f() # True and True
+    
+    
+def test_imported2():
+    """
+    >>> test_imported2()
+    1
+    """
+    return 测试.g()
 
 
 def test_cimported():