◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Changed
- Drop support for Python 2

### Fixed

Expand Down
27 changes: 25 additions & 2 deletions src/embed_tests/pyimport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void SetUp()
string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, s);

IntPtr str = Runtime.Runtime.PyString_FromString(testPath);
IntPtr path = Runtime.Runtime.PySys_GetObject("path");
Runtime.Runtime.PyList_Append(new BorrowedReference(path), str);
}

[TearDown]
Expand Down Expand Up @@ -83,5 +83,28 @@ public void TestCastGlobalVar()
Assert.AreEqual("2", foo.FOO.ToString());
Assert.AreEqual("2", foo.test_foo().ToString());
}
}
}
6 changes: 3 additions & 3 deletions src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ public static void SetError(IntPtr ob, string value)
/// Sets the current Python exception given a Python object.
/// This is a wrapper for the Python PyErr_SetObject call.
/// </remarks>
public static void SetError(IntPtr ob, IntPtr value)
{
Runtime.PyErr_SetObject(ob, value);
}

/// <summary>
Expand Down Expand Up @@ -286,7 +286,7 @@ public static void SetError(Exception e)

IntPtr op = CLRObject.GetInstHandle(e);
IntPtr etype = Runtime.PyObject_GetAttrString(op, "__class__");
Runtime.PyErr_SetObject(etype, op);
Runtime.XDecref(etype);
Runtime.XDecref(op);
}
Expand Down
34 changes: 19 additions & 15 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime
Expand Down Expand Up @@ -222,22 +223,12 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
// Check these BEFORE the built-in import runs; may as well
// do the Incref()ed return here, since we've already found
// the module.
if (mod_name == "clr")
{
IntPtr clr_module = GetCLRModule(fromList);
if (clr_module != IntPtr.Zero)
{
IntPtr sys_modules = Runtime.PyImport_GetModuleDict();
if (sys_modules != IntPtr.Zero)
{
Runtime.PyDict_SetItemString(sys_modules, "clr", clr_module);
}
}
return clr_module;
}
if (mod_name == "CLR")
{
Exceptions.deprecation("The CLR module is deprecated. Please use 'clr'.");
IntPtr clr_module = GetCLRModule(fromList);
if (clr_module != IntPtr.Zero)
{
Expand All @@ -249,8 +240,10 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
}
return clr_module;
}
string realname = mod_name;
string clr_prefix = null;
if (mod_name.StartsWith("CLR."))
{
clr_prefix = "CLR."; // prepend when adding the module to sys.modules
Expand Down Expand Up @@ -312,11 +305,22 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
AssemblyManager.UpdatePath();
if (!AssemblyManager.IsValidNamespace(realname))
{
if (!AssemblyManager.LoadImplicit(realname))
{
// May be called when a module being imported imports a module.
// In particular, I've seen decimal import copy import org.python.core
return Runtime.PyObject_Call(py_import, args, kw);
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/runtime/moduleobject.cs
Original file line number Diff line number Diff line change
@@ -119,7 +119,7 @@ public ManagedType GetAttribute(string name, bool guess)
// cost. Ask the AssemblyManager to do implicit loading for each
// of the steps in the qualified name, then try it again.
bool ignore = name.StartsWith("__");
if (AssemblyManager.LoadImplicit(qname, !ignore))
{
if (AssemblyManager.IsValidNamespace(qname))
{
Expand Down Expand Up @@ -161,6 +161,11 @@ public ManagedType GetAttribute(string name, bool guess)
return null;
}


/// <summary>
/// Stores an attribute in the instance dict for future lookups.
Expand Down Expand Up @@ -365,7 +370,7 @@ internal void InitializePreload()
if (interactive_preload)
{
interactive_preload = false;
if (Runtime.PySys_GetObject("ps1") != IntPtr.Zero)
{
preload = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/pylist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public PyList(IntPtr ptr) : base(ptr)
{
}


/// <summary>
/// PyList Constructor
Expand Down
Loading
Toggle all file notes Toggle all file annotations